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
16 use Getopt
::Long
qw(:config no_auto_abbrev);
38 Usage
: $P [OPTION
]... [FILE
]...
43 --no-tree run without a kernel tree
44 --no-signoff
do not check
for 'Signed-off-by' line
45 --patch treat FILE as patchfile
(default)
46 --emacs emacs compile window format
47 --terse one line per report
48 -f
, --file treat FILE as regular source file
49 --strict fail
if only warnings are found
50 --root
=PATH PATH to the kernel tree root
51 --no-summary suppress the per
-file summary
52 --mailback only produce a report
in case of warnings
/errors
53 --summary
-file include the filename
in summary
54 --debug KEY
=[0|1] turn on
/off debugging of KEY
, where KEY is one of
55 'values', 'possible', 'type', and 'attr' (default
57 --test
-only
=WORD report only warnings
/errors containing WORD
59 -h
, --help
, --version display this help
and exit
61 When FILE is
- read standard input
.
68 'q|quiet+' => \
$quiet,
70 'signoff!' => \
$chk_signoff,
71 'patch!' => \
$chk_patch,
75 'strict!' => \
$no_warnings,
77 'summary!' => \
$summary,
78 'mailback!' => \
$mailback,
79 'summary-file!' => \
$summary_file,
82 'test-only=s' => \
$tst_only,
92 print "$P: no input files\n";
101 my $dbg_adv_checking = 0;
103 for my $key (keys %debug) {
105 eval "\${dbg_$key} = '$debug{$key}';";
109 my $rpt_cleaners = 0;
118 if (!top_of_kernel_tree
($root)) {
119 die "$P: $root: --root does not point at a valid tree\n";
122 if (top_of_kernel_tree
('.')) {
124 } elsif ($0 =~ m@
(.*)/scripts/[^/]*$@
&&
125 top_of_kernel_tree
($1)) {
130 if (!defined $root) {
131 print "Must be run from the top-level dir. of a kernel tree\n";
136 my $emitted_corrupt = 0;
139 [A
-Za
-z_
][A
-Za
-z\d_
]*
140 (?
:\s
*\#\#\s
*[A
-Za
-z_
][A
-Za
-z\d_
]*)*
142 our $Storage = qr{extern|static|asmlinkage};
147 # Notes to $Attribute:
152 QEMU_WARN_UNUSED_RESULT
|
159 our $Inline = qr{inline};
160 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
161 our $Lval = qr{$Ident(?:$Member)*};
163 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
164 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
165 our $Compare = qr{<=|>=|==|!=|<|>};
169 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%
177 [\x09\x0A\x0D\x20-\x7E] # ASCII
178 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
179 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
180 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
181 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
182 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
183 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
184 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
187 # There are still some false positives, but this catches most
189 our $typeTypedefs = qr{(?x
:
190 [A
-Z
][A
-Z\d_
]*[a
-z
][A
-Za
-z\d_
]* # camelcase
191 | [A
-Z
][A
-Z\d_
]*AIOCB
# all uppercase
192 | [A
-Z
][A
-Z\d_
]*CPU
# all uppercase
193 | QEMUBH
# all uppercase
198 qr{(?:unsigned\s+)?char},
199 qr{(?:unsigned\s+)?short},
200 qr{(?:unsigned\s+)?int},
201 qr{(?:unsigned\s+)?long},
202 qr{(?:unsigned\s+)?long\s+int},
203 qr{(?:unsigned\s+)?long\s+long},
204 qr{(?:unsigned\s+)?long\s+long\s+int},
213 qr{${Ident}_handler
},
214 qr{${Ident}_handler_fn
},
215 qr{target_(?:u)?long},
218 # This can be modified by sub possible. Since it can be empty, be careful
219 # about regexes that always match, because they can cause infinite loops.
220 our @modifierList = (
224 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
225 if (@modifierList > 0) {
226 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
227 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
229 $Modifier = qr{(?:$Attribute|$Sparse)};
232 (?
:$Modifier\s
+|const\s
+)*
234 (?
:typeof
|__typeof__
)\s
*\
(\s
*\
**\s
*$Ident\s
*\
)|
238 (?
:\s
+$Modifier|\s
+const
)*
242 (?
:[\s\
*]+\s
*const
|[\s\
*]+|(?
:\s
*\
[\s
*\
])+)?
243 (?
:\s
+$Inline|\s
+$Modifier)*
245 $Declare = qr{(?:$Storage\s+)?$Type};
249 $chk_signoff = 0 if ($file);
254 for my $filename (@ARGV) {
257 open($FILE, '-|', "diff -u /dev/null $filename") ||
258 die "$P: $filename: diff failed - $!\n";
259 } elsif ($filename eq '-') {
260 open($FILE, '<&STDIN');
262 open($FILE, '<', "$filename") ||
263 die "$P: $filename: open failed - $!\n";
265 if ($filename eq '-') {
266 $vname = 'Your patch';
275 if (!process
($filename)) {
284 sub top_of_kernel_tree
{
288 "COPYING", "MAINTAINERS", "Makefile",
289 "README", "docs", "VERSION",
293 foreach my $check (@tree_check) {
294 if (! -e
$root . '/' . $check) {
306 for my $c (split(//, $str)) {
310 for (; ($n % 8) != 0; $n++) {
322 (my $res = shift) =~ tr/\t/ /c;
329 # Drop the diff line leader and expand tabs
331 $line = expand_tabs
($line);
333 # Pick the indent from the front of the line.
334 my ($white) = ($line =~ /^(\s*)/);
336 return (length($line), length($white));
339 my $sanitise_quote = '';
341 sub sanitise_line_reset
{
342 my ($in_comment) = @_;
345 $sanitise_quote = '*/';
347 $sanitise_quote = '';
360 # Always copy over the diff marker.
361 $res = substr($line, 0, 1);
363 for ($off = 1; $off < length($line); $off++) {
364 $c = substr($line, $off, 1);
366 # Comments we are wacking completely including the begin
367 # and end, all to $;.
368 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
369 $sanitise_quote = '*/';
371 substr($res, $off, 2, "$;$;");
375 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
376 $sanitise_quote = '';
377 substr($res, $off, 2, "$;$;");
381 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
382 $sanitise_quote = '//';
384 substr($res, $off, 2, $sanitise_quote);
389 # A \ in a string means ignore the next character.
390 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
392 substr($res, $off, 2, 'XX');
397 if ($c eq "'" || $c eq '"') {
398 if ($sanitise_quote eq '') {
399 $sanitise_quote = $c;
401 substr($res, $off, 1, $c);
403 } elsif ($sanitise_quote eq $c) {
404 $sanitise_quote = '';
408 #print "c<$c> SQ<$sanitise_quote>\n";
409 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
410 substr($res, $off, 1, $;);
411 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
412 substr($res, $off, 1, $;);
413 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
414 substr($res, $off, 1, 'X');
416 substr($res, $off, 1, $c);
420 if ($sanitise_quote eq '//') {
421 $sanitise_quote = '';
424 # The pathname on a #include may be surrounded by '<' and '>'.
425 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
426 my $clean = 'X' x
length($1);
427 $res =~ s@\
<.*\
>@
<$clean>@
;
429 # The whole of a #error is a string.
430 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
431 my $clean = 'X' x
length($1);
432 $res =~ s@
(\#\s
*(?
:error
|warning
)\s
+).*@
$1$clean@
;
438 sub ctx_statement_block
{
439 my ($linenr, $remain, $off) = @_;
440 my $line = $linenr - 1;
457 @stack = (['', 0]) if ($#stack == -1);
459 #warn "CSB: blk<$blk> remain<$remain>\n";
460 # If we are about to drop off the end, pull in more
463 for (; $remain > 0; $line++) {
464 last if (!defined $lines[$line]);
465 next if ($lines[$line] =~ /^-/);
468 $blk .= $lines[$line] . "\n";
473 # Bail if there is no further context.
474 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
480 $c = substr($blk, $off, 1);
481 $remainder = substr($blk, $off);
483 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
485 # Handle nested #if/#else.
486 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
487 push(@stack, [ $type, $level ]);
488 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
489 ($type, $level) = @
{$stack[$#stack - 1]};
490 } elsif ($remainder =~ /^#\s*endif\b/) {
491 ($type, $level) = @
{pop(@stack)};
494 # Statement ends at the ';' or a close '}' at the
496 if ($level == 0 && $c eq ';') {
500 # An else is really a conditional as long as its not else if
501 if ($level == 0 && $coff_set == 0 &&
502 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
503 $remainder =~ /^(else)(?:\s|{)/ &&
504 $remainder !~ /^else\s+if\b/) {
505 $coff = $off + length($1) - 1;
507 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
508 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
511 if (($type eq '' || $type eq '(') && $c eq '(') {
515 if ($type eq '(' && $c eq ')') {
517 $type = ($level != 0)?
'(' : '';
519 if ($level == 0 && $coff < $soff) {
522 #warn "CSB: mark coff<$coff>\n";
525 if (($type eq '' || $type eq '{') && $c eq '{') {
529 if ($type eq '{' && $c eq '}') {
531 $type = ($level != 0)?
'{' : '';
534 if (substr($blk, $off + 1, 1) eq ';') {
542 # We are truly at the end, so shuffle to the next line.
549 my $statement = substr($blk, $soff, $off - $soff + 1);
550 my $condition = substr($blk, $soff, $coff - $soff + 1);
552 #warn "STATEMENT<$statement>\n";
553 #warn "CONDITION<$condition>\n";
555 #print "coff<$coff> soff<$off> loff<$loff>\n";
557 return ($statement, $condition,
558 $line, $remain + 1, $off - $loff + 1, $level);
561 sub statement_lines
{
564 # Strip the diff line prefixes and rip blank lines at start and end.
565 $stmt =~ s/(^|\n)./$1/g;
569 my @stmt_lines = ($stmt =~ /\n/g);
571 return $#stmt_lines + 2;
574 sub statement_rawlines
{
577 my @stmt_lines = ($stmt =~ /\n/g);
579 return $#stmt_lines + 2;
582 sub statement_block_size
{
585 $stmt =~ s/(^|\n)./$1/g;
591 my @stmt_lines = ($stmt =~ /\n/g);
592 my @stmt_statements = ($stmt =~ /;/g);
594 my $stmt_lines = $#stmt_lines + 2;
595 my $stmt_statements = $#stmt_statements + 1;
597 if ($stmt_lines > $stmt_statements) {
600 return $stmt_statements;
604 sub ctx_statement_full
{
605 my ($linenr, $remain, $off) = @_;
606 my ($statement, $condition, $level);
610 # Grab the first conditional/block pair.
611 ($statement, $condition, $linenr, $remain, $off, $level) =
612 ctx_statement_block
($linenr, $remain, $off);
613 #print "F: c<$condition> s<$statement> remain<$remain>\n";
614 push(@chunks, [ $condition, $statement ]);
615 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
616 return ($level, $linenr, @chunks);
619 # Pull in the following conditional/block pairs and see if they
620 # could continue the statement.
622 ($statement, $condition, $linenr, $remain, $off, $level) =
623 ctx_statement_block
($linenr, $remain, $off);
624 #print "C: c<$condition> s<$statement> remain<$remain>\n";
625 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
627 push(@chunks, [ $condition, $statement ]);
630 return ($level, $linenr, @chunks);
634 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
636 my $start = $linenr - 1;
643 my @stack = ($level);
644 for ($line = $start; $remain > 0; $line++) {
645 next if ($rawlines[$line] =~ /^-/);
648 $blk .= $rawlines[$line];
650 # Handle nested #if/#else.
651 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
652 push(@stack, $level);
653 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
654 $level = $stack[$#stack - 1];
655 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
656 $level = pop(@stack);
659 foreach my $c (split(//, $lines[$line])) {
660 ##print "C<$c>L<$level><$open$close>O<$off>\n";
666 if ($c eq $close && $level > 0) {
668 last if ($level == 0);
669 } elsif ($c eq $open) {
674 if (!$outer || $level <= 1) {
675 push(@res, $rawlines[$line]);
678 last if ($level == 0);
681 return ($level, @res);
683 sub ctx_block_outer
{
684 my ($linenr, $remain) = @_;
686 my ($level, @r) = ctx_block_get
($linenr, $remain, 1, '{', '}', 0);
690 my ($linenr, $remain) = @_;
692 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
696 my ($linenr, $remain, $off) = @_;
698 my ($level, @r) = ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
701 sub ctx_block_level
{
702 my ($linenr, $remain) = @_;
704 return ctx_block_get
($linenr, $remain, 0, '{', '}', 0);
706 sub ctx_statement_level
{
707 my ($linenr, $remain, $off) = @_;
709 return ctx_block_get
($linenr, $remain, 0, '(', ')', $off);
712 sub ctx_locate_comment
{
713 my ($first_line, $end_line) = @_;
715 # Catch a comment on the end of the line itself.
716 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@
.*(/\*.*\*/)\s
*(?
:\\\s
*)?
$@
);
717 return $current_comment if (defined $current_comment);
719 # Look through the context and try and figure out if there is a
722 $current_comment = '';
723 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
724 my $line = $rawlines[$linenr - 1];
726 if ($linenr == $first_line and $line =~ m@
^.\s
*\
*@
) {
729 if ($line =~ m@
/\
*@
) {
732 if (!$in_comment && $current_comment ne '') {
733 $current_comment = '';
735 $current_comment .= $line . "\n" if ($in_comment);
736 if ($line =~ m@\
*/@
) {
741 chomp($current_comment);
742 return($current_comment);
744 sub ctx_has_comment
{
745 my ($first_line, $end_line) = @_;
746 my $cmt = ctx_locate_comment
($first_line, $end_line);
748 ##print "LINE: $rawlines[$end_line - 1 ]\n";
749 ##print "CMMT: $cmt\n";
755 my ($linenr, $cnt) = @_;
757 my $offset = $linenr - 1;
762 $line = $rawlines[$offset++];
763 next if (defined($line) && $line =~ /^-/);
775 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
778 $coded = sprintf("^%c", unpack('C', $2) + 64);
787 my $av_preprocessor = 0;
793 $av_preprocessor = 0;
795 @av_paren_type = ('E');
796 $av_pend_colon = 'O';
799 sub annotate_values
{
800 my ($stream, $type) = @_;
803 my $var = '_' x
length($stream);
806 print "$stream\n" if ($dbg_values > 1);
808 while (length($cur)) {
809 @av_paren_type = ('E') if ($#av_paren_type < 0);
810 print " <" . join('', @av_paren_type) .
811 "> <$type> <$av_pending>" if ($dbg_values > 1);
812 if ($cur =~ /^(\s+)/o) {
813 print "WS($1)\n" if ($dbg_values > 1);
814 if ($1 =~ /\n/ && $av_preprocessor) {
815 $type = pop(@av_paren_type);
816 $av_preprocessor = 0;
819 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
820 print "CAST($1)\n" if ($dbg_values > 1);
821 push(@av_paren_type, $type);
824 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
825 print "DECLARE($1)\n" if ($dbg_values > 1);
828 } elsif ($cur =~ /^($Modifier)\s*/) {
829 print "MODIFIER($1)\n" if ($dbg_values > 1);
832 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
833 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
834 $av_preprocessor = 1;
835 push(@av_paren_type, $type);
841 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
842 print "UNDEF($1)\n" if ($dbg_values > 1);
843 $av_preprocessor = 1;
844 push(@av_paren_type, $type);
846 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
847 print "PRE_START($1)\n" if ($dbg_values > 1);
848 $av_preprocessor = 1;
850 push(@av_paren_type, $type);
851 push(@av_paren_type, $type);
854 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
855 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
856 $av_preprocessor = 1;
858 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
862 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
863 print "PRE_END($1)\n" if ($dbg_values > 1);
865 $av_preprocessor = 1;
867 # Assume all arms of the conditional end as this
868 # one does, and continue as if the #endif was not here.
870 push(@av_paren_type, $type);
873 } elsif ($cur =~ /^(\\\n)/o) {
874 print "PRECONT($1)\n" if ($dbg_values > 1);
876 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
877 print "ATTR($1)\n" if ($dbg_values > 1);
881 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
882 print "SIZEOF($1)\n" if ($dbg_values > 1);
888 } elsif ($cur =~ /^(if|while|for)\b/o) {
889 print "COND($1)\n" if ($dbg_values > 1);
893 } elsif ($cur =~/^(case)/o) {
894 print "CASE($1)\n" if ($dbg_values > 1);
895 $av_pend_colon = 'C';
898 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
899 print "KEYWORD($1)\n" if ($dbg_values > 1);
902 } elsif ($cur =~ /^(\()/o) {
903 print "PAREN('$1')\n" if ($dbg_values > 1);
904 push(@av_paren_type, $av_pending);
908 } elsif ($cur =~ /^(\))/o) {
909 my $new_type = pop(@av_paren_type);
910 if ($new_type ne '_') {
912 print "PAREN('$1') -> $type\n"
913 if ($dbg_values > 1);
915 print "PAREN('$1')\n" if ($dbg_values > 1);
918 } elsif ($cur =~ /^($Ident)\s*\(/o) {
919 print "FUNC($1)\n" if ($dbg_values > 1);
923 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
924 if (defined $2 && $type eq 'C' || $type eq 'T') {
925 $av_pend_colon = 'B';
926 } elsif ($type eq 'E') {
927 $av_pend_colon = 'L';
929 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
932 } elsif ($cur =~ /^($Ident|$Constant)/o) {
933 print "IDENT($1)\n" if ($dbg_values > 1);
936 } elsif ($cur =~ /^($Assignment)/o) {
937 print "ASSIGN($1)\n" if ($dbg_values > 1);
940 } elsif ($cur =~/^(;|{|})/) {
941 print "END($1)\n" if ($dbg_values > 1);
943 $av_pend_colon = 'O';
945 } elsif ($cur =~/^(,)/) {
946 print "COMMA($1)\n" if ($dbg_values > 1);
949 } elsif ($cur =~ /^(\?)/o) {
950 print "QUESTION($1)\n" if ($dbg_values > 1);
953 } elsif ($cur =~ /^(:)/o) {
954 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
956 substr($var, length($res), 1, $av_pend_colon);
957 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
962 $av_pend_colon = 'O';
964 } elsif ($cur =~ /^(\[)/o) {
965 print "CLOSE($1)\n" if ($dbg_values > 1);
968 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
971 print "OPV($1)\n" if ($dbg_values > 1);
978 substr($var, length($res), 1, $variant);
981 } elsif ($cur =~ /^($Operators)/o) {
982 print "OP($1)\n" if ($dbg_values > 1);
983 if ($1 ne '++' && $1 ne '--') {
987 } elsif ($cur =~ /(^.)/o) {
988 print "C($1)\n" if ($dbg_values > 1);
991 $cur = substr($cur, length($1));
992 $res .= $type x
length($1);
1000 my ($possible, $line) = @_;
1001 my $notPermitted = qr{(?
:
1018 ^(?
:typedef
|struct
|enum
)\b
1020 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1021 if ($possible !~ $notPermitted) {
1022 # Check for modifiers.
1023 $possible =~ s/\s*$Storage\s*//g;
1024 $possible =~ s/\s*$Sparse\s*//g;
1025 if ($possible =~ /^\s*$/) {
1027 } elsif ($possible =~ /\s/) {
1028 $possible =~ s/\s*$Type\s*//g;
1029 for my $modifier (split(' ', $possible)) {
1030 if ($modifier !~ $notPermitted) {
1031 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1032 push(@modifierList, $modifier);
1037 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1038 push(@typeList, $possible);
1042 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1049 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1052 my $line = $prefix . $_[0];
1054 $line = (split('\n', $line))[0] . "\n" if ($terse);
1056 push(our @report, $line);
1064 if (report
("ERROR: $_[0]\n")) {
1070 if (report
("WARNING: $_[0]\n")) {
1077 my $filename = shift;
1083 my $stashrawline="";
1100 # Trace the real file/line as we go.
1106 my $comment_edge = 0;
1110 my $prev_values = 'E';
1113 my %suppress_ifbraces;
1114 my %suppress_whiletrailers;
1115 my %suppress_export;
1117 # Pre-scan the patch sanitizing the lines.
1119 sanitise_line_reset
();
1121 foreach my $rawline (@rawlines) {
1125 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1134 # Guestimate if this is a continuing comment. Run
1135 # the context looking for a comment "edge". If this
1136 # edge is a close comment then we must be in a comment
1140 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1141 next if (defined $rawlines[$ln - 1] &&
1142 $rawlines[$ln - 1] =~ /^-/);
1144 #print "RAW<$rawlines[$ln - 1]>\n";
1145 last if (!defined $rawlines[$ln - 1]);
1146 if ($rawlines[$ln - 1] =~ m@
(/\*|\*/)@
&&
1147 $rawlines[$ln - 1] !~ m@
"[^"]*(?
:/\*|\*/)[^"]*"@
) {
1152 if (defined $edge && $edge eq '*/') {
1156 # Guestimate if this is a continuing comment. If this
1157 # is the start of a diff block and this line starts
1158 # ' *' then it is very likely a comment.
1159 if (!defined $edge &&
1160 $rawlines[$linenr] =~ m@
^.\s
*(?
:\
*\
*+| \
*)(?
:\s
|$)@
)
1165 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1166 sanitise_line_reset
($in_comment);
1168 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1169 # Standardise the strings and chars within the input to
1170 # simplify matching -- only bother with positive lines.
1171 $line = sanitise_line
($rawline);
1173 push(@lines, $line);
1176 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1181 #print "==>$rawline\n";
1182 #print "-->$line\n";
1189 foreach my $line (@lines) {
1192 my $rawline = $rawlines[$linenr - 1];
1194 #extract the line range in the file after the patch is applied
1195 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1197 $first_line = $linenr + 1;
1207 %suppress_ifbraces = ();
1208 %suppress_whiletrailers = ();
1209 %suppress_export = ();
1212 # track the line number as we move through the hunk, note that
1213 # new versions of GNU diff omit the leading space on completely
1214 # blank context lines so we need to count that too.
1215 } elsif ($line =~ /^( |\+|$)/) {
1217 $realcnt-- if ($realcnt != 0);
1219 # Measure the line length and indent.
1220 ($length, $indent) = line_stats
($rawline);
1222 # Track the previous line.
1223 ($prevline, $stashline) = ($stashline, $line);
1224 ($previndent, $stashindent) = ($stashindent, $indent);
1225 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1227 #warn "line<$line>\n";
1229 } elsif ($realcnt == 1) {
1233 my $hunk_line = ($realcnt != 0);
1235 #make up the handle for any error we report on this line
1236 $prefix = "$filename:$realline: " if ($emacs && $file);
1237 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1239 $here = "#$linenr: " if (!$file);
1240 $here = "#$realline: " if ($file);
1242 # extract the filename as it passes
1243 if ($line =~ /^diff --git.*?(\S+)$/) {
1245 $realfile =~ s@
^([^/]*)/@@
;
1247 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1249 $realfile =~ s@
^([^/]*)/@@
;
1252 if (!$file && $tree && $p1_prefix ne '' &&
1253 -e
"$root/$p1_prefix") {
1254 WARN
("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1260 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1262 my $hereline = "$here\n$rawline\n";
1263 my $herecurr = "$here\n$rawline\n";
1264 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1266 $cnt_lines++ if ($realcnt != 0);
1268 # Check for incorrect file permissions
1269 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1270 my $permhere = $here . "FILE: $realfile\n";
1271 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1272 ERROR
("do not set execute permissions for source files\n" . $permhere);
1276 # Accept git diff extended headers as valid patches
1277 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\
.\
-]+\s
*$/) {
1281 #check the patch for a signoff:
1282 if ($line =~ /^\s*signed-off-by:/i) {
1283 # This is a signoff, if ugly, so do not double report.
1285 if (!($line =~ /^\s*Signed-off-by:/)) {
1286 ERROR
("The correct form is \"Signed-off-by\"\n" .
1289 if ($line =~ /^\s*signed-off-by:\S/i) {
1290 ERROR
("space required after Signed-off-by:\n" .
1295 # Check for wrappage within a valid hunk of the file
1296 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1297 ERROR
("patch seems to be corrupt (line wrapped?)\n" .
1298 $herecurr) if (!$emitted_corrupt++);
1301 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1302 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1303 $rawline !~ m/^$UTF8*$/) {
1304 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1306 my $blank = copy_spacing
($rawline);
1307 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1308 my $hereptr = "$hereline$ptr\n";
1310 ERROR
("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1313 # ignore non-hunk lines and lines being removed
1314 next if (!$hunk_line || $line =~ /^-/);
1316 # ignore files that are being periodically imported from Linux
1317 next if ($realfile =~ /^(linux-headers|include\/standard
-headers
)\
//);
1319 #trailing whitespace
1320 if ($line =~ /^\+.*\015/) {
1321 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1322 ERROR
("DOS line endings\n" . $herevet);
1324 } elsif ($realfile =~ /^docs\/.+\
.txt
/ ||
1325 $realfile =~ /^docs\/.+\
.md
/) {
1326 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1327 # TODO: properly check we're in a code block
1328 # (surrounding text is 4-column aligned)
1329 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1330 ERROR
("code blocks in documentation should have " .
1331 "empty lines with exactly 4 columns of " .
1332 "whitespace\n" . $herevet);
1334 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1335 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1336 ERROR
("trailing whitespace\n" . $herevet);
1340 # check we are in a valid source file if not then ignore this hunk
1341 next if ($realfile !~ /\.(h|c|cpp|s|S|pl|py|sh)$/);
1344 if ($line =~ /^\+/ &&
1345 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1349 ERROR
("line over 90 characters\n" . $herecurr);
1351 WARN
("line over 80 characters\n" . $herecurr);
1355 # check for spaces before a quoted newline
1356 if ($rawline =~ /^.*\".*\s\\n/) {
1357 ERROR
("unnecessary whitespace before a quoted newline\n" . $herecurr);
1360 # check for adding lines without a newline.
1361 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1362 ERROR
("adding a line without newline at end of file\n" . $herecurr);
1365 # check for RCS/CVS revision markers
1366 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1367 ERROR
("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1370 # tabs are only allowed in assembly source code, and in
1371 # some scripts we imported from other projects.
1372 next if ($realfile =~ /\.(s|S)$/);
1373 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1375 if ($rawline =~ /^\+.*\t/) {
1376 my $herevet = "$here\n" . cat_vet
($rawline) . "\n";
1377 ERROR
("code indent should never use tabs\n" . $herevet);
1381 # check we are in a valid C source file if not then ignore this hunk
1382 next if ($realfile !~ /\.(h|c|cpp)$/);
1384 # Check for potential 'bare' types
1385 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1387 if ($realcnt && $line =~ /.\s*\S/) {
1388 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1389 ctx_statement_block
($linenr, $realcnt, 0);
1390 $stat =~ s/\n./\n /g;
1391 $cond =~ s/\n./\n /g;
1393 # Find the real next line.
1394 $realline_next = $line_nr_next;
1395 if (defined $realline_next &&
1396 (!defined $lines[$realline_next - 1] ||
1397 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1404 # Ignore goto labels.
1405 if ($s =~ /$Ident:\*$/s) {
1407 # Ignore functions being called
1408 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1410 } elsif ($s =~ /^.\s*else\b/s) {
1412 # declarations always start with types
1413 } 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) {
1416 possible
($type, "A:" . $s);
1418 # definitions in global scope can only start with types
1419 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1420 possible
($1, "B:" . $s);
1423 # any (foo ... *) is a pointer cast, and foo is a type
1424 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1425 possible
($1, "C:" . $s);
1428 # Check for any sort of function declaration.
1429 # int foo(something bar, other baz);
1430 # void (*store_gdt)(x86_descr_ptr *);
1431 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1432 my ($name_len) = length($1);
1435 substr($ctx, 0, $name_len + 1, '');
1436 $ctx =~ s/\)[^\)]*$//;
1438 for my $arg (split(/\s*,\s*/, $ctx)) {
1439 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1441 possible
($1, "D:" . $s);
1449 # Checks which may be anchored in the context.
1452 # Check for switch () and associated case and default
1453 # statements should be at the same indent.
1454 if ($line=~/\bswitch\s*\(.*\)/) {
1457 my @ctx = ctx_block_outer
($linenr, $realcnt);
1459 for my $ctx (@ctx) {
1460 my ($clen, $cindent) = line_stats
($ctx);
1461 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1462 $indent != $cindent) {
1463 $err .= "$sep$ctx\n";
1470 ERROR
("switch and case should be at the same indent\n$hereline$err");
1474 # if/while/etc brace do not go on next line, unless defining a do while loop,
1475 # or if that brace on the next line is for something else
1476 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1477 my $pre_ctx = "$1$2";
1479 my ($level, @ctx) = ctx_statement_level
($linenr, $realcnt, 0);
1480 my $ctx_cnt = $realcnt - $#ctx - 1;
1481 my $ctx = join("\n", @ctx);
1483 my $ctx_ln = $linenr;
1484 my $ctx_skip = $realcnt;
1486 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1487 defined $lines[$ctx_ln - 1] &&
1488 $lines[$ctx_ln - 1] =~ /^-/)) {
1489 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1490 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1494 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1495 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1497 # The length of the "previous line" is checked against 80 because it
1498 # includes the + at the beginning of the line (if the actual line has
1499 # 79 or 80 characters, it is no longer possible to add a space and an
1500 # opening brace there)
1501 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1502 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1503 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1504 ERROR
("that open brace { should be on the previous line\n" .
1505 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1507 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1508 $ctx =~ /\)\s*\;\s*$/ &&
1509 defined $lines[$ctx_ln - 1])
1511 my ($nlength, $nindent) = line_stats
($lines[$ctx_ln - 1]);
1512 if ($nindent > $indent) {
1513 ERROR
("trailing semicolon indicates no statements, indent implies otherwise\n" .
1514 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1519 # Check relative indent for conditionals and blocks.
1520 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1521 my ($s, $c) = ($stat, $cond);
1523 substr($s, 0, length($c), '');
1525 # Make sure we remove the line prefixes as we have
1526 # none on the first line, and are going to readd them
1530 # Find out how long the conditional actually is.
1531 my @newlines = ($c =~ /\n/gs);
1532 my $cond_lines = 1 + $#newlines;
1534 # We want to check the first line inside the block
1535 # starting at the end of the conditional, so remove:
1536 # 1) any blank line termination
1537 # 2) any opening brace { on end of the line
1539 my $continuation = 0;
1541 $s =~ s/^.*\bdo\b//;
1543 if ($s =~ s/^\s*\\//) {
1546 if ($s =~ s/^\s*?\n//) {
1551 # Also ignore a loop construct at the end of a
1552 # preprocessor statement.
1553 if (($prevline =~ /^.\s*#\s*define\s/ ||
1554 $prevline =~ /\\\s*$/) && $continuation == 0) {
1560 while ($cond_ptr != $cond_lines) {
1561 $cond_ptr = $cond_lines;
1563 # If we see an #else/#elif then the code
1565 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1570 # 1) blank lines, they should be at 0,
1571 # 2) preprocessor lines, and
1573 if ($continuation ||
1575 $s =~ /^\s*#\s*?/ ||
1576 $s =~ /^\s*$Ident\s*:/) {
1577 $continuation = ($s =~ /^.*?\\\n/) ?
1 : 0;
1578 if ($s =~ s/^.*?\n//) {
1584 my (undef, $sindent) = line_stats
("+" . $s);
1585 my $stat_real = raw_line
($linenr, $cond_lines);
1587 # Check if either of these lines are modified, else
1588 # this is not this patch's fault.
1589 if (!defined($stat_real) ||
1590 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1593 if (defined($stat_real) && $cond_lines > 1) {
1594 $stat_real = "[...]\n$stat_real";
1597 #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";
1599 if ($check && (($sindent % 4) != 0 ||
1600 ($sindent <= $indent && $s ne ''))) {
1601 ERROR
("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1605 # Track the 'values' across context and added lines.
1606 my $opline = $line; $opline =~ s/^./ /;
1607 my ($curr_values, $curr_vars) =
1608 annotate_values
($opline . "\n", $prev_values);
1609 $curr_values = $prev_values . $curr_values;
1611 my $outline = $opline; $outline =~ s/\t/ /g;
1612 print "$linenr > .$outline\n";
1613 print "$linenr > $curr_values\n";
1614 print "$linenr > $curr_vars\n";
1616 $prev_values = substr($curr_values, -1);
1618 #ignore lines not being added
1619 if ($line=~/^[^\+]/) {next;}
1621 # TEST: allow direct testing of the type matcher.
1623 if ($line =~ /^.\s*$Declare\s*$/) {
1624 ERROR
("TEST: is type\n" . $herecurr);
1625 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1626 ERROR
("TEST: is not type ($1 is)\n". $herecurr);
1630 # TEST: allow direct testing of the attribute matcher.
1632 if ($line =~ /^.\s*$Modifier\s*$/) {
1633 ERROR
("TEST: is attr\n" . $herecurr);
1634 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1635 ERROR
("TEST: is not attr ($1 is)\n". $herecurr);
1640 # check for initialisation to aggregates open brace on the next line
1641 if ($line =~ /^.\s*\{/ &&
1642 $prevline =~ /(?:^|[^=])=\s*$/) {
1643 ERROR
("that open brace { should be on the previous line\n" . $hereprev);
1647 # Checks which are anchored on the added line.
1650 # check for malformed paths in #include statements (uses RAW line)
1651 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1653 if ($path =~ m{//}) {
1654 ERROR
("malformed #include filename\n" .
1659 # no C99 // comments
1660 if ($line =~ m{//}) {
1661 ERROR
("do not use C99 // comments\n" . $herecurr);
1663 # Remove C99 comments.
1665 $opline =~ s@
//.*@@
;
1667 # check for global initialisers.
1668 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1669 ERROR
("do not initialise globals to 0 or NULL\n" .
1672 # check for static initialisers.
1673 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1674 ERROR
("do not initialise statics to 0 or NULL\n" .
1678 # * goes on variable not on type
1680 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1681 my ($from, $to) = ($1, $1);
1683 # Should start with a space.
1684 $to =~ s/^(\S)/ $1/;
1685 # Should not end with a space.
1687 # '*'s should not have spaces between.
1688 while ($to =~ s/\*\s+\*/\*\*/) {
1691 #print "from<$from> to<$to>\n";
1693 ERROR
("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1695 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1696 my ($from, $to, $ident) = ($1, $1, $2);
1698 # Should start with a space.
1699 $to =~ s/^(\S)/ $1/;
1700 # Should not end with a space.
1702 # '*'s should not have spaces between.
1703 while ($to =~ s/\*\s+\*/\*\*/) {
1705 # Modifiers should have spaces.
1706 $to =~ s/(\b$Modifier$)/$1 /;
1708 #print "from<$from> to<$to> ident<$ident>\n";
1709 if ($from ne $to && $ident !~ /^$Modifier$/) {
1710 ERROR
("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1714 # function brace can't be on same line, except for #defines of do while,
1715 # or if closed on same line
1716 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1717 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1718 ERROR
("open brace '{' following function declarations go on the next line\n" . $herecurr);
1721 # open braces for enum, union and struct go on the same line.
1722 if ($line =~ /^.\s*\{/ &&
1723 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1724 ERROR
("open brace '{' following $1 go on the same line\n" . $hereprev);
1727 # missing space after union, struct or enum definition
1728 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1729 ERROR
("missing space after $1 definition\n" . $herecurr);
1732 # check for spacing round square brackets; allowed:
1733 # 1. with a type on the left -- int [] a;
1734 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1735 # 3. inside a curly brace -- = { [0...10] = 5 }
1736 # 4. after a comma -- [1] = 5, [2] = 6
1737 # 5. in a macro definition -- #define abc(x) [x] = y
1738 while ($line =~ /(.*?\s)\[/g) {
1739 my ($where, $prefix) = ($-[1], $1);
1740 if ($prefix !~ /$Type\s+$/ &&
1741 ($where != 0 || $prefix !~ /^.\s+$/) &&
1742 $prefix !~ /{\s+$/ &&
1743 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1744 $prefix !~ /,\s+$/) {
1745 ERROR
("space prohibited before open square bracket '['\n" . $herecurr);
1749 # check for spaces between functions and their parentheses.
1750 while ($line =~ /($Ident)\s+\(/g) {
1752 my $ctx_before = substr($line, 0, $-[1]);
1753 my $ctx = "$ctx_before$name";
1755 # Ignore those directives where spaces _are_ permitted.
1757 if|for|while|switch
|return|case
|
1758 volatile
|__volatile__
|coroutine_fn
|
1759 __attribute__
|format
|__extension__
|
1763 # Ignore 'catch (...)' in C++
1764 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1766 # cpp #define statements have non-optional spaces, ie
1767 # if there is a space between the name and the open
1768 # parenthesis it is simply not a parameter group.
1769 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1771 # cpp #elif statement condition may start with a (
1772 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1774 # If this whole things ends with a type its most
1775 # likely a typedef for a function.
1776 } elsif ($ctx =~ /$Type$/) {
1779 ERROR
("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1782 # Check operator spacing.
1783 if (!($line=~/\#\s*include/)) {
1785 <<=|>>=|<=|>=|==|!=|
1786 \
+=|-=|\
*=|\
/=|%=|\
^=|\
|=|&=|
1787 =>|->|<<|>>|<|>|=|!|~|
1788 &&|\
|\
||,|\
^|\
+\
+|--|&|\
||\
+|-|\
*|\
/|%|
1791 my @elements = split(/($ops|;)/, $opline);
1794 my $blank = copy_spacing
($opline);
1796 for (my $n = 0; $n < $#elements; $n += 2) {
1797 $off += length($elements[$n]);
1799 # Pick up the preceding and succeeding characters.
1800 my $ca = substr($opline, 0, $off);
1802 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1803 $cc = substr($opline, $off + length($elements[$n + 1]));
1805 my $cb = "$ca$;$cc";
1808 $a = 'V' if ($elements[$n] ne '');
1809 $a = 'W' if ($elements[$n] =~ /\s$/);
1810 $a = 'C' if ($elements[$n] =~ /$;$/);
1811 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1812 $a = 'O' if ($elements[$n] eq '');
1813 $a = 'E' if ($ca =~ /^\s*$/);
1815 my $op = $elements[$n + 1];
1818 if (defined $elements[$n + 2]) {
1819 $c = 'V' if ($elements[$n + 2] ne '');
1820 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1821 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1822 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1823 $c = 'O' if ($elements[$n + 2] eq '');
1824 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1829 my $ctx = "${a}x${c}";
1831 my $at = "(ctx:$ctx)";
1833 my $ptr = substr($blank, 0, $off) . "^";
1834 my $hereptr = "$hereline$ptr\n";
1836 # Pull out the value of this operator.
1837 my $op_type = substr($curr_values, $off + 1, 1);
1839 # Get the full operator variant.
1840 my $opv = $op . substr($curr_vars, $off, 1);
1842 # Ignore operators passed as parameters.
1843 if ($op_type ne 'V' &&
1844 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1847 # } elsif ($op =~ /^$;+$/) {
1849 # ; should have either the end of line or a space or \ after it
1850 } elsif ($op eq ';') {
1851 if ($ctx !~ /.x[WEBC]/ &&
1852 $cc !~ /^\\/ && $cc !~ /^;/) {
1853 ERROR
("space required after that '$op' $at\n" . $hereptr);
1857 } elsif ($op eq '//') {
1859 # Ignore : used in class declaration in C++
1860 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
1861 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
1865 # : when part of a bitfield
1866 } elsif ($op eq '->' || $opv eq ':B') {
1867 if ($ctx =~ /Wx.|.xW/) {
1868 ERROR
("spaces prohibited around that '$op' $at\n" . $hereptr);
1871 # , must have a space on the right.
1872 # not required when having a single },{ on one line
1873 } elsif ($op eq ',') {
1874 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
1875 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
1876 ERROR
("space required after that '$op' $at\n" . $hereptr);
1879 # '*' as part of a type definition -- reported already.
1880 } elsif ($opv eq '*_') {
1881 #warn "'*' is part of type\n";
1883 # unary operators should have a space before and
1884 # none after. May be left adjacent to another
1885 # unary operator, or a cast
1886 } elsif ($op eq '!' || $op eq '~' ||
1887 $opv eq '*U' || $opv eq '-U' ||
1888 $opv eq '&U' || $opv eq '&&U') {
1889 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
1890 # '~' used as a name of Destructor
1892 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1893 ERROR
("space required before that '$op' $at\n" . $hereptr);
1895 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1896 # A unary '*' may be const
1898 } elsif ($ctx =~ /.xW/) {
1899 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1902 # unary ++ and unary -- are allowed no space on one side.
1903 } elsif ($op eq '++' or $op eq '--') {
1904 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1905 ERROR
("space required one side of that '$op' $at\n" . $hereptr);
1907 if ($ctx =~ /Wx[BE]/ ||
1908 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1909 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1911 if ($ctx =~ /ExW/) {
1912 ERROR
("space prohibited after that '$op' $at\n" . $hereptr);
1915 # A colon needs no spaces before when it is
1916 # terminating a case value or a label.
1917 } elsif ($opv eq ':C' || $opv eq ':L') {
1918 if ($ctx =~ /Wx./) {
1919 ERROR
("space prohibited before that '$op' $at\n" . $hereptr);
1922 # All the others need spaces both sides.
1923 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1926 if ($realfile =~ /\.cpp|\.h$/) {
1927 # Ignore template arguments <...> in C++
1928 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
1938 # Ignore email addresses <foo@bar>
1940 $cc =~ /^\S+\@\S+>/) ||
1942 $ca =~ /<\S+\@\S+$/))
1948 if (($opv eq ':O' && $ca =~ /\?$/) ||
1949 ($op eq '?' && $cc =~ /^:/)) {
1954 ERROR
("spaces required around that '$op' $at\n" . $hereptr);
1957 $off += length($elements[$n + 1]);
1961 #need space before brace following if, while, etc
1962 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
1964 ERROR
("space required before the open brace '{'\n" . $herecurr);
1967 # closing brace should have a space following it when it has anything
1969 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1970 ERROR
("space required after that close brace '}'\n" . $herecurr);
1973 # check spacing on square brackets
1974 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1975 ERROR
("space prohibited after that open square bracket '['\n" . $herecurr);
1977 if ($line =~ /\s\]/) {
1978 ERROR
("space prohibited before that close square bracket ']'\n" . $herecurr);
1981 # check spacing on parentheses
1982 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1983 $line !~ /for\s*\(\s+;/) {
1984 ERROR
("space prohibited after that open parenthesis '('\n" . $herecurr);
1986 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1987 $line !~ /for\s*\(.*;\s+\)/ &&
1988 $line !~ /:\s+\)/) {
1989 ERROR
("space prohibited before that close parenthesis ')'\n" . $herecurr);
1992 # Return is not a function.
1993 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1997 # Flatten any parentheses
1998 $value =~ s/\(/ \(/g;
1999 $value =~ s/\)/\) /g;
2000 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2001 $value !~ /(?
:$Ident|-?
$Constant)\s
*
2003 (?
:$Ident|-?
$Constant)/x
&&
2004 $value =~ s/\([^\(\)]*\)/1/) {
2006 #print "value<$value>\n";
2007 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2008 ERROR
("return is not a function, parentheses are not required\n" . $herecurr);
2010 } elsif ($spacing !~ /\s+/) {
2011 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2014 # Return of what appears to be an errno should normally be -'ve
2015 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2017 if ($name ne 'EOF' && $name ne 'ERROR') {
2018 ERROR
("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2022 # Need a space before open parenthesis after if, while etc
2023 if ($line=~/\b(if|while|for|switch)\(/) {
2024 ERROR
("space required before the open parenthesis '('\n" . $herecurr);
2027 # Check for illegal assignment in if conditional -- and check for trailing
2028 # statements after the conditional.
2029 if ($line =~ /do\s*(?!{)/) {
2030 my ($stat_next) = ctx_statement_block
($line_nr_next,
2031 $remain_next, $off_next);
2032 $stat_next =~ s/\n./\n /g;
2033 ##print "stat<$stat> stat_next<$stat_next>\n";
2035 if ($stat_next =~ /^\s*while\b/) {
2036 # If the statement carries leading newlines,
2037 # then count those as offsets.
2039 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2041 statement_rawlines
($whitespace) - 1;
2043 $suppress_whiletrailers{$line_nr_next +
2047 if (!defined $suppress_whiletrailers{$linenr} &&
2048 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2049 my ($s, $c) = ($stat, $cond);
2051 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2052 ERROR
("do not use assignment in if condition\n" . $herecurr);
2055 # Find out what is on the end of the line after the
2057 substr($s, 0, length($c), '');
2059 $s =~ s/$;//g; # Remove any comments
2060 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2061 $c !~ /}\s*while\s*/)
2063 # Find out how long the conditional actually is.
2064 my @newlines = ($c =~ /\n/gs);
2065 my $cond_lines = 1 + $#newlines;
2068 $stat_real = raw_line
($linenr, $cond_lines)
2069 . "\n" if ($cond_lines);
2070 if (defined($stat_real) && $cond_lines > 1) {
2071 $stat_real = "[...]\n$stat_real";
2074 ERROR
("trailing statements should be on next line\n" . $herecurr . $stat_real);
2078 # Check for bitwise tests written as boolean
2090 ERROR
("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2093 # if and else should not have general statements after it
2094 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2096 $s =~ s/$;//g; # Remove any comments
2097 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2098 ERROR
("trailing statements should be on next line\n" . $herecurr);
2101 # if should not continue a brace
2102 if ($line =~ /}\s*if\b/) {
2103 ERROR
("trailing statements should be on next line\n" .
2106 # case and default should not have general statements after them
2107 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2109 (?
:\s
*$;*)(?
:\s
*{)?
(?
:\s
*$;*)(?
:\s
*\\)?\s
*$|
2113 ERROR
("trailing statements should be on next line\n" . $herecurr);
2116 # Check for }<nl>else {, these must be at the same
2117 # indent level to be relevant to each other.
2118 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2119 $previndent == $indent) {
2120 ERROR
("else should follow close brace '}'\n" . $hereprev);
2123 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2124 $previndent == $indent) {
2125 my ($s, $c) = ctx_statement_block
($linenr, $realcnt, 0);
2127 # Find out what is on the end of the line after the
2129 substr($s, 0, length($c), '');
2132 if ($s =~ /^\s*;/) {
2133 ERROR
("while should follow close brace '}'\n" . $hereprev);
2137 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2138 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2139 # print "No studly caps, use _\n";
2140 # print "$herecurr";
2144 #no spaces allowed after \ in define
2145 if ($line=~/\#\s*define.*\\\s$/) {
2146 ERROR
("Whitespace after \\ makes next lines useless\n" . $herecurr);
2149 # multi-statement macros should be enclosed in a do while loop, grab the
2150 # first statement and ensure its the whole macro if its not enclosed
2151 # in a known good container
2152 if ($realfile !~ m@
/vmlinux
.lds
.h
$@
&&
2153 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2156 my ($off, $dstat, $dcond, $rest);
2159 my $args = defined($1);
2161 # Find the end of the macro and limit our statement
2163 while ($cnt > 0 && defined $lines[$ln - 1] &&
2164 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2166 $ctx .= $rawlines[$ln - 1] . "\n";
2167 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2170 $ctx .= $rawlines[$ln - 1];
2172 ($dstat, $dcond, $ln, $cnt, $off) =
2173 ctx_statement_block
($linenr, $ln - $linenr + 1, 0);
2174 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2175 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2177 # Extract the remainder of the define (if any) and
2178 # rip off surrounding spaces, and trailing \'s.
2180 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2181 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2182 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2183 $rest .= substr($lines[$ln - 1], $off) . "\n";
2189 $rest =~ s/\\\n.//g;
2193 # Clean up the original statement.
2195 substr($dstat, 0, length($dcond), '');
2197 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2200 $dstat =~ s/\\\n.//g;
2201 $dstat =~ s/^\s*//s;
2202 $dstat =~ s/\s*$//s;
2204 # Flatten any parentheses and braces
2205 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2206 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2207 $dstat =~ s/\[[^\{\}]*\]/1/)
2211 my $exceptions = qr{
2223 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2224 if ($rest ne '' && $rest ne ',') {
2225 if ($rest !~ /while\s*\(/ &&
2226 $dstat !~ /$exceptions/)
2228 ERROR
("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2231 } elsif ($ctx !~ /;/) {
2233 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2234 $dstat !~ /$exceptions/ &&
2235 $dstat !~ /^\.$Ident\s*=/ &&
2236 $dstat =~ /$Operators/)
2238 ERROR
("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2243 # check for missing bracing round if etc
2244 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2245 my ($level, $endln, @chunks) =
2246 ctx_statement_full
($linenr, $realcnt, 1);
2248 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2249 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2252 if ($#chunks >= 0 && $level == 0) {
2255 my $herectx = $here . "\n";
2256 my $ln = $linenr - 1;
2257 for my $chunk (@chunks) {
2258 my ($cond, $block) = @
{$chunk};
2260 # If the condition carries leading newlines, then count those as offsets.
2261 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2262 my $offset = statement_rawlines
($whitespace) - 1;
2264 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2266 # We have looked at and allowed this specific line.
2267 $suppress_ifbraces{$ln + $offset} = 1;
2269 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2270 $ln += statement_rawlines
($block) - 1;
2272 substr($block, 0, length($cond), '');
2274 my $spaced_block = $block;
2275 $spaced_block =~ s/\n\+/ /g;
2277 $seen++ if ($spaced_block =~ /^\s*\{/);
2279 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2281 if (statement_lines
($cond) > 1) {
2282 print "APW: ALLOWED: cond<$cond>\n"
2286 if ($block =~/\b(?:if|for|while)\b/) {
2287 print "APW: ALLOWED: block<$block>\n"
2291 if (statement_block_size
($block) > 1) {
2292 print "APW: ALLOWED: lines block<$block>\n"
2297 if ($seen != ($#chunks + 1)) {
2298 ERROR
("braces {} are necessary for all arms of this statement\n" . $herectx);
2302 if (!defined $suppress_ifbraces{$linenr - 1} &&
2303 $line =~ /\b(if|while|for|else)\b/ &&
2304 $line !~ /\#\s*if/ &&
2305 $line !~ /\#\s*else/) {
2308 # Check the pre-context.
2309 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2312 if ($line !~ /else/) {
2313 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2319 my ($level, $endln, @chunks) =
2320 ctx_statement_full
($linenr, $realcnt, $-[0]);
2322 # Check the condition.
2323 my ($cond, $block) = @
{$chunks[0]};
2324 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2325 if $dbg_adv_checking;
2326 if (defined $cond) {
2327 substr($block, 0, length($cond), '');
2329 if (statement_lines
($cond) > 1) {
2330 print "APW: ALLOWED: cond<$cond>\n"
2334 if ($block =~/\b(?:if|for|while)\b/) {
2335 print "APW: ALLOWED: block<$block>\n"
2339 if (statement_block_size
($block) > 1) {
2340 print "APW: ALLOWED: lines block<$block>\n"
2344 # Check the post-context.
2345 if (defined $chunks[1]) {
2346 my ($cond, $block) = @
{$chunks[1]};
2347 if (defined $cond) {
2348 substr($block, 0, length($cond), '');
2350 if ($block =~ /^\s*\{/) {
2351 print "APW: ALLOWED: chunk-1 block<$block>\n"
2356 print "DCS: level=$level block<$block> allowed=$allowed\n"
2358 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2359 my $herectx = $here . "\n";;
2360 my $cnt = statement_rawlines
($block);
2362 for (my $n = 0; $n < $cnt; $n++) {
2363 $herectx .= raw_line
($linenr, $n) . "\n";;
2366 ERROR
("braces {} are necessary even for single statement blocks\n" . $herectx);
2370 # no volatiles please
2371 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2372 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2373 ERROR
("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2377 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2378 ERROR
("if this code is redundant consider removing it\n" .
2382 # check for needless g_free() checks
2383 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2385 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2386 ERROR
("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2390 # warn about #ifdefs in C files
2391 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2392 # print "#ifdef in C files should be avoided\n";
2393 # print "$herecurr";
2397 # warn about spacing in #ifdefs
2398 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2399 ERROR
("exactly one space required after that #$1\n" . $herecurr);
2401 # check for memory barriers without a comment.
2402 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2403 if (!ctx_has_comment
($first_line, $linenr)) {
2404 ERROR
("memory barrier without comment\n" . $herecurr);
2407 # check of hardware specific defines
2408 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2409 # where they might be necessary.
2410 if ($line =~ m@
^.\s
*\#\s
*if.*\b__@
) {
2411 WARN
("architecture specific defines should be avoided\n" . $herecurr);
2414 # Check that the storage class is at the beginning of a declaration
2415 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2416 ERROR
("storage class should be at the beginning of the declaration\n" . $herecurr)
2419 # check the location of the inline attribute, that it is between
2420 # storage class and type.
2421 if ($line =~ /\b$Type\s+$Inline\b/ ||
2422 $line =~ /\b$Inline\s+$Storage\b/) {
2423 ERROR
("inline keyword should sit between storage class and type\n" . $herecurr);
2426 # check for sizeof(&)
2427 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2428 ERROR
("sizeof(& should be avoided\n" . $herecurr);
2431 # check for new externs in .c files.
2432 if ($realfile =~ /\.c$/ && defined $stat &&
2433 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2435 my $function_name = $1;
2436 my $paren_space = $2;
2439 if (defined $cond) {
2440 substr($s, 0, length($cond), '');
2442 if ($s =~ /^\s*;/ &&
2443 $function_name ne 'uninitialized_var')
2445 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2448 if ($paren_space =~ /\n/) {
2449 ERROR
("arguments for function declarations should follow identifier\n" . $herecurr);
2452 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2453 $stat =~ /^.\s*extern\s+/)
2455 ERROR
("externs should be avoided in .c files\n" . $herecurr);
2458 # check for pointless casting of g_malloc return
2459 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2461 ERROR
("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2463 ERROR
("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2467 # check for gcc specific __FUNCTION__
2468 if ($line =~ /__FUNCTION__/) {
2469 ERROR
("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2472 # recommend qemu_strto* over strto* for numeric conversions
2473 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2474 ERROR
("consider using qemu_$1 in preference to $1\n" . $herecurr);
2476 # recommend sigaction over signal for portability, when establishing a handler
2477 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2478 ERROR
("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2480 # check for module_init(), use category-specific init macros explicitly please
2481 if ($line =~ /^module_init\s*\(/) {
2482 ERROR
("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2484 # check for various ops structs, ensure they are const.
2485 my $struct_ops = qr{AIOCBInfo
|
2489 DisplayChangeListenerOps
|
2493 MemoryRegionIOMMUOps
|
2499 Spice
[A
-Z
][a
-zA
-Z0
-9]*Interface
|
2501 USBDesc
[A
-Z
][a
-zA
-Z0
-9]*|
2505 if ($line !~ /\bconst\b/ &&
2506 $line =~ /\b($struct_ops)\b.*=/) {
2507 ERROR
("initializer for struct $1 should normally be const\n" .
2511 # check for %L{u,d,i} in strings
2513 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2514 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2515 $string =~ s/%%/__/g;
2516 if ($string =~ /(?<!%)%L[udi]/) {
2517 ERROR
("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2522 # QEMU specific tests
2523 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2524 ERROR
("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2527 # Qemu error function tests
2529 # Find newlines in error messages
2530 my $qemu_error_funcs = qr{error_setg
|
2533 error_setg_file_open
|
2540 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2541 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2544 # Continue checking for error messages that contains newlines. This
2545 # check handles cases where string literals are spread over multiple lines.
2547 # error_report("Error msg line #1"
2548 # "Error msg line #2\n");
2549 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2550 my $continued_str_literal = qr{\+\s*\".*\"};
2552 if ($rawline =~ /$quoted_newline_regex/) {
2553 # Backtrack to first line that does not contain only a quoted literal
2554 # and assume that it is the start of the statement.
2555 my $i = $linenr - 2;
2557 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2561 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2562 ERROR
("Error messages should not contain newlines\n" . $herecurr);
2566 # check for non-portable libc calls that have portable alternatives in QEMU
2567 if ($line =~ /\bffs\(/) {
2568 ERROR
("use ctz32() instead of ffs()\n" . $herecurr);
2570 if ($line =~ /\bffsl\(/) {
2571 ERROR
("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2573 if ($line =~ /\bffsll\(/) {
2574 ERROR
("use ctz64() instead of ffsll()\n" . $herecurr);
2576 if ($line =~ /\bbzero\(/) {
2577 ERROR
("use memset() instead of bzero()\n" . $herecurr);
2579 my $non_exit_glib_asserts = qr{g_assert_cmpstr
|
2590 g_test_assert_expected_messages
|
2591 g_test_trap_assert_passed
|
2592 g_test_trap_assert_stdout
|
2593 g_test_trap_assert_stdout_unmatched
|
2594 g_test_trap_assert_stderr
|
2595 g_test_trap_assert_stderr_unmatched
}x
;
2596 if ($realfile !~ /^tests\// &&
2597 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2598 ERROR
("Use g_assert or g_assert_not_reached\n". $herecurr);
2602 # If we have no input at all, then there is nothing to report on
2603 # so just keep quiet.
2604 if ($#rawlines == -1) {
2608 # In mailback mode only produce a report in the negative, for
2609 # things that appear to be patches.
2610 if ($mailback && ($clean == 1 || !$is_patch)) {
2614 # This is not a patch, and we are are in 'no-patch' mode so
2616 if (!$chk_patch && !$is_patch) {
2621 ERROR
("Does not appear to be a unified-diff format patch\n");
2623 if ($is_patch && $chk_signoff && $signoff == 0) {
2624 ERROR
("Missing Signed-off-by: line(s)\n");
2627 print report_dump
();
2628 if ($summary && !($clean == 1 && $quiet == 1)) {
2629 print "$filename " if ($summary_file);
2630 print "total: $cnt_error errors, $cnt_warn warnings, " .
2631 "$cnt_lines lines checked\n";
2632 print "\n" if ($quiet == 0);
2636 # If there were whitespace errors which cleanpatch can fix
2637 # then suggest that.
2638 # if ($rpt_cleaners) {
2639 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2640 # print " scripts/cleanfile\n\n";
2644 if ($clean == 1 && $quiet == 0) {
2645 print "$vname has no obvious style problems and is ready for submission.\n"
2647 if ($clean == 0 && $quiet == 0) {
2648 print "$vname has style problems, please review. If any of these errors\n";
2649 print "are false positives report them to the maintainer, see\n";
2650 print "CHECKPATCH in MAINTAINERS.\n";
2653 return ($no_warnings ?
$clean : $cnt_error == 0);