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