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
15 use Getopt
::Long
qw(:config no_auto_abbrev);
37 Usage
: $P [OPTION
]... [FILE
]...
42 --no-tree run without a kernel tree
43 --no-signoff
do not check
for 'Signed-off-by' line
44 --patch treat FILE as patchfile
(default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f
, --file treat FILE as regular source file
48 --strict fail
if only warnings are found
49 --root
=PATH PATH to the kernel tree root
50 --no-summary suppress the per
-file summary
51 --mailback only produce a report
in case of warnings
/errors
52 --summary
-file include the filename
in summary
53 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
56 --test
-only
=WORD report only warnings
/errors containing WORD
58 -h
, --help
, --version display this help
and exit
60 When FILE is
- read standard input
.
67 'q|quiet+' => \
$quiet,
69 'signoff!' => \
$chk_signoff,
70 'patch!' => \
$chk_patch,
74 'strict!' => \
$no_warnings,
76 'summary!' => \
$summary,
77 'mailback!' => \
$mailback,
78 'summary-file!' => \
$summary_file,
81 'test-only=s' => \
$tst_only,
91 print "$P: no input files\n";
100 my $dbg_adv_checking = 0;
102 for my $key (keys %debug) {
104 eval "\${dbg_$key} = '$debug{$key}';";
108 my $rpt_cleaners = 0;
117 if (!top_of_kernel_tree
($root)) {
118 die "$P: $root: --root does not point at a valid tree\n";
121 if (top_of_kernel_tree
('.')) {
123 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
124 top_of_kernel_tree
($1)) {
129 if (!defined $root) {
130 print "Must be run from the top-level dir. of a kernel tree\n";
135 my $emitted_corrupt = 0;
138 [A
-Za
-z_
][A
-Za
-z\d_
]*
139 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
141 our $Storage = qr{extern|static|asmlinkage};
146 # Notes to $Attribute:
151 QEMU_WARN_UNUSED_RESULT
|
158 our $Inline = qr{inline};
159 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
160 our $Lval = qr{$Ident(?:$Member)*};
162 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
163 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
164 our $Compare = qr{<=|>=|==|!=|<|>};
168 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
176 [\x09\x0A\x0D\x20-\x7E] # ASCII
177 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
178 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
179 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
180 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
181 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
182 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
183 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
186 # There are still some false positives, but this catches most
188 our $typeTypedefs = qr{(?x
:
189 [A
-Z
][A
-Z\d_
]*[a
-z
][A
-Za
-z\d_
]* # camelcase
190 | [A
-Z
][A
-Z\d_
]*AIOCB
# all uppercase
191 | [A
-Z
][A
-Z\d_
]*CPU
# all uppercase
192 | QEMUBH
# all uppercase
197 qr{(?:unsigned\s+)?char},
198 qr{(?:unsigned\s+)?short},
199 qr{(?:unsigned\s+)?int},
200 qr{(?:unsigned\s+)?long},
201 qr{(?:unsigned\s+)?long\s+int},
202 qr{(?:unsigned\s+)?long\s+long},
203 qr{(?:unsigned\s+)?long\s+long\s+int},
212 qr{${Ident}_handler
},
213 qr{${Ident}_handler_fn
},
214 qr{target_(?:u)?long},
217 # This can be modified by sub possible. Since it can be empty, be careful
218 # about regexes that always match, because they can cause infinite loops.
219 our @modifierList = (
223 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
224 if (@modifierList > 0) {
225 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
226 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
228 $Modifier = qr{(?:$Attribute|$Sparse)};
231 (?
:$Modifier\s
+|const\s
+)*
233 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
237 (?
:\s
+$Modifier|\s
+const
)*
241 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
242 (?
:\s
+$Inline|\s
+$Modifier)*
244 $Declare = qr{(?:$Storage\s+)?$Type};
248 $chk_signoff = 0 if ($file);
253 for my $filename (@ARGV) {
256 open($FILE, '-|', "diff -u /dev/null $filename") ||
257 die "$P: $filename: diff failed - $!\n";
258 } elsif ($filename eq '-') {
259 open($FILE, '<&STDIN');
261 open($FILE, '<', "$filename") ||
262 die "$P: $filename: open failed - $!\n";
264 if ($filename eq '-') {
265 $vname = 'Your patch';
274 if (!process
($filename)) {
283 sub top_of_kernel_tree
{
287 "COPYING", "MAINTAINERS", "Makefile",
288 "README", "docs", "VERSION",
292 foreach my $check (@tree_check) {
293 if (! -e
$root . '/' . $check) {
305 for my $c (split(//, $str)) {
309 for (; ($n % 8) != 0; $n++) {
321 (my $res = shift) =~ tr/\t/ /c;
328 # Drop the diff line leader and expand tabs
330 $line = expand_tabs
($line);
332 # Pick the indent from the front of the line.
333 my ($white) = ($line =~ /^(\s*)/);
335 return (length($line), length($white));
338 my $sanitise_quote = '';
340 sub sanitise_line_reset
{
341 my ($in_comment) = @_;
344 $sanitise_quote = '*/';
346 $sanitise_quote = '';
359 # Always copy over the diff marker.
360 $res = substr($line, 0, 1);
362 for ($off = 1; $off < length($line); $off++) {
363 $c = substr($line, $off, 1);
365 # Comments we are wacking completely including the begin
366 # and end, all to $;.
367 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
368 $sanitise_quote = '*/';
370 substr($res, $off, 2, "$;$;");
374 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
375 $sanitise_quote = '';
376 substr($res, $off, 2, "$;$;");
380 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
381 $sanitise_quote = '//';
383 substr($res, $off, 2, $sanitise_quote);
388 # A \ in a string means ignore the next character.
389 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
391 substr($res, $off, 2, 'XX');
396 if ($c eq "'" || $c eq '"') {
397 if ($sanitise_quote eq '') {
398 $sanitise_quote = $c;
400 substr($res, $off, 1, $c);
402 } elsif ($sanitise_quote eq $c) {
403 $sanitise_quote = '';
407 #print "c<$c> SQ<$sanitise_quote>\n";
408 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
409 substr($res, $off, 1, $;);
410 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
411 substr($res, $off, 1, $;);
412 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
413 substr($res, $off, 1, 'X');
415 substr($res, $off, 1, $c);
419 if ($sanitise_quote eq '//') {
420 $sanitise_quote = '';
423 # The pathname on a #include may be surrounded by '<' and '>'.
424 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
425 my $clean = 'X' x
length($1);
426 $res =~ s@\
<.*\
>@
<$clean>@
;
428 # The whole of a #error is a string.
429 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
430 my $clean = 'X' x
length($1);
431 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
437 sub ctx_statement_block
{
438 my ($linenr, $remain, $off) = @_;
439 my $line = $linenr - 1;
456 @stack = (['', 0]) if ($#stack == -1);
458 #warn "CSB: blk<$blk> remain<$remain>\n";
459 # If we are about to drop off the end, pull in more
462 for (; $remain > 0; $line++) {
463 last if (!defined $lines[$line]);
464 next if ($lines[$line] =~ /^-/);
467 $blk .= $lines[$line] . "\n";
472 # Bail if there is no further context.
473 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
479 $c = substr($blk, $off, 1);
480 $remainder = substr($blk, $off);
482 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
484 # Handle nested #if/#else.
485 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
486 push(@stack, [ $type, $level ]);
487 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
488 ($type, $level) = @
{$stack[$#stack - 1]};
489 } elsif ($remainder =~ /^#\s*endif\b/) {
490 ($type, $level) = @
{pop(@stack)};
493 # Statement ends at the ';' or a close '}' at the
495 if ($level == 0 && $c eq ';') {
499 # An else is really a conditional as long as its not else if
500 if ($level == 0 && $coff_set == 0 &&
501 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
502 $remainder =~ /^(else)(?:\s|{)/ &&
503 $remainder !~ /^else\s+if\b/) {
504 $coff = $off + length($1) - 1;
506 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
507 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
510 if (($type eq '' || $type eq '(') && $c eq '(') {
514 if ($type eq '(' && $c eq ')') {
516 $type = ($level != 0)?
'(' : '';
518 if ($level == 0 && $coff < $soff) {
521 #warn "CSB: mark coff<$coff>\n";
524 if (($type eq '' || $type eq '{') && $c eq '{') {
528 if ($type eq '{' && $c eq '}') {
530 $type = ($level != 0)?
'{' : '';
533 if (substr($blk, $off + 1, 1) eq ';') {
541 # We are truly at the end, so shuffle to the next line.
548 my $statement = substr($blk, $soff, $off - $soff + 1);
549 my $condition = substr($blk, $soff, $coff - $soff + 1);
551 #warn "STATEMENT<$statement>\n";
552 #warn "CONDITION<$condition>\n";
554 #print "coff<$coff> soff<$off> loff<$loff>\n";
556 return ($statement, $condition,
557 $line, $remain + 1, $off - $loff + 1, $level);
560 sub statement_lines
{
563 # Strip the diff line prefixes and rip blank lines at start and end.
564 $stmt =~ s/(^|\n)./$1/g;
568 my @stmt_lines = ($stmt =~ /\n/g);
570 return $#stmt_lines + 2;
573 sub statement_rawlines
{
576 my @stmt_lines = ($stmt =~ /\n/g);
578 return $#stmt_lines + 2;
581 sub statement_block_size
{
584 $stmt =~ s/(^|\n)./$1/g;
590 my @stmt_lines = ($stmt =~ /\n/g);
591 my @stmt_statements = ($stmt =~ /;/g);
593 my $stmt_lines = $#stmt_lines + 2;
594 my $stmt_statements = $#stmt_statements + 1;
596 if ($stmt_lines > $stmt_statements) {
599 return $stmt_statements;
603 sub ctx_statement_full
{
604 my ($linenr, $remain, $off) = @_;
605 my ($statement, $condition, $level);
609 # Grab the first conditional/block pair.
610 ($statement, $condition, $linenr, $remain, $off, $level) =
611 ctx_statement_block
($linenr, $remain, $off);
612 #print "F: c<$condition> s<$statement> remain<$remain>\n";
613 push(@chunks, [ $condition, $statement ]);
614 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
615 return ($level, $linenr, @chunks);
618 # Pull in the following conditional/block pairs and see if they
619 # could continue the statement.
621 ($statement, $condition, $linenr, $remain, $off, $level) =
622 ctx_statement_block
($linenr, $remain, $off);
623 #print "C: c<$condition> s<$statement> remain<$remain>\n";
624 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
626 push(@chunks, [ $condition, $statement ]);
629 return ($level, $linenr, @chunks);
633 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
635 my $start = $linenr - 1;
642 my @stack = ($level);
643 for ($line = $start; $remain > 0; $line++) {
644 next if ($rawlines[$line] =~ /^-/);
647 $blk .= $rawlines[$line];
649 # Handle nested #if/#else.
650 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
651 push(@stack, $level);
652 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
653 $level = $stack[$#stack - 1];
654 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
655 $level = pop(@stack);
658 foreach my $c (split(//, $lines[$line])) {
659 ##print "C<$c>L<$level><$open$close>O<$off>\n";
665 if ($c eq $close && $level > 0) {
667 last if ($level == 0);
668 } elsif ($c eq $open) {
673 if (!$outer || $level <= 1) {
674 push(@res, $rawlines[$line]);
677 last if ($level == 0);
680 return ($level, @res);
682 sub ctx_block_outer
{
683 my ($linenr, $remain) = @_;
685 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
689 my ($linenr, $remain) = @_;
691 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
695 my ($linenr, $remain, $off) = @_;
697 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
700 sub ctx_block_level
{
701 my ($linenr, $remain) = @_;
703 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
705 sub ctx_statement_level
{
706 my ($linenr, $remain, $off) = @_;
708 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
711 sub ctx_locate_comment
{
712 my ($first_line, $end_line) = @_;
714 # Catch a comment on the end of the line itself.
715 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
716 return $current_comment if (defined $current_comment);
718 # Look through the context and try and figure out if there is a
721 $current_comment = '';
722 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
723 my $line = $rawlines[$linenr - 1];
725 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
728 if ($line =~ m@
/\
*@
) {
731 if (!$in_comment && $current_comment ne '') {
732 $current_comment = '';
734 $current_comment .= $line . "\n" if ($in_comment);
735 if ($line =~ m@\
*/@
) {
740 chomp($current_comment);
741 return($current_comment);
743 sub ctx_has_comment
{
744 my ($first_line, $end_line) = @_;
745 my $cmt = ctx_locate_comment
($first_line, $end_line);
747 ##print "LINE: $rawlines[$end_line - 1 ]\n";
748 ##print "CMMT: $cmt\n";
754 my ($linenr, $cnt) = @_;
756 my $offset = $linenr - 1;
761 $line = $rawlines[$offset++];
762 next if (defined($line) && $line =~ /^-/);
774 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
777 $coded = sprintf("^%c", unpack('C', $2) + 64);
786 my $av_preprocessor = 0;
792 $av_preprocessor = 0;
794 @av_paren_type = ('E');
795 $av_pend_colon = 'O';
798 sub annotate_values
{
799 my ($stream, $type) = @_;
802 my $var = '_' x
length($stream);
805 print "$stream\n" if ($dbg_values > 1);
807 while (length($cur)) {
808 @av_paren_type = ('E') if ($#av_paren_type < 0);
809 print " <" . join('', @av_paren_type) .
810 "> <$type> <$av_pending>" if ($dbg_values > 1);
811 if ($cur =~ /^(\s+)/o) {
812 print "WS($1)\n" if ($dbg_values > 1);
813 if ($1 =~ /\n/ && $av_preprocessor) {
814 $type = pop(@av_paren_type);
815 $av_preprocessor = 0;
818 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
819 print "CAST($1)\n" if ($dbg_values > 1);
820 push(@av_paren_type, $type);
823 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
824 print "DECLARE($1)\n" if ($dbg_values > 1);
827 } elsif ($cur =~ /^($Modifier)\s*/) {
828 print "MODIFIER($1)\n" if ($dbg_values > 1);
831 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
832 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
833 $av_preprocessor = 1;
834 push(@av_paren_type, $type);
840 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
841 print "UNDEF($1)\n" if ($dbg_values > 1);
842 $av_preprocessor = 1;
843 push(@av_paren_type, $type);
845 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
846 print "PRE_START($1)\n" if ($dbg_values > 1);
847 $av_preprocessor = 1;
849 push(@av_paren_type, $type);
850 push(@av_paren_type, $type);
853 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
854 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
855 $av_preprocessor = 1;
857 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
861 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
862 print "PRE_END($1)\n" if ($dbg_values > 1);
864 $av_preprocessor = 1;
866 # Assume all arms of the conditional end as this
867 # one does, and continue as if the #endif was not here.
869 push(@av_paren_type, $type);
872 } elsif ($cur =~ /^(\\\n)/o) {
873 print "PRECONT($1)\n" if ($dbg_values > 1);
875 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
876 print "ATTR($1)\n" if ($dbg_values > 1);
880 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
881 print "SIZEOF($1)\n" if ($dbg_values > 1);
887 } elsif ($cur =~ /^(if|while|for)\b/o) {
888 print "COND($1)\n" if ($dbg_values > 1);
892 } elsif ($cur =~/^(case)/o) {
893 print "CASE($1)\n" if ($dbg_values > 1);
894 $av_pend_colon = 'C';
897 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
898 print "KEYWORD($1)\n" if ($dbg_values > 1);
901 } elsif ($cur =~ /^(\()/o) {
902 print "PAREN('$1')\n" if ($dbg_values > 1);
903 push(@av_paren_type, $av_pending);
907 } elsif ($cur =~ /^(\))/o) {
908 my $new_type = pop(@av_paren_type);
909 if ($new_type ne '_') {
911 print "PAREN('$1') -> $type\n"
912 if ($dbg_values > 1);
914 print "PAREN('$1')\n" if ($dbg_values > 1);
917 } elsif ($cur =~ /^($Ident)\s*\(/o) {
918 print "FUNC($1)\n" if ($dbg_values > 1);
922 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
923 if (defined $2 && $type eq 'C' || $type eq 'T') {
924 $av_pend_colon = 'B';
925 } elsif ($type eq 'E') {
926 $av_pend_colon = 'L';
928 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
931 } elsif ($cur =~ /^($Ident|$Constant)/o) {
932 print "IDENT($1)\n" if ($dbg_values > 1);
935 } elsif ($cur =~ /^($Assignment)/o) {
936 print "ASSIGN($1)\n" if ($dbg_values > 1);
939 } elsif ($cur =~/^(;|{|})/) {
940 print "END($1)\n" if ($dbg_values > 1);
942 $av_pend_colon = 'O';
944 } elsif ($cur =~/^(,)/) {
945 print "COMMA($1)\n" if ($dbg_values > 1);
948 } elsif ($cur =~ /^(\?)/o) {
949 print "QUESTION($1)\n" if ($dbg_values > 1);
952 } elsif ($cur =~ /^(:)/o) {
953 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
955 substr($var, length($res), 1, $av_pend_colon);
956 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
961 $av_pend_colon = 'O';
963 } elsif ($cur =~ /^(\[)/o) {
964 print "CLOSE($1)\n" if ($dbg_values > 1);
967 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
970 print "OPV($1)\n" if ($dbg_values > 1);
977 substr($var, length($res), 1, $variant);
980 } elsif ($cur =~ /^($Operators)/o) {
981 print "OP($1)\n" if ($dbg_values > 1);
982 if ($1 ne '++' && $1 ne '--') {
986 } elsif ($cur =~ /(^.)/o) {
987 print "C($1)\n" if ($dbg_values > 1);
990 $cur = substr($cur, length($1));
991 $res .= $type x
length($1);
999 my ($possible, $line) = @_;
1000 my $notPermitted = qr{(?
:
1017 ^(?
:typedef
|struct
|enum
)\b
1019 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1020 if ($possible !~ $notPermitted) {
1021 # Check for modifiers.
1022 $possible =~ s/\s*$Storage\s*//g;
1023 $possible =~ s/\s*$Sparse\s*//g;
1024 if ($possible =~ /^\s*$/) {
1026 } elsif ($possible =~ /\s/) {
1027 $possible =~ s/\s*$Type\s*//g;
1028 for my $modifier (split(' ', $possible)) {
1029 if ($modifier !~ $notPermitted) {
1030 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1031 push(@modifierList, $modifier);
1036 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1037 push(@typeList, $possible);
1041 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1048 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1051 my $line = $prefix . $_[0];
1053 $line = (split('\n', $line))[0] . "\n" if ($terse);
1055 push(our @report, $line);
1063 if (report
("ERROR: $_[0]\n")) {
1069 if (report
("WARNING: $_[0]\n")) {
1076 my $filename = shift;
1082 my $stashrawline="";
1099 # Trace the real file/line as we go.
1105 my $comment_edge = 0;
1109 my $prev_values = 'E';
1112 my %suppress_ifbraces;
1113 my %suppress_whiletrailers;
1114 my %suppress_export;
1116 # Pre-scan the patch sanitizing the lines.
1118 sanitise_line_reset
();
1120 foreach my $rawline (@rawlines) {
1124 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1133 # Guestimate if this is a continuing comment. Run
1134 # the context looking for a comment "edge". If this
1135 # edge is a close comment then we must be in a comment
1139 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1140 next if (defined $rawlines[$ln - 1] &&
1141 $rawlines[$ln - 1] =~ /^-/);
1143 #print "RAW<$rawlines[$ln - 1]>\n";
1144 last if (!defined $rawlines[$ln - 1]);
1145 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1146 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1151 if (defined $edge && $edge eq '*/') {
1155 # Guestimate if this is a continuing comment. If this
1156 # is the start of a diff block and this line starts
1157 # ' *' then it is very likely a comment.
1158 if (!defined $edge &&
1159 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1164 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1165 sanitise_line_reset
($in_comment);
1167 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1168 # Standardise the strings and chars within the input to
1169 # simplify matching -- only bother with positive lines.
1170 $line = sanitise_line
($rawline);
1172 push(@lines, $line);
1175 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1180 #print "==>$rawline\n";
1181 #print "-->$line\n";
1188 foreach my $line (@lines) {
1191 my $rawline = $rawlines[$linenr - 1];
1193 #extract the line range in the file after the patch is applied
1194 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1196 $first_line = $linenr + 1;
1206 %suppress_ifbraces = ();
1207 %suppress_whiletrailers = ();
1208 %suppress_export = ();
1211 # track the line number as we move through the hunk, note that
1212 # new versions of GNU diff omit the leading space on completely
1213 # blank context lines so we need to count that too.
1214 } elsif ($line =~ /^( |\+|$)/) {
1216 $realcnt-- if ($realcnt != 0);
1218 # Measure the line length and indent.
1219 ($length, $indent) = line_stats
($rawline);
1221 # Track the previous line.
1222 ($prevline, $stashline) = ($stashline, $line);
1223 ($previndent, $stashindent) = ($stashindent, $indent);
1224 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1226 #warn "line<$line>\n";
1228 } elsif ($realcnt == 1) {
1232 my $hunk_line = ($realcnt != 0);
1234 #make up the handle for any error we report on this line
1235 $prefix = "$filename:$realline: " if ($emacs && $file);
1236 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1238 $here = "#$linenr: " if (!$file);
1239 $here = "#$realline: " if ($file);
1241 # extract the filename as it passes
1242 if ($line =~ /^diff --git.*?(\S+)$/) {
1244 $realfile =~ s@
^([^/]*)/@@
;
1246 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1248 $realfile =~ s@
^([^/]*)/@@
;
1251 if (!$file && $tree && $p1_prefix ne '' &&
1252 -e
"$root/$p1_prefix") {
1253 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1259 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1261 my $hereline = "$here\n$rawline\n";
1262 my $herecurr = "$here\n$rawline\n";
1263 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1265 $cnt_lines++ if ($realcnt != 0);
1267 # Check for incorrect file permissions
1268 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1269 my $permhere = $here . "FILE: $realfile\n";
1270 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1271 ERROR
("do not set execute permissions for source files\n" . $permhere);
1275 # Accept git diff extended headers as valid patches
1276 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\
.\
-]+\s
*$/) {
1280 #check the patch for a signoff:
1281 if ($line =~ /^\s*signed-off-by:/i) {
1282 # This is a signoff, if ugly, so do not double report.
1284 if (!($line =~ /^\s*Signed-off-by:/)) {
1285 ERROR
("The correct form is \"Signed-off-by\"\n" .
1288 if ($line =~ /^\s*signed-off-by:\S/i) {
1289 ERROR
("space required after Signed-off-by:\n" .
1294 # Check for wrappage within a valid hunk of the file
1295 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1296 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1297 $herecurr) if (!$emitted_corrupt++);
1300 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1301 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1302 $rawline !~ m/^$UTF8*$/) {
1303 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1305 my $blank = copy_spacing
($rawline);
1306 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1307 my $hereptr = "$hereline$ptr\n";
1309 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1312 # ignore non-hunk lines and lines being removed
1313 next if (!$hunk_line || $line =~ /^-/);
1315 # ignore files that are being periodically imported from Linux
1316 next if ($realfile =~ /^(linux-headers|include\/standard
-headers
)\
//);
1318 #trailing whitespace
1319 if ($line =~ /^\+.*\015/) {
1320 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1321 ERROR
("DOS line endings\n" . $herevet);
1323 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1324 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1325 ERROR
("trailing whitespace\n" . $herevet);
1329 # check we are in a valid source file if not then ignore this hunk
1330 next if ($realfile !~ /\.(h|c|cpp|s|S|pl|py|sh)$/);
1333 if ($line =~ /^\+/ &&
1334 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1338 ERROR
("line over 90 characters\n" . $herecurr);
1340 WARN
("line over 80 characters\n" . $herecurr);
1344 # check for spaces before a quoted newline
1345 if ($rawline =~ /^.*\".*\s\\n/) {
1346 ERROR
("unnecessary whitespace before a quoted newline\n" . $herecurr);
1349 # check for adding lines without a newline.
1350 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1351 ERROR
("adding a line without newline at end of file\n" . $herecurr);
1354 # check for RCS/CVS revision markers
1355 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1356 ERROR
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1359 # tabs are only allowed in assembly source code, and in
1360 # some scripts we imported from other projects.
1361 next if ($realfile =~ /\.(s|S)$/);
1362 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1364 if ($rawline =~ /^\+.*\t/) {
1365 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1366 ERROR
("code indent should never use tabs\n" . $herevet);
1370 # check we are in a valid C source file if not then ignore this hunk
1371 next if ($realfile !~ /\.(h|c|cpp)$/);
1373 # Check for potential 'bare' types
1374 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1376 if ($realcnt && $line =~ /.\s*\S/) {
1377 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1378 ctx_statement_block
($linenr, $realcnt, 0);
1379 $stat =~ s/\n./\n /g;
1380 $cond =~ s/\n./\n /g;
1382 # Find the real next line.
1383 $realline_next = $line_nr_next;
1384 if (defined $realline_next &&
1385 (!defined $lines[$realline_next - 1] ||
1386 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1393 # Ignore goto labels.
1394 if ($s =~ /$Ident:\*$/s) {
1396 # Ignore functions being called
1397 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1399 } elsif ($s =~ /^.\s*else\b/s) {
1401 # declarations always start with types
1402 } 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) {
1405 possible
($type, "A:" . $s);
1407 # definitions in global scope can only start with types
1408 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1409 possible
($1, "B:" . $s);
1412 # any (foo ... *) is a pointer cast, and foo is a type
1413 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1414 possible
($1, "C:" . $s);
1417 # Check for any sort of function declaration.
1418 # int foo(something bar, other baz);
1419 # void (*store_gdt)(x86_descr_ptr *);
1420 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1421 my ($name_len) = length($1);
1424 substr($ctx, 0, $name_len + 1, '');
1425 $ctx =~ s/\)[^\)]*$//;
1427 for my $arg (split(/\s*,\s*/, $ctx)) {
1428 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1430 possible
($1, "D:" . $s);
1438 # Checks which may be anchored in the context.
1441 # Check for switch () and associated case and default
1442 # statements should be at the same indent.
1443 if ($line=~/\bswitch\s*\(.*\)/) {
1446 my @ctx = ctx_block_outer
($linenr, $realcnt);
1448 for my $ctx (@ctx) {
1449 my ($clen, $cindent) = line_stats
($ctx);
1450 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1451 $indent != $cindent) {
1452 $err .= "$sep$ctx\n";
1459 ERROR
("switch and case should be at the same indent\n$hereline$err");
1463 # if/while/etc brace do not go on next line, unless defining a do while loop,
1464 # or if that brace on the next line is for something else
1465 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1466 my $pre_ctx = "$1$2";
1468 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1469 my $ctx_cnt = $realcnt - $#ctx - 1;
1470 my $ctx = join("\n", @ctx);
1472 my $ctx_ln = $linenr;
1473 my $ctx_skip = $realcnt;
1475 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1476 defined $lines[$ctx_ln - 1] &&
1477 $lines[$ctx_ln - 1] =~ /^-/)) {
1478 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1479 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1483 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1484 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1486 # The length of the "previous line" is checked against 80 because it
1487 # includes the + at the beginning of the line (if the actual line has
1488 # 79 or 80 characters, it is no longer possible to add a space and an
1489 # opening brace there)
1490 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1491 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1492 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1493 ERROR
("that open brace { should be on the previous line\n" .
1494 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1496 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1497 $ctx =~ /\)\s*\;\s*$/ &&
1498 defined $lines[$ctx_ln - 1])
1500 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1501 if ($nindent > $indent) {
1502 ERROR
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1503 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1508 # Check relative indent for conditionals and blocks.
1509 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1510 my ($s, $c) = ($stat, $cond);
1512 substr($s, 0, length($c), '');
1514 # Make sure we remove the line prefixes as we have
1515 # none on the first line, and are going to readd them
1519 # Find out how long the conditional actually is.
1520 my @newlines = ($c =~ /\n/gs);
1521 my $cond_lines = 1 + $#newlines;
1523 # We want to check the first line inside the block
1524 # starting at the end of the conditional, so remove:
1525 # 1) any blank line termination
1526 # 2) any opening brace { on end of the line
1528 my $continuation = 0;
1530 $s =~ s/^.*\bdo\b//;
1532 if ($s =~ s/^\s*\\//) {
1535 if ($s =~ s/^\s*?\n//) {
1540 # Also ignore a loop construct at the end of a
1541 # preprocessor statement.
1542 if (($prevline =~ /^.\s*#\s*define\s/ ||
1543 $prevline =~ /\\\s*$/) && $continuation == 0) {
1549 while ($cond_ptr != $cond_lines) {
1550 $cond_ptr = $cond_lines;
1552 # If we see an #else/#elif then the code
1554 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1559 # 1) blank lines, they should be at 0,
1560 # 2) preprocessor lines, and
1562 if ($continuation ||
1564 $s =~ /^\s*#\s*?/ ||
1565 $s =~ /^\s*$Ident\s*:/) {
1566 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1567 if ($s =~ s/^.*?\n//) {
1573 my (undef, $sindent) = line_stats
("+" . $s);
1574 my $stat_real = raw_line
($linenr, $cond_lines);
1576 # Check if either of these lines are modified, else
1577 # this is not this patch's fault.
1578 if (!defined($stat_real) ||
1579 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1582 if (defined($stat_real) && $cond_lines > 1) {
1583 $stat_real = "[...]\n$stat_real";
1586 #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";
1588 if ($check && (($sindent % 4) != 0 ||
1589 ($sindent <= $indent && $s ne ''))) {
1590 ERROR
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1594 # Track the 'values' across context and added lines.
1595 my $opline = $line; $opline =~ s/^./ /;
1596 my ($curr_values, $curr_vars) =
1597 annotate_values
($opline . "\n", $prev_values);
1598 $curr_values = $prev_values . $curr_values;
1600 my $outline = $opline; $outline =~ s/\t/ /g;
1601 print "$linenr > .$outline\n";
1602 print "$linenr > $curr_values\n";
1603 print "$linenr > $curr_vars\n";
1605 $prev_values = substr($curr_values, -1);
1607 #ignore lines not being added
1608 if ($line=~/^[^\+]/) {next;}
1610 # TEST: allow direct testing of the type matcher.
1612 if ($line =~ /^.\s*$Declare\s*$/) {
1613 ERROR
("TEST: is type\n" . $herecurr);
1614 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1615 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1619 # TEST: allow direct testing of the attribute matcher.
1621 if ($line =~ /^.\s*$Modifier\s*$/) {
1622 ERROR
("TEST: is attr\n" . $herecurr);
1623 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1624 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1629 # check for initialisation to aggregates open brace on the next line
1630 if ($line =~ /^.\s*\{/ &&
1631 $prevline =~ /(?:^|[^=])=\s*$/) {
1632 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1636 # Checks which are anchored on the added line.
1639 # check for malformed paths in #include statements (uses RAW line)
1640 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1642 if ($path =~ m{//}) {
1643 ERROR
("malformed #include filename\n" .
1648 # no C99 // comments
1649 if ($line =~ m{//}) {
1650 ERROR
("do not use C99 // comments\n" . $herecurr);
1652 # Remove C99 comments.
1654 $opline =~ s@
//.*@@
;
1656 # check for global initialisers.
1657 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1658 ERROR
("do not initialise globals to 0 or NULL\n" .
1661 # check for static initialisers.
1662 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1663 ERROR
("do not initialise statics to 0 or NULL\n" .
1667 # * goes on variable not on type
1669 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1670 my ($from, $to) = ($1, $1);
1672 # Should start with a space.
1673 $to =~ s/^(\S)/ $1/;
1674 # Should not end with a space.
1676 # '*'s should not have spaces between.
1677 while ($to =~ s/\*\s+\*/\*\*/) {
1680 #print "from<$from> to<$to>\n";
1682 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1684 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1685 my ($from, $to, $ident) = ($1, $1, $2);
1687 # Should start with a space.
1688 $to =~ s/^(\S)/ $1/;
1689 # Should not end with a space.
1691 # '*'s should not have spaces between.
1692 while ($to =~ s/\*\s+\*/\*\*/) {
1694 # Modifiers should have spaces.
1695 $to =~ s/(\b$Modifier$)/$1 /;
1697 #print "from<$from> to<$to> ident<$ident>\n";
1698 if ($from ne $to && $ident !~ /^$Modifier$/) {
1699 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1703 # function brace can't be on same line, except for #defines of do while,
1704 # or if closed on same line
1705 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1706 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1707 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1710 # open braces for enum, union and struct go on the same line.
1711 if ($line =~ /^.\s*\{/ &&
1712 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1713 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1716 # missing space after union, struct or enum definition
1717 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1718 ERROR
("missing space after $1 definition\n" . $herecurr);
1721 # check for spacing round square brackets; allowed:
1722 # 1. with a type on the left -- int [] a;
1723 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1724 # 3. inside a curly brace -- = { [0...10] = 5 }
1725 # 4. after a comma -- [1] = 5, [2] = 6
1726 # 5. in a macro definition -- #define abc(x) [x] = y
1727 while ($line =~ /(.*?\s)\[/g) {
1728 my ($where, $prefix) = ($-[1], $1);
1729 if ($prefix !~ /$Type\s+$/ &&
1730 ($where != 0 || $prefix !~ /^.\s+$/) &&
1731 $prefix !~ /{\s+$/ &&
1732 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1733 $prefix !~ /,\s+$/) {
1734 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
1738 # check for spaces between functions and their parentheses.
1739 while ($line =~ /($Ident)\s+\(/g) {
1741 my $ctx_before = substr($line, 0, $-[1]);
1742 my $ctx = "$ctx_before$name";
1744 # Ignore those directives where spaces _are_ permitted.
1746 if|for|while|switch
|return|case
|
1747 volatile
|__volatile__
|
1748 __attribute__
|format
|__extension__
|
1752 # Ignore 'catch (...)' in C++
1753 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1755 # cpp #define statements have non-optional spaces, ie
1756 # if there is a space between the name and the open
1757 # parenthesis it is simply not a parameter group.
1758 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1760 # cpp #elif statement condition may start with a (
1761 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1763 # If this whole things ends with a type its most
1764 # likely a typedef for a function.
1765 } elsif ($ctx =~ /$Type$/) {
1768 ERROR
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1771 # Check operator spacing.
1772 if (!($line=~/\#\s*include/)) {
1774 <<=|>>=|<=|>=|==|!=|
1775 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1776 =>|->|<<|>>|<|>|=|!|~|
1777 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
1780 my @elements = split(/($ops|;)/, $opline);
1783 my $blank = copy_spacing
($opline);
1785 for (my $n = 0; $n < $#elements; $n += 2) {
1786 $off += length($elements[$n]);
1788 # Pick up the preceding and succeeding characters.
1789 my $ca = substr($opline, 0, $off);
1791 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1792 $cc = substr($opline, $off + length($elements[$n + 1]));
1794 my $cb = "$ca$;$cc";
1797 $a = 'V' if ($elements[$n] ne '');
1798 $a = 'W' if ($elements[$n] =~ /\s$/);
1799 $a = 'C' if ($elements[$n] =~ /$;$/);
1800 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1801 $a = 'O' if ($elements[$n] eq '');
1802 $a = 'E' if ($ca =~ /^\s*$/);
1804 my $op = $elements[$n + 1];
1807 if (defined $elements[$n + 2]) {
1808 $c = 'V' if ($elements[$n + 2] ne '');
1809 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1810 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1811 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1812 $c = 'O' if ($elements[$n + 2] eq '');
1813 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1818 my $ctx = "${a}x${c}";
1820 my $at = "(ctx:$ctx)";
1822 my $ptr = substr($blank, 0, $off) . "^";
1823 my $hereptr = "$hereline$ptr\n";
1825 # Pull out the value of this operator.
1826 my $op_type = substr($curr_values, $off + 1, 1);
1828 # Get the full operator variant.
1829 my $opv = $op . substr($curr_vars, $off, 1);
1831 # Ignore operators passed as parameters.
1832 if ($op_type ne 'V' &&
1833 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1836 # } elsif ($op =~ /^$;+$/) {
1838 # ; should have either the end of line or a space or \ after it
1839 } elsif ($op eq ';') {
1840 if ($ctx !~ /.x[WEBC]/ &&
1841 $cc !~ /^\\/ && $cc !~ /^;/) {
1842 ERROR
("space required after that '$op' $at\n" . $hereptr);
1846 } elsif ($op eq '//') {
1848 # Ignore : used in class declaration in C++
1849 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
1850 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
1854 # : when part of a bitfield
1855 } elsif ($op eq '->' || $opv eq ':B') {
1856 if ($ctx =~ /Wx.|.xW/) {
1857 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1860 # , must have a space on the right.
1861 # not required when having a single },{ on one line
1862 } elsif ($op eq ',') {
1863 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
1864 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
1865 ERROR
("space required after that '$op' $at\n" . $hereptr);
1868 # '*' as part of a type definition -- reported already.
1869 } elsif ($opv eq '*_') {
1870 #warn "'*' is part of type\n";
1872 # unary operators should have a space before and
1873 # none after. May be left adjacent to another
1874 # unary operator, or a cast
1875 } elsif ($op eq '!' || $op eq '~' ||
1876 $opv eq '*U' || $opv eq '-U' ||
1877 $opv eq '&U' || $opv eq '&&U') {
1878 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
1879 # '~' used as a name of Destructor
1881 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1882 ERROR
("space required before that '$op' $at\n" . $hereptr);
1884 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1885 # A unary '*' may be const
1887 } elsif ($ctx =~ /.xW/) {
1888 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1891 # unary ++ and unary -- are allowed no space on one side.
1892 } elsif ($op eq '++' or $op eq '--') {
1893 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1894 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
1896 if ($ctx =~ /Wx[BE]/ ||
1897 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1898 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1900 if ($ctx =~ /ExW/) {
1901 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1904 # A colon needs no spaces before when it is
1905 # terminating a case value or a label.
1906 } elsif ($opv eq ':C' || $opv eq ':L') {
1907 if ($ctx =~ /Wx./) {
1908 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1911 # All the others need spaces both sides.
1912 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1915 if ($realfile =~ /\.cpp|\.h$/) {
1916 # Ignore template arguments <...> in C++
1917 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
1927 # Ignore email addresses <foo@bar>
1929 $cc =~ /^\S+\@\S+>/) ||
1931 $ca =~ /<\S+\@\S+$/))
1937 if (($opv eq ':O' && $ca =~ /\?$/) ||
1938 ($op eq '?' && $cc =~ /^:/)) {
1943 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
1946 $off += length($elements[$n + 1]);
1950 #need space before brace following if, while, etc
1951 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
1953 ERROR
("space required before the open brace '{'\n" . $herecurr);
1956 # closing brace should have a space following it when it has anything
1958 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1959 ERROR
("space required after that close brace '}'\n" . $herecurr);
1962 # check spacing on square brackets
1963 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1964 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
1966 if ($line =~ /\s\]/) {
1967 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
1970 # check spacing on parentheses
1971 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1972 $line !~ /for\s*\(\s+;/) {
1973 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
1975 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1976 $line !~ /for\s*\(.*;\s+\)/ &&
1977 $line !~ /:\s+\)/) {
1978 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
1981 # Return is not a function.
1982 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1986 # Flatten any parentheses
1987 $value =~ s/\(/ \(/g;
1988 $value =~ s/\)/\) /g;
1989 while ($value =~ s/\[[^\{\}]*\]/1/ ||
1990 $value !~ /(?
:$Ident|-?
$Constant)\s
*
1992 (?
:$Ident|-?
$Constant)/x
&&
1993 $value =~ s/\([^\(\)]*\)/1/) {
1995 #print "value<$value>\n";
1996 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
1997 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
1999 } elsif ($spacing !~ /\s+/) {
2000 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2003 # Return of what appears to be an errno should normally be -'ve
2004 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2006 if ($name ne 'EOF' && $name ne 'ERROR') {
2007 ERROR
("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2011 # Need a space before open parenthesis after if, while etc
2012 if ($line=~/\b(if|while|for|switch)\(/) {
2013 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2016 # Check for illegal assignment in if conditional -- and check for trailing
2017 # statements after the conditional.
2018 if ($line =~ /do\s*(?!{)/) {
2019 my ($stat_next) = ctx_statement_block
($line_nr_next,
2020 $remain_next, $off_next);
2021 $stat_next =~ s/\n./\n /g;
2022 ##print "stat<$stat> stat_next<$stat_next>\n";
2024 if ($stat_next =~ /^\s*while\b/) {
2025 # If the statement carries leading newlines,
2026 # then count those as offsets.
2028 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2030 statement_rawlines
($whitespace) - 1;
2032 $suppress_whiletrailers{$line_nr_next +
2036 if (!defined $suppress_whiletrailers{$linenr} &&
2037 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2038 my ($s, $c) = ($stat, $cond);
2040 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2041 ERROR
("do not use assignment in if condition\n" . $herecurr);
2044 # Find out what is on the end of the line after the
2046 substr($s, 0, length($c), '');
2048 $s =~ s/$;//g; # Remove any comments
2049 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2050 $c !~ /}\s*while\s*/)
2052 # Find out how long the conditional actually is.
2053 my @newlines = ($c =~ /\n/gs);
2054 my $cond_lines = 1 + $#newlines;
2057 $stat_real = raw_line
($linenr, $cond_lines)
2058 . "\n" if ($cond_lines);
2059 if (defined($stat_real) && $cond_lines > 1) {
2060 $stat_real = "[...]\n$stat_real";
2063 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2067 # Check for bitwise tests written as boolean
2079 ERROR
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2082 # if and else should not have general statements after it
2083 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2085 $s =~ s/$;//g; # Remove any comments
2086 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2087 ERROR
("trailing statements should be on next line\n" . $herecurr);
2090 # if should not continue a brace
2091 if ($line =~ /}\s*if\b/) {
2092 ERROR
("trailing statements should be on next line\n" .
2095 # case and default should not have general statements after them
2096 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2098 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2102 ERROR
("trailing statements should be on next line\n" . $herecurr);
2105 # Check for }<nl>else {, these must be at the same
2106 # indent level to be relevant to each other.
2107 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2108 $previndent == $indent) {
2109 ERROR
("else should follow close brace '}'\n" . $hereprev);
2112 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2113 $previndent == $indent) {
2114 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2116 # Find out what is on the end of the line after the
2118 substr($s, 0, length($c), '');
2121 if ($s =~ /^\s*;/) {
2122 ERROR
("while should follow close brace '}'\n" . $hereprev);
2126 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2127 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2128 # print "No studly caps, use _\n";
2129 # print "$herecurr";
2133 #no spaces allowed after \ in define
2134 if ($line=~/\#\s*define.*\\\s$/) {
2135 ERROR
("Whitespace after \\ makes next lines useless\n" . $herecurr);
2138 # multi-statement macros should be enclosed in a do while loop, grab the
2139 # first statement and ensure its the whole macro if its not enclosed
2140 # in a known good container
2141 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2142 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2145 my ($off, $dstat, $dcond, $rest);
2148 my $args = defined($1);
2150 # Find the end of the macro and limit our statement
2152 while ($cnt > 0 && defined $lines[$ln - 1] &&
2153 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2155 $ctx .= $rawlines[$ln - 1] . "\n";
2156 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2159 $ctx .= $rawlines[$ln - 1];
2161 ($dstat, $dcond, $ln, $cnt, $off) =
2162 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2163 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2164 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2166 # Extract the remainder of the define (if any) and
2167 # rip off surrounding spaces, and trailing \'s.
2169 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2170 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2171 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2172 $rest .= substr($lines[$ln - 1], $off) . "\n";
2178 $rest =~ s/\\\n.//g;
2182 # Clean up the original statement.
2184 substr($dstat, 0, length($dcond), '');
2186 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2189 $dstat =~ s/\\\n.//g;
2190 $dstat =~ s/^\s*//s;
2191 $dstat =~ s/\s*$//s;
2193 # Flatten any parentheses and braces
2194 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2195 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2196 $dstat =~ s/\[[^\{\}]*\]/1/)
2200 my $exceptions = qr{
2212 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2213 if ($rest ne '' && $rest ne ',') {
2214 if ($rest !~ /while\s*\(/ &&
2215 $dstat !~ /$exceptions/)
2217 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2220 } elsif ($ctx !~ /;/) {
2222 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2223 $dstat !~ /$exceptions/ &&
2224 $dstat !~ /^\.$Ident\s*=/ &&
2225 $dstat =~ /$Operators/)
2227 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2232 # check for missing bracing round if etc
2233 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2234 my ($level, $endln, @chunks) =
2235 ctx_statement_full
($linenr, $realcnt, 1);
2237 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2238 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2241 if ($#chunks >= 0 && $level == 0) {
2244 my $herectx = $here . "\n";
2245 my $ln = $linenr - 1;
2246 for my $chunk (@chunks) {
2247 my ($cond, $block) = @
{$chunk};
2249 # If the condition carries leading newlines, then count those as offsets.
2250 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2251 my $offset = statement_rawlines
($whitespace) - 1;
2253 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2255 # We have looked at and allowed this specific line.
2256 $suppress_ifbraces{$ln + $offset} = 1;
2258 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2259 $ln += statement_rawlines
($block) - 1;
2261 substr($block, 0, length($cond), '');
2263 my $spaced_block = $block;
2264 $spaced_block =~ s/\n\+/ /g;
2266 $seen++ if ($spaced_block =~ /^\s*\{/);
2268 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2270 if (statement_lines
($cond) > 1) {
2271 print "APW: ALLOWED: cond<$cond>\n"
2275 if ($block =~/\b(?:if|for|while)\b/) {
2276 print "APW: ALLOWED: block<$block>\n"
2280 if (statement_block_size
($block) > 1) {
2281 print "APW: ALLOWED: lines block<$block>\n"
2286 if ($seen != ($#chunks + 1)) {
2287 ERROR
("braces {} are necessary for all arms of this statement\n" . $herectx);
2291 if (!defined $suppress_ifbraces{$linenr - 1} &&
2292 $line =~ /\b(if|while|for|else)\b/ &&
2293 $line !~ /\#\s*if/ &&
2294 $line !~ /\#\s*else/) {
2297 # Check the pre-context.
2298 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2301 if ($line !~ /else/) {
2302 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2308 my ($level, $endln, @chunks) =
2309 ctx_statement_full
($linenr, $realcnt, $-[0]);
2311 # Check the condition.
2312 my ($cond, $block) = @
{$chunks[0]};
2313 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2314 if $dbg_adv_checking;
2315 if (defined $cond) {
2316 substr($block, 0, length($cond), '');
2318 if (statement_lines
($cond) > 1) {
2319 print "APW: ALLOWED: cond<$cond>\n"
2323 if ($block =~/\b(?:if|for|while)\b/) {
2324 print "APW: ALLOWED: block<$block>\n"
2328 if (statement_block_size
($block) > 1) {
2329 print "APW: ALLOWED: lines block<$block>\n"
2333 # Check the post-context.
2334 if (defined $chunks[1]) {
2335 my ($cond, $block) = @
{$chunks[1]};
2336 if (defined $cond) {
2337 substr($block, 0, length($cond), '');
2339 if ($block =~ /^\s*\{/) {
2340 print "APW: ALLOWED: chunk-1 block<$block>\n"
2345 print "DCS: level=$level block<$block> allowed=$allowed\n"
2347 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2348 my $herectx = $here . "\n";;
2349 my $cnt = statement_rawlines
($block);
2351 for (my $n = 0; $n < $cnt; $n++) {
2352 $herectx .= raw_line
($linenr, $n) . "\n";;
2355 ERROR
("braces {} are necessary even for single statement blocks\n" . $herectx);
2359 # no volatiles please
2360 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2361 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2362 ERROR
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2366 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2367 ERROR
("if this code is redundant consider removing it\n" .
2371 # check for needless g_free() checks
2372 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2374 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2375 ERROR
("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2379 # warn about #ifdefs in C files
2380 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2381 # print "#ifdef in C files should be avoided\n";
2382 # print "$herecurr";
2386 # warn about spacing in #ifdefs
2387 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2388 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2390 # check for memory barriers without a comment.
2391 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2392 if (!ctx_has_comment
($first_line, $linenr)) {
2393 ERROR
("memory barrier without comment\n" . $herecurr);
2396 # check of hardware specific defines
2397 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2398 # where they might be necessary.
2399 if ($line =~ m@
^.\s
*\#\s
*if.*\b__@
) {
2400 ERROR
("architecture specific defines should be avoided\n" . $herecurr);
2403 # Check that the storage class is at the beginning of a declaration
2404 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2405 ERROR
("storage class should be at the beginning of the declaration\n" . $herecurr)
2408 # check the location of the inline attribute, that it is between
2409 # storage class and type.
2410 if ($line =~ /\b$Type\s+$Inline\b/ ||
2411 $line =~ /\b$Inline\s+$Storage\b/) {
2412 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2415 # check for sizeof(&)
2416 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2417 ERROR
("sizeof(& should be avoided\n" . $herecurr);
2420 # check for new externs in .c files.
2421 if ($realfile =~ /\.c$/ && defined $stat &&
2422 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2424 my $function_name = $1;
2425 my $paren_space = $2;
2428 if (defined $cond) {
2429 substr($s, 0, length($cond), '');
2431 if ($s =~ /^\s*;/ &&
2432 $function_name ne 'uninitialized_var')
2434 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2437 if ($paren_space =~ /\n/) {
2438 ERROR
("arguments for function declarations should follow identifier\n" . $herecurr);
2441 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2442 $stat =~ /^.\s*extern\s+/)
2444 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2447 # check for pointless casting of g_malloc return
2448 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2450 ERROR
("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2452 ERROR
("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2456 # check for gcc specific __FUNCTION__
2457 if ($line =~ /__FUNCTION__/) {
2458 ERROR
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2461 # recommend qemu_strto* over strto* for numeric conversions
2462 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2463 ERROR
("consider using qemu_$1 in preference to $1\n" . $herecurr);
2465 # check for module_init(), use category-specific init macros explicitly please
2466 if ($line =~ /^module_init\s*\(/) {
2467 ERROR
("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2469 # check for various ops structs, ensure they are const.
2470 my $struct_ops = qr{AIOCBInfo
|
2474 DisplayChangeListenerOps
|
2478 MemoryRegionIOMMUOps
|
2484 Spice
[A
-Z
][a
-zA
-Z0
-9]*Interface
|
2486 USBDesc
[A
-Z
][a
-zA
-Z0
-9]*|
2490 if ($line !~ /\bconst\b/ &&
2491 $line =~ /\b($struct_ops)\b/) {
2492 ERROR
("struct $1 should normally be const\n" .
2496 # check for %L{u,d,i} in strings
2498 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2499 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2500 $string =~ s/%%/__/g;
2501 if ($string =~ /(?<!%)%L[udi]/) {
2502 ERROR
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2507 # QEMU specific tests
2508 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2509 ERROR
("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2512 # Qemu error function tests
2514 # Find newlines in error messages
2515 my $qemu_error_funcs = qr{error_setg
|
2518 error_setg_file_open
|
2525 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2526 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2529 # Continue checking for error messages that contains newlines. This
2530 # check handles cases where string literals are spread over multiple lines.
2532 # error_report("Error msg line #1"
2533 # "Error msg line #2\n");
2534 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2535 my $continued_str_literal = qr{\+\s*\".*\"};
2537 if ($rawline =~ /$quoted_newline_regex/) {
2538 # Backtrack to first line that does not contain only a quoted literal
2539 # and assume that it is the start of the statement.
2540 my $i = $linenr - 2;
2542 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2546 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2547 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2551 # check for non-portable libc calls that have portable alternatives in QEMU
2552 if ($line =~ /\bffs\(/) {
2553 ERROR
("use ctz32() instead of ffs()\n" . $herecurr);
2555 if ($line =~ /\bffsl\(/) {
2556 ERROR
("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2558 if ($line =~ /\bffsll\(/) {
2559 ERROR
("use ctz64() instead of ffsll()\n" . $herecurr);
2561 if ($line =~ /\bbzero\(/) {
2562 ERROR
("use memset() instead of bzero()\n" . $herecurr);
2566 # If we have no input at all, then there is nothing to report on
2567 # so just keep quiet.
2568 if ($#rawlines == -1) {
2572 # In mailback mode only produce a report in the negative, for
2573 # things that appear to be patches.
2574 if ($mailback && ($clean == 1 || !$is_patch)) {
2578 # This is not a patch, and we are are in 'no-patch' mode so
2580 if (!$chk_patch && !$is_patch) {
2585 ERROR
("Does not appear to be a unified-diff format patch\n");
2587 if ($is_patch && $chk_signoff && $signoff == 0) {
2588 ERROR
("Missing Signed-off-by: line(s)\n");
2591 print report_dump
();
2592 if ($summary && !($clean == 1 && $quiet == 1)) {
2593 print "$filename " if ($summary_file);
2594 print "total: $cnt_error errors, $cnt_warn warnings, " .
2595 "$cnt_lines lines checked\n";
2596 print "\n" if ($quiet == 0);
2600 # If there were whitespace errors which cleanpatch can fix
2601 # then suggest that.
2602 # if ($rpt_cleaners) {
2603 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2604 # print " scripts/cleanfile\n\n";
2608 if ($clean == 1 && $quiet == 0) {
2609 print "$vname has no obvious style problems and is ready for submission.\n"
2611 if ($clean == 0 && $quiet == 0) {
2612 print "$vname has style problems, please review. If any of these errors\n";
2613 print "are false positives report them to the maintainer, see\n";
2614 print "CHECKPATCH in MAINTAINERS.\n";
2617 return ($no_warnings ?
$clean : $cnt_error == 0);