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;
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 $output .= BOLD if $color;
1180 $output .= RED if $color && $level eq 'ERROR';
1181 $output .= MAGENTA if $color && $level eq 'WARNING';
1182 $output .= $level . ':';
1183 $output .= RESET if $color;
1184 $output .= ' ' . $msg . "\n";
1186 $output = (split('\n', $output))[0] . "\n" if ($terse);
1188 push(our @report, $output);
1196 if (report("ERROR", $_[0])) {
1202 if (report("WARNING", $_[0])) {
1208 # According to tests/qtest/bios-tables-test.c: do not
1209 # change expected file in the same commit with adding test
1211 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_;
1213 # Note: shell script that rebuilds the expected files is in the same
1214 # directory as files themselves.
1215 # Note: allowed diff list can be changed both when changing expected
1216 # files and when changing tests.
1217 if ($name =~ m#^tests/data/acpi/# and not $name =~ m#^\.sh$#) {
1218 $$acpi_testexpected = $name;
1219 } elsif ($name !~ m#^tests/qtest/bios-tables-test-allowed-diff.h$#) {
1220 $$acpi_nontestexpected = $name;
1222 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) {
1223 ERROR("Do not add expected files together with tests, " .
1224 "follow instructions in " .
1225 "tests/qtest/bios-tables-test.c: both " .
1226 $$acpi_testexpected . " and " .
1227 $$acpi_nontestexpected . " found\n");
1232 my $filename = shift;
1238 my $stashrawline="";
1249 my $in_header_lines = $file ? 0 : 1;
1250 my $in_commit_log = 0; #Scanning lines before patch
1251 my $non_utf8_charset = 0;
1259 # Trace the real file/line as we go.
1265 my $comment_edge = 0;
1269 my $prev_values = 'E';
1272 my %suppress_ifbraces;
1273 my %suppress_whiletrailers;
1274 my %suppress_export;
1276 my $acpi_testexpected;
1277 my $acpi_nontestexpected;
1279 # Pre-scan the patch sanitizing the lines.
1281 sanitise_line_reset();
1283 foreach my $rawline (@rawlines) {
1287 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1296 # Guestimate if this is a continuing comment. Run
1297 # the context looking for a comment "edge". If this
1298 # edge is a close comment then we must be in a comment
1302 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1303 next if (defined $rawlines[$ln - 1] &&
1304 $rawlines[$ln - 1] =~ /^-/);
1306 #print "RAW<$rawlines[$ln - 1]>\n";
1307 last if (!defined $rawlines[$ln - 1]);
1308 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1309 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1314 if (defined $edge && $edge eq '*/') {
1318 # Guestimate if this is a continuing comment. If this
1319 # is the start of a diff block and this line starts
1320 # ' *' then it is very likely a comment.
1321 if (!defined $edge &&
1322 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1327 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1328 sanitise_line_reset($in_comment);
1330 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1331 # Standardise the strings and chars within the input to
1332 # simplify matching -- only bother with positive lines.
1333 $line = sanitise_line($rawline);
1335 push(@lines, $line);
1338 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1343 #print "==>$rawline\n";
1344 #print "-->$line\n";
1351 foreach my $line (@lines) {
1354 my $rawline = $rawlines[$linenr - 1];
1356 #extract the line range in the file after the patch is applied
1357 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1359 $first_line = $linenr + 1;
1369 %suppress_ifbraces = ();
1370 %suppress_whiletrailers = ();
1371 %suppress_export = ();
1374 # track the line number as we move through the hunk, note that
1375 # new versions of GNU diff omit the leading space on completely
1376 # blank context lines so we need to count that too.
1377 } elsif ($line =~ /^( |\+|$)/) {
1379 $realcnt-- if ($realcnt != 0);
1381 # Measure the line length and indent.
1382 ($length, $indent) = line_stats($rawline);
1384 # Track the previous line.
1385 ($prevline, $stashline) = ($stashline, $line);
1386 ($previndent, $stashindent) = ($stashindent, $indent);
1387 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1389 #warn "line<$line>\n";
1391 } elsif ($realcnt == 1) {
1395 my $hunk_line = ($realcnt != 0);
1397 #make up the handle for any error we report on this line
1398 $prefix = "$filename:$realline: " if ($emacs && $file);
1399 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1401 $here = "#$linenr: " if (!$file);
1402 $here = "#$realline: " if ($file);
1404 # extract the filename as it passes
1405 if ($line =~ /^diff --git.*?(\S+)$/) {
1407 $realfile =~ s@^([^/]*)/@@ if (!$file);
1408 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1409 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1411 $realfile =~ s@^([^/]*)/@@ if (!$file);
1412 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
1415 if (!$file && $tree && $p1_prefix ne '' &&
1416 -e "$root/$p1_prefix") {
1417 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1423 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1425 my $hereline = "$here\n$rawline\n";
1426 my $herecurr = "$here\n$rawline\n";
1427 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1429 $cnt_lines++ if ($realcnt != 0);
1431 # Check for incorrect file permissions
1432 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1433 my $permhere = $here . "FILE: $realfile\n";
1434 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1435 ERROR("do not set execute permissions for source files\n" . $permhere);
1439 # Only allow Python 3 interpreter
1440 if ($realline == 1 &&
1441 $line =~ /^\+#!\ *\/usr\/bin\/(?:env )?python$/) {
1442 ERROR("please use python3 interpreter\n" . $herecurr);
1445 # Accept git diff extended headers as valid patches
1446 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1450 if ($line =~ /^(Author|From): .*noreply.*/) {
1451 ERROR("Real email adress is needed\n" . $herecurr);
1454 #check the patch for a signoff:
1455 if ($line =~ /^\s*signed-off-by:/i) {
1456 # This is a signoff, if ugly, so do not double report.
1459 if (!($line =~ /^\s*Signed-off-by:/)) {
1460 ERROR("The correct form is \"Signed-off-by\"\n" .
1463 if ($line =~ /^\s*signed-off-by:\S/i) {
1464 ERROR("space required after Signed-off-by:\n" .
1469 # Check for wrappage within a valid hunk of the file
1470 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1471 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1472 $herecurr) if (!$emitted_corrupt++);
1475 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1476 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1477 $rawline !~ m/^$UTF8*$/) {
1478 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1480 my $blank = copy_spacing($rawline);
1481 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1482 my $hereptr = "$hereline$ptr\n";
1484 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1487 if ($rawline =~ m/$UTF8_MOJIBAKE/) {
1488 ERROR("Doubly-encoded UTF-8\n" . $herecurr);
1490 # Check if it's the start of a commit log
1491 # (not a header line and we haven't seen the patch filename)
1492 if ($in_header_lines && $realfile =~ /^$/ &&
1493 !($rawline =~ /^\s+\S/ ||
1494 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1495 $in_header_lines = 0;
1499 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1500 # declined it, i.e defined some charset where it is missing.
1501 if ($in_header_lines &&
1502 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1504 $non_utf8_charset = 1;
1507 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1508 $rawline =~ /$NON_ASCII_UTF8/) {
1509 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1512 # ignore non-hunk lines and lines being removed
1513 next if (!$hunk_line || $line =~ /^-/);
1515 # ignore files that are being periodically imported from Linux
1516 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
1518 #trailing whitespace
1519 if ($line =~ /^\+.*\015/) {
1520 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1521 ERROR("DOS line endings\n" . $herevet);
1523 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1524 $realfile =~ /^docs\/.+\.md/) {
1525 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1526 # TODO: properly check we're in a code block
1527 # (surrounding text is 4-column aligned)
1528 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1529 ERROR("code blocks in documentation should have " .
1530 "empty lines with exactly 4 columns of " .
1531 "whitespace\n" . $herevet);
1533 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1534 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1535 ERROR("trailing whitespace\n" . $herevet);
1539 # checks for trace-events files
1540 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1541 if ($rawline =~ /%[-+ 0]*#/) {
1542 ERROR("Don't use '#' flag of printf format ('%#') in " .
1543 "trace-events, use '0x' prefix instead\n" . $herecurr);
1546 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1548 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1549 my $tmpline = $rawline;
1550 $tmpline =~ s/($hex[.:\/ ])+$hex//g;
1552 if ($tmpline =~ /(?<!0x)$hex/) {
1553 ERROR("Hex numbers must be prefixed with '0x'\n" .
1559 # check we are in a valid source file if not then ignore this hunk
1560 next if ($realfile !~ /$SrcFile/);
1562 #120 column limit; exempt URLs, if no other words on line
1563 if ($line =~ /^\+/ &&
1564 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1565 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1567 $realfile !~ /\/tests\//)
1569 if ($length > 120) {
1570 ERROR("line over 120 characters\n" . $herecurr);
1572 WARN("line over 80 characters\n" . $herecurr);
1576 # check for spaces before a quoted newline
1577 if ($rawline =~ /^.*\".*\s\\n/) {
1578 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
1581 # check for adding lines without a newline.
1582 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1583 ERROR("adding a line without newline at end of file\n" . $herecurr);
1586 # check for RCS/CVS revision markers
1587 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1588 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1591 # check we are in a valid C source file if not then ignore this hunk
1592 next if ($realfile !~ /\.(h|c|cpp)$/);
1594 # Block comment styles
1596 # Block comments use /* on a line of its own
1597 if ($rawline !~ m@^\+.*/\*.*\*/[ \t)}]*$@ && #inline /*...*/
1598 $rawline =~ m@^\+.*/\*[*-]?+[ \t]*[^ \t]@) { # /* or /** or /*- non-blank
1599 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1602 # Block comments use * on subsequent lines
1603 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1604 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1605 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1606 $rawline =~ /^\+/ && #line is new
1607 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1608 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1611 # Block comments use */ on trailing lines
1612 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1613 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1614 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1615 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1616 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1619 # Block comment * alignment
1620 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1621 $line =~ /^\+[ \t]*$;/ && #leading comment
1622 $rawline =~ /^\+[ \t]*\*/ && #leading *
1623 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1624 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1625 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1627 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1629 $oldindent = expand_tabs($1);
1631 $prevrawline =~ m@^\+(.*/?)\*@;
1632 $oldindent = expand_tabs($1);
1634 $rawline =~ m@^\+([ \t]*)\*@;
1636 $newindent = expand_tabs($newindent);
1637 if (length($oldindent) ne length($newindent)) {
1638 WARN("Block comments should align the * on each line\n" . $hereprev);
1642 # Check for potential 'bare' types
1643 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1645 if ($realcnt && $line =~ /.\s*\S/) {
1646 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1647 ctx_statement_block($linenr, $realcnt, 0);
1648 $stat =~ s/\n./\n /g;
1649 $cond =~ s/\n./\n /g;
1651 # Find the real next line.
1652 $realline_next = $line_nr_next;
1653 if (defined $realline_next &&
1654 (!defined $lines[$realline_next - 1] ||
1655 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1662 # Ignore goto labels.
1663 if ($s =~ /$Ident:\*$/s) {
1665 # Ignore functions being called
1666 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1668 } elsif ($s =~ /^.\s*else\b/s) {
1670 # declarations always start with types
1671 } 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) {
1674 possible($type, "A:" . $s);
1676 # definitions in global scope can only start with types
1677 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1678 possible($1, "B:" . $s);
1681 # any (foo ... *) is a pointer cast, and foo is a type
1682 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1683 possible($1, "C:" . $s);
1686 # Check for any sort of function declaration.
1687 # int foo(something bar, other baz);
1688 # void (*store_gdt)(x86_descr_ptr *);
1689 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1690 my ($name_len) = length($1);
1693 substr($ctx, 0, $name_len + 1, '');
1694 $ctx =~ s/\)[^\)]*$//;
1696 for my $arg (split(/\s*,\s*/, $ctx)) {
1697 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1699 possible($1, "D:" . $s);
1707 # Checks which may be anchored in the context.
1710 # Check for switch () and associated case and default
1711 # statements should be at the same indent.
1712 if ($line=~/\bswitch\s*\(.*\)/) {
1715 my @ctx = ctx_block_outer($linenr, $realcnt);
1717 for my $ctx (@ctx) {
1718 my ($clen, $cindent) = line_stats($ctx);
1719 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1720 $indent != $cindent) {
1721 $err .= "$sep$ctx\n";
1728 ERROR("switch and case should be at the same indent\n$hereline$err");
1732 # if/while/etc brace do not go on next line, unless defining a do while loop,
1733 # or if that brace on the next line is for something else
1734 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1735 my $pre_ctx = "$1$2";
1737 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1738 my $ctx_cnt = $realcnt - $#ctx - 1;
1739 my $ctx = join("\n", @ctx);
1741 my $ctx_ln = $linenr;
1742 my $ctx_skip = $realcnt;
1744 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1745 defined $lines[$ctx_ln - 1] &&
1746 $lines[$ctx_ln - 1] =~ /^-/)) {
1747 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1748 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1752 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1753 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1755 # The length of the "previous line" is checked against 80 because it
1756 # includes the + at the beginning of the line (if the actual line has
1757 # 79 or 80 characters, it is no longer possible to add a space and an
1758 # opening brace there)
1759 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1760 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1761 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1762 ERROR("that open brace { should be on the previous line\n" .
1763 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1765 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1766 $ctx =~ /\)\s*\;\s*$/ &&
1767 defined $lines[$ctx_ln - 1])
1769 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1770 if ($nindent > $indent) {
1771 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
1772 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1777 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1778 if ($line =~ /while\s*\((0|false)\);/) {
1779 ERROR("suspicious ; after while (0)\n" . $herecurr);
1782 # Check superfluous trailing ';'
1783 if ($line =~ /;;$/) {
1784 ERROR("superfluous trailing semicolon\n" . $herecurr);
1787 # Check relative indent for conditionals and blocks.
1788 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1789 my ($s, $c) = ($stat, $cond);
1791 substr($s, 0, length($c), '');
1793 # Make sure we remove the line prefixes as we have
1794 # none on the first line, and are going to re-add them
1798 # Find out how long the conditional actually is.
1799 my @newlines = ($c =~ /\n/gs);
1800 my $cond_lines = 1 + $#newlines;
1802 # We want to check the first line inside the block
1803 # starting at the end of the conditional, so remove:
1804 # 1) any blank line termination
1805 # 2) any opening brace { on end of the line
1807 my $continuation = 0;
1809 $s =~ s/^.*\bdo\b//;
1811 if ($s =~ s/^\s*\\//) {
1814 if ($s =~ s/^\s*?\n//) {
1819 # Also ignore a loop construct at the end of a
1820 # preprocessor statement.
1821 if (($prevline =~ /^.\s*#\s*define\s/ ||
1822 $prevline =~ /\\\s*$/) && $continuation == 0) {
1828 while ($cond_ptr != $cond_lines) {
1829 $cond_ptr = $cond_lines;
1831 # If we see an #else/#elif then the code
1833 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1838 # 1) blank lines, they should be at 0,
1839 # 2) preprocessor lines, and
1841 if ($continuation ||
1843 $s =~ /^\s*#\s*?/ ||
1844 $s =~ /^\s*$Ident\s*:/) {
1845 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1846 if ($s =~ s/^.*?\n//) {
1852 my (undef, $sindent) = line_stats("+" . $s);
1853 my $stat_real = raw_line($linenr, $cond_lines);
1855 # Check if either of these lines are modified, else
1856 # this is not this patch's fault.
1857 if (!defined($stat_real) ||
1858 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1861 if (defined($stat_real) && $cond_lines > 1) {
1862 $stat_real = "[...]\n$stat_real";
1865 #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";
1867 if ($check && (($sindent % 4) != 0 ||
1868 ($sindent <= $indent && $s ne ''))) {
1869 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1873 # Track the 'values' across context and added lines.
1874 my $opline = $line; $opline =~ s/^./ /;
1875 my ($curr_values, $curr_vars) =
1876 annotate_values($opline . "\n", $prev_values);
1877 $curr_values = $prev_values . $curr_values;
1879 my $outline = $opline; $outline =~ s/\t/ /g;
1880 print "$linenr > .$outline\n";
1881 print "$linenr > $curr_values\n";
1882 print "$linenr > $curr_vars\n";
1884 $prev_values = substr($curr_values, -1);
1886 #ignore lines not being added
1887 if ($line=~/^[^\+]/) {next;}
1889 # TEST: allow direct testing of the type matcher.
1891 if ($line =~ /^.\s*$Declare\s*$/) {
1892 ERROR("TEST: is type\n" . $herecurr);
1893 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1894 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1898 # TEST: allow direct testing of the attribute matcher.
1900 if ($line =~ /^.\s*$Modifier\s*$/) {
1901 ERROR("TEST: is attr\n" . $herecurr);
1902 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1903 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1908 # check for initialisation to aggregates open brace on the next line
1909 if ($line =~ /^.\s*\{/ &&
1910 $prevline =~ /(?:^|[^=])=\s*$/) {
1911 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1915 # Checks which are anchored on the added line.
1918 # check for malformed paths in #include statements (uses RAW line)
1919 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1921 if ($path =~ m{//}) {
1922 ERROR("malformed #include filename\n" .
1927 # Remove C99 comments.
1929 $opline =~ s@//.*@@;
1931 # check for global initialisers.
1932 # if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1933 # ERROR("do not initialise globals to 0 or NULL\n" .
1936 # check for static initialisers.
1937 # if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1938 # ERROR("do not initialise statics to 0 or NULL\n" .
1942 # * goes on variable not on type
1944 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1945 my ($from, $to) = ($1, $1);
1947 # Should start with a space.
1948 $to =~ s/^(\S)/ $1/;
1949 # Should not end with a space.
1951 # '*'s should not have spaces between.
1952 while ($to =~ s/\*\s+\*/\*\*/) {
1955 #print "from<$from> to<$to>\n";
1957 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1959 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1960 my ($from, $to, $ident) = ($1, $1, $2);
1962 # Should start with a space.
1963 $to =~ s/^(\S)/ $1/;
1964 # Should not end with a space.
1966 # '*'s should not have spaces between.
1967 while ($to =~ s/\*\s+\*/\*\*/) {
1969 # Modifiers should have spaces.
1970 $to =~ s/(\b$Modifier$)/$1 /;
1972 #print "from<$from> to<$to> ident<$ident>\n";
1973 if ($from ne $to && $ident !~ /^$Modifier$/) {
1974 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1978 # function brace can't be on same line, except for #defines of do while,
1979 # or if closed on same line
1980 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1981 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1982 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1985 # missing space after union, struct or enum definition
1986 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1987 ERROR("missing space after $1 definition\n" . $herecurr);
1990 # check for spacing round square brackets; allowed:
1991 # 1. with a type on the left -- int [] a;
1992 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1993 # 3. inside a curly brace -- = { [0...10] = 5 }
1994 # 4. after a comma -- [1] = 5, [2] = 6
1995 # 5. in a macro definition -- #define abc(x) [x] = y
1996 while ($line =~ /(.*?\s)\[/g) {
1997 my ($where, $prefix) = ($-[1], $1);
1998 if ($prefix !~ /$Type\s+$/ &&
1999 ($where != 0 || $prefix !~ /^.\s+$/) &&
2000 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
2001 $prefix !~ /[,{:]\s+$/) {
2002 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2006 # check for spaces between functions and their parentheses.
2007 while ($line =~ /($Ident)\s+\(/g) {
2009 my $ctx_before = substr($line, 0, $-[1]);
2010 my $ctx = "$ctx_before$name";
2012 # Ignore those directives where spaces _are_ permitted.
2014 if|for|while|switch|return|case|
2015 volatile|__volatile__|coroutine_fn|
2016 __attribute__|format|__extension__|
2020 # Ignore 'catch (...)' in C++
2021 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
2023 # cpp #define statements have non-optional spaces, ie
2024 # if there is a space between the name and the open
2025 # parenthesis it is simply not a parameter group.
2026 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2028 # cpp #elif statement condition may start with a (
2029 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2031 # If this whole things ends with a type its most
2032 # likely a typedef for a function.
2033 } elsif ($ctx =~ /$Type$/) {
2036 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2039 # Check operator spacing.
2040 if (!($line=~/\#\s*include/)) {
2042 <<=|>>=|<=|>=|==|!=|
2043 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2044 =>|->|<<|>>|<|>|=|!|~|
2045 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2048 my @elements = split(/($ops|;)/, $opline);
2051 my $blank = copy_spacing($opline);
2053 for (my $n = 0; $n < $#elements; $n += 2) {
2054 $off += length($elements[$n]);
2056 # Pick up the preceding and succeeding characters.
2057 my $ca = substr($opline, 0, $off);
2059 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2060 $cc = substr($opline, $off + length($elements[$n + 1]));
2062 my $cb = "$ca$;$cc";
2065 $a = 'V' if ($elements[$n] ne '');
2066 $a = 'W' if ($elements[$n] =~ /\s$/);
2067 $a = 'C' if ($elements[$n] =~ /$;$/);
2068 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2069 $a = 'O' if ($elements[$n] eq '');
2070 $a = 'E' if ($ca =~ /^\s*$/);
2072 my $op = $elements[$n + 1];
2075 if (defined $elements[$n + 2]) {
2076 $c = 'V' if ($elements[$n + 2] ne '');
2077 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2078 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2079 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2080 $c = 'O' if ($elements[$n + 2] eq '');
2081 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2086 my $ctx = "${a}x${c}";
2088 my $at = "(ctx:$ctx)";
2090 my $ptr = substr($blank, 0, $off) . "^";
2091 my $hereptr = "$hereline$ptr\n";
2093 # Pull out the value of this operator.
2094 my $op_type = substr($curr_values, $off + 1, 1);
2096 # Get the full operator variant.
2097 my $opv = $op . substr($curr_vars, $off, 1);
2099 # Ignore operators passed as parameters.
2100 if ($op_type ne 'V' &&
2101 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2104 # } elsif ($op =~ /^$;+$/) {
2106 # ; should have either the end of line or a space or \ after it
2107 } elsif ($op eq ';') {
2108 if ($ctx !~ /.x[WEBC]/ &&
2109 $cc !~ /^\\/ && $cc !~ /^;/) {
2110 ERROR("space required after that '$op' $at\n" . $hereptr);
2114 } elsif ($op eq '//') {
2116 # Ignore : used in class declaration in C++
2117 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2118 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2122 # : when part of a bitfield
2123 } elsif ($op eq '->' || $opv eq ':B') {
2124 if ($ctx =~ /Wx.|.xW/) {
2125 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2128 # , must have a space on the right.
2129 # not required when having a single },{ on one line
2130 } elsif ($op eq ',') {
2131 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2132 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2133 ERROR("space required after that '$op' $at\n" . $hereptr);
2136 # '*' as part of a type definition -- reported already.
2137 } elsif ($opv eq '*_') {
2138 #warn "'*' is part of type\n";
2140 # unary operators should have a space before and
2141 # none after. May be left adjacent to another
2142 # unary operator, or a cast
2143 } elsif ($op eq '!' || $op eq '~' ||
2144 $opv eq '*U' || $opv eq '-U' ||
2145 $opv eq '&U' || $opv eq '&&U') {
2146 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2147 # '~' used as a name of Destructor
2149 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2150 ERROR("space required before that '$op' $at\n" . $hereptr);
2152 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2153 # A unary '*' may be const
2155 } elsif ($ctx =~ /.xW/) {
2156 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2159 # unary ++ and unary -- are allowed no space on one side.
2160 } elsif ($op eq '++' or $op eq '--') {
2161 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2162 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2164 if ($ctx =~ /Wx[BE]/ ||
2165 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2166 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2168 if ($ctx =~ /ExW/) {
2169 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2172 # A colon needs no spaces before when it is
2173 # terminating a case value or a label.
2174 } elsif ($opv eq ':C' || $opv eq ':L') {
2175 if ($ctx =~ /Wx./) {
2176 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2179 # All the others need spaces both sides.
2180 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2183 if ($realfile =~ /\.cpp|\.h$/) {
2184 # Ignore template arguments <...> in C++
2185 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2195 # Ignore email addresses <foo@bar>
2197 $cc =~ /^\S+\@\S+>/) ||
2199 $ca =~ /<\S+\@\S+$/))
2205 if (($opv eq ':O' && $ca =~ /\?$/) ||
2206 ($op eq '?' && $cc =~ /^:/)) {
2211 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2214 $off += length($elements[$n + 1]);
2218 #need space before brace following if, while, etc
2219 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2221 ERROR("space required before the open brace '{'\n" . $herecurr);
2224 # closing brace should have a space following it when it has anything
2226 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2227 ERROR("space required after that close brace '}'\n" . $herecurr);
2230 # check spacing on square brackets
2231 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2232 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2234 if ($line =~ /\s\]/) {
2235 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2238 # check spacing on parentheses
2239 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2240 $line !~ /for\s*\(\s+;/) {
2241 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2243 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2244 $line !~ /for\s*\(.*;\s+\)/ &&
2245 $line !~ /:\s+\)/) {
2246 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2249 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
2250 $line !~ /^.typedef/) {
2251 ERROR("named $1 should be typedefed separately\n" . $herecurr);
2254 # Return needs parens
2255 if ($line =~ /^.\s*return [^(]/) {
2256 ERROR("parentheses required on return\n" . $herecurr);
2259 # Need a space before open parenthesis after if, while etc
2260 if ($line=~/\b(if|while|for|switch|return)\(/) {
2261 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2264 # Check for illegal assignment in if conditional -- and check for trailing
2265 # statements after the conditional.
2266 if ($line =~ /do\s*(?!{)/) {
2267 my ($stat_next) = ctx_statement_block($line_nr_next,
2268 $remain_next, $off_next);
2269 $stat_next =~ s/\n./\n /g;
2270 ##print "stat<$stat> stat_next<$stat_next>\n";
2272 if ($stat_next =~ /^\s*while\b/) {
2273 # If the statement carries leading newlines,
2274 # then count those as offsets.
2276 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2278 statement_rawlines($whitespace) - 1;
2280 $suppress_whiletrailers{$line_nr_next +
2284 if (!defined $suppress_whiletrailers{$linenr} &&
2285 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2286 my ($s, $c) = ($stat, $cond);
2288 # if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2289 # ERROR("do not use assignment in if condition\n" . $herecurr);
2292 # Find out what is on the end of the line after the
2294 substr($s, 0, length($c), '');
2296 $s =~ s/$;//g; # Remove any comments
2297 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2298 $c !~ /}\s*while\s*/)
2300 # Find out how long the conditional actually is.
2301 my @newlines = ($c =~ /\n/gs);
2302 my $cond_lines = 1 + $#newlines;
2305 $stat_real = raw_line($linenr, $cond_lines)
2306 . "\n" if ($cond_lines);
2307 if (defined($stat_real) && $cond_lines > 1) {
2308 $stat_real = "[...]\n$stat_real";
2311 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2315 # Check for bitwise tests written as boolean
2327 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2330 # if and else should not have general statements after it
2331 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2333 $s =~ s/$;//g; # Remove any comments
2334 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2335 ERROR("trailing statements should be on next line\n" . $herecurr);
2338 # if should not continue a brace
2339 if ($line =~ /}\s*if\b/) {
2340 ERROR("trailing statements should be on next line\n" .
2343 # case and default should not have general statements after them
2344 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2346 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2350 ERROR("trailing statements should be on next line\n" . $herecurr);
2353 # Check for }<nl>else {, these must be at the same
2354 # indent level to be relevant to each other.
2355 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2356 $previndent == $indent) {
2357 ERROR("else should follow close brace '}'\n" . $hereprev);
2360 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2361 $previndent == $indent) {
2362 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2364 # Find out what is on the end of the line after the
2366 substr($s, 0, length($c), '');
2369 if ($s =~ /^\s*;/) {
2370 ERROR("while should follow close brace '}'\n" . $hereprev);
2374 #no spaces allowed after \ in define
2375 if ($line=~/\#\s*define.*\\\s$/) {
2376 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
2379 # multi-statement macros should be enclosed in a do while loop, grab the
2380 # first statement and ensure its the whole macro if its not enclosed
2381 # in a known good container
2382 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2383 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2386 my ($off, $dstat, $dcond, $rest);
2389 my $args = defined($1);
2391 # Find the end of the macro and limit our statement
2393 while ($cnt > 0 && defined $lines[$ln - 1] &&
2394 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2396 $ctx .= $rawlines[$ln - 1] . "\n";
2397 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2400 $ctx .= $rawlines[$ln - 1];
2402 ($dstat, $dcond, $ln, $cnt, $off) =
2403 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2404 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2405 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2407 # Extract the remainder of the define (if any) and
2408 # rip off surrounding spaces, and trailing \'s.
2410 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2411 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2412 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2413 $rest .= substr($lines[$ln - 1], $off) . "\n";
2419 $rest =~ s/\\\n.//g;
2423 # Clean up the original statement.
2425 substr($dstat, 0, length($dcond), '');
2427 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2430 $dstat =~ s/\\\n.//g;
2431 $dstat =~ s/^\s*//s;
2432 $dstat =~ s/\s*$//s;
2434 # Flatten any parentheses and braces
2435 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2436 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2437 $dstat =~ s/\[[^\{\}]*\]/1/)
2441 my $exceptions = qr{
2453 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2454 if ($rest ne '' && $rest ne ',') {
2455 if ($rest !~ /while\s*\(/ &&
2456 $dstat !~ /$exceptions/)
2458 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2461 } elsif ($ctx !~ /;/) {
2463 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2464 $dstat !~ /$exceptions/ &&
2465 $dstat !~ /^\.$Ident\s*=/ &&
2466 $dstat =~ /$Operators/)
2468 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2473 # check for missing bracing around if etc
2474 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2475 $line !~ /\#\s*if/) {
2478 # Check the pre-context.
2479 if ($line =~ /(\}.*?)$/) {
2482 if ($line !~ /else/) {
2483 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2488 my ($level, $endln, @chunks) =
2489 ctx_statement_full($linenr, $realcnt, 1);
2491 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2492 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2495 if ($#chunks >= 0 && $level == 0) {
2497 my $herectx = $here . "\n";
2498 my $ln = $linenr - 1;
2499 for my $chunk (@chunks) {
2500 my ($cond, $block) = @{$chunk};
2502 # If the condition carries leading newlines, then count those as offsets.
2503 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2504 my $offset = statement_rawlines($whitespace) - 1;
2506 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2508 # We have looked at and allowed this specific line.
2509 $suppress_ifbraces{$ln + $offset} = 1;
2511 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2512 $ln += statement_rawlines($block) - 1;
2514 substr($block, 0, length($cond), '');
2516 my $spaced_block = $block;
2517 $spaced_block =~ s/\n\+/ /g;
2519 $seen++ if ($spaced_block =~ /^\s*\{/);
2521 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2523 if (statement_lines($cond) > 1) {
2524 print "APW: ALLOWED: cond<$cond>\n"
2528 if ($block =~/\b(?:if|for|while)\b/) {
2529 print "APW: ALLOWED: block<$block>\n"
2533 if (statement_block_size($block) > 1) {
2534 print "APW: ALLOWED: lines block<$block>\n"
2539 $allowed=1; # disable for now.
2540 if ($seen != ($#chunks + 1) && !$allowed) {
2541 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
2545 if (!defined $suppress_ifbraces{$linenr - 1} &&
2546 $line =~ /\b(if|while|for|else)\b/ &&
2547 $line !~ /\#\s*if/ &&
2548 $line !~ /\#\s*else/) {
2551 # Check the pre-context.
2552 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2555 if ($line !~ /else/) {
2556 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2562 my ($level, $endln, @chunks) =
2563 ctx_statement_full($linenr, $realcnt, $-[0]);
2565 # Check the condition.
2566 my ($cond, $block) = @{$chunks[0]};
2567 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2568 if $dbg_adv_checking;
2569 if (defined $cond) {
2570 substr($block, 0, length($cond), '');
2572 if (statement_lines($cond) > 1) {
2573 print "APW: ALLOWED: cond<$cond>\n"
2577 if ($block =~/\b(?:if|for|while)\b/) {
2578 print "APW: ALLOWED: block<$block>\n"
2582 if (statement_block_size($block) > 1) {
2583 print "APW: ALLOWED: lines block<$block>\n"
2587 # Check the post-context.
2588 if (defined $chunks[1]) {
2589 my ($cond, $block) = @{$chunks[1]};
2590 if (defined $cond) {
2591 substr($block, 0, length($cond), '');
2593 if ($block =~ /^\s*\{/) {
2594 print "APW: ALLOWED: chunk-1 block<$block>\n"
2599 print "DCS: level=$level block<$block> allowed=$allowed\n"
2601 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2602 my $herectx = $here . "\n";;
2603 my $cnt = statement_rawlines($block);
2605 for (my $n = 0; $n < $cnt; $n++) {
2606 $herectx .= raw_line($linenr, $n) . "\n";;
2609 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
2614 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2615 ERROR("if this code is redundant consider removing it\n" .
2619 # Check that the storage class is at the beginning of a declaration
2620 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2621 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
2624 # check the location of the inline attribute, that it is between
2625 # storage class and type.
2626 if ($line =~ /\b$Type\s+$Inline\b/ ||
2627 $line =~ /\b$Inline\s+$Storage\b/) {
2628 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2631 # check for sizeof(&)
2632 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2633 ERROR("sizeof(& should be avoided\n" . $herecurr);
2636 # check for new externs in .c files.
2637 if ($realfile =~ /\.c$/ && defined $stat &&
2638 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2640 my $function_name = $1;
2641 my $paren_space = $2;
2644 if (defined $cond) {
2645 substr($s, 0, length($cond), '');
2647 if ($s =~ /^\s*;/ &&
2648 $function_name ne 'uninitialized_var')
2650 ERROR("externs should be avoided in .c files\n" . $herecurr);
2653 if ($paren_space =~ /\n/) {
2654 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
2657 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2658 $stat =~ /^.\s*extern\s+/)
2660 ERROR("externs should be avoided in .c files\n" . $herecurr);
2663 # check for gcc specific __FUNCTION__
2664 if ($line =~ /__FUNCTION__/) {
2665 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2668 # recommend sigaction over signal for portability, when establishing a handler
2669 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2670 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2673 # format strings checks
2675 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2676 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2677 $string =~ s/%%/__/g;
2678 # check for %L{u,d,i} in strings
2679 if ($string =~ /(?<!%)%L[udi]/) {
2680 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2684 # Continue checking for error messages that contains newlines. This
2685 # check handles cases where string literals are spread over multiple lines.
2687 # error_report("Error msg line #1"
2688 # "Error msg line #2\n");
2689 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2690 my $continued_str_literal = qr{\+\s*\".*\"};
2692 if ($rawline =~ /$quoted_newline_regex/) {
2693 # Backtrack to first line that does not contain only a quoted literal
2694 # and assume that it is the start of the statement.
2695 my $i = $linenr - 2;
2697 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2704 # If we have no input at all, then there is nothing to report on
2705 # so just keep quiet.
2706 if ($#rawlines == -1) {
2710 # In mailback mode only produce a report in the negative, for
2711 # things that appear to be patches.
2712 if ($mailback && ($clean == 1 || !$is_patch)) {
2716 # This is not a patch, and we are are in 'no-patch' mode so
2718 if (!$chk_patch && !$is_patch) {
2722 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
2723 ERROR("Does not appear to be a unified-diff format patch\n");
2726 print report_dump();
2727 if ($summary && !($clean == 1 && $quiet == 1)) {
2728 print "$filename " if ($summary_file);
2729 print "total: $cnt_error errors, $cnt_warn warnings, " .
2730 "$cnt_lines lines checked\n";
2731 print "\n" if ($quiet == 0);
2735 # If there were whitespace errors which cleanpatch can fix
2736 # then suggest that.
2737 # if ($rpt_cleaners) {
2738 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2739 # print " scripts/cleanfile\n\n";
2743 if ($clean == 1 && $quiet == 0) {
2744 print "$vname has no obvious style problems and is ready for submission.\n"
2747 return ($no_warnings ? $clean : $cnt_error == 0);