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 --no-tree run without a kernel tree
53 --no-signoff
do not check
for 'Signed-off-by' line
54 --patch treat FILE as patchfile
55 --branch treat args as GIT revision list
56 --emacs emacs compile window format
57 --terse one line per report
58 -f
, --file treat FILE as regular source file
59 --strict fail
if only warnings are found
60 --root
=PATH PATH to the kernel tree root
61 --no-summary suppress the per
-file summary
62 --mailback only produce a report
in case of warnings
/errors
63 --summary
-file include the filename
in summary
64 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
65 'values', 'possible', 'type', and 'attr' (default
67 --test
-only
=WORD report only warnings
/errors containing WORD
69 --color
[=WHEN
] Use colors
'always', 'never', or only
when output
70 is a terminal
('auto'). Default is
'auto'.
71 -h
, --help
, --version display this help
and exit
73 When FILE is
- read standard input
.
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;
193 if (!top_of_kernel_tree
($root)) {
194 die "$P: $root: --root does not point at a valid tree\n";
197 if (top_of_kernel_tree
('.')) {
199 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
200 top_of_kernel_tree
($1)) {
205 if (!defined $root) {
206 print "Must be run from the top-level dir. of a kernel tree\n";
211 my $emitted_corrupt = 0;
214 [A
-Za
-z_
][A
-Za
-z\d_
]*
215 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
217 our $Storage = qr{extern|static|asmlinkage};
222 # Notes to $Attribute:
227 QEMU_WARN_UNUSED_RESULT
|
233 our $Inline = qr{inline};
234 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
235 our $Lval = qr{$Ident(?:$Member)*};
237 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
238 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
239 our $Compare = qr{<=|>=|==|!=|<|>};
243 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
250 our $NON_ASCII_UTF8 = qr{
251 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
252 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
253 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
254 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
255 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
256 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
257 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
261 [\x09\x0A\x0D\x20-\x7E] # ASCII
265 # some readers default to ISO-8859-1 when showing email source. detect
266 # when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back.
267 # False positives are possible but very unlikely.
268 our $UTF8_MOJIBAKE = qr{
269 \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf
270 | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf
271 | \xC3[\xA1-\xAC\xAE\xAF] (?
: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf 80-bf
272 | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf
273 | \xC3\xB0 \xC2[\x90-\xBF] (?
: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-bf
274 | \xC3[\xB1-\xB3] (?
: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf 80-bf
275 | \xC3\xB4 \xC2[\x80-\x8F] (?
: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-bf
278 # There are still some false positives, but this catches most
280 our $typeTypedefs = qr{(?x
:
281 (?
![KMGTPE
]iB
) # IEC binary prefix (do not match)
282 [A
-Z
][A
-Z\d_
]*[a
-z
][A
-Za
-z\d_
]* # camelcase
283 | [A
-Z
][A
-Z\d_
]*AIOCB
# all uppercase
284 | [A
-Z
][A
-Z\d_
]*CPU
# all uppercase
285 | QEMUBH
# all uppercase
290 qr{(?:unsigned\s+)?char},
291 qr{(?:unsigned\s+)?short},
292 qr{(?:unsigned\s+)?int},
293 qr{(?:unsigned\s+)?long},
294 qr{(?:unsigned\s+)?long\s+int},
295 qr{(?:unsigned\s+)?long\s+long},
296 qr{(?:unsigned\s+)?long\s+long\s+int},
305 qr{${Ident}_handler
},
306 qr{${Ident}_handler_fn
},
307 qr{target_(?:u)?long},
341 # This can be modified by sub possible. Since it can be empty, be careful
342 # about regexes that always match, because they can cause infinite loops.
343 our @modifierList = (
347 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
348 if (@modifierList > 0) {
349 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
350 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
352 $Modifier = qr{(?:$Attribute|$Sparse)};
355 (?
:$Modifier\s
+|const\s
+)*
357 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
361 (?
:\s
+$Modifier|\s
+const
)*
365 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
366 (?
:\s
+$Inline|\s
+$Modifier)*
368 $Declare = qr{(?:$Storage\s+)?$Type};
372 $chk_signoff = 0 if ($file);
379 my %git_commits = ();
381 open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format=%H %s", $ARGV[0]) ||
382 die "$P: git log --reverse --no-merges --format='%H %s' $ARGV[0] failed - $!\n";
384 for my $line (<$HASH>) {
385 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
386 next if (!defined($1) || !defined($2));
389 push(@patches, $sha1);
390 $git_commits{$sha1} = $subject;
395 die "$P: no revisions returned for revlist '$chk_branch'\n"
399 my $num_patches = @patches;
400 for my $hash (@patches) {
402 open($FILE, '-|', "git", "show", $hash) ||
403 die "$P: git show $hash - $!\n";
409 $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')';
410 if ($num_patches > 1 && $quiet == 0) {
411 my $prefix = "$i/$num_patches";
412 $prefix = BLUE
. BOLD
. $prefix . RESET
if $color;
413 print "$prefix Checking commit $vname\n";
414 $vname = "Patch $i/$num_patches";
416 $vname = "Commit " . $vname;
418 if (!process
($hash)) {
420 print "\n" if ($num_patches > 1 && $quiet == 0);
427 for my $filename (@ARGV) {
430 open($FILE, '-|', "diff -u /dev/null $filename") ||
431 die "$P: $filename: diff failed - $!\n";
432 } elsif ($filename eq '-') {
433 open($FILE, '<&STDIN');
435 open($FILE, '<', "$filename") ||
436 die "$P: $filename: open failed - $!\n";
438 if ($filename eq '-') {
439 $vname = 'Your patch';
443 print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0;
449 if (!process
($filename)) {
459 sub top_of_kernel_tree
{
463 "COPYING", "MAINTAINERS", "Makefile",
464 "README.rst", "docs", "VERSION",
465 "linux-user", "softmmu"
468 foreach my $check (@tree_check) {
469 if (! -e
$root . '/' . $check) {
481 for my $c (split(//, $str)) {
485 for (; ($n % 8) != 0; $n++) {
497 (my $res = shift) =~ tr/\t/ /c;
504 # Drop the diff line leader and expand tabs
506 $line = expand_tabs
($line);
508 # Pick the indent from the front of the line.
509 my ($white) = ($line =~ /^(\s*)/);
511 return (length($line), length($white));
514 my $sanitise_quote = '';
516 sub sanitise_line_reset
{
517 my ($in_comment) = @_;
520 $sanitise_quote = '*/';
522 $sanitise_quote = '';
535 # Always copy over the diff marker.
536 $res = substr($line, 0, 1);
538 for ($off = 1; $off < length($line); $off++) {
539 $c = substr($line, $off, 1);
541 # Comments we are wacking completely including the begin
542 # and end, all to $;.
543 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
544 $sanitise_quote = '*/';
546 substr($res, $off, 2, "$;$;");
550 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
551 $sanitise_quote = '';
552 substr($res, $off, 2, "$;$;");
556 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
557 $sanitise_quote = '//';
559 substr($res, $off, 2, $sanitise_quote);
564 # A \ in a string means ignore the next character.
565 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
567 substr($res, $off, 2, 'XX');
572 if ($c eq "'" || $c eq '"') {
573 if ($sanitise_quote eq '') {
574 $sanitise_quote = $c;
576 substr($res, $off, 1, $c);
578 } elsif ($sanitise_quote eq $c) {
579 $sanitise_quote = '';
583 #print "c<$c> SQ<$sanitise_quote>\n";
584 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
585 substr($res, $off, 1, $;);
586 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
587 substr($res, $off, 1, $;);
588 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
589 substr($res, $off, 1, 'X');
591 substr($res, $off, 1, $c);
595 if ($sanitise_quote eq '//') {
596 $sanitise_quote = '';
599 # The pathname on a #include may be surrounded by '<' and '>'.
600 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
601 my $clean = 'X' x
length($1);
602 $res =~ s@\
<.*\
>@
<$clean>@
;
604 # The whole of a #error is a string.
605 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
606 my $clean = 'X' x
length($1);
607 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
613 sub ctx_statement_block
{
614 my ($linenr, $remain, $off) = @_;
615 my $line = $linenr - 1;
632 @stack = (['', 0]) if ($#stack == -1);
634 #warn "CSB: blk<$blk> remain<$remain>\n";
635 # If we are about to drop off the end, pull in more
638 for (; $remain > 0; $line++) {
639 last if (!defined $lines[$line]);
640 next if ($lines[$line] =~ /^-/);
643 $blk .= $lines[$line] . "\n";
648 # Bail if there is no further context.
649 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
655 $c = substr($blk, $off, 1);
656 $remainder = substr($blk, $off);
658 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
660 # Handle nested #if/#else.
661 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
662 push(@stack, [ $type, $level ]);
663 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
664 ($type, $level) = @
{$stack[$#stack - 1]};
665 } elsif ($remainder =~ /^#\s*endif\b/) {
666 ($type, $level) = @
{pop(@stack)};
669 # Statement ends at the ';' or a close '}' at the
671 if ($level == 0 && $c eq ';') {
675 # An else is really a conditional as long as its not else if
676 if ($level == 0 && $coff_set == 0 &&
677 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
678 $remainder =~ /^(else)(?:\s|{)/ &&
679 $remainder !~ /^else\s+if\b/) {
680 $coff = $off + length($1) - 1;
682 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
683 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
686 if (($type eq '' || $type eq '(') && $c eq '(') {
690 if ($type eq '(' && $c eq ')') {
692 $type = ($level != 0)?
'(' : '';
694 if ($level == 0 && $coff < $soff) {
697 #warn "CSB: mark coff<$coff>\n";
700 if (($type eq '' || $type eq '{') && $c eq '{') {
704 if ($type eq '{' && $c eq '}') {
706 $type = ($level != 0)?
'{' : '';
709 if (substr($blk, $off + 1, 1) eq ';') {
717 # We are truly at the end, so shuffle to the next line.
724 my $statement = substr($blk, $soff, $off - $soff + 1);
725 my $condition = substr($blk, $soff, $coff - $soff + 1);
727 #warn "STATEMENT<$statement>\n";
728 #warn "CONDITION<$condition>\n";
730 #print "coff<$coff> soff<$off> loff<$loff>\n";
732 return ($statement, $condition,
733 $line, $remain + 1, $off - $loff + 1, $level);
736 sub statement_lines
{
739 # Strip the diff line prefixes and rip blank lines at start and end.
740 $stmt =~ s/(^|\n)./$1/g;
744 my @stmt_lines = ($stmt =~ /\n/g);
746 return $#stmt_lines + 2;
749 sub statement_rawlines
{
752 my @stmt_lines = ($stmt =~ /\n/g);
754 return $#stmt_lines + 2;
757 sub statement_block_size
{
760 $stmt =~ s/(^|\n)./$1/g;
766 my @stmt_lines = ($stmt =~ /\n/g);
767 my @stmt_statements = ($stmt =~ /;/g);
769 my $stmt_lines = $#stmt_lines + 2;
770 my $stmt_statements = $#stmt_statements + 1;
772 if ($stmt_lines > $stmt_statements) {
775 return $stmt_statements;
779 sub ctx_statement_full
{
780 my ($linenr, $remain, $off) = @_;
781 my ($statement, $condition, $level);
785 # Grab the first conditional/block pair.
786 ($statement, $condition, $linenr, $remain, $off, $level) =
787 ctx_statement_block
($linenr, $remain, $off);
788 #print "F: c<$condition> s<$statement> remain<$remain>\n";
789 push(@chunks, [ $condition, $statement ]);
790 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
791 return ($level, $linenr, @chunks);
794 # Pull in the following conditional/block pairs and see if they
795 # could continue the statement.
797 ($statement, $condition, $linenr, $remain, $off, $level) =
798 ctx_statement_block
($linenr, $remain, $off);
799 #print "C: c<$condition> s<$statement> remain<$remain>\n";
800 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
802 push(@chunks, [ $condition, $statement ]);
805 return ($level, $linenr, @chunks);
809 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
811 my $start = $linenr - 1;
818 my @stack = ($level);
819 for ($line = $start; $remain > 0; $line++) {
820 next if ($rawlines[$line] =~ /^-/);
823 $blk .= $rawlines[$line];
825 # Handle nested #if/#else.
826 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
827 push(@stack, $level);
828 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
829 $level = $stack[$#stack - 1];
830 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
831 $level = pop(@stack);
834 foreach my $c (split(//, $lines[$line])) {
835 ##print "C<$c>L<$level><$open$close>O<$off>\n";
841 if ($c eq $close && $level > 0) {
843 last if ($level == 0);
844 } elsif ($c eq $open) {
849 if (!$outer || $level <= 1) {
850 push(@res, $rawlines[$line]);
853 last if ($level == 0);
856 return ($level, @res);
858 sub ctx_block_outer
{
859 my ($linenr, $remain) = @_;
861 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
865 my ($linenr, $remain) = @_;
867 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
871 my ($linenr, $remain, $off) = @_;
873 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
876 sub ctx_block_level
{
877 my ($linenr, $remain) = @_;
879 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
881 sub ctx_statement_level
{
882 my ($linenr, $remain, $off) = @_;
884 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
887 sub ctx_locate_comment
{
888 my ($first_line, $end_line) = @_;
890 # Catch a comment on the end of the line itself.
891 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
892 return $current_comment if (defined $current_comment);
894 # Look through the context and try and figure out if there is a
897 $current_comment = '';
898 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
899 my $line = $rawlines[$linenr - 1];
901 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
904 if ($line =~ m@
/\
*@
) {
907 if (!$in_comment && $current_comment ne '') {
908 $current_comment = '';
910 $current_comment .= $line . "\n" if ($in_comment);
911 if ($line =~ m@\
*/@
) {
916 chomp($current_comment);
917 return($current_comment);
919 sub ctx_has_comment
{
920 my ($first_line, $end_line) = @_;
921 my $cmt = ctx_locate_comment
($first_line, $end_line);
923 ##print "LINE: $rawlines[$end_line - 1 ]\n";
924 ##print "CMMT: $cmt\n";
930 my ($linenr, $cnt) = @_;
932 my $offset = $linenr - 1;
937 $line = $rawlines[$offset++];
938 next if (defined($line) && $line =~ /^-/);
950 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
953 $coded = sprintf("^%c", unpack('C', $2) + 64);
962 my $av_preprocessor = 0;
968 $av_preprocessor = 0;
970 @av_paren_type = ('E');
971 $av_pend_colon = 'O';
974 sub annotate_values
{
975 my ($stream, $type) = @_;
978 my $var = '_' x
length($stream);
981 print "$stream\n" if ($dbg_values > 1);
983 while (length($cur)) {
984 @av_paren_type = ('E') if ($#av_paren_type < 0);
985 print " <" . join('', @av_paren_type) .
986 "> <$type> <$av_pending>" if ($dbg_values > 1);
987 if ($cur =~ /^(\s+)/o) {
988 print "WS($1)\n" if ($dbg_values > 1);
989 if ($1 =~ /\n/ && $av_preprocessor) {
990 $type = pop(@av_paren_type);
991 $av_preprocessor = 0;
994 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
995 print "CAST($1)\n" if ($dbg_values > 1);
996 push(@av_paren_type, $type);
999 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1000 print "DECLARE($1)\n" if ($dbg_values > 1);
1003 } elsif ($cur =~ /^($Modifier)\s*/) {
1004 print "MODIFIER($1)\n" if ($dbg_values > 1);
1007 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1008 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1009 $av_preprocessor = 1;
1010 push(@av_paren_type, $type);
1016 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1017 print "UNDEF($1)\n" if ($dbg_values > 1);
1018 $av_preprocessor = 1;
1019 push(@av_paren_type, $type);
1021 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1022 print "PRE_START($1)\n" if ($dbg_values > 1);
1023 $av_preprocessor = 1;
1025 push(@av_paren_type, $type);
1026 push(@av_paren_type, $type);
1029 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1030 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1031 $av_preprocessor = 1;
1033 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1037 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1038 print "PRE_END($1)\n" if ($dbg_values > 1);
1040 $av_preprocessor = 1;
1042 # Assume all arms of the conditional end as this
1043 # one does, and continue as if the #endif was not here.
1044 pop(@av_paren_type);
1045 push(@av_paren_type, $type);
1048 } elsif ($cur =~ /^(\\\n)/o) {
1049 print "PRECONT($1)\n" if ($dbg_values > 1);
1051 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1052 print "ATTR($1)\n" if ($dbg_values > 1);
1053 $av_pending = $type;
1056 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1057 print "SIZEOF($1)\n" if ($dbg_values > 1);
1063 } elsif ($cur =~ /^(if|while|for)\b/o) {
1064 print "COND($1)\n" if ($dbg_values > 1);
1068 } elsif ($cur =~/^(case)/o) {
1069 print "CASE($1)\n" if ($dbg_values > 1);
1070 $av_pend_colon = 'C';
1073 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1074 print "KEYWORD($1)\n" if ($dbg_values > 1);
1077 } elsif ($cur =~ /^(\()/o) {
1078 print "PAREN('$1')\n" if ($dbg_values > 1);
1079 push(@av_paren_type, $av_pending);
1083 } elsif ($cur =~ /^(\))/o) {
1084 my $new_type = pop(@av_paren_type);
1085 if ($new_type ne '_') {
1087 print "PAREN('$1') -> $type\n"
1088 if ($dbg_values > 1);
1090 print "PAREN('$1')\n" if ($dbg_values > 1);
1093 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1094 print "FUNC($1)\n" if ($dbg_values > 1);
1098 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1099 if (defined $2 && $type eq 'C' || $type eq 'T') {
1100 $av_pend_colon = 'B';
1101 } elsif ($type eq 'E') {
1102 $av_pend_colon = 'L';
1104 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1107 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1108 print "IDENT($1)\n" if ($dbg_values > 1);
1111 } elsif ($cur =~ /^($Assignment)/o) {
1112 print "ASSIGN($1)\n" if ($dbg_values > 1);
1115 } elsif ($cur =~/^(;|{|})/) {
1116 print "END($1)\n" if ($dbg_values > 1);
1118 $av_pend_colon = 'O';
1120 } elsif ($cur =~/^(,)/) {
1121 print "COMMA($1)\n" if ($dbg_values > 1);
1124 } elsif ($cur =~ /^(\?)/o) {
1125 print "QUESTION($1)\n" if ($dbg_values > 1);
1128 } elsif ($cur =~ /^(:)/o) {
1129 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1131 substr($var, length($res), 1, $av_pend_colon);
1132 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1137 $av_pend_colon = 'O';
1139 } elsif ($cur =~ /^(\[)/o) {
1140 print "CLOSE($1)\n" if ($dbg_values > 1);
1143 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1146 print "OPV($1)\n" if ($dbg_values > 1);
1153 substr($var, length($res), 1, $variant);
1156 } elsif ($cur =~ /^($Operators)/o) {
1157 print "OP($1)\n" if ($dbg_values > 1);
1158 if ($1 ne '++' && $1 ne '--') {
1162 } elsif ($cur =~ /(^.)/o) {
1163 print "C($1)\n" if ($dbg_values > 1);
1166 $cur = substr($cur, length($1));
1167 $res .= $type x
length($1);
1171 return ($res, $var);
1175 my ($possible, $line) = @_;
1176 my $notPermitted = qr{(?
:
1191 ^(?
:typedef
|struct
|enum
)\b|
1194 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1195 if ($possible !~ $notPermitted) {
1196 # Check for modifiers.
1197 $possible =~ s/\s*$Storage\s*//g;
1198 $possible =~ s/\s*$Sparse\s*//g;
1199 if ($possible =~ /^\s*$/) {
1201 } elsif ($possible =~ /\s/) {
1202 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
1203 for my $modifier (split(' ', $possible)) {
1204 if ($modifier !~ $notPermitted) {
1205 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1206 push(@modifierList, $modifier);
1211 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1212 push(@typeList, $possible);
1216 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1223 my ($level, $msg) = @_;
1224 if (defined $tst_only && $msg !~ /\Q$tst_only\E/) {
1229 $output .= BOLD
if $color;
1231 $output .= RED
if $color && $level eq 'ERROR';
1232 $output .= MAGENTA
if $color && $level eq 'WARNING';
1233 $output .= $level . ':';
1234 $output .= RESET
if $color;
1235 $output .= ' ' . $msg . "\n";
1237 $output = (split('\n', $output))[0] . "\n" if ($terse);
1239 push(our @report, $output);
1247 if (report
("ERROR", $_[0])) {
1253 if (report
("WARNING", $_[0])) {
1259 # According to tests/qtest/bios-tables-test.c: do not
1260 # change expected file in the same commit with adding test
1262 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_;
1264 # Note: shell script that rebuilds the expected files is in the same
1265 # directory as files themselves.
1266 # Note: allowed diff list can be changed both when changing expected
1267 # files and when changing tests.
1268 if ($name =~ m
#^tests/data/acpi/# and not $name =~ m#^\.sh$#) {
1269 $$acpi_testexpected = $name;
1270 } elsif ($name !~ m
#^tests/qtest/bios-tables-test-allowed-diff.h$#) {
1271 $$acpi_nontestexpected = $name;
1273 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) {
1274 ERROR
("Do not add expected files together with tests, " .
1275 "follow instructions in " .
1276 "tests/qtest/bios-tables-test.c: both " .
1277 $$acpi_testexpected . " and " .
1278 $$acpi_nontestexpected . " found\n");
1283 my $filename = shift;
1289 my $stashrawline="";
1300 my $in_header_lines = $file ?
0 : 1;
1301 my $in_commit_log = 0; #Scanning lines before patch
1302 my $reported_maintainer_file = 0;
1303 my $non_utf8_charset = 0;
1311 # Trace the real file/line as we go.
1317 my $comment_edge = 0;
1321 my $prev_values = 'E';
1324 my %suppress_ifbraces;
1325 my %suppress_whiletrailers;
1326 my %suppress_export;
1328 my $acpi_testexpected;
1329 my $acpi_nontestexpected;
1331 # Pre-scan the patch sanitizing the lines.
1333 sanitise_line_reset
();
1335 foreach my $rawline (@rawlines) {
1339 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1348 # Guestimate if this is a continuing comment. Run
1349 # the context looking for a comment "edge". If this
1350 # edge is a close comment then we must be in a comment
1354 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1355 next if (defined $rawlines[$ln - 1] &&
1356 $rawlines[$ln - 1] =~ /^-/);
1358 #print "RAW<$rawlines[$ln - 1]>\n";
1359 last if (!defined $rawlines[$ln - 1]);
1360 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1361 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1366 if (defined $edge && $edge eq '*/') {
1370 # Guestimate if this is a continuing comment. If this
1371 # is the start of a diff block and this line starts
1372 # ' *' then it is very likely a comment.
1373 if (!defined $edge &&
1374 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1379 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1380 sanitise_line_reset
($in_comment);
1382 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1383 # Standardise the strings and chars within the input to
1384 # simplify matching -- only bother with positive lines.
1385 $line = sanitise_line
($rawline);
1387 push(@lines, $line);
1390 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1395 #print "==>$rawline\n";
1396 #print "-->$line\n";
1403 foreach my $line (@lines) {
1406 my $rawline = $rawlines[$linenr - 1];
1408 #extract the line range in the file after the patch is applied
1409 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1411 $first_line = $linenr + 1;
1421 %suppress_ifbraces = ();
1422 %suppress_whiletrailers = ();
1423 %suppress_export = ();
1426 # track the line number as we move through the hunk, note that
1427 # new versions of GNU diff omit the leading space on completely
1428 # blank context lines so we need to count that too.
1429 } elsif ($line =~ /^( |\+|$)/) {
1431 $realcnt-- if ($realcnt != 0);
1433 # Measure the line length and indent.
1434 ($length, $indent) = line_stats
($rawline);
1436 # Track the previous line.
1437 ($prevline, $stashline) = ($stashline, $line);
1438 ($previndent, $stashindent) = ($stashindent, $indent);
1439 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1441 #warn "line<$line>\n";
1443 } elsif ($realcnt == 1) {
1447 my $hunk_line = ($realcnt != 0);
1449 #make up the handle for any error we report on this line
1450 $prefix = "$filename:$realline: " if ($emacs && $file);
1451 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1453 $here = "#$linenr: " if (!$file);
1454 $here = "#$realline: " if ($file);
1456 # extract the filename as it passes
1457 if ($line =~ /^diff --git.*?(\S+)$/) {
1459 $realfile =~ s@
^([^/]*)/@@
if (!$file);
1460 checkfilename
($realfile, \
$acpi_testexpected, \
$acpi_nontestexpected);
1461 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1463 $realfile =~ s@
^([^/]*)/@@
if (!$file);
1464 checkfilename
($realfile, \
$acpi_testexpected, \
$acpi_nontestexpected);
1467 if (!$file && $tree && $p1_prefix ne '' &&
1468 -e
"$root/$p1_prefix") {
1469 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1475 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1477 my $hereline = "$here\n$rawline\n";
1478 my $herecurr = "$here\n$rawline\n";
1479 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1481 $cnt_lines++ if ($realcnt != 0);
1483 # Check for incorrect file permissions
1484 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1485 my $permhere = $here . "FILE: $realfile\n";
1486 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1487 ERROR
("do not set execute permissions for source files\n" . $permhere);
1491 # Only allow Python 3 interpreter
1492 if ($realline == 1 &&
1493 $line =~ /^\+#!\ *\/usr\
/bin\/(?
:env
)?python
$/) {
1494 ERROR
("please use python3 interpreter\n" . $herecurr);
1497 # Accept git diff extended headers as valid patches
1498 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\
.\
-]+\s
*$/) {
1502 if ($line =~ /^Author: .*via Qemu-devel.*<qemu-devel\@nongnu.org>/) {
1503 ERROR
("Author email address is mangled by the mailing list\n" . $herecurr);
1506 #check the patch for a signoff:
1507 if ($line =~ /^\s*signed-off-by:/i) {
1508 # This is a signoff, if ugly, so do not double report.
1512 if (!($line =~ /^\s*Signed-off-by:/)) {
1513 ERROR
("The correct form is \"Signed-off-by\"\n" .
1516 if ($line =~ /^\s*signed-off-by:\S/i) {
1517 ERROR
("space required after Signed-off-by:\n" .
1522 # Check if MAINTAINERS is being updated. If so, there's probably no need to
1523 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
1524 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
1525 $reported_maintainer_file = 1;
1528 # Check for added, moved or deleted files
1529 if (!$reported_maintainer_file && !$in_commit_log &&
1530 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
1531 $line =~ /^rename (?:from|to) [\w\/\
.\
-]+\s
*$/ ||
1532 ($line =~ /\{\s*([\w\/\
.\
-]*)\s
*\
=\
>\s
*([\w\
/\.\-]*)\s*\}/ &&
1533 (defined($1) || defined($2))))) {
1534 $reported_maintainer_file = 1;
1535 WARN
("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
1538 # Check for wrappage within a valid hunk of the file
1539 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1540 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1541 $herecurr) if (!$emitted_corrupt++);
1544 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1545 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1546 $rawline !~ m/^$UTF8*$/) {
1547 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1549 my $blank = copy_spacing
($rawline);
1550 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1551 my $hereptr = "$hereline$ptr\n";
1553 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1556 if ($rawline =~ m/$UTF8_MOJIBAKE/) {
1557 ERROR
("Doubly-encoded UTF-8\n" . $herecurr);
1559 # Check if it's the start of a commit log
1560 # (not a header line and we haven't seen the patch filename)
1561 if ($in_header_lines && $realfile =~ /^$/ &&
1562 !($rawline =~ /^\s+\S/ ||
1563 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1564 $in_header_lines = 0;
1568 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1569 # declined it, i.e defined some charset where it is missing.
1570 if ($in_header_lines &&
1571 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1573 $non_utf8_charset = 1;
1576 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1577 $rawline =~ /$NON_ASCII_UTF8/) {
1578 WARN
("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1581 # ignore non-hunk lines and lines being removed
1582 next if (!$hunk_line || $line =~ /^-/);
1584 # ignore files that are being periodically imported from Linux
1585 next if ($realfile =~ /^(linux-headers|include\/standard
-headers
)\
//);
1587 #trailing whitespace
1588 if ($line =~ /^\+.*\015/) {
1589 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1590 ERROR
("DOS line endings\n" . $herevet);
1592 } elsif ($realfile =~ /^docs\/.+\
.txt
/ ||
1593 $realfile =~ /^docs\/.+\
.md
/) {
1594 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1595 # TODO: properly check we're in a code block
1596 # (surrounding text is 4-column aligned)
1597 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1598 ERROR
("code blocks in documentation should have " .
1599 "empty lines with exactly 4 columns of " .
1600 "whitespace\n" . $herevet);
1602 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1603 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1604 ERROR
("trailing whitespace\n" . $herevet);
1608 # checks for trace-events files
1609 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1610 if ($rawline =~ /%[-+ 0]*#/) {
1611 ERROR
("Don't use '#' flag of printf format ('%#') in " .
1612 "trace-events, use '0x' prefix instead\n" . $herecurr);
1615 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1617 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1618 my $tmpline = $rawline;
1619 $tmpline =~ s/($hex[.:\/ ])+$hex//g
;
1621 if ($tmpline =~ /(?<!0x)$hex/) {
1622 ERROR
("Hex numbers must be prefixed with '0x'\n" .
1628 # check we are in a valid source file if not then ignore this hunk
1629 next if ($realfile !~ /$SrcFile/);
1631 #90 column limit; exempt URLs, if no other words on line
1632 if ($line =~ /^\+/ &&
1633 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1634 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1638 ERROR
("line over 90 characters\n" . $herecurr);
1640 WARN
("line over 80 characters\n" . $herecurr);
1644 # check for spaces before a quoted newline
1645 if ($rawline =~ /^.*\".*\s\\n/) {
1646 ERROR
("unnecessary whitespace before a quoted newline\n" . $herecurr);
1649 # check for adding lines without a newline.
1650 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1651 ERROR
("adding a line without newline at end of file\n" . $herecurr);
1654 # check for RCS/CVS revision markers
1655 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1656 ERROR
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1659 # tabs are only allowed in assembly source code, and in
1660 # some scripts we imported from other projects.
1661 next if ($realfile =~ /\.(s|S)$/);
1662 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1664 if ($rawline =~ /^\+.*\t/) {
1665 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1666 ERROR
("code indent should never use tabs\n" . $herevet);
1670 # check we are in a valid C source file if not then ignore this hunk
1671 next if ($realfile !~ /\.(h|c|cpp)$/);
1673 # Block comment styles
1675 # Block comments use /* on a line of its own
1676 if ($rawline !~ m@
^\
+.*/\*.*\*/[ \t)}]*$@
&& #inline /*...*/
1677 $rawline =~ m@
^\
+.*/\*\*?+[ \t]*[^ \t]@) { # /* or /** non
-blank
1678 WARN
("Block comments use a leading /* on a separate line\n" . $herecurr);
1681 # Block comments use * on subsequent lines
1682 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1683 $prevrawline =~ /^\+.*?\/\
*/ && #starting /*
1684 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1685 $rawline =~ /^\+/ && #line is new
1686 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1687 WARN
("Block comments use * on subsequent lines\n" . $hereprev);
1690 # Block comments use */ on trailing lines
1691 if ($rawline !~ m@
^\
+[ \t]*\
*/[ \t]*$@ && #trailing */
1692 $rawline !~ m@
^\
+.*/\*.*\*/[ \t]*$@
&& #inline /*...*/
1693 $rawline !~ m@
^\
+.*\
*{2,}/[ \t]*$@ && #trailing **/
1694 $rawline =~ m@
^\
+[ \t]*.+\
*\
/[ \t]*$@) { #non blank */
1695 WARN
("Block comments use a trailing */ on a separate line\n" . $herecurr);
1698 # Block comment * alignment
1699 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1700 $line =~ /^\+[ \t]*$;/ && #leading comment
1701 $rawline =~ /^\+[ \t]*\*/ && #leading *
1702 (($prevrawline =~ /^\+.*?\/\
*/ && #leading /*
1703 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1704 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1706 $prevrawline =~ m@
^\
+([ \t]*/?
)\
*@
;
1708 $oldindent = expand_tabs
($1);
1710 $prevrawline =~ m@
^\
+(.*/?
)\
*@
;
1711 $oldindent = expand_tabs
($1);
1713 $rawline =~ m@
^\
+([ \t]*)\
*@
;
1715 $newindent = expand_tabs
($newindent);
1716 if (length($oldindent) ne length($newindent)) {
1717 WARN
("Block comments should align the * on each line\n" . $hereprev);
1721 # Check for potential 'bare' types
1722 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1724 if ($realcnt && $line =~ /.\s*\S/) {
1725 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1726 ctx_statement_block
($linenr, $realcnt, 0);
1727 $stat =~ s/\n./\n /g;
1728 $cond =~ s/\n./\n /g;
1730 # Find the real next line.
1731 $realline_next = $line_nr_next;
1732 if (defined $realline_next &&
1733 (!defined $lines[$realline_next - 1] ||
1734 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1741 # Ignore goto labels.
1742 if ($s =~ /$Ident:\*$/s) {
1744 # Ignore functions being called
1745 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1747 } elsif ($s =~ /^.\s*else\b/s) {
1749 # declarations always start with types
1750 } 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) {
1753 possible
($type, "A:" . $s);
1755 # definitions in global scope can only start with types
1756 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1757 possible
($1, "B:" . $s);
1760 # any (foo ... *) is a pointer cast, and foo is a type
1761 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1762 possible
($1, "C:" . $s);
1765 # Check for any sort of function declaration.
1766 # int foo(something bar, other baz);
1767 # void (*store_gdt)(x86_descr_ptr *);
1768 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1769 my ($name_len) = length($1);
1772 substr($ctx, 0, $name_len + 1, '');
1773 $ctx =~ s/\)[^\)]*$//;
1775 for my $arg (split(/\s*,\s*/, $ctx)) {
1776 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1778 possible
($1, "D:" . $s);
1786 # Checks which may be anchored in the context.
1789 # Check for switch () and associated case and default
1790 # statements should be at the same indent.
1791 if ($line=~/\bswitch\s*\(.*\)/) {
1794 my @ctx = ctx_block_outer
($linenr, $realcnt);
1796 for my $ctx (@ctx) {
1797 my ($clen, $cindent) = line_stats
($ctx);
1798 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1799 $indent != $cindent) {
1800 $err .= "$sep$ctx\n";
1807 ERROR
("switch and case should be at the same indent\n$hereline$err");
1811 # if/while/etc brace do not go on next line, unless defining a do while loop,
1812 # or if that brace on the next line is for something else
1813 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1814 my $pre_ctx = "$1$2";
1816 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1817 my $ctx_cnt = $realcnt - $#ctx - 1;
1818 my $ctx = join("\n", @ctx);
1820 my $ctx_ln = $linenr;
1821 my $ctx_skip = $realcnt;
1823 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1824 defined $lines[$ctx_ln - 1] &&
1825 $lines[$ctx_ln - 1] =~ /^-/)) {
1826 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1827 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1831 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1832 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1834 # The length of the "previous line" is checked against 80 because it
1835 # includes the + at the beginning of the line (if the actual line has
1836 # 79 or 80 characters, it is no longer possible to add a space and an
1837 # opening brace there)
1838 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1839 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1840 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1841 ERROR
("that open brace { should be on the previous line\n" .
1842 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1844 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1845 $ctx =~ /\)\s*\;\s*$/ &&
1846 defined $lines[$ctx_ln - 1])
1848 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1849 if ($nindent > $indent) {
1850 ERROR
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1851 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1856 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1857 if ($line =~ /while\s*\((0|false)\);/) {
1858 ERROR
("suspicious ; after while (0)\n" . $herecurr);
1861 # Check superfluous trailing ';'
1862 if ($line =~ /;;$/) {
1863 ERROR
("superfluous trailing semicolon\n" . $herecurr);
1866 # Check relative indent for conditionals and blocks.
1867 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1868 my ($s, $c) = ($stat, $cond);
1870 substr($s, 0, length($c), '');
1872 # Make sure we remove the line prefixes as we have
1873 # none on the first line, and are going to readd them
1877 # Find out how long the conditional actually is.
1878 my @newlines = ($c =~ /\n/gs);
1879 my $cond_lines = 1 + $#newlines;
1881 # We want to check the first line inside the block
1882 # starting at the end of the conditional, so remove:
1883 # 1) any blank line termination
1884 # 2) any opening brace { on end of the line
1886 my $continuation = 0;
1888 $s =~ s/^.*\bdo\b//;
1890 if ($s =~ s/^\s*\\//) {
1893 if ($s =~ s/^\s*?\n//) {
1898 # Also ignore a loop construct at the end of a
1899 # preprocessor statement.
1900 if (($prevline =~ /^.\s*#\s*define\s/ ||
1901 $prevline =~ /\\\s*$/) && $continuation == 0) {
1907 while ($cond_ptr != $cond_lines) {
1908 $cond_ptr = $cond_lines;
1910 # If we see an #else/#elif then the code
1912 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1917 # 1) blank lines, they should be at 0,
1918 # 2) preprocessor lines, and
1920 if ($continuation ||
1922 $s =~ /^\s*#\s*?/ ||
1923 $s =~ /^\s*$Ident\s*:/) {
1924 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1925 if ($s =~ s/^.*?\n//) {
1931 my (undef, $sindent) = line_stats
("+" . $s);
1932 my $stat_real = raw_line
($linenr, $cond_lines);
1934 # Check if either of these lines are modified, else
1935 # this is not this patch's fault.
1936 if (!defined($stat_real) ||
1937 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1940 if (defined($stat_real) && $cond_lines > 1) {
1941 $stat_real = "[...]\n$stat_real";
1944 #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";
1946 if ($check && (($sindent % 4) != 0 ||
1947 ($sindent <= $indent && $s ne ''))) {
1948 ERROR
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1952 # Track the 'values' across context and added lines.
1953 my $opline = $line; $opline =~ s/^./ /;
1954 my ($curr_values, $curr_vars) =
1955 annotate_values
($opline . "\n", $prev_values);
1956 $curr_values = $prev_values . $curr_values;
1958 my $outline = $opline; $outline =~ s/\t/ /g;
1959 print "$linenr > .$outline\n";
1960 print "$linenr > $curr_values\n";
1961 print "$linenr > $curr_vars\n";
1963 $prev_values = substr($curr_values, -1);
1965 #ignore lines not being added
1966 if ($line=~/^[^\+]/) {next;}
1968 # TEST: allow direct testing of the type matcher.
1970 if ($line =~ /^.\s*$Declare\s*$/) {
1971 ERROR
("TEST: is type\n" . $herecurr);
1972 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1973 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1977 # TEST: allow direct testing of the attribute matcher.
1979 if ($line =~ /^.\s*$Modifier\s*$/) {
1980 ERROR
("TEST: is attr\n" . $herecurr);
1981 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1982 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1987 # check for initialisation to aggregates open brace on the next line
1988 if ($line =~ /^.\s*\{/ &&
1989 $prevline =~ /(?:^|[^=])=\s*$/) {
1990 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1994 # Checks which are anchored on the added line.
1997 # check for malformed paths in #include statements (uses RAW line)
1998 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2000 if ($path =~ m{//}) {
2001 ERROR
("malformed #include filename\n" .
2006 # no C99 // comments
2007 if ($line =~ m{//} &&
2008 $rawline !~ m{// SPDX-License-Identifier: }) {
2009 ERROR
("do not use C99 // comments\n" . $herecurr);
2011 # Remove C99 comments.
2013 $opline =~ s@
//.*@@
;
2015 # check for global initialisers.
2016 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2017 ERROR
("do not initialise globals to 0 or NULL\n" .
2020 # check for static initialisers.
2021 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2022 ERROR
("do not initialise statics to 0 or NULL\n" .
2026 # * goes on variable not on type
2028 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
2029 my ($from, $to) = ($1, $1);
2031 # Should start with a space.
2032 $to =~ s/^(\S)/ $1/;
2033 # Should not end with a space.
2035 # '*'s should not have spaces between.
2036 while ($to =~ s/\*\s+\*/\*\*/) {
2039 #print "from<$from> to<$to>\n";
2041 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2043 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
2044 my ($from, $to, $ident) = ($1, $1, $2);
2046 # Should start with a space.
2047 $to =~ s/^(\S)/ $1/;
2048 # Should not end with a space.
2050 # '*'s should not have spaces between.
2051 while ($to =~ s/\*\s+\*/\*\*/) {
2053 # Modifiers should have spaces.
2054 $to =~ s/(\b$Modifier$)/$1 /;
2056 #print "from<$from> to<$to> ident<$ident>\n";
2057 if ($from ne $to && $ident !~ /^$Modifier$/) {
2058 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2062 # function brace can't be on same line, except for #defines of do while,
2063 # or if closed on same line
2064 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
2065 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
2066 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
2069 # open braces for enum, union and struct go on the same line.
2070 if ($line =~ /^.\s*\{/ &&
2071 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2072 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
2075 # missing space after union, struct or enum definition
2076 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2077 ERROR
("missing space after $1 definition\n" . $herecurr);
2080 # check for spacing round square brackets; allowed:
2081 # 1. with a type on the left -- int [] a;
2082 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2083 # 3. inside a curly brace -- = { [0...10] = 5 }
2084 # 4. after a comma -- [1] = 5, [2] = 6
2085 # 5. in a macro definition -- #define abc(x) [x] = y
2086 while ($line =~ /(.*?\s)\[/g) {
2087 my ($where, $prefix) = ($-[1], $1);
2088 if ($prefix !~ /$Type\s+$/ &&
2089 ($where != 0 || $prefix !~ /^.\s+$/) &&
2090 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
2091 $prefix !~ /[,{:]\s+$/) {
2092 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
2096 # check for spaces between functions and their parentheses.
2097 while ($line =~ /($Ident)\s+\(/g) {
2099 my $ctx_before = substr($line, 0, $-[1]);
2100 my $ctx = "$ctx_before$name";
2102 # Ignore those directives where spaces _are_ permitted.
2104 if|for|while|switch
|return|case
|
2105 volatile
|__volatile__
|coroutine_fn
|
2106 __attribute__
|format
|__extension__
|
2110 # Ignore 'catch (...)' in C++
2111 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
2113 # cpp #define statements have non-optional spaces, ie
2114 # if there is a space between the name and the open
2115 # parenthesis it is simply not a parameter group.
2116 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2118 # cpp #elif statement condition may start with a (
2119 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2121 # If this whole things ends with a type its most
2122 # likely a typedef for a function.
2123 } elsif ($ctx =~ /$Type$/) {
2126 ERROR
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2129 # Check operator spacing.
2130 if (!($line=~/\#\s*include/)) {
2132 <<=|>>=|<=|>=|==|!=|
2133 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
2134 =>|->|<<|>>|<|>|=|!|~|
2135 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
2138 my @elements = split(/($ops|;)/, $opline);
2141 my $blank = copy_spacing
($opline);
2143 for (my $n = 0; $n < $#elements; $n += 2) {
2144 $off += length($elements[$n]);
2146 # Pick up the preceding and succeeding characters.
2147 my $ca = substr($opline, 0, $off);
2149 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2150 $cc = substr($opline, $off + length($elements[$n + 1]));
2152 my $cb = "$ca$;$cc";
2155 $a = 'V' if ($elements[$n] ne '');
2156 $a = 'W' if ($elements[$n] =~ /\s$/);
2157 $a = 'C' if ($elements[$n] =~ /$;$/);
2158 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2159 $a = 'O' if ($elements[$n] eq '');
2160 $a = 'E' if ($ca =~ /^\s*$/);
2162 my $op = $elements[$n + 1];
2165 if (defined $elements[$n + 2]) {
2166 $c = 'V' if ($elements[$n + 2] ne '');
2167 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2168 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2169 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2170 $c = 'O' if ($elements[$n + 2] eq '');
2171 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2176 my $ctx = "${a}x${c}";
2178 my $at = "(ctx:$ctx)";
2180 my $ptr = substr($blank, 0, $off) . "^";
2181 my $hereptr = "$hereline$ptr\n";
2183 # Pull out the value of this operator.
2184 my $op_type = substr($curr_values, $off + 1, 1);
2186 # Get the full operator variant.
2187 my $opv = $op . substr($curr_vars, $off, 1);
2189 # Ignore operators passed as parameters.
2190 if ($op_type ne 'V' &&
2191 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2194 # } elsif ($op =~ /^$;+$/) {
2196 # ; should have either the end of line or a space or \ after it
2197 } elsif ($op eq ';') {
2198 if ($ctx !~ /.x[WEBC]/ &&
2199 $cc !~ /^\\/ && $cc !~ /^;/) {
2200 ERROR
("space required after that '$op' $at\n" . $hereptr);
2204 } elsif ($op eq '//') {
2206 # Ignore : used in class declaration in C++
2207 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2208 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2212 # : when part of a bitfield
2213 } elsif ($op eq '->' || $opv eq ':B') {
2214 if ($ctx =~ /Wx.|.xW/) {
2215 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
2218 # , must have a space on the right.
2219 # not required when having a single },{ on one line
2220 } elsif ($op eq ',') {
2221 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2222 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2223 ERROR
("space required after that '$op' $at\n" . $hereptr);
2226 # '*' as part of a type definition -- reported already.
2227 } elsif ($opv eq '*_') {
2228 #warn "'*' is part of type\n";
2230 # unary operators should have a space before and
2231 # none after. May be left adjacent to another
2232 # unary operator, or a cast
2233 } elsif ($op eq '!' || $op eq '~' ||
2234 $opv eq '*U' || $opv eq '-U' ||
2235 $opv eq '&U' || $opv eq '&&U') {
2236 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2237 # '~' used as a name of Destructor
2239 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2240 ERROR
("space required before that '$op' $at\n" . $hereptr);
2242 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2243 # A unary '*' may be const
2245 } elsif ($ctx =~ /.xW/) {
2246 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2249 # unary ++ and unary -- are allowed no space on one side.
2250 } elsif ($op eq '++' or $op eq '--') {
2251 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2252 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
2254 if ($ctx =~ /Wx[BE]/ ||
2255 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2256 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2258 if ($ctx =~ /ExW/) {
2259 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
2262 # A colon needs no spaces before when it is
2263 # terminating a case value or a label.
2264 } elsif ($opv eq ':C' || $opv eq ':L') {
2265 if ($ctx =~ /Wx./) {
2266 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
2269 # All the others need spaces both sides.
2270 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2273 if ($realfile =~ /\.cpp|\.h$/) {
2274 # Ignore template arguments <...> in C++
2275 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2285 # Ignore email addresses <foo@bar>
2287 $cc =~ /^\S+\@\S+>/) ||
2289 $ca =~ /<\S+\@\S+$/))
2295 if (($opv eq ':O' && $ca =~ /\?$/) ||
2296 ($op eq '?' && $cc =~ /^:/)) {
2301 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
2304 $off += length($elements[$n + 1]);
2308 #need space before brace following if, while, etc
2309 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2311 ERROR
("space required before the open brace '{'\n" . $herecurr);
2314 # closing brace should have a space following it when it has anything
2316 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2317 ERROR
("space required after that close brace '}'\n" . $herecurr);
2320 # check spacing on square brackets
2321 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2322 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
2324 if ($line =~ /\s\]/) {
2325 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
2328 # check spacing on parentheses
2329 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2330 $line !~ /for\s*\(\s+;/) {
2331 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
2333 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2334 $line !~ /for\s*\(.*;\s+\)/ &&
2335 $line !~ /:\s+\)/) {
2336 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
2339 # Return is not a function.
2340 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2344 # Flatten any parentheses
2345 $value =~ s/\(/ \(/g;
2346 $value =~ s/\)/\) /g;
2347 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2348 $value !~ /(?
:$Ident|-?
$Constant)\s
*
2350 (?
:$Ident|-?
$Constant)/x
&&
2351 $value =~ s/\([^\(\)]*\)/1/) {
2353 #print "value<$value>\n";
2354 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/ &&
2356 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
2358 } elsif ($spacing !~ /\s+/) {
2359 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2362 # Return of what appears to be an errno should normally be -'ve
2363 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2365 if ($name ne 'EOF' && $name ne 'ERROR') {
2366 ERROR
("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2370 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
2371 $line !~ /^.typedef/) {
2372 ERROR
("named $1 should be typedefed separately\n" . $herecurr);
2375 # Need a space before open parenthesis after if, while etc
2376 if ($line=~/\b(if|while|for|switch)\(/) {
2377 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2380 # Check for illegal assignment in if conditional -- and check for trailing
2381 # statements after the conditional.
2382 if ($line =~ /do\s*(?!{)/) {
2383 my ($stat_next) = ctx_statement_block
($line_nr_next,
2384 $remain_next, $off_next);
2385 $stat_next =~ s/\n./\n /g;
2386 ##print "stat<$stat> stat_next<$stat_next>\n";
2388 if ($stat_next =~ /^\s*while\b/) {
2389 # If the statement carries leading newlines,
2390 # then count those as offsets.
2392 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2394 statement_rawlines
($whitespace) - 1;
2396 $suppress_whiletrailers{$line_nr_next +
2400 if (!defined $suppress_whiletrailers{$linenr} &&
2401 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2402 my ($s, $c) = ($stat, $cond);
2404 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2405 ERROR
("do not use assignment in if condition\n" . $herecurr);
2408 # Find out what is on the end of the line after the
2410 substr($s, 0, length($c), '');
2412 $s =~ s/$;//g; # Remove any comments
2413 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2414 $c !~ /}\s*while\s*/)
2416 # Find out how long the conditional actually is.
2417 my @newlines = ($c =~ /\n/gs);
2418 my $cond_lines = 1 + $#newlines;
2421 $stat_real = raw_line
($linenr, $cond_lines)
2422 . "\n" if ($cond_lines);
2423 if (defined($stat_real) && $cond_lines > 1) {
2424 $stat_real = "[...]\n$stat_real";
2427 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2431 # Check for bitwise tests written as boolean
2443 ERROR
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2446 # if and else should not have general statements after it
2447 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2449 $s =~ s/$;//g; # Remove any comments
2450 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2451 ERROR
("trailing statements should be on next line\n" . $herecurr);
2454 # if should not continue a brace
2455 if ($line =~ /}\s*if\b/) {
2456 ERROR
("trailing statements should be on next line\n" .
2459 # case and default should not have general statements after them
2460 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2462 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2466 ERROR
("trailing statements should be on next line\n" . $herecurr);
2469 # Check for }<nl>else {, these must be at the same
2470 # indent level to be relevant to each other.
2471 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2472 $previndent == $indent) {
2473 ERROR
("else should follow close brace '}'\n" . $hereprev);
2476 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2477 $previndent == $indent) {
2478 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2480 # Find out what is on the end of the line after the
2482 substr($s, 0, length($c), '');
2485 if ($s =~ /^\s*;/) {
2486 ERROR
("while should follow close brace '}'\n" . $hereprev);
2490 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2491 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2492 # print "No studly caps, use _\n";
2493 # print "$herecurr";
2497 #no spaces allowed after \ in define
2498 if ($line=~/\#\s*define.*\\\s$/) {
2499 ERROR
("Whitespace after \\ makes next lines useless\n" . $herecurr);
2502 # multi-statement macros should be enclosed in a do while loop, grab the
2503 # first statement and ensure its the whole macro if its not enclosed
2504 # in a known good container
2505 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2506 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2509 my ($off, $dstat, $dcond, $rest);
2512 my $args = defined($1);
2514 # Find the end of the macro and limit our statement
2516 while ($cnt > 0 && defined $lines[$ln - 1] &&
2517 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2519 $ctx .= $rawlines[$ln - 1] . "\n";
2520 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2523 $ctx .= $rawlines[$ln - 1];
2525 ($dstat, $dcond, $ln, $cnt, $off) =
2526 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2527 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2528 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2530 # Extract the remainder of the define (if any) and
2531 # rip off surrounding spaces, and trailing \'s.
2533 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2534 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2535 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2536 $rest .= substr($lines[$ln - 1], $off) . "\n";
2542 $rest =~ s/\\\n.//g;
2546 # Clean up the original statement.
2548 substr($dstat, 0, length($dcond), '');
2550 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2553 $dstat =~ s/\\\n.//g;
2554 $dstat =~ s/^\s*//s;
2555 $dstat =~ s/\s*$//s;
2557 # Flatten any parentheses and braces
2558 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2559 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2560 $dstat =~ s/\[[^\{\}]*\]/1/)
2564 my $exceptions = qr{
2576 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2577 if ($rest ne '' && $rest ne ',') {
2578 if ($rest !~ /while\s*\(/ &&
2579 $dstat !~ /$exceptions/)
2581 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2584 } elsif ($ctx !~ /;/) {
2586 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2587 $dstat !~ /$exceptions/ &&
2588 $dstat !~ /^\.$Ident\s*=/ &&
2589 $dstat =~ /$Operators/)
2591 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2596 # check for missing bracing around if etc
2597 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2598 $line !~ /\#\s*if/) {
2601 # Check the pre-context.
2602 if ($line =~ /(\}.*?)$/) {
2605 if ($line !~ /else/) {
2606 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2611 my ($level, $endln, @chunks) =
2612 ctx_statement_full
($linenr, $realcnt, 1);
2614 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2615 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2618 if ($#chunks >= 0 && $level == 0) {
2620 my $herectx = $here . "\n";
2621 my $ln = $linenr - 1;
2622 for my $chunk (@chunks) {
2623 my ($cond, $block) = @
{$chunk};
2625 # If the condition carries leading newlines, then count those as offsets.
2626 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2627 my $offset = statement_rawlines
($whitespace) - 1;
2629 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2631 # We have looked at and allowed this specific line.
2632 $suppress_ifbraces{$ln + $offset} = 1;
2634 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2635 $ln += statement_rawlines
($block) - 1;
2637 substr($block, 0, length($cond), '');
2639 my $spaced_block = $block;
2640 $spaced_block =~ s/\n\+/ /g;
2642 $seen++ if ($spaced_block =~ /^\s*\{/);
2644 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2646 if (statement_lines
($cond) > 1) {
2647 print "APW: ALLOWED: cond<$cond>\n"
2651 if ($block =~/\b(?:if|for|while)\b/) {
2652 print "APW: ALLOWED: block<$block>\n"
2656 if (statement_block_size
($block) > 1) {
2657 print "APW: ALLOWED: lines block<$block>\n"
2662 if ($seen != ($#chunks + 1) && !$allowed) {
2663 ERROR
("braces {} are necessary for all arms of this statement\n" . $herectx);
2667 if (!defined $suppress_ifbraces{$linenr - 1} &&
2668 $line =~ /\b(if|while|for|else)\b/ &&
2669 $line !~ /\#\s*if/ &&
2670 $line !~ /\#\s*else/) {
2673 # Check the pre-context.
2674 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2677 if ($line !~ /else/) {
2678 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2684 my ($level, $endln, @chunks) =
2685 ctx_statement_full
($linenr, $realcnt, $-[0]);
2687 # Check the condition.
2688 my ($cond, $block) = @
{$chunks[0]};
2689 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2690 if $dbg_adv_checking;
2691 if (defined $cond) {
2692 substr($block, 0, length($cond), '');
2694 if (statement_lines
($cond) > 1) {
2695 print "APW: ALLOWED: cond<$cond>\n"
2699 if ($block =~/\b(?:if|for|while)\b/) {
2700 print "APW: ALLOWED: block<$block>\n"
2704 if (statement_block_size
($block) > 1) {
2705 print "APW: ALLOWED: lines block<$block>\n"
2709 # Check the post-context.
2710 if (defined $chunks[1]) {
2711 my ($cond, $block) = @
{$chunks[1]};
2712 if (defined $cond) {
2713 substr($block, 0, length($cond), '');
2715 if ($block =~ /^\s*\{/) {
2716 print "APW: ALLOWED: chunk-1 block<$block>\n"
2721 print "DCS: level=$level block<$block> allowed=$allowed\n"
2723 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2724 my $herectx = $here . "\n";;
2725 my $cnt = statement_rawlines
($block);
2727 for (my $n = 0; $n < $cnt; $n++) {
2728 $herectx .= raw_line
($linenr, $n) . "\n";;
2731 ERROR
("braces {} are necessary even for single statement blocks\n" . $herectx);
2735 # no volatiles please
2736 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2737 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
2738 $line !~ /sig_atomic_t/ &&
2739 !ctx_has_comment
($first_line, $linenr)) {
2740 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
2745 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2746 ERROR
("if this code is redundant consider removing it\n" .
2750 # check for needless g_free() checks
2751 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2753 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2754 ERROR
("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2758 # warn about #ifdefs in C files
2759 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2760 # print "#ifdef in C files should be avoided\n";
2761 # print "$herecurr";
2765 # warn about spacing in #ifdefs
2766 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2767 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2769 # check for memory barriers without a comment.
2770 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2771 if (!ctx_has_comment
($first_line, $linenr)) {
2772 ERROR
("memory barrier without comment\n" . $herecurr);
2775 # check of hardware specific defines
2776 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2777 # where they might be necessary.
2778 if ($line =~ m@
^.\s
*\#\s
*if.*\b__@
) {
2779 WARN
("architecture specific defines should be avoided\n" . $herecurr);
2782 # Check that the storage class is at the beginning of a declaration
2783 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2784 ERROR
("storage class should be at the beginning of the declaration\n" . $herecurr)
2787 # check the location of the inline attribute, that it is between
2788 # storage class and type.
2789 if ($line =~ /\b$Type\s+$Inline\b/ ||
2790 $line =~ /\b$Inline\s+$Storage\b/) {
2791 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2794 # check for sizeof(&)
2795 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2796 ERROR
("sizeof(& should be avoided\n" . $herecurr);
2799 # check for new externs in .c files.
2800 if ($realfile =~ /\.c$/ && defined $stat &&
2801 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2803 my $function_name = $1;
2804 my $paren_space = $2;
2807 if (defined $cond) {
2808 substr($s, 0, length($cond), '');
2810 if ($s =~ /^\s*;/ &&
2811 $function_name ne 'uninitialized_var')
2813 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2816 if ($paren_space =~ /\n/) {
2817 ERROR
("arguments for function declarations should follow identifier\n" . $herecurr);
2820 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2821 $stat =~ /^.\s*extern\s+/)
2823 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2826 # check for pointless casting of g_malloc return
2827 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2829 ERROR
("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2831 ERROR
("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2835 # check for gcc specific __FUNCTION__
2836 if ($line =~ /__FUNCTION__/) {
2837 ERROR
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2840 # recommend g_path_get_* over g_strdup(basename/dirname(...))
2841 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
2842 WARN
("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
2845 # recommend qemu_strto* over strto* for numeric conversions
2846 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2847 ERROR
("consider using qemu_$1 in preference to $1\n" . $herecurr);
2849 # recommend sigaction over signal for portability, when establishing a handler
2850 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2851 ERROR
("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2853 # check for module_init(), use category-specific init macros explicitly please
2854 if ($line =~ /^module_init\s*\(/) {
2855 ERROR
("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2857 # check for various ops structs, ensure they are const.
2858 my $struct_ops = qr{AIOCBInfo
|
2862 DisplayChangeListenerOps
|
2866 MemoryRegionIOMMUOps
|
2872 Spice
[A
-Z
][a
-zA
-Z0
-9]*Interface
|
2873 USBDesc
[A
-Z
][a
-zA
-Z0
-9]*|
2877 if ($line !~ /\bconst\b/ &&
2878 $line =~ /\b($struct_ops)\b.*=/) {
2879 ERROR
("initializer for struct $1 should normally be const\n" .
2883 # check for %L{u,d,i} in strings
2885 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2886 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2887 $string =~ s/%%/__/g;
2888 if ($string =~ /(?<!%)%L[udi]/) {
2889 ERROR
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2894 # QEMU specific tests
2895 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2896 ERROR
("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2899 # Qemu error function tests
2901 # Find newlines in error messages
2902 my $qemu_error_funcs = qr{error_setg
|
2905 error_setg_file_open
|
2918 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2919 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2922 # Continue checking for error messages that contains newlines. This
2923 # check handles cases where string literals are spread over multiple lines.
2925 # error_report("Error msg line #1"
2926 # "Error msg line #2\n");
2927 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2928 my $continued_str_literal = qr{\+\s*\".*\"};
2930 if ($rawline =~ /$quoted_newline_regex/) {
2931 # Backtrack to first line that does not contain only a quoted literal
2932 # and assume that it is the start of the statement.
2933 my $i = $linenr - 2;
2935 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2939 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2940 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2944 # check for non-portable libc calls that have portable alternatives in QEMU
2945 if ($line =~ /\bffs\(/) {
2946 ERROR
("use ctz32() instead of ffs()\n" . $herecurr);
2948 if ($line =~ /\bffsl\(/) {
2949 ERROR
("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2951 if ($line =~ /\bffsll\(/) {
2952 ERROR
("use ctz64() instead of ffsll()\n" . $herecurr);
2954 if ($line =~ /\bbzero\(/) {
2955 ERROR
("use memset() instead of bzero()\n" . $herecurr);
2957 if ($line =~ /\bgetpagesize\(\)/) {
2958 ERROR
("use qemu_real_host_page_size instead of getpagesize()\n" . $herecurr);
2960 if ($line =~ /\bsysconf\(_SC_PAGESIZE\)/) {
2961 ERROR
("use qemu_real_host_page_size instead of sysconf(_SC_PAGESIZE)\n" . $herecurr);
2963 my $non_exit_glib_asserts = qr{g_assert_cmpstr
|
2974 g_test_assert_expected_messages
|
2975 g_test_trap_assert_passed
|
2976 g_test_trap_assert_stdout
|
2977 g_test_trap_assert_stdout_unmatched
|
2978 g_test_trap_assert_stderr
|
2979 g_test_trap_assert_stderr_unmatched
}x
;
2980 if ($realfile !~ /^tests\// &&
2981 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2982 ERROR
("Use g_assert or g_assert_not_reached\n". $herecurr);
2986 if ($is_patch && $chk_signoff && $signoff == 0) {
2987 ERROR
("Missing Signed-off-by: line(s)\n");
2990 # If we have no input at all, then there is nothing to report on
2991 # so just keep quiet.
2992 if ($#rawlines == -1) {
2996 # In mailback mode only produce a report in the negative, for
2997 # things that appear to be patches.
2998 if ($mailback && ($clean == 1 || !$is_patch)) {
3002 # This is not a patch, and we are are in 'no-patch' mode so
3004 if (!$chk_patch && !$is_patch) {
3009 ERROR
("Does not appear to be a unified-diff format patch\n");
3012 print report_dump
();
3013 if ($summary && !($clean == 1 && $quiet == 1)) {
3014 print "$filename " if ($summary_file);
3015 print "total: $cnt_error errors, $cnt_warn warnings, " .
3016 "$cnt_lines lines checked\n";
3017 print "\n" if ($quiet == 0);
3021 # If there were whitespace errors which cleanpatch can fix
3022 # then suggest that.
3023 # if ($rpt_cleaners) {
3024 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3025 # print " scripts/cleanfile\n\n";
3029 if ($clean == 1 && $quiet == 0) {
3030 print "$vname has no obvious style problems and is ready for submission.\n"
3032 if ($clean == 0 && $quiet == 0) {
3033 print "$vname has style problems, please review. If any of these errors\n";
3034 print "are false positives report them to the maintainer, see\n";
3035 print "CHECKPATCH in MAINTAINERS.\n";
3038 return ($no_warnings ?
$clean : $cnt_error == 0);