1#!/usr/bin/env perl 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 7 8use strict; 9use warnings; 10 11my $P = $0; 12$P =~ s@.*/@@g; 13 14our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$}; 15 16my $V = '0.31'; 17 18use Getopt::Long qw(:config no_auto_abbrev); 19 20my $quiet = 0; 21my $tree = 1; 22my $chk_signoff = 1; 23my $chk_patch = undef; 24my $chk_branch = undef; 25my $tst_only; 26my $emacs = 0; 27my $terse = 0; 28my $file = undef; 29my $no_warnings = 0; 30my $summary = 1; 31my $mailback = 0; 32my $summary_file = 0; 33my $root; 34my %debug; 35my $help = 0; 36 37sub help { 38 my ($exitcode) = @_; 39 40 print << "EOM"; 41Usage: 42 43 $P [OPTION]... [FILE]... 44 $P [OPTION]... [GIT-REV-LIST] 45 46Version: $V 47 48Options: 49 -q, --quiet quiet 50 --no-tree run without a kernel tree 51 --no-signoff do not check for 'Signed-off-by' line 52 --patch treat FILE as patchfile 53 --branch treat args as GIT revision list 54 --emacs emacs compile window format 55 --terse one line per report 56 -f, --file treat FILE as regular source file 57 --strict fail if only warnings are found 58 --root=PATH PATH to the kernel tree root 59 --no-summary suppress the per-file summary 60 --mailback only produce a report in case of warnings/errors 61 --summary-file include the filename in summary 62 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 63 'values', 'possible', 'type', and 'attr' (default 64 is all off) 65 --test-only=WORD report only warnings/errors containing WORD 66 literally 67 -h, --help, --version display this help and exit 68 69When FILE is - read standard input. 70EOM 71 72 exit($exitcode); 73} 74 75GetOptions( 76 'q|quiet+' => \$quiet, 77 'tree!' => \$tree, 78 'signoff!' => \$chk_signoff, 79 'patch!' => \$chk_patch, 80 'branch!' => \$chk_branch, 81 'emacs!' => \$emacs, 82 'terse!' => \$terse, 83 'f|file!' => \$file, 84 'strict!' => \$no_warnings, 85 'root=s' => \$root, 86 'summary!' => \$summary, 87 'mailback!' => \$mailback, 88 'summary-file!' => \$summary_file, 89 90 'debug=s' => \%debug, 91 'test-only=s' => \$tst_only, 92 'h|help' => \$help, 93 'version' => \$help 94) or help(1); 95 96help(0) if ($help); 97 98my $exit = 0; 99 100if ($#ARGV < 0) { 101 print "$P: no input files\n"; 102 exit(1); 103} 104 105if (!defined $chk_branch && !defined $chk_patch && !defined $file) { 106 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; 107 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0; 108 $chk_patch = $chk_branch || $file ? 0 : 1; 109} elsif (!defined $chk_branch && !defined $chk_patch) { 110 if ($file) { 111 $chk_branch = $chk_patch = 0; 112 } else { 113 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; 114 $chk_patch = $chk_branch ? 0 : 1; 115 } 116} elsif (!defined $chk_branch && !defined $file) { 117 if ($chk_patch) { 118 $chk_branch = $file = 0; 119 } else { 120 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; 121 $file = $chk_branch ? 0 : 1; 122 } 123} elsif (!defined $chk_patch && !defined $file) { 124 if ($chk_branch) { 125 $chk_patch = $file = 0; 126 } else { 127 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0; 128 $chk_patch = $file ? 0 : 1; 129 } 130} elsif (!defined $chk_branch) { 131 $chk_branch = $chk_patch || $file ? 0 : 1; 132} elsif (!defined $chk_patch) { 133 $chk_patch = $chk_branch || $file ? 0 : 1; 134} elsif (!defined $file) { 135 $file = $chk_patch || $chk_branch ? 0 : 1; 136} 137 138if (($chk_patch && $chk_branch) || 139 ($chk_patch && $file) || 140 ($chk_branch && $file)) { 141 die "Only one of --file, --branch, --patch is permitted\n"; 142} 143if (!$chk_patch && !$chk_branch && !$file) { 144 die "One of --file, --branch, --patch is required\n"; 145} 146 147my $dbg_values = 0; 148my $dbg_possible = 0; 149my $dbg_type = 0; 150my $dbg_attr = 0; 151my $dbg_adv_dcs = 0; 152my $dbg_adv_checking = 0; 153my $dbg_adv_apw = 0; 154for my $key (keys %debug) { 155 ## no critic 156 eval "\${dbg_$key} = '$debug{$key}';"; 157 die "$@" if ($@); 158} 159 160my $rpt_cleaners = 0; 161 162if ($terse) { 163 $emacs = 1; 164 $quiet++; 165} 166 167if ($tree) { 168 if (defined $root) { 169 if (!top_of_kernel_tree($root)) { 170 die "$P: $root: --root does not point at a valid tree\n"; 171 } 172 } else { 173 if (top_of_kernel_tree('.')) { 174 $root = '.'; 175 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 176 top_of_kernel_tree($1)) { 177 $root = $1; 178 } 179 } 180 181 if (!defined $root) { 182 print "Must be run from the top-level dir. of a kernel tree\n"; 183 exit(2); 184 } 185} 186 187my $emitted_corrupt = 0; 188 189our $Ident = qr{ 190 [A-Za-z_][A-Za-z\d_]* 191 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 192 }x; 193our $Storage = qr{extern|static|asmlinkage}; 194our $Sparse = qr{ 195 __force 196 }x; 197 198# Notes to $Attribute: 199our $Attribute = qr{ 200 const| 201 volatile| 202 QEMU_NORETURN| 203 QEMU_WARN_UNUSED_RESULT| 204 QEMU_SENTINEL| 205 QEMU_ARTIFICIAL| 206 QEMU_PACKED| 207 GCC_FMT_ATTR 208 }x; 209our $Modifier; 210our $Inline = qr{inline}; 211our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 212our $Lval = qr{$Ident(?:$Member)*}; 213 214our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 215our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 216our $Compare = qr{<=|>=|==|!=|<|>}; 217our $Operators = qr{ 218 <=|>=|==|!=| 219 =>|->|<<|>>|<|>|!|~| 220 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 221 }x; 222 223our $NonptrType; 224our $Type; 225our $Declare; 226 227our $UTF8 = qr { 228 [\x09\x0A\x0D\x20-\x7E] # ASCII 229 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 230 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 231 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 232 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 233 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 234 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 235 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 236}x; 237 238# There are still some false positives, but this catches most 239# common cases. 240our $typeTypedefs = qr{(?x: 241 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase 242 | [A-Z][A-Z\d_]*AIOCB # all uppercase 243 | [A-Z][A-Z\d_]*CPU # all uppercase 244 | QEMUBH # all uppercase 245)}; 246 247our @typeList = ( 248 qr{void}, 249 qr{(?:unsigned\s+)?char}, 250 qr{(?:unsigned\s+)?short}, 251 qr{(?:unsigned\s+)?int}, 252 qr{(?:unsigned\s+)?long}, 253 qr{(?:unsigned\s+)?long\s+int}, 254 qr{(?:unsigned\s+)?long\s+long}, 255 qr{(?:unsigned\s+)?long\s+long\s+int}, 256 qr{unsigned}, 257 qr{float}, 258 qr{double}, 259 qr{bool}, 260 qr{struct\s+$Ident}, 261 qr{union\s+$Ident}, 262 qr{enum\s+$Ident}, 263 qr{${Ident}_t}, 264 qr{${Ident}_handler}, 265 qr{${Ident}_handler_fn}, 266 qr{target_(?:u)?long}, 267 qr{hwaddr}, 268); 269 270# This can be modified by sub possible. Since it can be empty, be careful 271# about regexes that always match, because they can cause infinite loops. 272our @modifierList = ( 273); 274 275sub build_types { 276 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 277 if (@modifierList > 0) { 278 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 279 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 280 } else { 281 $Modifier = qr{(?:$Attribute|$Sparse)}; 282 } 283 $NonptrType = qr{ 284 (?:$Modifier\s+|const\s+)* 285 (?: 286 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 287 (?:$typeTypedefs\b)| 288 (?:${all}\b) 289 ) 290 (?:\s+$Modifier|\s+const)* 291 }x; 292 $Type = qr{ 293 $NonptrType 294 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 295 (?:\s+$Inline|\s+$Modifier)* 296 }x; 297 $Declare = qr{(?:$Storage\s+)?$Type}; 298} 299build_types(); 300 301$chk_signoff = 0 if ($file); 302 303my @rawlines = (); 304my @lines = (); 305my $vname; 306if ($chk_branch) { 307 my @patches; 308 my $HASH; 309 open($HASH, "-|", "git", "log", "--format=%H", $ARGV[0]) || 310 die "$P: git log --format=%H $ARGV[0] failed - $!\n"; 311 312 while (<$HASH>) { 313 chomp; 314 push @patches, $_; 315 } 316 317 close $HASH; 318 319 die "$P: no revisions returned for revlist '$chk_branch'\n" 320 unless @patches; 321 322 for my $hash (@patches) { 323 my $FILE; 324 open($FILE, '-|', "git", "show", $hash) || 325 die "$P: git show $hash - $!\n"; 326 $vname = $hash; 327 while (<$FILE>) { 328 chomp; 329 push(@rawlines, $_); 330 } 331 close($FILE); 332 if (!process($hash)) { 333 $exit = 1; 334 } 335 @rawlines = (); 336 @lines = (); 337 } 338} else { 339 for my $filename (@ARGV) { 340 my $FILE; 341 if ($file) { 342 open($FILE, '-|', "diff -u /dev/null $filename") || 343 die "$P: $filename: diff failed - $!\n"; 344 } elsif ($filename eq '-') { 345 open($FILE, '<&STDIN'); 346 } else { 347 open($FILE, '<', "$filename") || 348 die "$P: $filename: open failed - $!\n"; 349 } 350 if ($filename eq '-') { 351 $vname = 'Your patch'; 352 } else { 353 $vname = $filename; 354 } 355 while (<$FILE>) { 356 chomp; 357 push(@rawlines, $_); 358 } 359 close($FILE); 360 if (!process($filename)) { 361 $exit = 1; 362 } 363 @rawlines = (); 364 @lines = (); 365 } 366} 367 368exit($exit); 369 370sub top_of_kernel_tree { 371 my ($root) = @_; 372 373 my @tree_check = ( 374 "COPYING", "MAINTAINERS", "Makefile", 375 "README", "docs", "VERSION", 376 "vl.c" 377 ); 378 379 foreach my $check (@tree_check) { 380 if (! -e $root . '/' . $check) { 381 return 0; 382 } 383 } 384 return 1; 385} 386 387sub expand_tabs { 388 my ($str) = @_; 389 390 my $res = ''; 391 my $n = 0; 392 for my $c (split(//, $str)) { 393 if ($c eq "\t") { 394 $res .= ' '; 395 $n++; 396 for (; ($n % 8) != 0; $n++) { 397 $res .= ' '; 398 } 399 next; 400 } 401 $res .= $c; 402 $n++; 403 } 404 405 return $res; 406} 407sub copy_spacing { 408 (my $res = shift) =~ tr/\t/ /c; 409 return $res; 410} 411 412sub line_stats { 413 my ($line) = @_; 414 415 # Drop the diff line leader and expand tabs 416 $line =~ s/^.//; 417 $line = expand_tabs($line); 418 419 # Pick the indent from the front of the line. 420 my ($white) = ($line =~ /^(\s*)/); 421 422 return (length($line), length($white)); 423} 424 425my $sanitise_quote = ''; 426 427sub sanitise_line_reset { 428 my ($in_comment) = @_; 429 430 if ($in_comment) { 431 $sanitise_quote = '*/'; 432 } else { 433 $sanitise_quote = ''; 434 } 435} 436sub sanitise_line { 437 my ($line) = @_; 438 439 my $res = ''; 440 my $l = ''; 441 442 my $qlen = 0; 443 my $off = 0; 444 my $c; 445 446 # Always copy over the diff marker. 447 $res = substr($line, 0, 1); 448 449 for ($off = 1; $off < length($line); $off++) { 450 $c = substr($line, $off, 1); 451 452 # Comments we are wacking completely including the begin 453 # and end, all to $;. 454 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 455 $sanitise_quote = '*/'; 456 457 substr($res, $off, 2, "$;$;"); 458 $off++; 459 next; 460 } 461 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 462 $sanitise_quote = ''; 463 substr($res, $off, 2, "$;$;"); 464 $off++; 465 next; 466 } 467 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 468 $sanitise_quote = '//'; 469 470 substr($res, $off, 2, $sanitise_quote); 471 $off++; 472 next; 473 } 474 475 # A \ in a string means ignore the next character. 476 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 477 $c eq "\\") { 478 substr($res, $off, 2, 'XX'); 479 $off++; 480 next; 481 } 482 # Regular quotes. 483 if ($c eq "'" || $c eq '"') { 484 if ($sanitise_quote eq '') { 485 $sanitise_quote = $c; 486 487 substr($res, $off, 1, $c); 488 next; 489 } elsif ($sanitise_quote eq $c) { 490 $sanitise_quote = ''; 491 } 492 } 493 494 #print "c<$c> SQ<$sanitise_quote>\n"; 495 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 496 substr($res, $off, 1, $;); 497 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 498 substr($res, $off, 1, $;); 499 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 500 substr($res, $off, 1, 'X'); 501 } else { 502 substr($res, $off, 1, $c); 503 } 504 } 505 506 if ($sanitise_quote eq '//') { 507 $sanitise_quote = ''; 508 } 509 510 # The pathname on a #include may be surrounded by '<' and '>'. 511 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 512 my $clean = 'X' x length($1); 513 $res =~ s@\<.*\>@<$clean>@; 514 515 # The whole of a #error is a string. 516 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 517 my $clean = 'X' x length($1); 518 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 519 } 520 521 return $res; 522} 523 524sub ctx_statement_block { 525 my ($linenr, $remain, $off) = @_; 526 my $line = $linenr - 1; 527 my $blk = ''; 528 my $soff = $off; 529 my $coff = $off - 1; 530 my $coff_set = 0; 531 532 my $loff = 0; 533 534 my $type = ''; 535 my $level = 0; 536 my @stack = (); 537 my $p; 538 my $c; 539 my $len = 0; 540 541 my $remainder; 542 while (1) { 543 @stack = (['', 0]) if ($#stack == -1); 544 545 #warn "CSB: blk<$blk> remain<$remain>\n"; 546 # If we are about to drop off the end, pull in more 547 # context. 548 if ($off >= $len) { 549 for (; $remain > 0; $line++) { 550 last if (!defined $lines[$line]); 551 next if ($lines[$line] =~ /^-/); 552 $remain--; 553 $loff = $len; 554 $blk .= $lines[$line] . "\n"; 555 $len = length($blk); 556 $line++; 557 last; 558 } 559 # Bail if there is no further context. 560 #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 561 if ($off >= $len) { 562 last; 563 } 564 } 565 $p = $c; 566 $c = substr($blk, $off, 1); 567 $remainder = substr($blk, $off); 568 569 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 570 571 # Handle nested #if/#else. 572 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 573 push(@stack, [ $type, $level ]); 574 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 575 ($type, $level) = @{$stack[$#stack - 1]}; 576 } elsif ($remainder =~ /^#\s*endif\b/) { 577 ($type, $level) = @{pop(@stack)}; 578 } 579 580 # Statement ends at the ';' or a close '}' at the 581 # outermost level. 582 if ($level == 0 && $c eq ';') { 583 last; 584 } 585 586 # An else is really a conditional as long as its not else if 587 if ($level == 0 && $coff_set == 0 && 588 (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 589 $remainder =~ /^(else)(?:\s|{)/ && 590 $remainder !~ /^else\s+if\b/) { 591 $coff = $off + length($1) - 1; 592 $coff_set = 1; 593 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 594 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 595 } 596 597 if (($type eq '' || $type eq '(') && $c eq '(') { 598 $level++; 599 $type = '('; 600 } 601 if ($type eq '(' && $c eq ')') { 602 $level--; 603 $type = ($level != 0)? '(' : ''; 604 605 if ($level == 0 && $coff < $soff) { 606 $coff = $off; 607 $coff_set = 1; 608 #warn "CSB: mark coff<$coff>\n"; 609 } 610 } 611 if (($type eq '' || $type eq '{') && $c eq '{') { 612 $level++; 613 $type = '{'; 614 } 615 if ($type eq '{' && $c eq '}') { 616 $level--; 617 $type = ($level != 0)? '{' : ''; 618 619 if ($level == 0) { 620 if (substr($blk, $off + 1, 1) eq ';') { 621 $off++; 622 } 623 last; 624 } 625 } 626 $off++; 627 } 628 # We are truly at the end, so shuffle to the next line. 629 if ($off == $len) { 630 $loff = $len + 1; 631 $line++; 632 $remain--; 633 } 634 635 my $statement = substr($blk, $soff, $off - $soff + 1); 636 my $condition = substr($blk, $soff, $coff - $soff + 1); 637 638 #warn "STATEMENT<$statement>\n"; 639 #warn "CONDITION<$condition>\n"; 640 641 #print "coff<$coff> soff<$off> loff<$loff>\n"; 642 643 return ($statement, $condition, 644 $line, $remain + 1, $off - $loff + 1, $level); 645} 646 647sub statement_lines { 648 my ($stmt) = @_; 649 650 # Strip the diff line prefixes and rip blank lines at start and end. 651 $stmt =~ s/(^|\n)./$1/g; 652 $stmt =~ s/^\s*//; 653 $stmt =~ s/\s*$//; 654 655 my @stmt_lines = ($stmt =~ /\n/g); 656 657 return $#stmt_lines + 2; 658} 659 660sub statement_rawlines { 661 my ($stmt) = @_; 662 663 my @stmt_lines = ($stmt =~ /\n/g); 664 665 return $#stmt_lines + 2; 666} 667 668sub statement_block_size { 669 my ($stmt) = @_; 670 671 $stmt =~ s/(^|\n)./$1/g; 672 $stmt =~ s/^\s*\{//; 673 $stmt =~ s/}\s*$//; 674 $stmt =~ s/^\s*//; 675 $stmt =~ s/\s*$//; 676 677 my @stmt_lines = ($stmt =~ /\n/g); 678 my @stmt_statements = ($stmt =~ /;/g); 679 680 my $stmt_lines = $#stmt_lines + 2; 681 my $stmt_statements = $#stmt_statements + 1; 682 683 if ($stmt_lines > $stmt_statements) { 684 return $stmt_lines; 685 } else { 686 return $stmt_statements; 687 } 688} 689 690sub ctx_statement_full { 691 my ($linenr, $remain, $off) = @_; 692 my ($statement, $condition, $level); 693 694 my (@chunks); 695 696 # Grab the first conditional/block pair. 697 ($statement, $condition, $linenr, $remain, $off, $level) = 698 ctx_statement_block($linenr, $remain, $off); 699 #print "F: c<$condition> s<$statement> remain<$remain>\n"; 700 push(@chunks, [ $condition, $statement ]); 701 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 702 return ($level, $linenr, @chunks); 703 } 704 705 # Pull in the following conditional/block pairs and see if they 706 # could continue the statement. 707 for (;;) { 708 ($statement, $condition, $linenr, $remain, $off, $level) = 709 ctx_statement_block($linenr, $remain, $off); 710 #print "C: c<$condition> s<$statement> remain<$remain>\n"; 711 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 712 #print "C: push\n"; 713 push(@chunks, [ $condition, $statement ]); 714 } 715 716 return ($level, $linenr, @chunks); 717} 718 719sub ctx_block_get { 720 my ($linenr, $remain, $outer, $open, $close, $off) = @_; 721 my $line; 722 my $start = $linenr - 1; 723 my $blk = ''; 724 my @o; 725 my @c; 726 my @res = (); 727 728 my $level = 0; 729 my @stack = ($level); 730 for ($line = $start; $remain > 0; $line++) { 731 next if ($rawlines[$line] =~ /^-/); 732 $remain--; 733 734 $blk .= $rawlines[$line]; 735 736 # Handle nested #if/#else. 737 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 738 push(@stack, $level); 739 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 740 $level = $stack[$#stack - 1]; 741 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 742 $level = pop(@stack); 743 } 744 745 foreach my $c (split(//, $lines[$line])) { 746 ##print "C<$c>L<$level><$open$close>O<$off>\n"; 747 if ($off > 0) { 748 $off--; 749 next; 750 } 751 752 if ($c eq $close && $level > 0) { 753 $level--; 754 last if ($level == 0); 755 } elsif ($c eq $open) { 756 $level++; 757 } 758 } 759 760 if (!$outer || $level <= 1) { 761 push(@res, $rawlines[$line]); 762 } 763 764 last if ($level == 0); 765 } 766 767 return ($level, @res); 768} 769sub ctx_block_outer { 770 my ($linenr, $remain) = @_; 771 772 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 773 return @r; 774} 775sub ctx_block { 776 my ($linenr, $remain) = @_; 777 778 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 779 return @r; 780} 781sub ctx_statement { 782 my ($linenr, $remain, $off) = @_; 783 784 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 785 return @r; 786} 787sub ctx_block_level { 788 my ($linenr, $remain) = @_; 789 790 return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 791} 792sub ctx_statement_level { 793 my ($linenr, $remain, $off) = @_; 794 795 return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 796} 797 798sub ctx_locate_comment { 799 my ($first_line, $end_line) = @_; 800 801 # Catch a comment on the end of the line itself. 802 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 803 return $current_comment if (defined $current_comment); 804 805 # Look through the context and try and figure out if there is a 806 # comment. 807 my $in_comment = 0; 808 $current_comment = ''; 809 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 810 my $line = $rawlines[$linenr - 1]; 811 #warn " $line\n"; 812 if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 813 $in_comment = 1; 814 } 815 if ($line =~ m@/\*@) { 816 $in_comment = 1; 817 } 818 if (!$in_comment && $current_comment ne '') { 819 $current_comment = ''; 820 } 821 $current_comment .= $line . "\n" if ($in_comment); 822 if ($line =~ m@\*/@) { 823 $in_comment = 0; 824 } 825 } 826 827 chomp($current_comment); 828 return($current_comment); 829} 830sub ctx_has_comment { 831 my ($first_line, $end_line) = @_; 832 my $cmt = ctx_locate_comment($first_line, $end_line); 833 834 ##print "LINE: $rawlines[$end_line - 1 ]\n"; 835 ##print "CMMT: $cmt\n"; 836 837 return ($cmt ne ''); 838} 839 840sub raw_line { 841 my ($linenr, $cnt) = @_; 842 843 my $offset = $linenr - 1; 844 $cnt++; 845 846 my $line; 847 while ($cnt) { 848 $line = $rawlines[$offset++]; 849 next if (defined($line) && $line =~ /^-/); 850 $cnt--; 851 } 852 853 return $line; 854} 855 856sub cat_vet { 857 my ($vet) = @_; 858 my ($res, $coded); 859 860 $res = ''; 861 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 862 $res .= $1; 863 if ($2 ne '') { 864 $coded = sprintf("^%c", unpack('C', $2) + 64); 865 $res .= $coded; 866 } 867 } 868 $res =~ s/$/\$/; 869 870 return $res; 871} 872 873my $av_preprocessor = 0; 874my $av_pending; 875my @av_paren_type; 876my $av_pend_colon; 877 878sub annotate_reset { 879 $av_preprocessor = 0; 880 $av_pending = '_'; 881 @av_paren_type = ('E'); 882 $av_pend_colon = 'O'; 883} 884 885sub annotate_values { 886 my ($stream, $type) = @_; 887 888 my $res; 889 my $var = '_' x length($stream); 890 my $cur = $stream; 891 892 print "$stream\n" if ($dbg_values > 1); 893 894 while (length($cur)) { 895 @av_paren_type = ('E') if ($#av_paren_type < 0); 896 print " <" . join('', @av_paren_type) . 897 "> <$type> <$av_pending>" if ($dbg_values > 1); 898 if ($cur =~ /^(\s+)/o) { 899 print "WS($1)\n" if ($dbg_values > 1); 900 if ($1 =~ /\n/ && $av_preprocessor) { 901 $type = pop(@av_paren_type); 902 $av_preprocessor = 0; 903 } 904 905 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 906 print "CAST($1)\n" if ($dbg_values > 1); 907 push(@av_paren_type, $type); 908 $type = 'C'; 909 910 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 911 print "DECLARE($1)\n" if ($dbg_values > 1); 912 $type = 'T'; 913 914 } elsif ($cur =~ /^($Modifier)\s*/) { 915 print "MODIFIER($1)\n" if ($dbg_values > 1); 916 $type = 'T'; 917 918 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 919 print "DEFINE($1,$2)\n" if ($dbg_values > 1); 920 $av_preprocessor = 1; 921 push(@av_paren_type, $type); 922 if ($2 ne '') { 923 $av_pending = 'N'; 924 } 925 $type = 'E'; 926 927 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 928 print "UNDEF($1)\n" if ($dbg_values > 1); 929 $av_preprocessor = 1; 930 push(@av_paren_type, $type); 931 932 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 933 print "PRE_START($1)\n" if ($dbg_values > 1); 934 $av_preprocessor = 1; 935 936 push(@av_paren_type, $type); 937 push(@av_paren_type, $type); 938 $type = 'E'; 939 940 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 941 print "PRE_RESTART($1)\n" if ($dbg_values > 1); 942 $av_preprocessor = 1; 943 944 push(@av_paren_type, $av_paren_type[$#av_paren_type]); 945 946 $type = 'E'; 947 948 } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 949 print "PRE_END($1)\n" if ($dbg_values > 1); 950 951 $av_preprocessor = 1; 952 953 # Assume all arms of the conditional end as this 954 # one does, and continue as if the #endif was not here. 955 pop(@av_paren_type); 956 push(@av_paren_type, $type); 957 $type = 'E'; 958 959 } elsif ($cur =~ /^(\\\n)/o) { 960 print "PRECONT($1)\n" if ($dbg_values > 1); 961 962 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 963 print "ATTR($1)\n" if ($dbg_values > 1); 964 $av_pending = $type; 965 $type = 'N'; 966 967 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 968 print "SIZEOF($1)\n" if ($dbg_values > 1); 969 if (defined $2) { 970 $av_pending = 'V'; 971 } 972 $type = 'N'; 973 974 } elsif ($cur =~ /^(if|while|for)\b/o) { 975 print "COND($1)\n" if ($dbg_values > 1); 976 $av_pending = 'E'; 977 $type = 'N'; 978 979 } elsif ($cur =~/^(case)/o) { 980 print "CASE($1)\n" if ($dbg_values > 1); 981 $av_pend_colon = 'C'; 982 $type = 'N'; 983 984 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 985 print "KEYWORD($1)\n" if ($dbg_values > 1); 986 $type = 'N'; 987 988 } elsif ($cur =~ /^(\()/o) { 989 print "PAREN('$1')\n" if ($dbg_values > 1); 990 push(@av_paren_type, $av_pending); 991 $av_pending = '_'; 992 $type = 'N'; 993 994 } elsif ($cur =~ /^(\))/o) { 995 my $new_type = pop(@av_paren_type); 996 if ($new_type ne '_') { 997 $type = $new_type; 998 print "PAREN('$1') -> $type\n" 999 if ($dbg_values > 1); 1000 } else { 1001 print "PAREN('$1')\n" if ($dbg_values > 1); 1002 } 1003 1004 } elsif ($cur =~ /^($Ident)\s*\(/o) { 1005 print "FUNC($1)\n" if ($dbg_values > 1); 1006 $type = 'V'; 1007 $av_pending = 'V'; 1008 1009 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 1010 if (defined $2 && $type eq 'C' || $type eq 'T') { 1011 $av_pend_colon = 'B'; 1012 } elsif ($type eq 'E') { 1013 $av_pend_colon = 'L'; 1014 } 1015 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 1016 $type = 'V'; 1017 1018 } elsif ($cur =~ /^($Ident|$Constant)/o) { 1019 print "IDENT($1)\n" if ($dbg_values > 1); 1020 $type = 'V'; 1021 1022 } elsif ($cur =~ /^($Assignment)/o) { 1023 print "ASSIGN($1)\n" if ($dbg_values > 1); 1024 $type = 'N'; 1025 1026 } elsif ($cur =~/^(;|{|})/) { 1027 print "END($1)\n" if ($dbg_values > 1); 1028 $type = 'E'; 1029 $av_pend_colon = 'O'; 1030 1031 } elsif ($cur =~/^(,)/) { 1032 print "COMMA($1)\n" if ($dbg_values > 1); 1033 $type = 'C'; 1034 1035 } elsif ($cur =~ /^(\?)/o) { 1036 print "QUESTION($1)\n" if ($dbg_values > 1); 1037 $type = 'N'; 1038 1039 } elsif ($cur =~ /^(:)/o) { 1040 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 1041 1042 substr($var, length($res), 1, $av_pend_colon); 1043 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 1044 $type = 'E'; 1045 } else { 1046 $type = 'N'; 1047 } 1048 $av_pend_colon = 'O'; 1049 1050 } elsif ($cur =~ /^(\[)/o) { 1051 print "CLOSE($1)\n" if ($dbg_values > 1); 1052 $type = 'N'; 1053 1054 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 1055 my $variant; 1056 1057 print "OPV($1)\n" if ($dbg_values > 1); 1058 if ($type eq 'V') { 1059 $variant = 'B'; 1060 } else { 1061 $variant = 'U'; 1062 } 1063 1064 substr($var, length($res), 1, $variant); 1065 $type = 'N'; 1066 1067 } elsif ($cur =~ /^($Operators)/o) { 1068 print "OP($1)\n" if ($dbg_values > 1); 1069 if ($1 ne '++' && $1 ne '--') { 1070 $type = 'N'; 1071 } 1072 1073 } elsif ($cur =~ /(^.)/o) { 1074 print "C($1)\n" if ($dbg_values > 1); 1075 } 1076 if (defined $1) { 1077 $cur = substr($cur, length($1)); 1078 $res .= $type x length($1); 1079 } 1080 } 1081 1082 return ($res, $var); 1083} 1084 1085sub possible { 1086 my ($possible, $line) = @_; 1087 my $notPermitted = qr{(?: 1088 ^(?: 1089 $Modifier| 1090 $Storage| 1091 $Type| 1092 DEFINE_\S+ 1093 )$| 1094 ^(?: 1095 goto| 1096 return| 1097 case| 1098 else| 1099 asm|__asm__| 1100 do| 1101 \#| 1102 \#\# 1103 )(?:\s|$)| 1104 ^(?:typedef|struct|enum)\b 1105 )}x; 1106 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 1107 if ($possible !~ $notPermitted) { 1108 # Check for modifiers. 1109 $possible =~ s/\s*$Storage\s*//g; 1110 $possible =~ s/\s*$Sparse\s*//g; 1111 if ($possible =~ /^\s*$/) { 1112 1113 } elsif ($possible =~ /\s/) { 1114 $possible =~ s/\s*$Type\s*//g; 1115 for my $modifier (split(' ', $possible)) { 1116 if ($modifier !~ $notPermitted) { 1117 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1118 push(@modifierList, $modifier); 1119 } 1120 } 1121 1122 } else { 1123 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1124 push(@typeList, $possible); 1125 } 1126 build_types(); 1127 } else { 1128 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 1129 } 1130} 1131 1132my $prefix = ''; 1133 1134sub report { 1135 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { 1136 return 0; 1137 } 1138 my $line = $prefix . $_[0]; 1139 1140 $line = (split('\n', $line))[0] . "\n" if ($terse); 1141 1142 push(our @report, $line); 1143 1144 return 1; 1145} 1146sub report_dump { 1147 our @report; 1148} 1149sub ERROR { 1150 if (report("ERROR: $_[0]\n")) { 1151 our $clean = 0; 1152 our $cnt_error++; 1153 } 1154} 1155sub WARN { 1156 if (report("WARNING: $_[0]\n")) { 1157 our $clean = 0; 1158 our $cnt_warn++; 1159 } 1160} 1161 1162sub process { 1163 my $filename = shift; 1164 1165 my $linenr=0; 1166 my $prevline=""; 1167 my $prevrawline=""; 1168 my $stashline=""; 1169 my $stashrawline=""; 1170 1171 my $length; 1172 my $indent; 1173 my $previndent=0; 1174 my $stashindent=0; 1175 1176 our $clean = 1; 1177 my $signoff = 0; 1178 my $is_patch = 0; 1179 1180 our @report = (); 1181 our $cnt_lines = 0; 1182 our $cnt_error = 0; 1183 our $cnt_warn = 0; 1184 our $cnt_chk = 0; 1185 1186 # Trace the real file/line as we go. 1187 my $realfile = ''; 1188 my $realline = 0; 1189 my $realcnt = 0; 1190 my $here = ''; 1191 my $in_comment = 0; 1192 my $comment_edge = 0; 1193 my $first_line = 0; 1194 my $p1_prefix = ''; 1195 1196 my $prev_values = 'E'; 1197 1198 # suppression flags 1199 my %suppress_ifbraces; 1200 my %suppress_whiletrailers; 1201 my %suppress_export; 1202 1203 # Pre-scan the patch sanitizing the lines. 1204 1205 sanitise_line_reset(); 1206 my $line; 1207 foreach my $rawline (@rawlines) { 1208 $linenr++; 1209 $line = $rawline; 1210 1211 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1212 $realline=$1-1; 1213 if (defined $2) { 1214 $realcnt=$3+1; 1215 } else { 1216 $realcnt=1+1; 1217 } 1218 $in_comment = 0; 1219 1220 # Guestimate if this is a continuing comment. Run 1221 # the context looking for a comment "edge". If this 1222 # edge is a close comment then we must be in a comment 1223 # at context start. 1224 my $edge; 1225 my $cnt = $realcnt; 1226 for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 1227 next if (defined $rawlines[$ln - 1] && 1228 $rawlines[$ln - 1] =~ /^-/); 1229 $cnt--; 1230 #print "RAW<$rawlines[$ln - 1]>\n"; 1231 last if (!defined $rawlines[$ln - 1]); 1232 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1233 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1234 ($edge) = $1; 1235 last; 1236 } 1237 } 1238 if (defined $edge && $edge eq '*/') { 1239 $in_comment = 1; 1240 } 1241 1242 # Guestimate if this is a continuing comment. If this 1243 # is the start of a diff block and this line starts 1244 # ' *' then it is very likely a comment. 1245 if (!defined $edge && 1246 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1247 { 1248 $in_comment = 1; 1249 } 1250 1251 ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1252 sanitise_line_reset($in_comment); 1253 1254 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1255 # Standardise the strings and chars within the input to 1256 # simplify matching -- only bother with positive lines. 1257 $line = sanitise_line($rawline); 1258 } 1259 push(@lines, $line); 1260 1261 if ($realcnt > 1) { 1262 $realcnt-- if ($line =~ /^(?:\+| |$)/); 1263 } else { 1264 $realcnt = 0; 1265 } 1266 1267 #print "==>$rawline\n"; 1268 #print "-->$line\n"; 1269 } 1270 1271 $prefix = ''; 1272 1273 $realcnt = 0; 1274 $linenr = 0; 1275 foreach my $line (@lines) { 1276 $linenr++; 1277 1278 my $rawline = $rawlines[$linenr - 1]; 1279 1280#extract the line range in the file after the patch is applied 1281 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1282 $is_patch = 1; 1283 $first_line = $linenr + 1; 1284 $realline=$1-1; 1285 if (defined $2) { 1286 $realcnt=$3+1; 1287 } else { 1288 $realcnt=1+1; 1289 } 1290 annotate_reset(); 1291 $prev_values = 'E'; 1292 1293 %suppress_ifbraces = (); 1294 %suppress_whiletrailers = (); 1295 %suppress_export = (); 1296 next; 1297 1298# track the line number as we move through the hunk, note that 1299# new versions of GNU diff omit the leading space on completely 1300# blank context lines so we need to count that too. 1301 } elsif ($line =~ /^( |\+|$)/) { 1302 $realline++; 1303 $realcnt-- if ($realcnt != 0); 1304 1305 # Measure the line length and indent. 1306 ($length, $indent) = line_stats($rawline); 1307 1308 # Track the previous line. 1309 ($prevline, $stashline) = ($stashline, $line); 1310 ($previndent, $stashindent) = ($stashindent, $indent); 1311 ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1312 1313 #warn "line<$line>\n"; 1314 1315 } elsif ($realcnt == 1) { 1316 $realcnt--; 1317 } 1318 1319 my $hunk_line = ($realcnt != 0); 1320 1321#make up the handle for any error we report on this line 1322 $prefix = "$filename:$realline: " if ($emacs && $file); 1323 $prefix = "$filename:$linenr: " if ($emacs && !$file); 1324 1325 $here = "#$linenr: " if (!$file); 1326 $here = "#$realline: " if ($file); 1327 1328 # extract the filename as it passes 1329 if ($line =~ /^diff --git.*?(\S+)$/) { 1330 $realfile = $1; 1331 $realfile =~ s@^([^/]*)/@@; 1332 1333 } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1334 $realfile = $1; 1335 $realfile =~ s@^([^/]*)/@@; 1336 1337 $p1_prefix = $1; 1338 if (!$file && $tree && $p1_prefix ne '' && 1339 -e "$root/$p1_prefix") { 1340 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 1341 } 1342 1343 next; 1344 } 1345 1346 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 1347 1348 my $hereline = "$here\n$rawline\n"; 1349 my $herecurr = "$here\n$rawline\n"; 1350 my $hereprev = "$here\n$prevrawline\n$rawline\n"; 1351 1352 $cnt_lines++ if ($realcnt != 0); 1353 1354# Check for incorrect file permissions 1355 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 1356 my $permhere = $here . "FILE: $realfile\n"; 1357 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) { 1358 ERROR("do not set execute permissions for source files\n" . $permhere); 1359 } 1360 } 1361 1362# Accept git diff extended headers as valid patches 1363 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) { 1364 $is_patch = 1; 1365 } 1366 1367#check the patch for a signoff: 1368 if ($line =~ /^\s*signed-off-by:/i) { 1369 # This is a signoff, if ugly, so do not double report. 1370 $signoff++; 1371 if (!($line =~ /^\s*Signed-off-by:/)) { 1372 ERROR("The correct form is \"Signed-off-by\"\n" . 1373 $herecurr); 1374 } 1375 if ($line =~ /^\s*signed-off-by:\S/i) { 1376 ERROR("space required after Signed-off-by:\n" . 1377 $herecurr); 1378 } 1379 } 1380 1381# Check for wrappage within a valid hunk of the file 1382 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1383 ERROR("patch seems to be corrupt (line wrapped?)\n" . 1384 $herecurr) if (!$emitted_corrupt++); 1385 } 1386 1387# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1388 if (($realfile =~ /^$/ || $line =~ /^\+/) && 1389 $rawline !~ m/^$UTF8*$/) { 1390 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1391 1392 my $blank = copy_spacing($rawline); 1393 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1394 my $hereptr = "$hereline$ptr\n"; 1395 1396 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 1397 } 1398 1399# ignore non-hunk lines and lines being removed 1400 next if (!$hunk_line || $line =~ /^-/); 1401 1402# ignore files that are being periodically imported from Linux 1403 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//); 1404 1405#trailing whitespace 1406 if ($line =~ /^\+.*\015/) { 1407 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1408 ERROR("DOS line endings\n" . $herevet); 1409 1410 } elsif ($realfile =~ /^docs\/.+\.txt/ || 1411 $realfile =~ /^docs\/.+\.md/) { 1412 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) { 1413 # TODO: properly check we're in a code block 1414 # (surrounding text is 4-column aligned) 1415 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1416 ERROR("code blocks in documentation should have " . 1417 "empty lines with exactly 4 columns of " . 1418 "whitespace\n" . $herevet); 1419 } 1420 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1421 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1422 ERROR("trailing whitespace\n" . $herevet); 1423 $rpt_cleaners = 1; 1424 } 1425 1426# checks for trace-events files 1427 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) { 1428 if ($rawline =~ /%[-+ 0]*#/) { 1429 ERROR("Don't use '#' flag of printf format ('%#') in " . 1430 "trace-events, use '0x' prefix instead\n" . $herecurr); 1431 } else { 1432 my $hex = 1433 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/; 1434 1435 # don't consider groups splitted by [.:/ ], like 2A.20:12ab 1436 my $tmpline = $rawline; 1437 $tmpline =~ s/($hex[.:\/ ])+$hex//g; 1438 1439 if ($tmpline =~ /(?<!0x)$hex/) { 1440 ERROR("Hex numbers must be prefixed with '0x'\n" . 1441 $herecurr); 1442 } 1443 } 1444 } 1445 1446# check we are in a valid source file if not then ignore this hunk 1447 next if ($realfile !~ /$SrcFile/); 1448 1449#90 column limit 1450 if ($line =~ /^\+/ && 1451 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1452 $length > 80) 1453 { 1454 if ($length > 90) { 1455 ERROR("line over 90 characters\n" . $herecurr); 1456 } else { 1457 WARN("line over 80 characters\n" . $herecurr); 1458 } 1459 } 1460 1461# check for spaces before a quoted newline 1462 if ($rawline =~ /^.*\".*\s\\n/) { 1463 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr); 1464 } 1465 1466# check for adding lines without a newline. 1467 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1468 ERROR("adding a line without newline at end of file\n" . $herecurr); 1469 } 1470 1471# check for RCS/CVS revision markers 1472 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) { 1473 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1474 } 1475 1476# tabs are only allowed in assembly source code, and in 1477# some scripts we imported from other projects. 1478 next if ($realfile =~ /\.(s|S)$/); 1479 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/); 1480 1481 if ($rawline =~ /^\+.*\t/) { 1482 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1483 ERROR("code indent should never use tabs\n" . $herevet); 1484 $rpt_cleaners = 1; 1485 } 1486 1487# check we are in a valid C source file if not then ignore this hunk 1488 next if ($realfile !~ /\.(h|c|cpp)$/); 1489 1490# Check for potential 'bare' types 1491 my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 1492 $realline_next); 1493 if ($realcnt && $line =~ /.\s*\S/) { 1494 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1495 ctx_statement_block($linenr, $realcnt, 0); 1496 $stat =~ s/\n./\n /g; 1497 $cond =~ s/\n./\n /g; 1498 1499 # Find the real next line. 1500 $realline_next = $line_nr_next; 1501 if (defined $realline_next && 1502 (!defined $lines[$realline_next - 1] || 1503 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 1504 $realline_next++; 1505 } 1506 1507 my $s = $stat; 1508 $s =~ s/{.*$//s; 1509 1510 # Ignore goto labels. 1511 if ($s =~ /$Ident:\*$/s) { 1512 1513 # Ignore functions being called 1514 } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1515 1516 } elsif ($s =~ /^.\s*else\b/s) { 1517 1518 # declarations always start with types 1519 } 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) { 1520 my $type = $1; 1521 $type =~ s/\s+/ /g; 1522 possible($type, "A:" . $s); 1523 1524 # definitions in global scope can only start with types 1525 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1526 possible($1, "B:" . $s); 1527 } 1528 1529 # any (foo ... *) is a pointer cast, and foo is a type 1530 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1531 possible($1, "C:" . $s); 1532 } 1533 1534 # Check for any sort of function declaration. 1535 # int foo(something bar, other baz); 1536 # void (*store_gdt)(x86_descr_ptr *); 1537 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 1538 my ($name_len) = length($1); 1539 1540 my $ctx = $s; 1541 substr($ctx, 0, $name_len + 1, ''); 1542 $ctx =~ s/\)[^\)]*$//; 1543 1544 for my $arg (split(/\s*,\s*/, $ctx)) { 1545 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 1546 1547 possible($1, "D:" . $s); 1548 } 1549 } 1550 } 1551 1552 } 1553 1554# 1555# Checks which may be anchored in the context. 1556# 1557 1558# Check for switch () and associated case and default 1559# statements should be at the same indent. 1560 if ($line=~/\bswitch\s*\(.*\)/) { 1561 my $err = ''; 1562 my $sep = ''; 1563 my @ctx = ctx_block_outer($linenr, $realcnt); 1564 shift(@ctx); 1565 for my $ctx (@ctx) { 1566 my ($clen, $cindent) = line_stats($ctx); 1567 if ($ctx =~ /^\+\s*(case\s+|default:)/ && 1568 $indent != $cindent) { 1569 $err .= "$sep$ctx\n"; 1570 $sep = ''; 1571 } else { 1572 $sep = "[...]\n"; 1573 } 1574 } 1575 if ($err ne '') { 1576 ERROR("switch and case should be at the same indent\n$hereline$err"); 1577 } 1578 } 1579 1580# if/while/etc brace do not go on next line, unless defining a do while loop, 1581# or if that brace on the next line is for something else 1582 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1583 my $pre_ctx = "$1$2"; 1584 1585 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1586 my $ctx_cnt = $realcnt - $#ctx - 1; 1587 my $ctx = join("\n", @ctx); 1588 1589 my $ctx_ln = $linenr; 1590 my $ctx_skip = $realcnt; 1591 1592 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1593 defined $lines[$ctx_ln - 1] && 1594 $lines[$ctx_ln - 1] =~ /^-/)) { 1595 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1596 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1597 $ctx_ln++; 1598 } 1599 1600 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 1601 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1602 1603 # The length of the "previous line" is checked against 80 because it 1604 # includes the + at the beginning of the line (if the actual line has 1605 # 79 or 80 characters, it is no longer possible to add a space and an 1606 # opening brace there) 1607 if ($#ctx == 0 && $ctx !~ /{\s*/ && 1608 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ && 1609 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) { 1610 ERROR("that open brace { should be on the previous line\n" . 1611 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 1612 } 1613 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1614 $ctx =~ /\)\s*\;\s*$/ && 1615 defined $lines[$ctx_ln - 1]) 1616 { 1617 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 1618 if ($nindent > $indent) { 1619 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" . 1620 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 1621 } 1622 } 1623 } 1624 1625# 'do ... while (0/false)' only makes sense in macros, without trailing ';' 1626 if ($line =~ /while\s*\((0|false)\);/) { 1627 ERROR("suspicious ; after while (0)\n" . $herecurr); 1628 } 1629 1630# Check relative indent for conditionals and blocks. 1631 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 1632 my ($s, $c) = ($stat, $cond); 1633 1634 substr($s, 0, length($c), ''); 1635 1636 # Make sure we remove the line prefixes as we have 1637 # none on the first line, and are going to readd them 1638 # where necessary. 1639 $s =~ s/\n./\n/gs; 1640 1641 # Find out how long the conditional actually is. 1642 my @newlines = ($c =~ /\n/gs); 1643 my $cond_lines = 1 + $#newlines; 1644 1645 # We want to check the first line inside the block 1646 # starting at the end of the conditional, so remove: 1647 # 1) any blank line termination 1648 # 2) any opening brace { on end of the line 1649 # 3) any do (...) { 1650 my $continuation = 0; 1651 my $check = 0; 1652 $s =~ s/^.*\bdo\b//; 1653 $s =~ s/^\s*\{//; 1654 if ($s =~ s/^\s*\\//) { 1655 $continuation = 1; 1656 } 1657 if ($s =~ s/^\s*?\n//) { 1658 $check = 1; 1659 $cond_lines++; 1660 } 1661 1662 # Also ignore a loop construct at the end of a 1663 # preprocessor statement. 1664 if (($prevline =~ /^.\s*#\s*define\s/ || 1665 $prevline =~ /\\\s*$/) && $continuation == 0) { 1666 $check = 0; 1667 } 1668 1669 my $cond_ptr = -1; 1670 $continuation = 0; 1671 while ($cond_ptr != $cond_lines) { 1672 $cond_ptr = $cond_lines; 1673 1674 # If we see an #else/#elif then the code 1675 # is not linear. 1676 if ($s =~ /^\s*\#\s*(?:else|elif)/) { 1677 $check = 0; 1678 } 1679 1680 # Ignore: 1681 # 1) blank lines, they should be at 0, 1682 # 2) preprocessor lines, and 1683 # 3) labels. 1684 if ($continuation || 1685 $s =~ /^\s*?\n/ || 1686 $s =~ /^\s*#\s*?/ || 1687 $s =~ /^\s*$Ident\s*:/) { 1688 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 1689 if ($s =~ s/^.*?\n//) { 1690 $cond_lines++; 1691 } 1692 } 1693 } 1694 1695 my (undef, $sindent) = line_stats("+" . $s); 1696 my $stat_real = raw_line($linenr, $cond_lines); 1697 1698 # Check if either of these lines are modified, else 1699 # this is not this patch's fault. 1700 if (!defined($stat_real) || 1701 $stat !~ /^\+/ && $stat_real !~ /^\+/) { 1702 $check = 0; 1703 } 1704 if (defined($stat_real) && $cond_lines > 1) { 1705 $stat_real = "[...]\n$stat_real"; 1706 } 1707 1708 #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"; 1709 1710 if ($check && (($sindent % 4) != 0 || 1711 ($sindent <= $indent && $s ne ''))) { 1712 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 1713 } 1714 } 1715 1716 # Track the 'values' across context and added lines. 1717 my $opline = $line; $opline =~ s/^./ /; 1718 my ($curr_values, $curr_vars) = 1719 annotate_values($opline . "\n", $prev_values); 1720 $curr_values = $prev_values . $curr_values; 1721 if ($dbg_values) { 1722 my $outline = $opline; $outline =~ s/\t/ /g; 1723 print "$linenr > .$outline\n"; 1724 print "$linenr > $curr_values\n"; 1725 print "$linenr > $curr_vars\n"; 1726 } 1727 $prev_values = substr($curr_values, -1); 1728 1729#ignore lines not being added 1730 if ($line=~/^[^\+]/) {next;} 1731 1732# TEST: allow direct testing of the type matcher. 1733 if ($dbg_type) { 1734 if ($line =~ /^.\s*$Declare\s*$/) { 1735 ERROR("TEST: is type\n" . $herecurr); 1736 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 1737 ERROR("TEST: is not type ($1 is)\n". $herecurr); 1738 } 1739 next; 1740 } 1741# TEST: allow direct testing of the attribute matcher. 1742 if ($dbg_attr) { 1743 if ($line =~ /^.\s*$Modifier\s*$/) { 1744 ERROR("TEST: is attr\n" . $herecurr); 1745 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 1746 ERROR("TEST: is not attr ($1 is)\n". $herecurr); 1747 } 1748 next; 1749 } 1750 1751# check for initialisation to aggregates open brace on the next line 1752 if ($line =~ /^.\s*\{/ && 1753 $prevline =~ /(?:^|[^=])=\s*$/) { 1754 ERROR("that open brace { should be on the previous line\n" . $hereprev); 1755 } 1756 1757# 1758# Checks which are anchored on the added line. 1759# 1760 1761# check for malformed paths in #include statements (uses RAW line) 1762 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 1763 my $path = $1; 1764 if ($path =~ m{//}) { 1765 ERROR("malformed #include filename\n" . 1766 $herecurr); 1767 } 1768 } 1769 1770# no C99 // comments 1771 if ($line =~ m{//}) { 1772 ERROR("do not use C99 // comments\n" . $herecurr); 1773 } 1774 # Remove C99 comments. 1775 $line =~ s@//.*@@; 1776 $opline =~ s@//.*@@; 1777 1778# check for global initialisers. 1779 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 1780 ERROR("do not initialise globals to 0 or NULL\n" . 1781 $herecurr); 1782 } 1783# check for static initialisers. 1784 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 1785 ERROR("do not initialise statics to 0 or NULL\n" . 1786 $herecurr); 1787 } 1788 1789# * goes on variable not on type 1790 # (char*[ const]) 1791 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 1792 my ($from, $to) = ($1, $1); 1793 1794 # Should start with a space. 1795 $to =~ s/^(\S)/ $1/; 1796 # Should not end with a space. 1797 $to =~ s/\s+$//; 1798 # '*'s should not have spaces between. 1799 while ($to =~ s/\*\s+\*/\*\*/) { 1800 } 1801 1802 #print "from<$from> to<$to>\n"; 1803 if ($from ne $to) { 1804 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 1805 } 1806 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 1807 my ($from, $to, $ident) = ($1, $1, $2); 1808 1809 # Should start with a space. 1810 $to =~ s/^(\S)/ $1/; 1811 # Should not end with a space. 1812 $to =~ s/\s+$//; 1813 # '*'s should not have spaces between. 1814 while ($to =~ s/\*\s+\*/\*\*/) { 1815 } 1816 # Modifiers should have spaces. 1817 $to =~ s/(\b$Modifier$)/$1 /; 1818 1819 #print "from<$from> to<$to> ident<$ident>\n"; 1820 if ($from ne $to && $ident !~ /^$Modifier$/) { 1821 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 1822 } 1823 } 1824 1825# function brace can't be on same line, except for #defines of do while, 1826# or if closed on same line 1827 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and 1828 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) { 1829 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 1830 } 1831 1832# open braces for enum, union and struct go on the same line. 1833 if ($line =~ /^.\s*\{/ && 1834 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 1835 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 1836 } 1837 1838# missing space after union, struct or enum definition 1839 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 1840 ERROR("missing space after $1 definition\n" . $herecurr); 1841 } 1842 1843# check for spacing round square brackets; allowed: 1844# 1. with a type on the left -- int [] a; 1845# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1846# 3. inside a curly brace -- = { [0...10] = 5 } 1847# 4. after a comma -- [1] = 5, [2] = 6 1848# 5. in a macro definition -- #define abc(x) [x] = y 1849 while ($line =~ /(.*?\s)\[/g) { 1850 my ($where, $prefix) = ($-[1], $1); 1851 if ($prefix !~ /$Type\s+$/ && 1852 ($where != 0 || $prefix !~ /^.\s+$/) && 1853 $prefix !~ /{\s+$/ && 1854 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ && 1855 $prefix !~ /,\s+$/) { 1856 ERROR("space prohibited before open square bracket '['\n" . $herecurr); 1857 } 1858 } 1859 1860# check for spaces between functions and their parentheses. 1861 while ($line =~ /($Ident)\s+\(/g) { 1862 my $name = $1; 1863 my $ctx_before = substr($line, 0, $-[1]); 1864 my $ctx = "$ctx_before$name"; 1865 1866 # Ignore those directives where spaces _are_ permitted. 1867 if ($name =~ /^(?: 1868 if|for|while|switch|return|case| 1869 volatile|__volatile__|coroutine_fn| 1870 __attribute__|format|__extension__| 1871 asm|__asm__)$/x) 1872 { 1873 1874 # Ignore 'catch (...)' in C++ 1875 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) { 1876 1877 # cpp #define statements have non-optional spaces, ie 1878 # if there is a space between the name and the open 1879 # parenthesis it is simply not a parameter group. 1880 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 1881 1882 # cpp #elif statement condition may start with a ( 1883 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 1884 1885 # If this whole things ends with a type its most 1886 # likely a typedef for a function. 1887 } elsif ($ctx =~ /$Type$/) { 1888 1889 } else { 1890 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr); 1891 } 1892 } 1893# Check operator spacing. 1894 if (!($line=~/\#\s*include/)) { 1895 my $ops = qr{ 1896 <<=|>>=|<=|>=|==|!=| 1897 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 1898 =>|->|<<|>>|<|>|=|!|~| 1899 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 1900 \?|::|: 1901 }x; 1902 my @elements = split(/($ops|;)/, $opline); 1903 my $off = 0; 1904 1905 my $blank = copy_spacing($opline); 1906 1907 for (my $n = 0; $n < $#elements; $n += 2) { 1908 $off += length($elements[$n]); 1909 1910 # Pick up the preceding and succeeding characters. 1911 my $ca = substr($opline, 0, $off); 1912 my $cc = ''; 1913 if (length($opline) >= ($off + length($elements[$n + 1]))) { 1914 $cc = substr($opline, $off + length($elements[$n + 1])); 1915 } 1916 my $cb = "$ca$;$cc"; 1917 1918 my $a = ''; 1919 $a = 'V' if ($elements[$n] ne ''); 1920 $a = 'W' if ($elements[$n] =~ /\s$/); 1921 $a = 'C' if ($elements[$n] =~ /$;$/); 1922 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 1923 $a = 'O' if ($elements[$n] eq ''); 1924 $a = 'E' if ($ca =~ /^\s*$/); 1925 1926 my $op = $elements[$n + 1]; 1927 1928 my $c = ''; 1929 if (defined $elements[$n + 2]) { 1930 $c = 'V' if ($elements[$n + 2] ne ''); 1931 $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1932 $c = 'C' if ($elements[$n + 2] =~ /^$;/); 1933 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 1934 $c = 'O' if ($elements[$n + 2] eq ''); 1935 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 1936 } else { 1937 $c = 'E'; 1938 } 1939 1940 my $ctx = "${a}x${c}"; 1941 1942 my $at = "(ctx:$ctx)"; 1943 1944 my $ptr = substr($blank, 0, $off) . "^"; 1945 my $hereptr = "$hereline$ptr\n"; 1946 1947 # Pull out the value of this operator. 1948 my $op_type = substr($curr_values, $off + 1, 1); 1949 1950 # Get the full operator variant. 1951 my $opv = $op . substr($curr_vars, $off, 1); 1952 1953 # Ignore operators passed as parameters. 1954 if ($op_type ne 'V' && 1955 $ca =~ /\s$/ && $cc =~ /^\s*,/) { 1956 1957# # Ignore comments 1958# } elsif ($op =~ /^$;+$/) { 1959 1960 # ; should have either the end of line or a space or \ after it 1961 } elsif ($op eq ';') { 1962 if ($ctx !~ /.x[WEBC]/ && 1963 $cc !~ /^\\/ && $cc !~ /^;/) { 1964 ERROR("space required after that '$op' $at\n" . $hereptr); 1965 } 1966 1967 # // is a comment 1968 } elsif ($op eq '//') { 1969 1970 # Ignore : used in class declaration in C++ 1971 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ && 1972 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) { 1973 1974 # No spaces for: 1975 # -> 1976 # : when part of a bitfield 1977 } elsif ($op eq '->' || $opv eq ':B') { 1978 if ($ctx =~ /Wx.|.xW/) { 1979 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 1980 } 1981 1982 # , must have a space on the right. 1983 # not required when having a single },{ on one line 1984 } elsif ($op eq ',') { 1985 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ && 1986 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") { 1987 ERROR("space required after that '$op' $at\n" . $hereptr); 1988 } 1989 1990 # '*' as part of a type definition -- reported already. 1991 } elsif ($opv eq '*_') { 1992 #warn "'*' is part of type\n"; 1993 1994 # unary operators should have a space before and 1995 # none after. May be left adjacent to another 1996 # unary operator, or a cast 1997 } elsif ($op eq '!' || $op eq '~' || 1998 $opv eq '*U' || $opv eq '-U' || 1999 $opv eq '&U' || $opv eq '&&U') { 2000 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) { 2001 # '~' used as a name of Destructor 2002 2003 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2004 ERROR("space required before that '$op' $at\n" . $hereptr); 2005 } 2006 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2007 # A unary '*' may be const 2008 2009 } elsif ($ctx =~ /.xW/) { 2010 ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2011 } 2012 2013 # unary ++ and unary -- are allowed no space on one side. 2014 } elsif ($op eq '++' or $op eq '--') { 2015 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2016 ERROR("space required one side of that '$op' $at\n" . $hereptr); 2017 } 2018 if ($ctx =~ /Wx[BE]/ || 2019 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2020 ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2021 } 2022 if ($ctx =~ /ExW/) { 2023 ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2024 } 2025 2026 # A colon needs no spaces before when it is 2027 # terminating a case value or a label. 2028 } elsif ($opv eq ':C' || $opv eq ':L') { 2029 if ($ctx =~ /Wx./) { 2030 ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2031 } 2032 2033 # All the others need spaces both sides. 2034 } elsif ($ctx !~ /[EWC]x[CWE]/) { 2035 my $ok = 0; 2036 2037 if ($realfile =~ /\.cpp|\.h$/) { 2038 # Ignore template arguments <...> in C++ 2039 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) { 2040 $ok = 1; 2041 } 2042 2043 # Ignore :: in C++ 2044 if ($op eq '::') { 2045 $ok = 1; 2046 } 2047 } 2048 2049 # Ignore email addresses <foo@bar> 2050 if (($op eq '<' && 2051 $cc =~ /^\S+\@\S+>/) || 2052 ($op eq '>' && 2053 $ca =~ /<\S+\@\S+$/)) 2054 { 2055 $ok = 1; 2056 } 2057 2058 # Ignore ?: 2059 if (($opv eq ':O' && $ca =~ /\?$/) || 2060 ($op eq '?' && $cc =~ /^:/)) { 2061 $ok = 1; 2062 } 2063 2064 if ($ok == 0) { 2065 ERROR("spaces required around that '$op' $at\n" . $hereptr); 2066 } 2067 } 2068 $off += length($elements[$n + 1]); 2069 } 2070 } 2071 2072#need space before brace following if, while, etc 2073 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 2074 $line =~ /do\{/) { 2075 ERROR("space required before the open brace '{'\n" . $herecurr); 2076 } 2077 2078# closing brace should have a space following it when it has anything 2079# on the line 2080 if ($line =~ /}(?!(?:,|;|\)))\S/) { 2081 ERROR("space required after that close brace '}'\n" . $herecurr); 2082 } 2083 2084# check spacing on square brackets 2085 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2086 ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 2087 } 2088 if ($line =~ /\s\]/) { 2089 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 2090 } 2091 2092# check spacing on parentheses 2093 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 2094 $line !~ /for\s*\(\s+;/) { 2095 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 2096 } 2097 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2098 $line !~ /for\s*\(.*;\s+\)/ && 2099 $line !~ /:\s+\)/) { 2100 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 2101 } 2102 2103# Return is not a function. 2104 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2105 my $spacing = $1; 2106 my $value = $2; 2107 2108 # Flatten any parentheses 2109 $value =~ s/\(/ \(/g; 2110 $value =~ s/\)/\) /g; 2111 while ($value =~ s/\[[^\{\}]*\]/1/ || 2112 $value !~ /(?:$Ident|-?$Constant)\s* 2113 $Compare\s* 2114 (?:$Ident|-?$Constant)/x && 2115 $value =~ s/\([^\(\)]*\)/1/) { 2116 } 2117#print "value<$value>\n"; 2118 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 2119 ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2120 2121 } elsif ($spacing !~ /\s+/) { 2122 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2123 } 2124 } 2125# Return of what appears to be an errno should normally be -'ve 2126 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 2127 my $name = $1; 2128 if ($name ne 'EOF' && $name ne 'ERROR') { 2129 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr); 2130 } 2131 } 2132 2133# Need a space before open parenthesis after if, while etc 2134 if ($line=~/\b(if|while|for|switch)\(/) { 2135 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2136 } 2137 2138# Check for illegal assignment in if conditional -- and check for trailing 2139# statements after the conditional. 2140 if ($line =~ /do\s*(?!{)/) { 2141 my ($stat_next) = ctx_statement_block($line_nr_next, 2142 $remain_next, $off_next); 2143 $stat_next =~ s/\n./\n /g; 2144 ##print "stat<$stat> stat_next<$stat_next>\n"; 2145 2146 if ($stat_next =~ /^\s*while\b/) { 2147 # If the statement carries leading newlines, 2148 # then count those as offsets. 2149 my ($whitespace) = 2150 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2151 my $offset = 2152 statement_rawlines($whitespace) - 1; 2153 2154 $suppress_whiletrailers{$line_nr_next + 2155 $offset} = 1; 2156 } 2157 } 2158 if (!defined $suppress_whiletrailers{$linenr} && 2159 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2160 my ($s, $c) = ($stat, $cond); 2161 2162 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2163 ERROR("do not use assignment in if condition\n" . $herecurr); 2164 } 2165 2166 # Find out what is on the end of the line after the 2167 # conditional. 2168 substr($s, 0, length($c), ''); 2169 $s =~ s/\n.*//g; 2170 $s =~ s/$;//g; # Remove any comments 2171 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 2172 $c !~ /}\s*while\s*/) 2173 { 2174 # Find out how long the conditional actually is. 2175 my @newlines = ($c =~ /\n/gs); 2176 my $cond_lines = 1 + $#newlines; 2177 my $stat_real = ''; 2178 2179 $stat_real = raw_line($linenr, $cond_lines) 2180 . "\n" if ($cond_lines); 2181 if (defined($stat_real) && $cond_lines > 1) { 2182 $stat_real = "[...]\n$stat_real"; 2183 } 2184 2185 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 2186 } 2187 } 2188 2189# Check for bitwise tests written as boolean 2190 if ($line =~ / 2191 (?: 2192 (?:\[|\(|\&\&|\|\|) 2193 \s*0[xX][0-9]+\s* 2194 (?:\&\&|\|\|) 2195 | 2196 (?:\&\&|\|\|) 2197 \s*0[xX][0-9]+\s* 2198 (?:\&\&|\|\||\)|\]) 2199 )/x) 2200 { 2201 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 2202 } 2203 2204# if and else should not have general statements after it 2205 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 2206 my $s = $1; 2207 $s =~ s/$;//g; # Remove any comments 2208 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 2209 ERROR("trailing statements should be on next line\n" . $herecurr); 2210 } 2211 } 2212# if should not continue a brace 2213 if ($line =~ /}\s*if\b/) { 2214 ERROR("trailing statements should be on next line\n" . 2215 $herecurr); 2216 } 2217# case and default should not have general statements after them 2218 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2219 $line !~ /\G(?: 2220 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2221 \s*return\s+ 2222 )/xg) 2223 { 2224 ERROR("trailing statements should be on next line\n" . $herecurr); 2225 } 2226 2227 # Check for }<nl>else {, these must be at the same 2228 # indent level to be relevant to each other. 2229 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 2230 $previndent == $indent) { 2231 ERROR("else should follow close brace '}'\n" . $hereprev); 2232 } 2233 2234 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2235 $previndent == $indent) { 2236 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2237 2238 # Find out what is on the end of the line after the 2239 # conditional. 2240 substr($s, 0, length($c), ''); 2241 $s =~ s/\n.*//g; 2242 2243 if ($s =~ /^\s*;/) { 2244 ERROR("while should follow close brace '}'\n" . $hereprev); 2245 } 2246 } 2247 2248#studly caps, commented out until figure out how to distinguish between use of existing and adding new 2249# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 2250# print "No studly caps, use _\n"; 2251# print "$herecurr"; 2252# $clean = 0; 2253# } 2254 2255#no spaces allowed after \ in define 2256 if ($line=~/\#\s*define.*\\\s$/) { 2257 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr); 2258 } 2259 2260# multi-statement macros should be enclosed in a do while loop, grab the 2261# first statement and ensure its the whole macro if its not enclosed 2262# in a known good container 2263 if ($realfile !~ m@/vmlinux.lds.h$@ && 2264 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2265 my $ln = $linenr; 2266 my $cnt = $realcnt; 2267 my ($off, $dstat, $dcond, $rest); 2268 my $ctx = ''; 2269 2270 my $args = defined($1); 2271 2272 # Find the end of the macro and limit our statement 2273 # search to that. 2274 while ($cnt > 0 && defined $lines[$ln - 1] && 2275 $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2276 { 2277 $ctx .= $rawlines[$ln - 1] . "\n"; 2278 $cnt-- if ($lines[$ln - 1] !~ /^-/); 2279 $ln++; 2280 } 2281 $ctx .= $rawlines[$ln - 1]; 2282 2283 ($dstat, $dcond, $ln, $cnt, $off) = 2284 ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2285 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2286 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2287 2288 # Extract the remainder of the define (if any) and 2289 # rip off surrounding spaces, and trailing \'s. 2290 $rest = ''; 2291 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2292 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2293 if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2294 $rest .= substr($lines[$ln - 1], $off) . "\n"; 2295 $cnt--; 2296 } 2297 $ln++; 2298 $off = 0; 2299 } 2300 $rest =~ s/\\\n.//g; 2301 $rest =~ s/^\s*//s; 2302 $rest =~ s/\s*$//s; 2303 2304 # Clean up the original statement. 2305 if ($args) { 2306 substr($dstat, 0, length($dcond), ''); 2307 } else { 2308 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2309 } 2310 $dstat =~ s/$;//g; 2311 $dstat =~ s/\\\n.//g; 2312 $dstat =~ s/^\s*//s; 2313 $dstat =~ s/\s*$//s; 2314 2315 # Flatten any parentheses and braces 2316 while ($dstat =~ s/\([^\(\)]*\)/1/ || 2317 $dstat =~ s/\{[^\{\}]*\}/1/ || 2318 $dstat =~ s/\[[^\{\}]*\]/1/) 2319 { 2320 } 2321 2322 my $exceptions = qr{ 2323 $Declare| 2324 module_param_named| 2325 MODULE_PARAM_DESC| 2326 DECLARE_PER_CPU| 2327 DEFINE_PER_CPU| 2328 __typeof__\(| 2329 union| 2330 struct| 2331 \.$Ident\s*=\s*| 2332 ^\"|\"$ 2333 }x; 2334 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 2335 if ($rest ne '' && $rest ne ',') { 2336 if ($rest !~ /while\s*\(/ && 2337 $dstat !~ /$exceptions/) 2338 { 2339 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2340 } 2341 2342 } elsif ($ctx !~ /;/) { 2343 if ($dstat ne '' && 2344 $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2345 $dstat !~ /$exceptions/ && 2346 $dstat !~ /^\.$Ident\s*=/ && 2347 $dstat =~ /$Operators/) 2348 { 2349 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2350 } 2351 } 2352 } 2353 2354# check for missing bracing round if etc 2355 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) { 2356 my ($level, $endln, @chunks) = 2357 ctx_statement_full($linenr, $realcnt, 1); 2358 if ($dbg_adv_apw) { 2359 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2360 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n" 2361 if $#chunks >= 1; 2362 } 2363 if ($#chunks >= 0 && $level == 0) { 2364 my $allowed = 0; 2365 my $seen = 0; 2366 my $herectx = $here . "\n"; 2367 my $ln = $linenr - 1; 2368 for my $chunk (@chunks) { 2369 my ($cond, $block) = @{$chunk}; 2370 2371 # If the condition carries leading newlines, then count those as offsets. 2372 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2373 my $offset = statement_rawlines($whitespace) - 1; 2374 2375 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2376 2377 # We have looked at and allowed this specific line. 2378 $suppress_ifbraces{$ln + $offset} = 1; 2379 2380 $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2381 $ln += statement_rawlines($block) - 1; 2382 2383 substr($block, 0, length($cond), ''); 2384 2385 my $spaced_block = $block; 2386 $spaced_block =~ s/\n\+/ /g; 2387 2388 $seen++ if ($spaced_block =~ /^\s*\{/); 2389 2390 print "APW: cond<$cond> block<$block> allowed<$allowed>\n" 2391 if $dbg_adv_apw; 2392 if (statement_lines($cond) > 1) { 2393 print "APW: ALLOWED: cond<$cond>\n" 2394 if $dbg_adv_apw; 2395 $allowed = 1; 2396 } 2397 if ($block =~/\b(?:if|for|while)\b/) { 2398 print "APW: ALLOWED: block<$block>\n" 2399 if $dbg_adv_apw; 2400 $allowed = 1; 2401 } 2402 if (statement_block_size($block) > 1) { 2403 print "APW: ALLOWED: lines block<$block>\n" 2404 if $dbg_adv_apw; 2405 $allowed = 1; 2406 } 2407 } 2408 if ($seen != ($#chunks + 1)) { 2409 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx); 2410 } 2411 } 2412 } 2413 if (!defined $suppress_ifbraces{$linenr - 1} && 2414 $line =~ /\b(if|while|for|else)\b/ && 2415 $line !~ /\#\s*if/ && 2416 $line !~ /\#\s*else/) { 2417 my $allowed = 0; 2418 2419 # Check the pre-context. 2420 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2421 my $pre = $1; 2422 2423 if ($line !~ /else/) { 2424 print "APW: ALLOWED: pre<$pre> line<$line>\n" 2425 if $dbg_adv_apw; 2426 $allowed = 1; 2427 } 2428 } 2429 2430 my ($level, $endln, @chunks) = 2431 ctx_statement_full($linenr, $realcnt, $-[0]); 2432 2433 # Check the condition. 2434 my ($cond, $block) = @{$chunks[0]}; 2435 print "CHECKING<$linenr> cond<$cond> block<$block>\n" 2436 if $dbg_adv_checking; 2437 if (defined $cond) { 2438 substr($block, 0, length($cond), ''); 2439 } 2440 if (statement_lines($cond) > 1) { 2441 print "APW: ALLOWED: cond<$cond>\n" 2442 if $dbg_adv_apw; 2443 $allowed = 1; 2444 } 2445 if ($block =~/\b(?:if|for|while)\b/) { 2446 print "APW: ALLOWED: block<$block>\n" 2447 if $dbg_adv_apw; 2448 $allowed = 1; 2449 } 2450 if (statement_block_size($block) > 1) { 2451 print "APW: ALLOWED: lines block<$block>\n" 2452 if $dbg_adv_apw; 2453 $allowed = 1; 2454 } 2455 # Check the post-context. 2456 if (defined $chunks[1]) { 2457 my ($cond, $block) = @{$chunks[1]}; 2458 if (defined $cond) { 2459 substr($block, 0, length($cond), ''); 2460 } 2461 if ($block =~ /^\s*\{/) { 2462 print "APW: ALLOWED: chunk-1 block<$block>\n" 2463 if $dbg_adv_apw; 2464 $allowed = 1; 2465 } 2466 } 2467 print "DCS: level=$level block<$block> allowed=$allowed\n" 2468 if $dbg_adv_dcs; 2469 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) { 2470 my $herectx = $here . "\n";; 2471 my $cnt = statement_rawlines($block); 2472 2473 for (my $n = 0; $n < $cnt; $n++) { 2474 $herectx .= raw_line($linenr, $n) . "\n";; 2475 } 2476 2477 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx); 2478 } 2479 } 2480 2481# no volatiles please 2482 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 2483 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ && 2484 $line !~ /sig_atomic_t/ && 2485 !ctx_has_comment($first_line, $linenr)) { 2486 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr; 2487 ERROR($msg); 2488 } 2489 2490# warn about #if 0 2491 if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2492 ERROR("if this code is redundant consider removing it\n" . 2493 $herecurr); 2494 } 2495 2496# check for needless g_free() checks 2497 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2498 my $expr = $1; 2499 if ($line =~ /\bg_free\(\Q$expr\E\);/) { 2500 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev); 2501 } 2502 } 2503 2504# warn about #ifdefs in C files 2505# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 2506# print "#ifdef in C files should be avoided\n"; 2507# print "$herecurr"; 2508# $clean = 0; 2509# } 2510 2511# warn about spacing in #ifdefs 2512 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 2513 ERROR("exactly one space required after that #$1\n" . $herecurr); 2514 } 2515# check for memory barriers without a comment. 2516 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 2517 if (!ctx_has_comment($first_line, $linenr)) { 2518 ERROR("memory barrier without comment\n" . $herecurr); 2519 } 2520 } 2521# check of hardware specific defines 2522# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases 2523# where they might be necessary. 2524 if ($line =~ m@^.\s*\#\s*if.*\b__@) { 2525 WARN("architecture specific defines should be avoided\n" . $herecurr); 2526 } 2527 2528# Check that the storage class is at the beginning of a declaration 2529 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 2530 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr) 2531 } 2532 2533# check the location of the inline attribute, that it is between 2534# storage class and type. 2535 if ($line =~ /\b$Type\s+$Inline\b/ || 2536 $line =~ /\b$Inline\s+$Storage\b/) { 2537 ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2538 } 2539 2540# check for sizeof(&) 2541 if ($line =~ /\bsizeof\s*\(\s*\&/) { 2542 ERROR("sizeof(& should be avoided\n" . $herecurr); 2543 } 2544 2545# check for new externs in .c files. 2546 if ($realfile =~ /\.c$/ && defined $stat && 2547 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2548 { 2549 my $function_name = $1; 2550 my $paren_space = $2; 2551 2552 my $s = $stat; 2553 if (defined $cond) { 2554 substr($s, 0, length($cond), ''); 2555 } 2556 if ($s =~ /^\s*;/ && 2557 $function_name ne 'uninitialized_var') 2558 { 2559 ERROR("externs should be avoided in .c files\n" . $herecurr); 2560 } 2561 2562 if ($paren_space =~ /\n/) { 2563 ERROR("arguments for function declarations should follow identifier\n" . $herecurr); 2564 } 2565 2566 } elsif ($realfile =~ /\.c$/ && defined $stat && 2567 $stat =~ /^.\s*extern\s+/) 2568 { 2569 ERROR("externs should be avoided in .c files\n" . $herecurr); 2570 } 2571 2572# check for pointless casting of g_malloc return 2573 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) { 2574 if ($2 == 'm') { 2575 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr); 2576 } else { 2577 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr); 2578 } 2579 } 2580 2581# check for gcc specific __FUNCTION__ 2582 if ($line =~ /__FUNCTION__/) { 2583 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 2584 } 2585 2586# recommend qemu_strto* over strto* for numeric conversions 2587 if ($line =~ /\b(strto[^kd].*?)\s*\(/) { 2588 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr); 2589 } 2590# recommend sigaction over signal for portability, when establishing a handler 2591 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) { 2592 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr); 2593 } 2594# check for module_init(), use category-specific init macros explicitly please 2595 if ($line =~ /^module_init\s*\(/) { 2596 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr); 2597 } 2598# check for various ops structs, ensure they are const. 2599 my $struct_ops = qr{AIOCBInfo| 2600 BdrvActionOps| 2601 BlockDevOps| 2602 BlockJobDriver| 2603 DisplayChangeListenerOps| 2604 GraphicHwOps| 2605 IDEDMAOps| 2606 KVMCapabilityInfo| 2607 MemoryRegionIOMMUOps| 2608 MemoryRegionOps| 2609 MemoryRegionPortio| 2610 QEMUFileOps| 2611 SCSIBusInfo| 2612 SCSIReqOps| 2613 Spice[A-Z][a-zA-Z0-9]*Interface| 2614 USBDesc[A-Z][a-zA-Z0-9]*| 2615 VhostOps| 2616 VMStateDescription| 2617 VMStateInfo}x; 2618 if ($line !~ /\bconst\b/ && 2619 $line =~ /\b($struct_ops)\b.*=/) { 2620 ERROR("initializer for struct $1 should normally be const\n" . 2621 $herecurr); 2622 } 2623 2624# check for %L{u,d,i} in strings 2625 my $string; 2626 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 2627 $string = substr($rawline, $-[1], $+[1] - $-[1]); 2628 $string =~ s/%%/__/g; 2629 if ($string =~ /(?<!%)%L[udi]/) { 2630 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 2631 last; 2632 } 2633 } 2634 2635# QEMU specific tests 2636 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) { 2637 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr); 2638 } 2639 2640# Qemu error function tests 2641 2642 # Find newlines in error messages 2643 my $qemu_error_funcs = qr{error_setg| 2644 error_setg_errno| 2645 error_setg_win32| 2646 error_setg_file_open| 2647 error_set| 2648 error_prepend| 2649 warn_reportf_err| 2650 error_reportf_err| 2651 error_vreport| 2652 warn_vreport| 2653 info_vreport| 2654 error_report| 2655 warn_report| 2656 info_report}x; 2657 2658 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) { 2659 ERROR("Error messages should not contain newlines\n" . $herecurr); 2660 } 2661 2662 # Continue checking for error messages that contains newlines. This 2663 # check handles cases where string literals are spread over multiple lines. 2664 # Example: 2665 # error_report("Error msg line #1" 2666 # "Error msg line #2\n"); 2667 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"}; 2668 my $continued_str_literal = qr{\+\s*\".*\"}; 2669 2670 if ($rawline =~ /$quoted_newline_regex/) { 2671 # Backtrack to first line that does not contain only a quoted literal 2672 # and assume that it is the start of the statement. 2673 my $i = $linenr - 2; 2674 2675 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) { 2676 $i--; 2677 } 2678 2679 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) { 2680 ERROR("Error messages should not contain newlines\n" . $herecurr); 2681 } 2682 } 2683 2684# check for non-portable libc calls that have portable alternatives in QEMU 2685 if ($line =~ /\bffs\(/) { 2686 ERROR("use ctz32() instead of ffs()\n" . $herecurr); 2687 } 2688 if ($line =~ /\bffsl\(/) { 2689 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr); 2690 } 2691 if ($line =~ /\bffsll\(/) { 2692 ERROR("use ctz64() instead of ffsll()\n" . $herecurr); 2693 } 2694 if ($line =~ /\bbzero\(/) { 2695 ERROR("use memset() instead of bzero()\n" . $herecurr); 2696 } 2697 my $non_exit_glib_asserts = qr{g_assert_cmpstr| 2698 g_assert_cmpint| 2699 g_assert_cmpuint| 2700 g_assert_cmphex| 2701 g_assert_cmpfloat| 2702 g_assert_true| 2703 g_assert_false| 2704 g_assert_nonnull| 2705 g_assert_null| 2706 g_assert_no_error| 2707 g_assert_error| 2708 g_test_assert_expected_messages| 2709 g_test_trap_assert_passed| 2710 g_test_trap_assert_stdout| 2711 g_test_trap_assert_stdout_unmatched| 2712 g_test_trap_assert_stderr| 2713 g_test_trap_assert_stderr_unmatched}x; 2714 if ($realfile !~ /^tests\// && 2715 $line =~ /\b(?:$non_exit_glib_asserts)\(/) { 2716 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr); 2717 } 2718 } 2719 2720 # If we have no input at all, then there is nothing to report on 2721 # so just keep quiet. 2722 if ($#rawlines == -1) { 2723 exit(0); 2724 } 2725 2726 # In mailback mode only produce a report in the negative, for 2727 # things that appear to be patches. 2728 if ($mailback && ($clean == 1 || !$is_patch)) { 2729 exit(0); 2730 } 2731 2732 # This is not a patch, and we are are in 'no-patch' mode so 2733 # just keep quiet. 2734 if (!$chk_patch && !$is_patch) { 2735 exit(0); 2736 } 2737 2738 if (!$is_patch) { 2739 ERROR("Does not appear to be a unified-diff format patch\n"); 2740 } 2741 if ($is_patch && $chk_signoff && $signoff == 0) { 2742 ERROR("Missing Signed-off-by: line(s)\n"); 2743 } 2744 2745 print report_dump(); 2746 if ($summary && !($clean == 1 && $quiet == 1)) { 2747 print "$filename " if ($summary_file); 2748 print "total: $cnt_error errors, $cnt_warn warnings, " . 2749 "$cnt_lines lines checked\n"; 2750 print "\n" if ($quiet == 0); 2751 } 2752 2753 if ($quiet == 0) { 2754 # If there were whitespace errors which cleanpatch can fix 2755 # then suggest that. 2756# if ($rpt_cleaners) { 2757# print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 2758# print " scripts/cleanfile\n\n"; 2759# } 2760 } 2761 2762 if ($clean == 1 && $quiet == 0) { 2763 print "$vname has no obvious style problems and is ready for submission.\n" 2764 } 2765 if ($clean == 0 && $quiet == 0) { 2766 print "$vname has style problems, please review. If any of these errors\n"; 2767 print "are false positives report them to the maintainer, see\n"; 2768 print "CHECKPATCH in MAINTAINERS.\n"; 2769 } 2770 2771 return ($no_warnings ? $clean : $cnt_error == 0); 2772} 2773