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;
1181 $output .= RED if $do_color && $level eq 'ERROR';
1182 $output .= MAGENTA if $do_color && $level eq 'WARNING';
1183 $output .= $level . ':' if !$github;
1184 $output .= RESET if $do_color;
1185 $output .= ' ' . $msg . "\n";
1187 $output = (split('\n', $output))[0] . "\n" if ($terse);
1189 push(our @report, $output);
1197 if (report("ERROR", $_[0])) {
1203 if (report("WARNING", $_[0])) {
1209 # According to tests/qtest/bios-tables-test.c: do not
1210 # change expected file in the same commit with adding test
1212 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_;
1214 # Note: shell script that rebuilds the expected files is in the same
1215 # directory as files themselves.
1216 # Note: allowed diff list can be changed both when changing expected
1217 # files and when changing tests.
1218 if ($name =~ m#^tests/data/acpi/# and not $name =~ m#^\.sh$#) {
1219 $$acpi_testexpected = $name;
1220 } elsif ($name !~ m#^tests/qtest/bios-tables-test-allowed-diff.h$#) {
1221 $$acpi_nontestexpected = $name;
1223 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) {
1224 ERROR("Do not add expected files together with tests, " .
1225 "follow instructions in " .
1226 "tests/qtest/bios-tables-test.c: both " .
1227 $$acpi_testexpected . " and " .
1228 $$acpi_nontestexpected . " found\n");
1233 my $filename = shift;
1239 my $stashrawline="";
1250 my $in_header_lines = $file ? 0 : 1;
1251 my $in_commit_log = 0; #Scanning lines before patch
1252 my $non_utf8_charset = 0;
1260 # Trace the real file/line as we go.
1266 my $comment_edge = 0;
1270 my $prev_values = 'E';
1273 my %suppress_ifbraces;
1274 my %suppress_whiletrailers;
1275 my %suppress_export;
1277 my $acpi_testexpected;
1278 my $acpi_nontestexpected;
1280 # Pre-scan the patch sanitizing the lines.
1282 sanitise_line_reset();
1284 foreach my $rawline (@rawlines) {
1288 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1297 # Guestimate if this is a continuing comment. Run
1298 # the context looking for a comment "edge". If this
1299 # edge is a close comment then we must be in a comment
1303 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1304 next if (defined $rawlines[$ln - 1] &&
1305 $rawlines[$ln - 1] =~ /^-/);
1307 #print "RAW<$rawlines[$ln - 1]>\n";
1308 last if (!defined $rawlines[$ln - 1]);
1309 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1310 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1315 if (defined $edge && $edge eq '*/') {
1319 # Guestimate if this is a continuing comment. If this
1320 # is the start of a diff block and this line starts
1321 # ' *' then it is very likely a comment.
1322 if (!defined $edge &&
1323 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1328 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1329 sanitise_line_reset($in_comment);
1331 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1332 # Standardise the strings and chars within the input to
1333 # simplify matching -- only bother with positive lines.
1334 $line = sanitise_line($rawline);
1336 push(@lines, $line);
1339 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1344 #print "==>$rawline\n";
1345 #print "-->$line\n";
1352 foreach my $line (@lines) {
1355 my $rawline = $rawlines[$linenr - 1];
1357 #extract the line range in the file after the patch is applied
1358 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1360 $first_line = $linenr + 1;
1370 %suppress_ifbraces = ();
1371 %suppress_whiletrailers = ();
1372 %suppress_export = ();
1375 # track the line number as we move through the hunk, note that
1376 # new versions of GNU diff omit the leading space on completely
1377 # blank context lines so we need to count that too.
1378 } elsif ($line =~ /^( |\+|$)/) {
1380 $realcnt-- if ($realcnt != 0);
1382 # Measure the line length and indent.
1383 ($length, $indent) = line_stats($rawline);
1385 # Track the previous line.
1386 ($prevline, $stashline) = ($stashline, $line);
1387 ($previndent, $stashindent) = ($stashindent, $indent);
1388 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1390 #warn "line<$line>\n";
1392 } elsif ($realcnt == 1) {
1396 my $hunk_line = ($realcnt != 0);
1398 #make up the handle for any error we report on this line
1399 $prefix = "$filename:$realline: " if ($emacs && $file);
1400 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1401 $prefix = "::error file=$filename,line=$realline:\:" if ($github && $file);
1402 $prefix = "::error file=$realfile,line=$linenr:\:" if ($github && !$file);
1404 $here = "#$linenr: " if (!$file);
1405 $here = "#$realline: " if ($file);
1407 # extract the filename as it passes
1408 if ($line =~ /^diff --git.*?(\S+)$/) {
1410 $realfile =~ s@^([^/]*)/@@ if (!$file);
1411 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1412 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1414 $realfile =~ s@^([^/]*)/@@ if (!$file);
1415 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1418 if (!$file && $tree && $p1_prefix ne '' && defined $root &&
1419 -e "$root/$p1_prefix") {
1420 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1426 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1428 my $hereline = "$here\n$rawline\n";
1429 my $herecurr = "$here\n$rawline\n";
1430 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1432 $cnt_lines++ if ($realcnt != 0);
1434 # Check for incorrect file permissions
1435 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1436 my $permhere = $here . "FILE: $realfile\n";
1437 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1438 ERROR("do not set execute permissions for source files\n" . $permhere);
1442 # Accept git diff extended headers as valid patches
1443 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1447 # Filter out bad email addresses.
1448 if ($line =~ /^(Author|From): .*noreply.*/) {
1449 ERROR("Real email adress is needed\n" . $herecurr);
1452 #check the patch for a signoff:
1453 if ($line =~ /^\s*signed-off-by:/i) {
1454 # This is a signoff, if ugly, so do not double report.
1457 if (!($line =~ /^\s*Signed-off-by:/)) {
1458 ERROR("The correct form is \"Signed-off-by\"\n" .
1461 if ($line =~ /^\s*signed-off-by:\S/i) {
1462 ERROR("space required after Signed-off-by:\n" .
1467 # Check for wrappage within a valid hunk of the file
1468 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1469 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1470 $herecurr) if (!$emitted_corrupt++);
1473 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1474 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1475 $rawline !~ m/^$UTF8*$/) {
1476 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1478 my $blank = copy_spacing($rawline);
1479 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1480 my $hereptr = "$hereline$ptr\n";
1482 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1485 if ($rawline =~ m/$UTF8_MOJIBAKE/) {
1486 ERROR("Doubly-encoded UTF-8\n" . $herecurr);
1488 # Check if it's the start of a commit log
1489 # (not a header line and we haven't seen the patch filename)
1490 if ($in_header_lines && $realfile =~ /^$/ &&
1491 !($rawline =~ /^\s+\S/ ||
1492 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1493 $in_header_lines = 0;
1497 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1498 # declined it, i.e defined some charset where it is missing.
1499 if ($in_header_lines &&
1500 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1502 $non_utf8_charset = 1;
1505 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1506 $rawline =~ /$NON_ASCII_UTF8/) {
1507 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1510 # ignore non-hunk lines and lines being removed
1511 next if (!$hunk_line || $line =~ /^-/);
1513 #trailing whitespace
1514 if ($line =~ /^\+.*\015/) {
1515 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1516 ERROR("DOS line endings\n" . $herevet);
1518 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1519 $realfile =~ /^docs\/.+\.md/) {
1520 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1521 # TODO: properly check we're in a code block
1522 # (surrounding text is 4-column aligned)
1523 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1524 ERROR("code blocks in documentation should have " .
1525 "empty lines with exactly 4 columns of " .
1526 "whitespace\n" . $herevet);
1528 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1529 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1530 ERROR("trailing whitespace\n" . $herevet);
1534 # check we are in a valid source file if not then ignore this hunk
1535 next if ($realfile !~ /$SrcFile/);
1537 #120 column limit; exempt URLs, if no other words on line
1538 if ($line =~ /^\+/ &&
1539 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1540 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1542 $realfile !~ /\/tests\//)
1544 if ($length > 120) {
1545 ERROR("line over 120 characters\n" . $herecurr);
1547 WARN("line over 80 characters\n" . $herecurr);
1551 # check for spaces before a quoted newline
1552 if ($rawline =~ /^.*\".*\s\\n/) {
1553 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
1556 # check for adding lines without a newline.
1557 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1558 ERROR("adding a line without newline at end of file\n" . $herecurr);
1561 # check for RCS/CVS revision markers
1562 if ($rawline =~ /^\+.*\$(FreeBSD|Revision|Log|Id)(?:\$|\b)/) {
1563 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1566 # check we are in a valid C source file if not then ignore this hunk
1567 next if ($realfile !~ /\.(h|c|cpp)$/);
1569 # Block comment styles
1571 # Block comments use /* on a line of its own
1572 if ($rawline !~ m@^\+.*/\*.*\*/[ \t)}]*$@ && #inline /*...*/
1573 $rawline =~ m@^\+.*/\*[*-]?+[ \t]*[^ \t]@) { # /* or /** or /*- non-blank
1574 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1577 # Block comments use * on subsequent lines
1578 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1579 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1580 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1581 $rawline =~ /^\+/ && #line is new
1582 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1583 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1586 # Block comments use */ on trailing lines
1587 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1588 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1589 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1590 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1591 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1594 # Block comment * alignment
1595 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1596 $line =~ /^\+[ \t]*$;/ && #leading comment
1597 $rawline =~ /^\+[ \t]*\*/ && #leading *
1598 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1599 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1600 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1602 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1604 $oldindent = expand_tabs($1);
1606 $prevrawline =~ m@^\+(.*/?)\*@;
1607 $oldindent = expand_tabs($1);
1609 $rawline =~ m@^\+([ \t]*)\*@;
1611 $newindent = expand_tabs($newindent);
1612 if (length($oldindent) ne length($newindent)) {
1613 WARN("Block comments should align the * on each line\n" . $hereprev);
1617 # Check for potential 'bare' types
1618 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1620 if ($realcnt && $line =~ /.\s*\S/) {
1621 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1622 ctx_statement_block($linenr, $realcnt, 0);
1623 $stat =~ s/\n./\n /g;
1624 $cond =~ s/\n./\n /g;
1626 # Find the real next line.
1627 $realline_next = $line_nr_next;
1628 if (defined $realline_next &&
1629 (!defined $lines[$realline_next - 1] ||
1630 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1637 # Ignore goto labels.
1638 if ($s =~ /$Ident:\*$/s) {
1640 # Ignore functions being called
1641 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1643 } elsif ($s =~ /^.\s*else\b/s) {
1645 # declarations always start with types
1646 } 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) {
1649 possible($type, "A:" . $s);
1651 # definitions in global scope can only start with types
1652 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1653 possible($1, "B:" . $s);
1656 # any (foo ... *) is a pointer cast, and foo is a type
1657 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1658 possible($1, "C:" . $s);
1661 # Check for any sort of function declaration.
1662 # int foo(something bar, other baz);
1663 # void (*store_gdt)(x86_descr_ptr *);
1664 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1665 my ($name_len) = length($1);
1668 substr($ctx, 0, $name_len + 1, '');
1669 $ctx =~ s/\)[^\)]*$//;
1671 for my $arg (split(/\s*,\s*/, $ctx)) {
1672 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1674 possible($1, "D:" . $s);
1682 # Checks which may be anchored in the context.
1685 # Check for switch () and associated case and default
1686 # statements should be at the same indent.
1687 if ($line=~/\bswitch\s*\(.*\)/) {
1690 my @ctx = ctx_block_outer($linenr, $realcnt);
1692 for my $ctx (@ctx) {
1693 my ($clen, $cindent) = line_stats($ctx);
1694 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1695 $indent != $cindent) {
1696 $err .= "$sep$ctx\n";
1703 ERROR("switch and case should be at the same indent\n$hereline$err");
1707 # if/while/etc brace do not go on next line, unless defining a do while loop,
1708 # or if that brace on the next line is for something else
1709 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1710 my $pre_ctx = "$1$2";
1712 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1713 my $ctx_cnt = $realcnt - $#ctx - 1;
1714 my $ctx = join("\n", @ctx);
1716 my $ctx_ln = $linenr;
1717 my $ctx_skip = $realcnt;
1719 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1720 defined $lines[$ctx_ln - 1] &&
1721 $lines[$ctx_ln - 1] =~ /^-/)) {
1722 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1723 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1727 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1728 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1730 # The length of the "previous line" is checked against 80 because it
1731 # includes the + at the beginning of the line (if the actual line has
1732 # 79 or 80 characters, it is no longer possible to add a space and an
1733 # opening brace there)
1734 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1735 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1736 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1737 ERROR("that open brace { should be on the previous line\n" .
1738 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1740 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1741 $ctx =~ /\)\s*\;\s*$/ &&
1742 defined $lines[$ctx_ln - 1])
1744 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1745 if ($nindent > $indent) {
1746 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
1747 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1752 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1753 if ($line =~ /while\s*\((0|false)\);/) {
1754 ERROR("suspicious ; after while (0)\n" . $herecurr);
1757 # Check superfluous trailing ';'
1758 if ($line =~ /;;$/) {
1759 ERROR("superfluous trailing semicolon\n" . $herecurr);
1762 # Check relative indent for conditionals and blocks.
1763 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1764 my ($s, $c) = ($stat, $cond);
1766 substr($s, 0, length($c), '');
1768 # Make sure we remove the line prefixes as we have
1769 # none on the first line, and are going to re-add them
1773 # Find out how long the conditional actually is.
1774 my @newlines = ($c =~ /\n/gs);
1775 my $cond_lines = 1 + $#newlines;
1777 # We want to check the first line inside the block
1778 # starting at the end of the conditional, so remove:
1779 # 1) any blank line termination
1780 # 2) any opening brace { on end of the line
1782 my $continuation = 0;
1784 $s =~ s/^.*\bdo\b//;
1786 if ($s =~ s/^\s*\\//) {
1789 if ($s =~ s/^\s*?\n//) {
1794 # Also ignore a loop construct at the end of a
1795 # preprocessor statement.
1796 if (($prevline =~ /^.\s*#\s*define\s/ ||
1797 $prevline =~ /\\\s*$/) && $continuation == 0) {
1803 while ($cond_ptr != $cond_lines) {
1804 $cond_ptr = $cond_lines;
1806 # If we see an #else/#elif then the code
1808 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1813 # 1) blank lines, they should be at 0,
1814 # 2) preprocessor lines, and
1816 if ($continuation ||
1818 $s =~ /^\s*#\s*?/ ||
1819 $s =~ /^\s*$Ident\s*:/) {
1820 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1821 if ($s =~ s/^.*?\n//) {
1827 my (undef, $sindent) = line_stats("+" . $s);
1828 my $stat_real = raw_line($linenr, $cond_lines);
1830 # Check if either of these lines are modified, else
1831 # this is not this patch's fault.
1832 if (!defined($stat_real) ||
1833 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1836 if (defined($stat_real) && $cond_lines > 1) {
1837 $stat_real = "[...]\n$stat_real";
1840 #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";
1842 if ($check && (($sindent % 4) != 0 ||
1843 ($sindent <= $indent && $s ne ''))) {
1844 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1848 # Track the 'values' across context and added lines.
1849 my $opline = $line; $opline =~ s/^./ /;
1850 my ($curr_values, $curr_vars) =
1851 annotate_values($opline . "\n", $prev_values);
1852 $curr_values = $prev_values . $curr_values;
1854 my $outline = $opline; $outline =~ s/\t/ /g;
1855 print "$linenr > .$outline\n";
1856 print "$linenr > $curr_values\n";
1857 print "$linenr > $curr_vars\n";
1859 $prev_values = substr($curr_values, -1);
1861 #ignore lines not being added
1862 if ($line=~/^[^\+]/) {next;}
1864 # check for initialisation to aggregates open brace on the next line
1865 if ($line =~ /^.\s*\{/ &&
1866 $prevline =~ /(?:^|[^=])=\s*$/) {
1867 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1871 # Checks which are anchored on the added line.
1874 # check for malformed paths in #include statements (uses RAW line)
1875 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1877 if ($path =~ m{//}) {
1878 ERROR("malformed #include filename\n" .
1883 # Remove C99 comments.
1885 $opline =~ s@//.*@@;
1887 # * goes on variable not on type
1889 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1890 my ($from, $to) = ($1, $1);
1892 # Should start with a space.
1893 $to =~ s/^(\S)/ $1/;
1894 # Should not end with a space.
1896 # '*'s should not have spaces between.
1897 while ($to =~ s/\*\s+\*/\*\*/) {
1900 #print "from<$from> to<$to>\n";
1902 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1904 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1905 my ($from, $to, $ident) = ($1, $1, $2);
1907 # Should start with a space.
1908 $to =~ s/^(\S)/ $1/;
1909 # Should not end with a space.
1911 # '*'s should not have spaces between.
1912 while ($to =~ s/\*\s+\*/\*\*/) {
1914 # Modifiers should have spaces.
1915 $to =~ s/(\b$Modifier$)/$1 /;
1917 #print "from<$from> to<$to> ident<$ident>\n";
1918 if ($from ne $to && $ident !~ /^$Modifier$/) {
1919 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1923 # function brace can't be on same line, except for #defines of do while,
1924 # or if closed on same line
1925 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1926 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1927 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1930 # missing space after union, struct or enum definition
1931 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1932 ERROR("missing space after $1 definition\n" . $herecurr);
1935 # check for spacing round square brackets; allowed:
1936 # 1. with a type on the left -- int [] a;
1937 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1938 # 3. inside a curly brace -- = { [0...10] = 5 }
1939 # 4. after a comma -- [1] = 5, [2] = 6
1940 # 5. in a macro definition -- #define abc(x) [x] = y
1941 while ($line =~ /(.*?\s)\[/g) {
1942 my ($where, $prefix) = ($-[1], $1);
1943 if ($prefix !~ /$Type\s+$/ &&
1944 ($where != 0 || $prefix !~ /^.\s+$/) &&
1945 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1946 $prefix !~ /[,{:]\s+$/) {
1947 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1951 # check for spaces between functions and their parentheses.
1952 while ($line =~ /($Ident)\s+\(/g) {
1954 my $ctx_before = substr($line, 0, $-[1]);
1955 my $ctx = "$ctx_before$name";
1957 # Ignore those directives where spaces _are_ permitted.
1959 if|for|while|switch|return|case|
1960 volatile|__volatile__|coroutine_fn|
1961 __attribute__|format|__extension__|
1965 # Ignore 'catch (...)' in C++
1966 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1968 # cpp #define statements have non-optional spaces, ie
1969 # if there is a space between the name and the open
1970 # parenthesis it is simply not a parameter group.
1971 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1973 # cpp #elif statement condition may start with a (
1974 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1976 # If this whole things ends with a type its most
1977 # likely a typedef for a function.
1978 } elsif ($ctx =~ /$Type$/) {
1981 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1984 # Check operator spacing.
1985 if (!($line=~/\#\s*include/)) {
1987 <<=|>>=|<=|>=|==|!=|
1988 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1989 =>|->|<<|>>|<|>|=|!|~|
1990 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1993 my @elements = split(/($ops|;)/, $opline);
1996 my $blank = copy_spacing($opline);
1998 for (my $n = 0; $n < $#elements; $n += 2) {
1999 $off += length($elements[$n]);
2001 # Pick up the preceding and succeeding characters.
2002 my $ca = substr($opline, 0, $off);
2004 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2005 $cc = substr($opline, $off + length($elements[$n + 1]));
2007 my $cb = "$ca$;$cc";
2010 $a = 'V' if ($elements[$n] ne '');
2011 $a = 'W' if ($elements[$n] =~ /\s$/);
2012 $a = 'C' if ($elements[$n] =~ /$;$/);
2013 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2014 $a = 'O' if ($elements[$n] eq '');
2015 $a = 'E' if ($ca =~ /^\s*$/);
2017 my $op = $elements[$n + 1];
2020 if (defined $elements[$n + 2]) {
2021 $c = 'V' if ($elements[$n + 2] ne '');
2022 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2023 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2024 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2025 $c = 'O' if ($elements[$n + 2] eq '');
2026 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2031 my $ctx = "${a}x${c}";
2033 my $at = "(ctx:$ctx)";
2035 my $ptr = substr($blank, 0, $off) . "^";
2036 my $hereptr = "$hereline$ptr\n";
2038 # Pull out the value of this operator.
2039 my $op_type = substr($curr_values, $off + 1, 1);
2041 # Get the full operator variant.
2042 my $opv = $op . substr($curr_vars, $off, 1);
2044 # Ignore operators passed as parameters.
2045 if ($op_type ne 'V' &&
2046 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2049 # } elsif ($op =~ /^$;+$/) {
2051 # ; should have either the end of line or a space or \ after it
2052 } elsif ($op eq ';') {
2053 if ($ctx !~ /.x[WEBC]/ &&
2054 $cc !~ /^\\/ && $cc !~ /^;/) {
2055 ERROR("space required after that '$op' $at\n" . $hereptr);
2059 } elsif ($op eq '//') {
2061 # Ignore : used in class declaration in C++
2062 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2063 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2067 # : when part of a bitfield
2068 } elsif ($op eq '->' || $opv eq ':B') {
2069 if ($ctx =~ /Wx.|.xW/) {
2070 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2073 # , must have a space on the right.
2074 # not required when having a single },{ on one line
2075 } elsif ($op eq ',') {
2076 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2077 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2078 ERROR("space required after that '$op' $at\n" . $hereptr);
2081 # '*' as part of a type definition -- reported already.
2082 } elsif ($opv eq '*_') {
2083 #warn "'*' is part of type\n";
2085 # unary operators should have a space before and
2086 # none after. May be left adjacent to another
2087 # unary operator, or a cast
2088 } elsif ($op eq '!' || $op eq '~' ||
2089 $opv eq '*U' || $opv eq '-U' ||
2090 $opv eq '&U' || $opv eq '&&U') {
2091 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2092 # '~' used as a name of Destructor
2094 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2095 ERROR("space required before that '$op' $at\n" . $hereptr);
2097 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2098 # A unary '*' may be const
2100 } elsif ($ctx =~ /.xW/) {
2101 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2104 # unary ++ and unary -- are allowed no space on one side.
2105 } elsif ($op eq '++' or $op eq '--') {
2106 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2107 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2109 if ($ctx =~ /Wx[BE]/ ||
2110 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2111 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2113 if ($ctx =~ /ExW/) {
2114 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2117 # A colon needs no spaces before when it is
2118 # terminating a case value or a label.
2119 } elsif ($opv eq ':C' || $opv eq ':L') {
2120 if ($ctx =~ /Wx./) {
2121 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2124 # All the others need spaces both sides.
2125 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2128 if ($realfile =~ /\.cpp|\.h$/) {
2129 # Ignore template arguments <...> in C++
2130 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2140 # Ignore email addresses <foo@bar>
2142 $cc =~ /^\S+\@\S+>/) ||
2144 $ca =~ /<\S+\@\S+$/))
2150 if (($opv eq ':O' && $ca =~ /\?$/) ||
2151 ($op eq '?' && $cc =~ /^:/)) {
2156 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2159 $off += length($elements[$n + 1]);
2163 #need space before brace following if, while, etc
2164 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2166 ERROR("space required before the open brace '{'\n" . $herecurr);
2169 # closing brace should have a space following it when it has anything
2171 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2172 ERROR("space required after that close brace '}'\n" . $herecurr);
2175 # check spacing on square brackets
2176 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2177 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2179 if ($line =~ /\s\]/) {
2180 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2183 # check spacing on parentheses
2184 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2185 $line !~ /for\s*\(\s+;/) {
2186 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2188 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2189 $line !~ /for\s*\(.*;\s+\)/ &&
2190 $line !~ /:\s+\)/) {
2191 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2194 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
2195 $line !~ /^.typedef/) {
2196 ERROR("named $1 should be typedefed separately\n" . $herecurr);
2199 # Return needs parens
2200 if ($line =~ /^.\s*return [^(]/) {
2201 ERROR("parentheses required on return\n" . $herecurr);
2204 # Need a space before open parenthesis after if, while etc
2205 if ($line=~/\b(if|while|for|switch|return)\(/) {
2206 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2209 # Check for illegal assignment in if conditional -- and check for trailing
2210 # statements after the conditional.
2211 if ($line =~ /do\s*(?!{)/) {
2212 my ($stat_next) = ctx_statement_block($line_nr_next,
2213 $remain_next, $off_next);
2214 $stat_next =~ s/\n./\n /g;
2215 ##print "stat<$stat> stat_next<$stat_next>\n";
2217 if ($stat_next =~ /^\s*while\b/) {
2218 # If the statement carries leading newlines,
2219 # then count those as offsets.
2221 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2223 statement_rawlines($whitespace) - 1;
2225 $suppress_whiletrailers{$line_nr_next +
2229 if (!defined $suppress_whiletrailers{$linenr} &&
2230 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2231 my ($s, $c) = ($stat, $cond);
2233 # if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2234 # ERROR("do not use assignment in if condition\n" . $herecurr);
2237 # Find out what is on the end of the line after the
2239 substr($s, 0, length($c), '');
2241 $s =~ s/$;//g; # Remove any comments
2242 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2243 $c !~ /}\s*while\s*/)
2245 # Find out how long the conditional actually is.
2246 my @newlines = ($c =~ /\n/gs);
2247 my $cond_lines = 1 + $#newlines;
2250 $stat_real = raw_line($linenr, $cond_lines)
2251 . "\n" if ($cond_lines);
2252 if (defined($stat_real) && $cond_lines > 1) {
2253 $stat_real = "[...]\n$stat_real";
2256 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2260 # Check for bitwise tests written as boolean
2272 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2275 # if and else should not have general statements after it
2276 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2278 $s =~ s/$;//g; # Remove any comments
2279 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2280 ERROR("trailing statements should be on next line\n" . $herecurr);
2283 # if should not continue a brace
2284 if ($line =~ /}\s*if\b/) {
2285 ERROR("trailing statements should be on next line\n" .
2288 # case and default should not have general statements after them
2289 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2291 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2295 ERROR("trailing statements should be on next line\n" . $herecurr);
2298 # Check for }<nl>else {, these must be at the same
2299 # indent level to be relevant to each other.
2300 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2301 $previndent == $indent) {
2302 ERROR("else should follow close brace '}'\n" . $hereprev);
2305 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2306 $previndent == $indent) {
2307 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2309 # Find out what is on the end of the line after the
2311 substr($s, 0, length($c), '');
2314 if ($s =~ /^\s*;/) {
2315 ERROR("while should follow close brace '}'\n" . $hereprev);
2319 #no spaces allowed after \ in define
2320 if ($line=~/\#\s*define.*\\\s$/) {
2321 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
2324 # multi-statement macros should be enclosed in a do while loop, grab the
2325 # first statement and ensure its the whole macro if its not enclosed
2326 # in a known good container
2327 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2328 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2331 my ($off, $dstat, $dcond, $rest);
2334 my $args = defined($1);
2336 # Find the end of the macro and limit our statement
2338 while ($cnt > 0 && defined $lines[$ln - 1] &&
2339 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2341 $ctx .= $rawlines[$ln - 1] . "\n";
2342 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2345 $ctx .= $rawlines[$ln - 1];
2347 ($dstat, $dcond, $ln, $cnt, $off) =
2348 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2349 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2350 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2352 # Extract the remainder of the define (if any) and
2353 # rip off surrounding spaces, and trailing \'s.
2355 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2356 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2357 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2358 $rest .= substr($lines[$ln - 1], $off) . "\n";
2364 $rest =~ s/\\\n.//g;
2368 # Clean up the original statement.
2370 substr($dstat, 0, length($dcond), '');
2372 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2375 $dstat =~ s/\\\n.//g;
2376 $dstat =~ s/^\s*//s;
2377 $dstat =~ s/\s*$//s;
2379 # Flatten any parentheses and braces
2380 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2381 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2382 $dstat =~ s/\[[^\{\}]*\]/1/)
2386 my $exceptions = qr{
2398 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2399 if ($rest ne '' && $rest ne ',') {
2400 if ($rest !~ /while\s*\(/ &&
2401 $dstat !~ /$exceptions/)
2403 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2406 } elsif ($ctx !~ /;/) {
2408 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2409 $dstat !~ /$exceptions/ &&
2410 $dstat !~ /^\.$Ident\s*=/ &&
2411 $dstat =~ /$Operators/)
2413 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2418 # check for missing bracing around if etc
2419 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2420 $line !~ /\#\s*if/) {
2423 # Check the pre-context.
2424 if ($line =~ /(\}.*?)$/) {
2427 if ($line !~ /else/) {
2428 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2433 my ($level, $endln, @chunks) =
2434 ctx_statement_full($linenr, $realcnt, 1);
2436 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2437 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2440 if ($#chunks >= 0 && $level == 0) {
2442 my $herectx = $here . "\n";
2443 my $ln = $linenr - 1;
2444 for my $chunk (@chunks) {
2445 my ($cond, $block) = @{$chunk};
2447 # If the condition carries leading newlines, then count those as offsets.
2448 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2449 my $offset = statement_rawlines($whitespace) - 1;
2451 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2453 # We have looked at and allowed this specific line.
2454 $suppress_ifbraces{$ln + $offset} = 1;
2456 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2457 $ln += statement_rawlines($block) - 1;
2459 substr($block, 0, length($cond), '');
2461 my $spaced_block = $block;
2462 $spaced_block =~ s/\n\+/ /g;
2464 $seen++ if ($spaced_block =~ /^\s*\{/);
2466 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2468 if (statement_lines($cond) > 1) {
2469 print "APW: ALLOWED: cond<$cond>\n"
2473 if ($block =~/\b(?:if|for|while)\b/) {
2474 print "APW: ALLOWED: block<$block>\n"
2478 if (statement_block_size($block) > 1) {
2479 print "APW: ALLOWED: lines block<$block>\n"
2484 $allowed=1; # disable for now.
2485 if ($seen != ($#chunks + 1) && !$allowed) {
2486 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
2490 if (!defined $suppress_ifbraces{$linenr - 1} &&
2491 $line =~ /\b(if|while|for|else)\b/ &&
2492 $line !~ /\#\s*if/ &&
2493 $line !~ /\#\s*else/) {
2496 # Check the pre-context.
2497 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2500 if ($line !~ /else/) {
2501 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2507 my ($level, $endln, @chunks) =
2508 ctx_statement_full($linenr, $realcnt, $-[0]);
2510 # Check the condition.
2511 my ($cond, $block) = @{$chunks[0]};
2512 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2513 if $dbg_adv_checking;
2514 if (defined $cond) {
2515 substr($block, 0, length($cond), '');
2517 if (statement_lines($cond) > 1) {
2518 print "APW: ALLOWED: cond<$cond>\n"
2522 if ($block =~/\b(?:if|for|while)\b/) {
2523 print "APW: ALLOWED: block<$block>\n"
2527 if (statement_block_size($block) > 1) {
2528 print "APW: ALLOWED: lines block<$block>\n"
2532 # Check the post-context.
2533 if (defined $chunks[1]) {
2534 my ($cond, $block) = @{$chunks[1]};
2535 if (defined $cond) {
2536 substr($block, 0, length($cond), '');
2538 if ($block =~ /^\s*\{/) {
2539 print "APW: ALLOWED: chunk-1 block<$block>\n"
2544 print "DCS: level=$level block<$block> allowed=$allowed\n"
2546 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2547 my $herectx = $here . "\n";;
2548 my $cnt = statement_rawlines($block);
2550 for (my $n = 0; $n < $cnt; $n++) {
2551 $herectx .= raw_line($linenr, $n) . "\n";;
2554 WARN("braces {} are encouraged even for single statement blocks\n" . $herectx);
2559 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2560 ERROR("if this code is redundant consider removing it\n" .
2564 # Check that the storage class is at the beginning of a declaration
2565 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2566 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
2569 # check the location of the inline attribute, that it is between
2570 # storage class and type.
2571 if ($line =~ /\b$Type\s+$Inline\b/ ||
2572 $line =~ /\b$Inline\s+$Storage\b/) {
2573 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2576 # check for sizeof(&)
2577 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2578 ERROR("sizeof(& should be avoided\n" . $herecurr);
2581 # check for new externs in .c files.
2582 if ($realfile =~ /\.c$/ && defined $stat &&
2583 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2585 my $function_name = $1;
2586 my $paren_space = $2;
2589 if (defined $cond) {
2590 substr($s, 0, length($cond), '');
2592 if ($s =~ /^\s*;/ &&
2593 $function_name ne 'uninitialized_var')
2595 ERROR("externs should be avoided in .c files\n" . $herecurr);
2598 if ($paren_space =~ /\n/) {
2599 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
2602 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2603 $stat =~ /^.\s*extern\s+/)
2605 ERROR("externs should be avoided in .c files\n" . $herecurr);
2608 # check for gcc specific __FUNCTION__
2609 if ($line =~ /__FUNCTION__/) {
2610 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2613 # recommend sigaction over signal for portability, when establishing a handler
2614 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2615 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2618 # format strings checks
2620 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2621 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2622 $string =~ s/%%/__/g;
2623 # check for %L{u,d,i} in strings
2624 if ($string =~ /(?<!%)%L[udi]/) {
2625 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2629 # Continue checking for error messages that contains newlines. This
2630 # check handles cases where string literals are spread over multiple lines.
2632 # error_report("Error msg line #1"
2633 # "Error msg line #2\n");
2634 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2635 my $continued_str_literal = qr{\+\s*\".*\"};
2637 if ($rawline =~ /$quoted_newline_regex/) {
2638 # Backtrack to first line that does not contain only a quoted literal
2639 # and assume that it is the start of the statement.
2640 my $i = $linenr - 2;
2642 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2649 # If we have no input at all, then there is nothing to report on
2650 # so just keep quiet.
2651 if ($#rawlines == -1) {
2655 # In mailback mode only produce a report in the negative, for
2656 # things that appear to be patches.
2657 if ($mailback && ($clean == 1 || !$is_patch)) {
2661 # This is not a patch, and we are are in 'no-patch' mode so
2663 if (!$chk_patch && !$is_patch) {
2667 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
2668 ERROR("Does not appear to be a unified-diff format patch\n");
2671 print report_dump();
2672 if ($summary && !($clean == 1 && $quiet == 1)) {
2673 print "$filename " if ($summary_file);
2674 print "total: $cnt_error errors, $cnt_warn warnings, " .
2675 "$cnt_lines lines checked\n";
2676 print "\n" if ($quiet == 0);
2680 # If there were whitespace errors which cleanpatch can fix
2681 # then suggest that.
2682 # if ($rpt_cleaners) {
2683 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2684 # print " scripts/cleanfile\n\n";
2688 if ($clean == 1 && $quiet == 0) {
2689 print "$vname has no obvious style problems and is ready for submission.\n"
2692 return ($no_warnings ? $clean : $cnt_error == 0);