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;
45 $P [OPTION]... [FILE]...
46 $P [OPTION]... [GIT-REV-LIST]
52 --patch treat FILE as patchfile
53 --branch treat args as GIT revision list
54 --emacs emacs compile window format
55 --terse one line per report
56 -f, --file treat FILE as regular source file
57 --strict fail if only warnings are found
58 --no-summary suppress the per-file summary
59 --mailback only produce a report in case of warnings/errors
60 --summary-file include the filename in summary
61 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
62 'values', 'possible', 'type', and 'attr' (default
64 --test-only=WORD report only warnings/errors containing WORD
66 --color[=WHEN] Use colors 'always', 'never', or only when output
67 is a terminal ('auto'). Default is 'auto'.
68 -h, --help, --version display this help and exit
70 When FILE is - read standard input.
76 # Use at your own risk
77 print "\n", MAGENTA, "WARNING:", RESET, " This code is highly experimental ... likely isn't a great style(9) match yet\n\n";
79 # Perl's Getopt::Long allows options to take optional arguments after a space.
80 # Prevent --color by itself from consuming other arguments
82 if ($_ eq "--color" || $_ eq "-color") {
83 $_ = "--color=$color";
88 'q|quiet+' => \$quiet,
90 'signoff!' => \$chk_signoff,
91 'patch!' => \$chk_patch,
92 'branch!' => \$chk_branch,
96 'strict!' => \$no_warnings,
98 'summary!' => \$summary,
99 'mailback!' => \$mailback,
100 'summary-file!' => \$summary_file,
102 'debug=s' => \%debug,
103 'test-only=s' => \$tst_only,
104 'color=s' => \$color,
105 'no-color' => sub { $color = 'never'; },
115 print "$P: no input files\n";
119 if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
120 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
121 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
122 $chk_patch = $chk_branch || $file ? 0 : 1;
123 } elsif (!defined $chk_branch && !defined $chk_patch) {
125 $chk_branch = $chk_patch = 0;
127 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
128 $chk_patch = $chk_branch ? 0 : 1;
130 } elsif (!defined $chk_branch && !defined $file) {
132 $chk_branch = $file = 0;
134 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
135 $file = $chk_branch ? 0 : 1;
137 } elsif (!defined $chk_patch && !defined $file) {
139 $chk_patch = $file = 0;
141 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
142 $chk_patch = $file ? 0 : 1;
144 } elsif (!defined $chk_branch) {
145 $chk_branch = $chk_patch || $file ? 0 : 1;
146 } elsif (!defined $chk_patch) {
147 $chk_patch = $chk_branch || $file ? 0 : 1;
148 } elsif (!defined $file) {
149 $file = $chk_patch || $chk_branch ? 0 : 1;
152 if (($chk_patch && $chk_branch) ||
153 ($chk_patch && $file) ||
154 ($chk_branch && $file)) {
155 die "Only one of --file, --branch, --patch is permitted\n";
157 if (!$chk_patch && !$chk_branch && !$file) {
158 die "One of --file, --branch, --patch is required\n";
161 if ($color =~ /^always$/i) {
163 } elsif ($color =~ /^never$/i) {
165 } elsif ($color =~ /^auto$/i) {
166 $color = (-t STDOUT);
168 die "Invalid color mode: $color\n";
172 my $dbg_possible = 0;
174 my $dbg_adv_checking = 0;
176 for my $key (keys %debug) {
178 eval "\${dbg_$key} = '$debug{$key}';";
182 my $rpt_cleaners = 0;
189 my $emitted_corrupt = 0;
192 [A-Za-z_][A-Za-z\d_]*
193 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
195 our $Storage = qr{extern|static|asmlinkage};
200 # Notes to $Attribute:
205 QEMU_WARN_UNUSED_RESULT|
211 our $Inline = qr{inline};
212 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
213 our $Lval = qr{$Ident(?:$Member)*};
215 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
216 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
217 our $Compare = qr{<=|>=|==|!=|<|>};
221 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
228 our $NON_ASCII_UTF8 = qr{
229 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
230 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
231 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
232 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
233 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
234 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
235 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
239 [\x09\x0A\x0D\x20-\x7E] # ASCII
243 # some readers default to ISO-8859-1 when showing email source. detect
244 # when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back.
245 # False positives are possible but very unlikely.
246 our $UTF8_MOJIBAKE = qr{
247 \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf
248 | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf
249 | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf 80-bf
250 | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf
251 | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-bf
252 | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf 80-bf
253 | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-bf
256 # There are still some false positives, but this catches most
258 our $typeTypedefs = qr{(?x:
259 (?![KMGTPE]iB) # IEC binary prefix (do not match)
260 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase
261 | [A-Z][A-Z\d_]*AIOCB # all uppercase
262 | [A-Z][A-Z\d_]*CPU # all uppercase
263 | QEMUBH # all uppercase
268 qr{(?:unsigned\s+)?char},
269 qr{(?:unsigned\s+)?short},
270 qr{(?:unsigned\s+)?int},
271 qr{(?:unsigned\s+)?long},
272 qr{(?:unsigned\s+)?long\s+int},
273 qr{(?:unsigned\s+)?long\s+long},
274 qr{(?:unsigned\s+)?long\s+long\s+int},
283 qr{${Ident}_handler},
284 qr{${Ident}_handler_fn},
285 qr{target_(?:u)?long},
289 # This can be modified by sub possible. Since it can be empty, be careful
290 # about regexes that always match, because they can cause infinite loops.
291 our @modifierList = (
295 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
296 if (@modifierList > 0) {
297 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
298 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
300 $Modifier = qr{(?:$Attribute|$Sparse)};
303 (?:$Modifier\s+|const\s+)*
305 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
309 (?:\s+$Modifier|\s+const)*
313 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
314 (?:\s+$Inline|\s+$Modifier)*
316 $Declare = qr{(?:$Storage\s+)?$Type};
320 $chk_signoff = 0 if ($file);
327 my %git_commits = ();
329 open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format=%H %s", $ARGV[0]) ||
330 die "$P: git log --reverse --no-merges --format='%H %s' $ARGV[0] failed - $!\n";
332 for my $line (<$HASH>) {
333 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
334 next if (!defined($1) || !defined($2));
337 push(@patches, $sha1);
338 $git_commits{$sha1} = $subject;
343 die "$P: no revisions returned for revlist '$ARGV[0]'\n"
347 my $num_patches = @patches;
348 for my $hash (@patches) {
350 open($FILE, '-|', "git", "show", "--patch-with-stat", $hash) ||
351 die "$P: git show $hash - $!\n";
357 $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')';
358 if ($num_patches > 1 && $quiet == 0) {
359 my $prefix = "$i/$num_patches";
360 $prefix = BLUE . BOLD . $prefix . RESET if $color;
361 print "$prefix Checking commit $vname\n";
362 $vname = "Patch $i/$num_patches";
364 $vname = "Commit " . $vname;
366 if (!process($hash)) {
368 print "\n" if ($num_patches > 1 && $quiet == 0);
375 for my $filename (@ARGV) {
378 open($FILE, '-|', "diff -u /dev/null $filename") ||
379 die "$P: $filename: diff failed - $!\n";
380 } elsif ($filename eq '-') {
381 open($FILE, '<&STDIN');
383 open($FILE, '<', "$filename") ||
384 die "$P: $filename: open failed - $!\n";
386 if ($filename eq '-') {
387 $vname = 'Your patch';
391 print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0;
397 if (!process($filename)) {
407 sub top_of_kernel_tree {
411 "Makefile.inc1", "README.md", "sys",
415 foreach my $check (@tree_check) {
416 if (! -e $root . '/' . $check) {
428 for my $c (split(//, $str)) {
432 for (; ($n % 8) != 0; $n++) {
444 (my $res = shift) =~ tr/\t/ /c;
451 # Drop the diff line leader and expand tabs
453 $line = expand_tabs($line);
455 # Pick the indent from the front of the line.
456 my ($white) = ($line =~ /^(\s*)/);
458 return (length($line), length($white));
461 my $sanitise_quote = '';
463 sub sanitise_line_reset {
464 my ($in_comment) = @_;
467 $sanitise_quote = '*/';
469 $sanitise_quote = '';
482 # Always copy over the diff marker.
483 $res = substr($line, 0, 1);
485 for ($off = 1; $off < length($line); $off++) {
486 $c = substr($line, $off, 1);
488 # Comments we are wacking completely including the begin
489 # and end, all to $;.
490 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
491 $sanitise_quote = '*/';
493 substr($res, $off, 2, "$;$;");
497 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
498 $sanitise_quote = '';
499 substr($res, $off, 2, "$;$;");
503 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
504 $sanitise_quote = '//';
506 substr($res, $off, 2, $sanitise_quote);
511 # A \ in a string means ignore the next character.
512 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
514 substr($res, $off, 2, 'XX');
519 if ($c eq "'" || $c eq '"') {
520 if ($sanitise_quote eq '') {
521 $sanitise_quote = $c;
523 substr($res, $off, 1, $c);
525 } elsif ($sanitise_quote eq $c) {
526 $sanitise_quote = '';
530 #print "c<$c> SQ<$sanitise_quote>\n";
531 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
532 substr($res, $off, 1, $;);
533 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
534 substr($res, $off, 1, $;);
535 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
536 substr($res, $off, 1, 'X');
538 substr($res, $off, 1, $c);
542 if ($sanitise_quote eq '//') {
543 $sanitise_quote = '';
546 # The pathname on a #include may be surrounded by '<' and '>'.
547 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
548 my $clean = 'X' x length($1);
549 $res =~ s@\<.*\>@<$clean>@;
551 # The whole of a #error is a string.
552 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
553 my $clean = 'X' x length($1);
554 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
560 sub ctx_statement_block {
561 my ($linenr, $remain, $off) = @_;
562 my $line = $linenr - 1;
579 @stack = (['', 0]) if ($#stack == -1);
581 #warn "CSB: blk<$blk> remain<$remain>\n";
582 # If we are about to drop off the end, pull in more
585 for (; $remain > 0; $line++) {
586 last if (!defined $lines[$line]);
587 next if ($lines[$line] =~ /^-/);
590 $blk .= $lines[$line] . "\n";
595 # Bail if there is no further context.
596 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
602 $c = substr($blk, $off, 1);
603 $remainder = substr($blk, $off);
605 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
607 # Handle nested #if/#else.
608 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
609 push(@stack, [ $type, $level ]);
610 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
611 ($type, $level) = @{$stack[$#stack - 1]};
612 } elsif ($remainder =~ /^#\s*endif\b/) {
613 ($type, $level) = @{pop(@stack)};
616 # Statement ends at the ';' or a close '}' at the
618 if ($level == 0 && $c eq ';') {
622 # An else is really a conditional as long as its not else if
623 if ($level == 0 && $coff_set == 0 &&
624 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
625 $remainder =~ /^(else)(?:\s|{)/ &&
626 $remainder !~ /^else\s+if\b/) {
627 $coff = $off + length($1) - 1;
629 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
630 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
633 if (($type eq '' || $type eq '(') && $c eq '(') {
637 if ($type eq '(' && $c eq ')') {
639 $type = ($level != 0)? '(' : '';
641 if ($level == 0 && $coff < $soff) {
644 #warn "CSB: mark coff<$coff>\n";
647 if (($type eq '' || $type eq '{') && $c eq '{') {
651 if ($type eq '{' && $c eq '}') {
653 $type = ($level != 0)? '{' : '';
656 if (substr($blk, $off + 1, 1) eq ';') {
664 # We are truly at the end, so shuffle to the next line.
671 my $statement = substr($blk, $soff, $off - $soff + 1);
672 my $condition = substr($blk, $soff, $coff - $soff + 1);
674 #warn "STATEMENT<$statement>\n";
675 #warn "CONDITION<$condition>\n";
677 #print "coff<$coff> soff<$off> loff<$loff>\n";
679 return ($statement, $condition,
680 $line, $remain + 1, $off - $loff + 1, $level);
683 sub statement_lines {
686 # Strip the diff line prefixes and rip blank lines at start and end.
687 $stmt =~ s/(^|\n)./$1/g;
691 my @stmt_lines = ($stmt =~ /\n/g);
693 return $#stmt_lines + 2;
696 sub statement_rawlines {
699 my @stmt_lines = ($stmt =~ /\n/g);
701 return $#stmt_lines + 2;
704 sub statement_block_size {
707 $stmt =~ s/(^|\n)./$1/g;
713 my @stmt_lines = ($stmt =~ /\n/g);
714 my @stmt_statements = ($stmt =~ /;/g);
716 my $stmt_lines = $#stmt_lines + 2;
717 my $stmt_statements = $#stmt_statements + 1;
719 if ($stmt_lines > $stmt_statements) {
722 return $stmt_statements;
726 sub ctx_statement_full {
727 my ($linenr, $remain, $off) = @_;
728 my ($statement, $condition, $level);
732 # Grab the first conditional/block pair.
733 ($statement, $condition, $linenr, $remain, $off, $level) =
734 ctx_statement_block($linenr, $remain, $off);
735 #print "F: c<$condition> s<$statement> remain<$remain>\n";
736 push(@chunks, [ $condition, $statement ]);
737 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
738 return ($level, $linenr, @chunks);
741 # Pull in the following conditional/block pairs and see if they
742 # could continue the statement.
744 ($statement, $condition, $linenr, $remain, $off, $level) =
745 ctx_statement_block($linenr, $remain, $off);
746 #print "C: c<$condition> s<$statement> remain<$remain>\n";
747 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
749 push(@chunks, [ $condition, $statement ]);
752 return ($level, $linenr, @chunks);
756 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
758 my $start = $linenr - 1;
765 my @stack = ($level);
766 for ($line = $start; $remain > 0; $line++) {
767 next if ($rawlines[$line] =~ /^-/);
770 $blk .= $rawlines[$line];
772 # Handle nested #if/#else.
773 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
774 push(@stack, $level);
775 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
776 $level = $stack[$#stack - 1];
777 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
778 $level = pop(@stack);
781 foreach my $c (split(//, $lines[$line])) {
782 ##print "C<$c>L<$level><$open$close>O<$off>\n";
788 if ($c eq $close && $level > 0) {
790 last if ($level == 0);
791 } elsif ($c eq $open) {
796 if (!$outer || $level <= 1) {
797 push(@res, $rawlines[$line]);
800 last if ($level == 0);
803 return ($level, @res);
805 sub ctx_block_outer {
806 my ($linenr, $remain) = @_;
808 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
812 my ($linenr, $remain) = @_;
814 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
818 my ($linenr, $remain, $off) = @_;
820 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
823 sub ctx_block_level {
824 my ($linenr, $remain) = @_;
826 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
828 sub ctx_statement_level {
829 my ($linenr, $remain, $off) = @_;
831 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
834 sub ctx_locate_comment {
835 my ($first_line, $end_line) = @_;
837 # Catch a comment on the end of the line itself.
838 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
839 return $current_comment if (defined $current_comment);
841 # Look through the context and try and figure out if there is a
844 $current_comment = '';
845 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
846 my $line = $rawlines[$linenr - 1];
848 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
851 if ($line =~ m@/\*@) {
854 if (!$in_comment && $current_comment ne '') {
855 $current_comment = '';
857 $current_comment .= $line . "\n" if ($in_comment);
858 if ($line =~ m@\*/@) {
863 chomp($current_comment);
864 return($current_comment);
866 sub ctx_has_comment {
867 my ($first_line, $end_line) = @_;
868 my $cmt = ctx_locate_comment($first_line, $end_line);
870 ##print "LINE: $rawlines[$end_line - 1 ]\n";
871 ##print "CMMT: $cmt\n";
877 my ($linenr, $cnt) = @_;
879 my $offset = $linenr - 1;
884 $line = $rawlines[$offset++];
885 next if (defined($line) && $line =~ /^-/);
897 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
900 $coded = sprintf("^%c", unpack('C', $2) + 64);
909 my $av_preprocessor = 0;
915 $av_preprocessor = 0;
917 @av_paren_type = ('E');
918 $av_pend_colon = 'O';
921 sub annotate_values {
922 my ($stream, $type) = @_;
925 my $var = '_' x length($stream);
928 print "$stream\n" if ($dbg_values > 1);
930 while (length($cur)) {
931 @av_paren_type = ('E') if ($#av_paren_type < 0);
932 print " <" . join('', @av_paren_type) .
933 "> <$type> <$av_pending>" if ($dbg_values > 1);
934 if ($cur =~ /^(\s+)/o) {
935 print "WS($1)\n" if ($dbg_values > 1);
936 if ($1 =~ /\n/ && $av_preprocessor) {
937 $type = pop(@av_paren_type);
938 $av_preprocessor = 0;
941 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
942 print "CAST($1)\n" if ($dbg_values > 1);
943 push(@av_paren_type, $type);
946 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
947 print "DECLARE($1)\n" if ($dbg_values > 1);
950 } elsif ($cur =~ /^($Modifier)\s*/) {
951 print "MODIFIER($1)\n" if ($dbg_values > 1);
954 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
955 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
956 $av_preprocessor = 1;
957 push(@av_paren_type, $type);
963 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
964 print "UNDEF($1)\n" if ($dbg_values > 1);
965 $av_preprocessor = 1;
966 push(@av_paren_type, $type);
968 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
969 print "PRE_START($1)\n" if ($dbg_values > 1);
970 $av_preprocessor = 1;
972 push(@av_paren_type, $type);
973 push(@av_paren_type, $type);
976 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
977 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
978 $av_preprocessor = 1;
980 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
984 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
985 print "PRE_END($1)\n" if ($dbg_values > 1);
987 $av_preprocessor = 1;
989 # Assume all arms of the conditional end as this
990 # one does, and continue as if the #endif was not here.
992 push(@av_paren_type, $type);
995 } elsif ($cur =~ /^(\\\n)/o) {
996 print "PRECONT($1)\n" if ($dbg_values > 1);
998 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
999 print "ATTR($1)\n" if ($dbg_values > 1);
1000 $av_pending = $type;
1003 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1004 print "SIZEOF($1)\n" if ($dbg_values > 1);
1010 } elsif ($cur =~ /^(if|while|for)\b/o) {
1011 print "COND($1)\n" if ($dbg_values > 1);
1015 } elsif ($cur =~/^(case)/o) {
1016 print "CASE($1)\n" if ($dbg_values > 1);
1017 $av_pend_colon = 'C';
1020 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1021 print "KEYWORD($1)\n" if ($dbg_values > 1);
1024 } elsif ($cur =~ /^(\()/o) {
1025 print "PAREN('$1')\n" if ($dbg_values > 1);
1026 push(@av_paren_type, $av_pending);
1030 } elsif ($cur =~ /^(\))/o) {
1031 my $new_type = pop(@av_paren_type);
1032 if ($new_type ne '_') {
1034 print "PAREN('$1') -> $type\n"
1035 if ($dbg_values > 1);
1037 print "PAREN('$1')\n" if ($dbg_values > 1);
1040 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1041 print "FUNC($1)\n" if ($dbg_values > 1);
1045 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1046 if (defined $2 && $type eq 'C' || $type eq 'T') {
1047 $av_pend_colon = 'B';
1048 } elsif ($type eq 'E') {
1049 $av_pend_colon = 'L';
1051 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1054 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1055 print "IDENT($1)\n" if ($dbg_values > 1);
1058 } elsif ($cur =~ /^($Assignment)/o) {
1059 print "ASSIGN($1)\n" if ($dbg_values > 1);
1062 } elsif ($cur =~/^(;|{|})/) {
1063 print "END($1)\n" if ($dbg_values > 1);
1065 $av_pend_colon = 'O';
1067 } elsif ($cur =~/^(,)/) {
1068 print "COMMA($1)\n" if ($dbg_values > 1);
1071 } elsif ($cur =~ /^(\?)/o) {
1072 print "QUESTION($1)\n" if ($dbg_values > 1);
1075 } elsif ($cur =~ /^(:)/o) {
1076 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1078 substr($var, length($res), 1, $av_pend_colon);
1079 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1084 $av_pend_colon = 'O';
1086 } elsif ($cur =~ /^(\[)/o) {
1087 print "CLOSE($1)\n" if ($dbg_values > 1);
1090 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1093 print "OPV($1)\n" if ($dbg_values > 1);
1100 substr($var, length($res), 1, $variant);
1103 } elsif ($cur =~ /^($Operators)/o) {
1104 print "OP($1)\n" if ($dbg_values > 1);
1105 if ($1 ne '++' && $1 ne '--') {
1109 } elsif ($cur =~ /(^.)/o) {
1110 print "C($1)\n" if ($dbg_values > 1);
1113 $cur = substr($cur, length($1));
1114 $res .= $type x length($1);
1118 return ($res, $var);
1122 my ($possible, $line) = @_;
1123 my $notPermitted = qr{(?:
1138 ^(?:typedef|struct|enum)\b|
1141 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1142 if ($possible !~ $notPermitted) {
1143 # Check for modifiers.
1144 $possible =~ s/\s*$Storage\s*//g;
1145 $possible =~ s/\s*$Sparse\s*//g;
1146 if ($possible =~ /^\s*$/) {
1148 } elsif ($possible =~ /\s/) {
1149 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
1150 for my $modifier (split(' ', $possible)) {
1151 if ($modifier !~ $notPermitted) {
1152 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1153 push(@modifierList, $modifier);
1158 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1159 push(@typeList, $possible);
1163 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1170 my ($level, $msg) = @_;
1171 if (defined $tst_only && $msg !~ /\Q$tst_only\E/) {
1176 $output .= BOLD if $color;
1178 $output .= RED if $color && $level eq 'ERROR';
1179 $output .= MAGENTA if $color && $level eq 'WARNING';
1180 $output .= $level . ':';
1181 $output .= RESET if $color;
1182 $output .= ' ' . $msg . "\n";
1184 $output = (split('\n', $output))[0] . "\n" if ($terse);
1186 push(our @report, $output);
1194 if (report("ERROR", $_[0])) {
1200 if (report("WARNING", $_[0])) {
1206 # According to tests/qtest/bios-tables-test.c: do not
1207 # change expected file in the same commit with adding test
1209 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_;
1211 # Note: shell script that rebuilds the expected files is in the same
1212 # directory as files themselves.
1213 # Note: allowed diff list can be changed both when changing expected
1214 # files and when changing tests.
1215 if ($name =~ m#^tests/data/acpi/# and not $name =~ m#^\.sh$#) {
1216 $$acpi_testexpected = $name;
1217 } elsif ($name !~ m#^tests/qtest/bios-tables-test-allowed-diff.h$#) {
1218 $$acpi_nontestexpected = $name;
1220 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) {
1221 ERROR("Do not add expected files together with tests, " .
1222 "follow instructions in " .
1223 "tests/qtest/bios-tables-test.c: both " .
1224 $$acpi_testexpected . " and " .
1225 $$acpi_nontestexpected . " found\n");
1230 my $filename = shift;
1236 my $stashrawline="";
1247 my $in_header_lines = $file ? 0 : 1;
1248 my $in_commit_log = 0; #Scanning lines before patch
1249 my $non_utf8_charset = 0;
1257 # Trace the real file/line as we go.
1263 my $comment_edge = 0;
1267 my $prev_values = 'E';
1270 my %suppress_ifbraces;
1271 my %suppress_whiletrailers;
1272 my %suppress_export;
1274 my $acpi_testexpected;
1275 my $acpi_nontestexpected;
1277 # Pre-scan the patch sanitizing the lines.
1279 sanitise_line_reset();
1281 foreach my $rawline (@rawlines) {
1285 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1294 # Guestimate if this is a continuing comment. Run
1295 # the context looking for a comment "edge". If this
1296 # edge is a close comment then we must be in a comment
1300 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1301 next if (defined $rawlines[$ln - 1] &&
1302 $rawlines[$ln - 1] =~ /^-/);
1304 #print "RAW<$rawlines[$ln - 1]>\n";
1305 last if (!defined $rawlines[$ln - 1]);
1306 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1307 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1312 if (defined $edge && $edge eq '*/') {
1316 # Guestimate if this is a continuing comment. If this
1317 # is the start of a diff block and this line starts
1318 # ' *' then it is very likely a comment.
1319 if (!defined $edge &&
1320 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1325 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1326 sanitise_line_reset($in_comment);
1328 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1329 # Standardise the strings and chars within the input to
1330 # simplify matching -- only bother with positive lines.
1331 $line = sanitise_line($rawline);
1333 push(@lines, $line);
1336 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1341 #print "==>$rawline\n";
1342 #print "-->$line\n";
1349 foreach my $line (@lines) {
1352 my $rawline = $rawlines[$linenr - 1];
1354 #extract the line range in the file after the patch is applied
1355 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1357 $first_line = $linenr + 1;
1367 %suppress_ifbraces = ();
1368 %suppress_whiletrailers = ();
1369 %suppress_export = ();
1372 # track the line number as we move through the hunk, note that
1373 # new versions of GNU diff omit the leading space on completely
1374 # blank context lines so we need to count that too.
1375 } elsif ($line =~ /^( |\+|$)/) {
1377 $realcnt-- if ($realcnt != 0);
1379 # Measure the line length and indent.
1380 ($length, $indent) = line_stats($rawline);
1382 # Track the previous line.
1383 ($prevline, $stashline) = ($stashline, $line);
1384 ($previndent, $stashindent) = ($stashindent, $indent);
1385 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1387 #warn "line<$line>\n";
1389 } elsif ($realcnt == 1) {
1393 my $hunk_line = ($realcnt != 0);
1395 #make up the handle for any error we report on this line
1396 $prefix = "$filename:$realline: " if ($emacs && $file);
1397 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1399 $here = "#$linenr: " if (!$file);
1400 $here = "#$realline: " if ($file);
1402 # extract the filename as it passes
1403 if ($line =~ /^diff --git.*?(\S+)$/) {
1405 $realfile =~ s@^([^/]*)/@@ if (!$file);
1406 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1407 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1409 $realfile =~ s@^([^/]*)/@@ if (!$file);
1410 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1413 if (!$file && $tree && $p1_prefix ne '' && defined $root &&
1414 -e "$root/$p1_prefix") {
1415 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1421 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1423 my $hereline = "$here\n$rawline\n";
1424 my $herecurr = "$here\n$rawline\n";
1425 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1427 $cnt_lines++ if ($realcnt != 0);
1429 # Check for incorrect file permissions
1430 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1431 my $permhere = $here . "FILE: $realfile\n";
1432 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1433 ERROR("do not set execute permissions for source files\n" . $permhere);
1437 # Accept git diff extended headers as valid patches
1438 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1442 # Filter out bad email addresses.
1443 if ($line =~ /^(Author|From): .*noreply.*/) {
1444 ERROR("Real email adress is needed\n" . $herecurr);
1447 #check the patch for a signoff:
1448 if ($line =~ /^\s*signed-off-by:/i) {
1449 # This is a signoff, if ugly, so do not double report.
1452 if (!($line =~ /^\s*Signed-off-by:/)) {
1453 ERROR("The correct form is \"Signed-off-by\"\n" .
1456 if ($line =~ /^\s*signed-off-by:\S/i) {
1457 ERROR("space required after Signed-off-by:\n" .
1462 # Check for wrappage within a valid hunk of the file
1463 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1464 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1465 $herecurr) if (!$emitted_corrupt++);
1468 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1469 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1470 $rawline !~ m/^$UTF8*$/) {
1471 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1473 my $blank = copy_spacing($rawline);
1474 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1475 my $hereptr = "$hereline$ptr\n";
1477 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1480 if ($rawline =~ m/$UTF8_MOJIBAKE/) {
1481 ERROR("Doubly-encoded UTF-8\n" . $herecurr);
1483 # Check if it's the start of a commit log
1484 # (not a header line and we haven't seen the patch filename)
1485 if ($in_header_lines && $realfile =~ /^$/ &&
1486 !($rawline =~ /^\s+\S/ ||
1487 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1488 $in_header_lines = 0;
1492 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1493 # declined it, i.e defined some charset where it is missing.
1494 if ($in_header_lines &&
1495 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1497 $non_utf8_charset = 1;
1500 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1501 $rawline =~ /$NON_ASCII_UTF8/) {
1502 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1505 # ignore non-hunk lines and lines being removed
1506 next if (!$hunk_line || $line =~ /^-/);
1508 #trailing whitespace
1509 if ($line =~ /^\+.*\015/) {
1510 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1511 ERROR("DOS line endings\n" . $herevet);
1513 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1514 $realfile =~ /^docs\/.+\.md/) {
1515 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1516 # TODO: properly check we're in a code block
1517 # (surrounding text is 4-column aligned)
1518 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1519 ERROR("code blocks in documentation should have " .
1520 "empty lines with exactly 4 columns of " .
1521 "whitespace\n" . $herevet);
1523 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1524 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1525 ERROR("trailing whitespace\n" . $herevet);
1529 # check we are in a valid source file if not then ignore this hunk
1530 next if ($realfile !~ /$SrcFile/);
1532 #120 column limit; exempt URLs, if no other words on line
1533 if ($line =~ /^\+/ &&
1534 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1535 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1537 $realfile !~ /\/tests\//)
1539 if ($length > 120) {
1540 ERROR("line over 120 characters\n" . $herecurr);
1542 WARN("line over 80 characters\n" . $herecurr);
1546 # check for spaces before a quoted newline
1547 if ($rawline =~ /^.*\".*\s\\n/) {
1548 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
1551 # check for adding lines without a newline.
1552 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1553 ERROR("adding a line without newline at end of file\n" . $herecurr);
1556 # check for RCS/CVS revision markers
1557 if ($rawline =~ /^\+.*\$(FreeBSD|Revision|Log|Id)(?:\$|\b)/) {
1558 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1561 # check we are in a valid C source file if not then ignore this hunk
1562 next if ($realfile !~ /\.(h|c|cpp)$/);
1564 # Block comment styles
1566 # Block comments use /* on a line of its own
1567 if ($rawline !~ m@^\+.*/\*.*\*/[ \t)}]*$@ && #inline /*...*/
1568 $rawline =~ m@^\+.*/\*[*-]?+[ \t]*[^ \t]@) { # /* or /** or /*- non-blank
1569 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1572 # Block comments use * on subsequent lines
1573 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1574 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1575 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1576 $rawline =~ /^\+/ && #line is new
1577 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1578 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1581 # Block comments use */ on trailing lines
1582 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1583 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1584 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1585 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1586 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1589 # Block comment * alignment
1590 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1591 $line =~ /^\+[ \t]*$;/ && #leading comment
1592 $rawline =~ /^\+[ \t]*\*/ && #leading *
1593 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1594 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1595 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1597 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1599 $oldindent = expand_tabs($1);
1601 $prevrawline =~ m@^\+(.*/?)\*@;
1602 $oldindent = expand_tabs($1);
1604 $rawline =~ m@^\+([ \t]*)\*@;
1606 $newindent = expand_tabs($newindent);
1607 if (length($oldindent) ne length($newindent)) {
1608 WARN("Block comments should align the * on each line\n" . $hereprev);
1612 # Check for potential 'bare' types
1613 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1615 if ($realcnt && $line =~ /.\s*\S/) {
1616 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1617 ctx_statement_block($linenr, $realcnt, 0);
1618 $stat =~ s/\n./\n /g;
1619 $cond =~ s/\n./\n /g;
1621 # Find the real next line.
1622 $realline_next = $line_nr_next;
1623 if (defined $realline_next &&
1624 (!defined $lines[$realline_next - 1] ||
1625 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1632 # Ignore goto labels.
1633 if ($s =~ /$Ident:\*$/s) {
1635 # Ignore functions being called
1636 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1638 } elsif ($s =~ /^.\s*else\b/s) {
1640 # declarations always start with types
1641 } 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) {
1644 possible($type, "A:" . $s);
1646 # definitions in global scope can only start with types
1647 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1648 possible($1, "B:" . $s);
1651 # any (foo ... *) is a pointer cast, and foo is a type
1652 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1653 possible($1, "C:" . $s);
1656 # Check for any sort of function declaration.
1657 # int foo(something bar, other baz);
1658 # void (*store_gdt)(x86_descr_ptr *);
1659 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1660 my ($name_len) = length($1);
1663 substr($ctx, 0, $name_len + 1, '');
1664 $ctx =~ s/\)[^\)]*$//;
1666 for my $arg (split(/\s*,\s*/, $ctx)) {
1667 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1669 possible($1, "D:" . $s);
1677 # Checks which may be anchored in the context.
1680 # Check for switch () and associated case and default
1681 # statements should be at the same indent.
1682 if ($line=~/\bswitch\s*\(.*\)/) {
1685 my @ctx = ctx_block_outer($linenr, $realcnt);
1687 for my $ctx (@ctx) {
1688 my ($clen, $cindent) = line_stats($ctx);
1689 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1690 $indent != $cindent) {
1691 $err .= "$sep$ctx\n";
1698 ERROR("switch and case should be at the same indent\n$hereline$err");
1702 # if/while/etc brace do not go on next line, unless defining a do while loop,
1703 # or if that brace on the next line is for something else
1704 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1705 my $pre_ctx = "$1$2";
1707 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1708 my $ctx_cnt = $realcnt - $#ctx - 1;
1709 my $ctx = join("\n", @ctx);
1711 my $ctx_ln = $linenr;
1712 my $ctx_skip = $realcnt;
1714 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1715 defined $lines[$ctx_ln - 1] &&
1716 $lines[$ctx_ln - 1] =~ /^-/)) {
1717 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1718 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1722 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1723 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1725 # The length of the "previous line" is checked against 80 because it
1726 # includes the + at the beginning of the line (if the actual line has
1727 # 79 or 80 characters, it is no longer possible to add a space and an
1728 # opening brace there)
1729 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1730 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1731 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1732 ERROR("that open brace { should be on the previous line\n" .
1733 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1735 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1736 $ctx =~ /\)\s*\;\s*$/ &&
1737 defined $lines[$ctx_ln - 1])
1739 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1740 if ($nindent > $indent) {
1741 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
1742 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1747 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1748 if ($line =~ /while\s*\((0|false)\);/) {
1749 ERROR("suspicious ; after while (0)\n" . $herecurr);
1752 # Check superfluous trailing ';'
1753 if ($line =~ /;;$/) {
1754 ERROR("superfluous trailing semicolon\n" . $herecurr);
1757 # Check relative indent for conditionals and blocks.
1758 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1759 my ($s, $c) = ($stat, $cond);
1761 substr($s, 0, length($c), '');
1763 # Make sure we remove the line prefixes as we have
1764 # none on the first line, and are going to re-add them
1768 # Find out how long the conditional actually is.
1769 my @newlines = ($c =~ /\n/gs);
1770 my $cond_lines = 1 + $#newlines;
1772 # We want to check the first line inside the block
1773 # starting at the end of the conditional, so remove:
1774 # 1) any blank line termination
1775 # 2) any opening brace { on end of the line
1777 my $continuation = 0;
1779 $s =~ s/^.*\bdo\b//;
1781 if ($s =~ s/^\s*\\//) {
1784 if ($s =~ s/^\s*?\n//) {
1789 # Also ignore a loop construct at the end of a
1790 # preprocessor statement.
1791 if (($prevline =~ /^.\s*#\s*define\s/ ||
1792 $prevline =~ /\\\s*$/) && $continuation == 0) {
1798 while ($cond_ptr != $cond_lines) {
1799 $cond_ptr = $cond_lines;
1801 # If we see an #else/#elif then the code
1803 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1808 # 1) blank lines, they should be at 0,
1809 # 2) preprocessor lines, and
1811 if ($continuation ||
1813 $s =~ /^\s*#\s*?/ ||
1814 $s =~ /^\s*$Ident\s*:/) {
1815 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1816 if ($s =~ s/^.*?\n//) {
1822 my (undef, $sindent) = line_stats("+" . $s);
1823 my $stat_real = raw_line($linenr, $cond_lines);
1825 # Check if either of these lines are modified, else
1826 # this is not this patch's fault.
1827 if (!defined($stat_real) ||
1828 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1831 if (defined($stat_real) && $cond_lines > 1) {
1832 $stat_real = "[...]\n$stat_real";
1835 #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";
1837 if ($check && (($sindent % 4) != 0 ||
1838 ($sindent <= $indent && $s ne ''))) {
1839 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1843 # Track the 'values' across context and added lines.
1844 my $opline = $line; $opline =~ s/^./ /;
1845 my ($curr_values, $curr_vars) =
1846 annotate_values($opline . "\n", $prev_values);
1847 $curr_values = $prev_values . $curr_values;
1849 my $outline = $opline; $outline =~ s/\t/ /g;
1850 print "$linenr > .$outline\n";
1851 print "$linenr > $curr_values\n";
1852 print "$linenr > $curr_vars\n";
1854 $prev_values = substr($curr_values, -1);
1856 #ignore lines not being added
1857 if ($line=~/^[^\+]/) {next;}
1859 # check for initialisation to aggregates open brace on the next line
1860 if ($line =~ /^.\s*\{/ &&
1861 $prevline =~ /(?:^|[^=])=\s*$/) {
1862 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1866 # Checks which are anchored on the added line.
1869 # check for malformed paths in #include statements (uses RAW line)
1870 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1872 if ($path =~ m{//}) {
1873 ERROR("malformed #include filename\n" .
1878 # Remove C99 comments.
1880 $opline =~ s@//.*@@;
1882 # * goes on variable not on type
1884 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1885 my ($from, $to) = ($1, $1);
1887 # Should start with a space.
1888 $to =~ s/^(\S)/ $1/;
1889 # Should not end with a space.
1891 # '*'s should not have spaces between.
1892 while ($to =~ s/\*\s+\*/\*\*/) {
1895 #print "from<$from> to<$to>\n";
1897 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1899 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1900 my ($from, $to, $ident) = ($1, $1, $2);
1902 # Should start with a space.
1903 $to =~ s/^(\S)/ $1/;
1904 # Should not end with a space.
1906 # '*'s should not have spaces between.
1907 while ($to =~ s/\*\s+\*/\*\*/) {
1909 # Modifiers should have spaces.
1910 $to =~ s/(\b$Modifier$)/$1 /;
1912 #print "from<$from> to<$to> ident<$ident>\n";
1913 if ($from ne $to && $ident !~ /^$Modifier$/) {
1914 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1918 # function brace can't be on same line, except for #defines of do while,
1919 # or if closed on same line
1920 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1921 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1922 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1925 # missing space after union, struct or enum definition
1926 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1927 ERROR("missing space after $1 definition\n" . $herecurr);
1930 # check for spacing round square brackets; allowed:
1931 # 1. with a type on the left -- int [] a;
1932 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1933 # 3. inside a curly brace -- = { [0...10] = 5 }
1934 # 4. after a comma -- [1] = 5, [2] = 6
1935 # 5. in a macro definition -- #define abc(x) [x] = y
1936 while ($line =~ /(.*?\s)\[/g) {
1937 my ($where, $prefix) = ($-[1], $1);
1938 if ($prefix !~ /$Type\s+$/ &&
1939 ($where != 0 || $prefix !~ /^.\s+$/) &&
1940 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1941 $prefix !~ /[,{:]\s+$/) {
1942 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1946 # check for spaces between functions and their parentheses.
1947 while ($line =~ /($Ident)\s+\(/g) {
1949 my $ctx_before = substr($line, 0, $-[1]);
1950 my $ctx = "$ctx_before$name";
1952 # Ignore those directives where spaces _are_ permitted.
1954 if|for|while|switch|return|case|
1955 volatile|__volatile__|coroutine_fn|
1956 __attribute__|format|__extension__|
1960 # Ignore 'catch (...)' in C++
1961 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1963 # cpp #define statements have non-optional spaces, ie
1964 # if there is a space between the name and the open
1965 # parenthesis it is simply not a parameter group.
1966 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1968 # cpp #elif statement condition may start with a (
1969 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1971 # If this whole things ends with a type its most
1972 # likely a typedef for a function.
1973 } elsif ($ctx =~ /$Type$/) {
1976 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1979 # Check operator spacing.
1980 if (!($line=~/\#\s*include/)) {
1982 <<=|>>=|<=|>=|==|!=|
1983 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1984 =>|->|<<|>>|<|>|=|!|~|
1985 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1988 my @elements = split(/($ops|;)/, $opline);
1991 my $blank = copy_spacing($opline);
1993 for (my $n = 0; $n < $#elements; $n += 2) {
1994 $off += length($elements[$n]);
1996 # Pick up the preceding and succeeding characters.
1997 my $ca = substr($opline, 0, $off);
1999 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2000 $cc = substr($opline, $off + length($elements[$n + 1]));
2002 my $cb = "$ca$;$cc";
2005 $a = 'V' if ($elements[$n] ne '');
2006 $a = 'W' if ($elements[$n] =~ /\s$/);
2007 $a = 'C' if ($elements[$n] =~ /$;$/);
2008 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2009 $a = 'O' if ($elements[$n] eq '');
2010 $a = 'E' if ($ca =~ /^\s*$/);
2012 my $op = $elements[$n + 1];
2015 if (defined $elements[$n + 2]) {
2016 $c = 'V' if ($elements[$n + 2] ne '');
2017 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2018 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2019 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2020 $c = 'O' if ($elements[$n + 2] eq '');
2021 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2026 my $ctx = "${a}x${c}";
2028 my $at = "(ctx:$ctx)";
2030 my $ptr = substr($blank, 0, $off) . "^";
2031 my $hereptr = "$hereline$ptr\n";
2033 # Pull out the value of this operator.
2034 my $op_type = substr($curr_values, $off + 1, 1);
2036 # Get the full operator variant.
2037 my $opv = $op . substr($curr_vars, $off, 1);
2039 # Ignore operators passed as parameters.
2040 if ($op_type ne 'V' &&
2041 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2044 # } elsif ($op =~ /^$;+$/) {
2046 # ; should have either the end of line or a space or \ after it
2047 } elsif ($op eq ';') {
2048 if ($ctx !~ /.x[WEBC]/ &&
2049 $cc !~ /^\\/ && $cc !~ /^;/) {
2050 ERROR("space required after that '$op' $at\n" . $hereptr);
2054 } elsif ($op eq '//') {
2056 # Ignore : used in class declaration in C++
2057 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2058 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2062 # : when part of a bitfield
2063 } elsif ($op eq '->' || $opv eq ':B') {
2064 if ($ctx =~ /Wx.|.xW/) {
2065 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2068 # , must have a space on the right.
2069 # not required when having a single },{ on one line
2070 } elsif ($op eq ',') {
2071 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2072 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2073 ERROR("space required after that '$op' $at\n" . $hereptr);
2076 # '*' as part of a type definition -- reported already.
2077 } elsif ($opv eq '*_') {
2078 #warn "'*' is part of type\n";
2080 # unary operators should have a space before and
2081 # none after. May be left adjacent to another
2082 # unary operator, or a cast
2083 } elsif ($op eq '!' || $op eq '~' ||
2084 $opv eq '*U' || $opv eq '-U' ||
2085 $opv eq '&U' || $opv eq '&&U') {
2086 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2087 # '~' used as a name of Destructor
2089 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2090 ERROR("space required before that '$op' $at\n" . $hereptr);
2092 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2093 # A unary '*' may be const
2095 } elsif ($ctx =~ /.xW/) {
2096 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2099 # unary ++ and unary -- are allowed no space on one side.
2100 } elsif ($op eq '++' or $op eq '--') {
2101 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2102 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2104 if ($ctx =~ /Wx[BE]/ ||
2105 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2106 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2108 if ($ctx =~ /ExW/) {
2109 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2112 # A colon needs no spaces before when it is
2113 # terminating a case value or a label.
2114 } elsif ($opv eq ':C' || $opv eq ':L') {
2115 if ($ctx =~ /Wx./) {
2116 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2119 # All the others need spaces both sides.
2120 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2123 if ($realfile =~ /\.cpp|\.h$/) {
2124 # Ignore template arguments <...> in C++
2125 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2135 # Ignore email addresses <foo@bar>
2137 $cc =~ /^\S+\@\S+>/) ||
2139 $ca =~ /<\S+\@\S+$/))
2145 if (($opv eq ':O' && $ca =~ /\?$/) ||
2146 ($op eq '?' && $cc =~ /^:/)) {
2151 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2154 $off += length($elements[$n + 1]);
2158 #need space before brace following if, while, etc
2159 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2161 ERROR("space required before the open brace '{'\n" . $herecurr);
2164 # closing brace should have a space following it when it has anything
2166 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2167 ERROR("space required after that close brace '}'\n" . $herecurr);
2170 # check spacing on square brackets
2171 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2172 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2174 if ($line =~ /\s\]/) {
2175 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2178 # check spacing on parentheses
2179 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2180 $line !~ /for\s*\(\s+;/) {
2181 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2183 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2184 $line !~ /for\s*\(.*;\s+\)/ &&
2185 $line !~ /:\s+\)/) {
2186 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2189 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
2190 $line !~ /^.typedef/) {
2191 ERROR("named $1 should be typedefed separately\n" . $herecurr);
2194 # Return needs parens
2195 if ($line =~ /^.\s*return [^(]/) {
2196 ERROR("parentheses required on return\n" . $herecurr);
2199 # Need a space before open parenthesis after if, while etc
2200 if ($line=~/\b(if|while|for|switch|return)\(/) {
2201 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2204 # Check for illegal assignment in if conditional -- and check for trailing
2205 # statements after the conditional.
2206 if ($line =~ /do\s*(?!{)/) {
2207 my ($stat_next) = ctx_statement_block($line_nr_next,
2208 $remain_next, $off_next);
2209 $stat_next =~ s/\n./\n /g;
2210 ##print "stat<$stat> stat_next<$stat_next>\n";
2212 if ($stat_next =~ /^\s*while\b/) {
2213 # If the statement carries leading newlines,
2214 # then count those as offsets.
2216 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2218 statement_rawlines($whitespace) - 1;
2220 $suppress_whiletrailers{$line_nr_next +
2224 if (!defined $suppress_whiletrailers{$linenr} &&
2225 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2226 my ($s, $c) = ($stat, $cond);
2228 # if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2229 # ERROR("do not use assignment in if condition\n" . $herecurr);
2232 # Find out what is on the end of the line after the
2234 substr($s, 0, length($c), '');
2236 $s =~ s/$;//g; # Remove any comments
2237 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2238 $c !~ /}\s*while\s*/)
2240 # Find out how long the conditional actually is.
2241 my @newlines = ($c =~ /\n/gs);
2242 my $cond_lines = 1 + $#newlines;
2245 $stat_real = raw_line($linenr, $cond_lines)
2246 . "\n" if ($cond_lines);
2247 if (defined($stat_real) && $cond_lines > 1) {
2248 $stat_real = "[...]\n$stat_real";
2251 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2255 # Check for bitwise tests written as boolean
2267 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2270 # if and else should not have general statements after it
2271 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2273 $s =~ s/$;//g; # Remove any comments
2274 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2275 ERROR("trailing statements should be on next line\n" . $herecurr);
2278 # if should not continue a brace
2279 if ($line =~ /}\s*if\b/) {
2280 ERROR("trailing statements should be on next line\n" .
2283 # case and default should not have general statements after them
2284 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2286 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2290 ERROR("trailing statements should be on next line\n" . $herecurr);
2293 # Check for }<nl>else {, these must be at the same
2294 # indent level to be relevant to each other.
2295 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2296 $previndent == $indent) {
2297 ERROR("else should follow close brace '}'\n" . $hereprev);
2300 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2301 $previndent == $indent) {
2302 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2304 # Find out what is on the end of the line after the
2306 substr($s, 0, length($c), '');
2309 if ($s =~ /^\s*;/) {
2310 ERROR("while should follow close brace '}'\n" . $hereprev);
2314 #no spaces allowed after \ in define
2315 if ($line=~/\#\s*define.*\\\s$/) {
2316 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
2319 # multi-statement macros should be enclosed in a do while loop, grab the
2320 # first statement and ensure its the whole macro if its not enclosed
2321 # in a known good container
2322 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2323 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2326 my ($off, $dstat, $dcond, $rest);
2329 my $args = defined($1);
2331 # Find the end of the macro and limit our statement
2333 while ($cnt > 0 && defined $lines[$ln - 1] &&
2334 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2336 $ctx .= $rawlines[$ln - 1] . "\n";
2337 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2340 $ctx .= $rawlines[$ln - 1];
2342 ($dstat, $dcond, $ln, $cnt, $off) =
2343 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2344 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2345 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2347 # Extract the remainder of the define (if any) and
2348 # rip off surrounding spaces, and trailing \'s.
2350 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2351 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2352 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2353 $rest .= substr($lines[$ln - 1], $off) . "\n";
2359 $rest =~ s/\\\n.//g;
2363 # Clean up the original statement.
2365 substr($dstat, 0, length($dcond), '');
2367 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2370 $dstat =~ s/\\\n.//g;
2371 $dstat =~ s/^\s*//s;
2372 $dstat =~ s/\s*$//s;
2374 # Flatten any parentheses and braces
2375 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2376 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2377 $dstat =~ s/\[[^\{\}]*\]/1/)
2381 my $exceptions = qr{
2393 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2394 if ($rest ne '' && $rest ne ',') {
2395 if ($rest !~ /while\s*\(/ &&
2396 $dstat !~ /$exceptions/)
2398 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2401 } elsif ($ctx !~ /;/) {
2403 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2404 $dstat !~ /$exceptions/ &&
2405 $dstat !~ /^\.$Ident\s*=/ &&
2406 $dstat =~ /$Operators/)
2408 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2413 # check for missing bracing around if etc
2414 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2415 $line !~ /\#\s*if/) {
2418 # Check the pre-context.
2419 if ($line =~ /(\}.*?)$/) {
2422 if ($line !~ /else/) {
2423 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2428 my ($level, $endln, @chunks) =
2429 ctx_statement_full($linenr, $realcnt, 1);
2431 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2432 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2435 if ($#chunks >= 0 && $level == 0) {
2437 my $herectx = $here . "\n";
2438 my $ln = $linenr - 1;
2439 for my $chunk (@chunks) {
2440 my ($cond, $block) = @{$chunk};
2442 # If the condition carries leading newlines, then count those as offsets.
2443 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2444 my $offset = statement_rawlines($whitespace) - 1;
2446 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2448 # We have looked at and allowed this specific line.
2449 $suppress_ifbraces{$ln + $offset} = 1;
2451 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2452 $ln += statement_rawlines($block) - 1;
2454 substr($block, 0, length($cond), '');
2456 my $spaced_block = $block;
2457 $spaced_block =~ s/\n\+/ /g;
2459 $seen++ if ($spaced_block =~ /^\s*\{/);
2461 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2463 if (statement_lines($cond) > 1) {
2464 print "APW: ALLOWED: cond<$cond>\n"
2468 if ($block =~/\b(?:if|for|while)\b/) {
2469 print "APW: ALLOWED: block<$block>\n"
2473 if (statement_block_size($block) > 1) {
2474 print "APW: ALLOWED: lines block<$block>\n"
2479 $allowed=1; # disable for now.
2480 if ($seen != ($#chunks + 1) && !$allowed) {
2481 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
2485 if (!defined $suppress_ifbraces{$linenr - 1} &&
2486 $line =~ /\b(if|while|for|else)\b/ &&
2487 $line !~ /\#\s*if/ &&
2488 $line !~ /\#\s*else/) {
2491 # Check the pre-context.
2492 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2495 if ($line !~ /else/) {
2496 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2502 my ($level, $endln, @chunks) =
2503 ctx_statement_full($linenr, $realcnt, $-[0]);
2505 # Check the condition.
2506 my ($cond, $block) = @{$chunks[0]};
2507 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2508 if $dbg_adv_checking;
2509 if (defined $cond) {
2510 substr($block, 0, length($cond), '');
2512 if (statement_lines($cond) > 1) {
2513 print "APW: ALLOWED: cond<$cond>\n"
2517 if ($block =~/\b(?:if|for|while)\b/) {
2518 print "APW: ALLOWED: block<$block>\n"
2522 if (statement_block_size($block) > 1) {
2523 print "APW: ALLOWED: lines block<$block>\n"
2527 # Check the post-context.
2528 if (defined $chunks[1]) {
2529 my ($cond, $block) = @{$chunks[1]};
2530 if (defined $cond) {
2531 substr($block, 0, length($cond), '');
2533 if ($block =~ /^\s*\{/) {
2534 print "APW: ALLOWED: chunk-1 block<$block>\n"
2539 print "DCS: level=$level block<$block> allowed=$allowed\n"
2541 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2542 my $herectx = $here . "\n";;
2543 my $cnt = statement_rawlines($block);
2545 for (my $n = 0; $n < $cnt; $n++) {
2546 $herectx .= raw_line($linenr, $n) . "\n";;
2549 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
2554 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2555 ERROR("if this code is redundant consider removing it\n" .
2559 # Check that the storage class is at the beginning of a declaration
2560 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2561 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
2564 # check the location of the inline attribute, that it is between
2565 # storage class and type.
2566 if ($line =~ /\b$Type\s+$Inline\b/ ||
2567 $line =~ /\b$Inline\s+$Storage\b/) {
2568 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2571 # check for sizeof(&)
2572 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2573 ERROR("sizeof(& should be avoided\n" . $herecurr);
2576 # check for new externs in .c files.
2577 if ($realfile =~ /\.c$/ && defined $stat &&
2578 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2580 my $function_name = $1;
2581 my $paren_space = $2;
2584 if (defined $cond) {
2585 substr($s, 0, length($cond), '');
2587 if ($s =~ /^\s*;/ &&
2588 $function_name ne 'uninitialized_var')
2590 ERROR("externs should be avoided in .c files\n" . $herecurr);
2593 if ($paren_space =~ /\n/) {
2594 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
2597 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2598 $stat =~ /^.\s*extern\s+/)
2600 ERROR("externs should be avoided in .c files\n" . $herecurr);
2603 # check for gcc specific __FUNCTION__
2604 if ($line =~ /__FUNCTION__/) {
2605 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2608 # recommend sigaction over signal for portability, when establishing a handler
2609 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2610 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2613 # format strings checks
2615 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2616 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2617 $string =~ s/%%/__/g;
2618 # check for %L{u,d,i} in strings
2619 if ($string =~ /(?<!%)%L[udi]/) {
2620 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2624 # Continue checking for error messages that contains newlines. This
2625 # check handles cases where string literals are spread over multiple lines.
2627 # error_report("Error msg line #1"
2628 # "Error msg line #2\n");
2629 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2630 my $continued_str_literal = qr{\+\s*\".*\"};
2632 if ($rawline =~ /$quoted_newline_regex/) {
2633 # Backtrack to first line that does not contain only a quoted literal
2634 # and assume that it is the start of the statement.
2635 my $i = $linenr - 2;
2637 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2644 # If we have no input at all, then there is nothing to report on
2645 # so just keep quiet.
2646 if ($#rawlines == -1) {
2650 # In mailback mode only produce a report in the negative, for
2651 # things that appear to be patches.
2652 if ($mailback && ($clean == 1 || !$is_patch)) {
2656 # This is not a patch, and we are are in 'no-patch' mode so
2658 if (!$chk_patch && !$is_patch) {
2662 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
2663 ERROR("Does not appear to be a unified-diff format patch\n");
2666 print report_dump();
2667 if ($summary && !($clean == 1 && $quiet == 1)) {
2668 print "$filename " if ($summary_file);
2669 print "total: $cnt_error errors, $cnt_warn warnings, " .
2670 "$cnt_lines lines checked\n";
2671 print "\n" if ($quiet == 0);
2675 # If there were whitespace errors which cleanpatch can fix
2676 # then suggest that.
2677 # if ($rpt_cleaners) {
2678 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2679 # print " scripts/cleanfile\n\n";
2683 if ($clean == 1 && $quiet == 0) {
2684 print "$vname has no obvious style problems and is ready for submission.\n"
2687 return ($no_warnings ? $clean : $cnt_error == 0);