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