checkpatch.pl revision c2fdda0dfbe85ad5d68d4799ff7c5af89db8ac19
1#!/usr/bin/perl -w 2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit) 3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) 4# (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc) 5# Licensed under the terms of the GNU GPL License version 2 6 7use strict; 8 9my $P = $0; 10$P =~ s@.*/@@g; 11 12my $V = '0.13'; 13 14use Getopt::Long qw(:config no_auto_abbrev); 15 16my $quiet = 0; 17my $tree = 1; 18my $chk_signoff = 1; 19my $chk_patch = 1; 20my $tst_type = 0; 21my $emacs = 0; 22my $terse = 0; 23my $file = 0; 24my $check = 0; 25my $summary = 1; 26my $mailback = 0; 27my $root; 28my %debug; 29GetOptions( 30 'q|quiet+' => \$quiet, 31 'tree!' => \$tree, 32 'signoff!' => \$chk_signoff, 33 'patch!' => \$chk_patch, 34 'test-type!' => \$tst_type, 35 'emacs!' => \$emacs, 36 'terse!' => \$terse, 37 'file!' => \$file, 38 'subjective!' => \$check, 39 'strict!' => \$check, 40 'root=s' => \$root, 41 'summary!' => \$summary, 42 'mailback!' => \$mailback, 43 'debug=s' => \%debug, 44) or exit; 45 46my $exit = 0; 47 48if ($#ARGV < 0) { 49 print "usage: $P [options] patchfile\n"; 50 print "version: $V\n"; 51 print "options: -q => quiet\n"; 52 print " --no-tree => run without a kernel tree\n"; 53 print " --terse => one line per report\n"; 54 print " --emacs => emacs compile window format\n"; 55 print " --file => check a source file\n"; 56 print " --strict => enable more subjective tests\n"; 57 print " --root => path to the kernel tree root\n"; 58 exit(1); 59} 60 61my $dbg_values = 0; 62my $dbg_possible = 0; 63for my $key (keys %debug) { 64 eval "\${dbg_$key} = '$debug{$key}';" 65} 66 67if ($terse) { 68 $emacs = 1; 69 $quiet++; 70} 71 72if ($tree) { 73 if (defined $root) { 74 if (!top_of_kernel_tree($root)) { 75 die "$P: $root: --root does not point at a valid tree\n"; 76 } 77 } else { 78 if (top_of_kernel_tree('.')) { 79 $root = '.'; 80 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 81 top_of_kernel_tree($1)) { 82 $root = $1; 83 } 84 } 85 86 if (!defined $root) { 87 print "Must be run from the top-level dir. of a kernel tree\n"; 88 exit(2); 89 } 90} 91 92my $emitted_corrupt = 0; 93 94our $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; 95our $Storage = qr{extern|static|asmlinkage}; 96our $Sparse = qr{ 97 __user| 98 __kernel| 99 __force| 100 __iomem| 101 __must_check| 102 __init_refok| 103 __kprobes| 104 fastcall 105 }x; 106our $Attribute = qr{ 107 const| 108 __read_mostly| 109 __kprobes| 110 __(?:mem|cpu|dev|)(?:initdata|init) 111 }x; 112our $Inline = qr{inline|__always_inline|noinline}; 113our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 114our $Lval = qr{$Ident(?:$Member)*}; 115 116our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 117our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 118our $Operators = qr{ 119 <=|>=|==|!=| 120 =>|->|<<|>>|<|>|!|~| 121 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 122 }x; 123 124our $NonptrType; 125our $Type; 126our $Declare; 127 128our @typeList = ( 129 qr{void}, 130 qr{char}, 131 qr{short}, 132 qr{int}, 133 qr{long}, 134 qr{unsigned}, 135 qr{float}, 136 qr{double}, 137 qr{bool}, 138 qr{long\s+int}, 139 qr{long\s+long}, 140 qr{long\s+long\s+int}, 141 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)}, 142 qr{struct\s+$Ident}, 143 qr{union\s+$Ident}, 144 qr{enum\s+$Ident}, 145 qr{${Ident}_t}, 146 qr{${Ident}_handler}, 147 qr{${Ident}_handler_fn}, 148); 149 150sub build_types { 151 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)"; 152 $NonptrType = qr{ 153 \b 154 (?:const\s+)? 155 (?:unsigned\s+)? 156 $all 157 (?:\s+$Sparse|\s+const)* 158 \b 159 }x; 160 $Type = qr{ 161 \b$NonptrType\b 162 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? 163 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)* 164 }x; 165 $Declare = qr{(?:$Storage\s+)?$Type}; 166} 167build_types(); 168 169$chk_signoff = 0 if ($file); 170 171my @dep_includes = (); 172my @dep_functions = (); 173my $removal = "Documentation/feature-removal-schedule.txt"; 174if ($tree && -f "$root/$removal") { 175 open(REMOVE, "<$root/$removal") || 176 die "$P: $removal: open failed - $!\n"; 177 while (<REMOVE>) { 178 if (/^Check:\s+(.*\S)/) { 179 for my $entry (split(/[, ]+/, $1)) { 180 if ($entry =~ m@include/(.*)@) { 181 push(@dep_includes, $1); 182 183 } elsif ($entry !~ m@/@) { 184 push(@dep_functions, $entry); 185 } 186 } 187 } 188 } 189} 190 191my @rawlines = (); 192my @lines = (); 193my $vname; 194for my $filename (@ARGV) { 195 if ($file) { 196 open(FILE, "diff -u /dev/null $filename|") || 197 die "$P: $filename: diff failed - $!\n"; 198 } else { 199 open(FILE, "<$filename") || 200 die "$P: $filename: open failed - $!\n"; 201 } 202 if ($filename eq '-') { 203 $vname = 'Your patch'; 204 } else { 205 $vname = $filename; 206 } 207 while (<FILE>) { 208 chomp; 209 push(@rawlines, $_); 210 } 211 close(FILE); 212 if (!process($filename)) { 213 $exit = 1; 214 } 215 @rawlines = (); 216} 217 218exit($exit); 219 220sub top_of_kernel_tree { 221 my ($root) = @_; 222 223 my @tree_check = ( 224 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 225 "README", "Documentation", "arch", "include", "drivers", 226 "fs", "init", "ipc", "kernel", "lib", "scripts", 227 ); 228 229 foreach my $check (@tree_check) { 230 if (! -e $root . '/' . $check) { 231 return 0; 232 } 233 } 234 return 1; 235} 236 237sub expand_tabs { 238 my ($str) = @_; 239 240 my $res = ''; 241 my $n = 0; 242 for my $c (split(//, $str)) { 243 if ($c eq "\t") { 244 $res .= ' '; 245 $n++; 246 for (; ($n % 8) != 0; $n++) { 247 $res .= ' '; 248 } 249 next; 250 } 251 $res .= $c; 252 $n++; 253 } 254 255 return $res; 256} 257sub copy_spacing { 258 my ($str) = @_; 259 260 my $res = ''; 261 for my $c (split(//, $str)) { 262 if ($c eq "\t") { 263 $res .= $c; 264 } else { 265 $res .= ' '; 266 } 267 } 268 269 return $res; 270} 271 272sub line_stats { 273 my ($line) = @_; 274 275 # Drop the diff line leader and expand tabs 276 $line =~ s/^.//; 277 $line = expand_tabs($line); 278 279 # Pick the indent from the front of the line. 280 my ($white) = ($line =~ /^(\s*)/); 281 282 return (length($line), length($white)); 283} 284 285sub sanitise_line { 286 my ($line) = @_; 287 288 my $res = ''; 289 my $l = ''; 290 291 my $quote = ''; 292 my $qlen = 0; 293 294 foreach my $c (split(//, $line)) { 295 # The second backslash of a pair is not a "quote". 296 if ($l eq "\\" && $c eq "\\") { 297 $c = 'X'; 298 } 299 if ($l ne "\\" && ($c eq "'" || $c eq '"')) { 300 if ($quote eq '') { 301 $quote = $c; 302 $res .= $c; 303 $l = $c; 304 $qlen = 0; 305 next; 306 } elsif ($quote eq $c) { 307 $quote = ''; 308 } 309 } 310 if ($quote eq "'" && $qlen > 1) { 311 $quote = ''; 312 } 313 if ($quote && $c ne "\t") { 314 $res .= "X"; 315 $qlen++; 316 } else { 317 $res .= $c; 318 } 319 320 $l = $c; 321 } 322 323 # Clear out the comments. 324 while ($res =~ m@(/\*.*?\*/)@) { 325 substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); 326 } 327 if ($res =~ m@(/\*.*)@) { 328 substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); 329 } 330 if ($res =~ m@^.(.*\*/)@) { 331 substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); 332 } 333 334 # The pathname on a #include may be surrounded by '<' and '>'. 335 if ($res =~ /^.#\s*include\s+\<(.*)\>/) { 336 my $clean = 'X' x length($1); 337 $res =~ s@\<.*\>@<$clean>@; 338 339 # The whole of a #error is a string. 340 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) { 341 my $clean = 'X' x length($1); 342 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@; 343 } 344 345 return $res; 346} 347 348sub ctx_statement_block { 349 my ($linenr, $remain, $off) = @_; 350 my $line = $linenr - 1; 351 my $blk = ''; 352 my $soff = $off; 353 my $coff = $off - 1; 354 355 my $type = ''; 356 my $level = 0; 357 my $c; 358 my $len = 0; 359 while (1) { 360 #warn "CSB: blk<$blk>\n"; 361 # If we are about to drop off the end, pull in more 362 # context. 363 if ($off >= $len) { 364 for (; $remain > 0; $line++) { 365 next if ($lines[$line] =~ /^-/); 366 $remain--; 367 $blk .= $lines[$line] . "\n"; 368 $len = length($blk); 369 $line++; 370 last; 371 } 372 # Bail if there is no further context. 373 #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 374 if ($off == $len) { 375 last; 376 } 377 } 378 $c = substr($blk, $off, 1); 379 380 #warn "CSB: c<$c> type<$type> level<$level>\n"; 381 # Statement ends at the ';' or a close '}' at the 382 # outermost level. 383 if ($level == 0 && $c eq ';') { 384 last; 385 } 386 387 if (($type eq '' || $type eq '(') && $c eq '(') { 388 $level++; 389 $type = '('; 390 } 391 if ($type eq '(' && $c eq ')') { 392 $level--; 393 $type = ($level != 0)? '(' : ''; 394 395 if ($level == 0 && $coff < $soff) { 396 $coff = $off; 397 } 398 } 399 if (($type eq '' || $type eq '{') && $c eq '{') { 400 $level++; 401 $type = '{'; 402 } 403 if ($type eq '{' && $c eq '}') { 404 $level--; 405 $type = ($level != 0)? '{' : ''; 406 407 if ($level == 0) { 408 last; 409 } 410 } 411 $off++; 412 } 413 414 my $statement = substr($blk, $soff, $off - $soff + 1); 415 my $condition = substr($blk, $soff, $coff - $soff + 1); 416 417 #warn "STATEMENT<$statement>\n"; 418 #warn "CONDITION<$condition>\n"; 419 420 return ($statement, $condition); 421} 422 423sub ctx_block_get { 424 my ($linenr, $remain, $outer, $open, $close, $off) = @_; 425 my $line; 426 my $start = $linenr - 1; 427 my $blk = ''; 428 my @o; 429 my @c; 430 my @res = (); 431 432 my $level = 0; 433 for ($line = $start; $remain > 0; $line++) { 434 next if ($rawlines[$line] =~ /^-/); 435 $remain--; 436 437 $blk .= $rawlines[$line]; 438 foreach my $c (split(//, $rawlines[$line])) { 439 ##print "C<$c>L<$level><$open$close>O<$off>\n"; 440 if ($off > 0) { 441 $off--; 442 next; 443 } 444 445 if ($c eq $close && $level > 0) { 446 $level--; 447 last if ($level == 0); 448 } elsif ($c eq $open) { 449 $level++; 450 } 451 } 452 453 if (!$outer || $level <= 1) { 454 push(@res, $rawlines[$line]); 455 } 456 457 last if ($level == 0); 458 } 459 460 return ($level, @res); 461} 462sub ctx_block_outer { 463 my ($linenr, $remain) = @_; 464 465 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 466 return @r; 467} 468sub ctx_block { 469 my ($linenr, $remain) = @_; 470 471 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 472 return @r; 473} 474sub ctx_statement { 475 my ($linenr, $remain, $off) = @_; 476 477 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 478 return @r; 479} 480sub ctx_block_level { 481 my ($linenr, $remain) = @_; 482 483 return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 484} 485sub ctx_statement_level { 486 my ($linenr, $remain, $off) = @_; 487 488 return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 489} 490 491sub ctx_locate_comment { 492 my ($first_line, $end_line) = @_; 493 494 # Catch a comment on the end of the line itself. 495 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); 496 return $current_comment if (defined $current_comment); 497 498 # Look through the context and try and figure out if there is a 499 # comment. 500 my $in_comment = 0; 501 $current_comment = ''; 502 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 503 my $line = $rawlines[$linenr - 1]; 504 #warn " $line\n"; 505 if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 506 $in_comment = 1; 507 } 508 if ($line =~ m@/\*@) { 509 $in_comment = 1; 510 } 511 if (!$in_comment && $current_comment ne '') { 512 $current_comment = ''; 513 } 514 $current_comment .= $line . "\n" if ($in_comment); 515 if ($line =~ m@\*/@) { 516 $in_comment = 0; 517 } 518 } 519 520 chomp($current_comment); 521 return($current_comment); 522} 523sub ctx_has_comment { 524 my ($first_line, $end_line) = @_; 525 my $cmt = ctx_locate_comment($first_line, $end_line); 526 527 ##print "LINE: $rawlines[$end_line - 1 ]\n"; 528 ##print "CMMT: $cmt\n"; 529 530 return ($cmt ne ''); 531} 532 533sub cat_vet { 534 my ($vet) = @_; 535 my ($res, $coded); 536 537 $res = ''; 538 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 539 $res .= $1; 540 if ($2 ne '') { 541 $coded = sprintf("^%c", unpack('C', $2) + 64); 542 $res .= $coded; 543 } 544 } 545 $res =~ s/$/\$/; 546 547 return $res; 548} 549 550my $av_preprocessor = 0; 551my $av_paren = 0; 552my @av_paren_type; 553 554sub annotate_reset { 555 $av_preprocessor = 0; 556 $av_paren = 0; 557 @av_paren_type = (); 558} 559 560sub annotate_values { 561 my ($stream, $type) = @_; 562 563 my $res; 564 my $cur = $stream; 565 566 print "$stream\n" if ($dbg_values > 1); 567 568 while (length($cur)) { 569 print " <$type> " if ($dbg_values > 1); 570 if ($cur =~ /^(\s+)/o) { 571 print "WS($1)\n" if ($dbg_values > 1); 572 if ($1 =~ /\n/ && $av_preprocessor) { 573 $av_preprocessor = 0; 574 $type = 'N'; 575 } 576 577 } elsif ($cur =~ /^($Type)/) { 578 print "DECLARE($1)\n" if ($dbg_values > 1); 579 $type = 'T'; 580 581 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { 582 print "DEFINE($1)\n" if ($dbg_values > 1); 583 $av_preprocessor = 1; 584 $av_paren_type[$av_paren] = 'N'; 585 586 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) { 587 print "PRE($1)\n" if ($dbg_values > 1); 588 $av_preprocessor = 1; 589 $type = 'N'; 590 591 } elsif ($cur =~ /^(\\\n)/o) { 592 print "PRECONT($1)\n" if ($dbg_values > 1); 593 594 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 595 print "SIZEOF($1)\n" if ($dbg_values > 1); 596 if (defined $2) { 597 $av_paren_type[$av_paren] = 'V'; 598 } 599 $type = 'N'; 600 601 } elsif ($cur =~ /^(if|while|typeof|for)\b/o) { 602 print "COND($1)\n" if ($dbg_values > 1); 603 $av_paren_type[$av_paren] = 'N'; 604 $type = 'N'; 605 606 } elsif ($cur =~/^(return|case|else)/o) { 607 print "KEYWORD($1)\n" if ($dbg_values > 1); 608 $type = 'N'; 609 610 } elsif ($cur =~ /^(\()/o) { 611 print "PAREN('$1')\n" if ($dbg_values > 1); 612 $av_paren++; 613 $type = 'N'; 614 615 } elsif ($cur =~ /^(\))/o) { 616 $av_paren-- if ($av_paren > 0); 617 if (defined $av_paren_type[$av_paren]) { 618 $type = $av_paren_type[$av_paren]; 619 undef $av_paren_type[$av_paren]; 620 print "PAREN('$1') -> $type\n" 621 if ($dbg_values > 1); 622 } else { 623 print "PAREN('$1')\n" if ($dbg_values > 1); 624 } 625 626 } elsif ($cur =~ /^($Ident)\(/o) { 627 print "FUNC($1)\n" if ($dbg_values > 1); 628 $av_paren_type[$av_paren] = 'V'; 629 630 } elsif ($cur =~ /^($Ident|$Constant)/o) { 631 print "IDENT($1)\n" if ($dbg_values > 1); 632 $type = 'V'; 633 634 } elsif ($cur =~ /^($Assignment)/o) { 635 print "ASSIGN($1)\n" if ($dbg_values > 1); 636 $type = 'N'; 637 638 } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { 639 print "END($1)\n" if ($dbg_values > 1); 640 $type = 'N'; 641 642 } elsif ($cur =~ /^($Operators)/o) { 643 print "OP($1)\n" if ($dbg_values > 1); 644 if ($1 ne '++' && $1 ne '--') { 645 $type = 'N'; 646 } 647 648 } elsif ($cur =~ /(^.)/o) { 649 print "C($1)\n" if ($dbg_values > 1); 650 } 651 if (defined $1) { 652 $cur = substr($cur, length($1)); 653 $res .= $type x length($1); 654 } 655 } 656 657 return $res; 658} 659 660sub possible { 661 my ($possible) = @_; 662 663 #print "CHECK<$possible>\n"; 664 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && 665 $possible ne 'goto' && $possible ne 'return' && 666 $possible ne 'struct' && $possible ne 'enum' && 667 $possible ne 'case' && $possible ne 'else' && 668 $possible ne 'typedef') { 669 warn "POSSIBLE: $possible\n" if ($dbg_possible); 670 push(@typeList, $possible); 671 build_types(); 672 } 673} 674 675my $prefix = ''; 676 677my @report = (); 678sub report { 679 my $line = $prefix . $_[0]; 680 681 $line = (split('\n', $line))[0] . "\n" if ($terse); 682 683 push(@report, $line); 684} 685sub report_dump { 686 @report; 687} 688sub ERROR { 689 report("ERROR: $_[0]\n"); 690 our $clean = 0; 691 our $cnt_error++; 692} 693sub WARN { 694 report("WARNING: $_[0]\n"); 695 our $clean = 0; 696 our $cnt_warn++; 697} 698sub CHK { 699 if ($check) { 700 report("CHECK: $_[0]\n"); 701 our $clean = 0; 702 our $cnt_chk++; 703 } 704} 705 706sub process { 707 my $filename = shift; 708 709 my $linenr=0; 710 my $prevline=""; 711 my $prevrawline=""; 712 my $stashline=""; 713 my $stashrawline=""; 714 715 my $length; 716 my $indent; 717 my $previndent=0; 718 my $stashindent=0; 719 720 our $clean = 1; 721 my $signoff = 0; 722 my $is_patch = 0; 723 724 our $cnt_lines = 0; 725 our $cnt_error = 0; 726 our $cnt_warn = 0; 727 our $cnt_chk = 0; 728 729 # Trace the real file/line as we go. 730 my $realfile = ''; 731 my $realline = 0; 732 my $realcnt = 0; 733 my $here = ''; 734 my $in_comment = 0; 735 my $comment_edge = 0; 736 my $first_line = 0; 737 738 my $prev_values = 'N'; 739 740 # Pre-scan the patch sanitizing the lines. 741 # Pre-scan the patch looking for any __setup documentation. 742 # 743 my @setup_docs = (); 744 my $setup_docs = 0; 745 my $line; 746 foreach my $rawline (@rawlines) { 747 # Standardise the strings and chars within the input to 748 # simplify matching. 749 $line = sanitise_line($rawline); 750 push(@lines, $line); 751 752 ##print "==>$rawline\n"; 753 ##print "-->$line\n"; 754 755 if ($line=~/^\+\+\+\s+(\S+)/) { 756 $setup_docs = 0; 757 if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 758 $setup_docs = 1; 759 } 760 next; 761 } 762 763 if ($setup_docs && $line =~ /^\+/) { 764 push(@setup_docs, $line); 765 } 766 } 767 768 $prefix = ''; 769 770 foreach my $line (@lines) { 771 $linenr++; 772 773 my $rawline = $rawlines[$linenr - 1]; 774 775#extract the filename as it passes 776 if ($line=~/^\+\+\+\s+(\S+)/) { 777 $realfile=$1; 778 $realfile =~ s@^[^/]*/@@; 779 $in_comment = 0; 780 next; 781 } 782#extract the line range in the file after the patch is applied 783 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 784 $is_patch = 1; 785 $first_line = $linenr + 1; 786 $in_comment = 0; 787 $realline=$1-1; 788 if (defined $2) { 789 $realcnt=$3+1; 790 } else { 791 $realcnt=1+1; 792 } 793 annotate_reset(); 794 $prev_values = 'N'; 795 next; 796 } 797 798# track the line number as we move through the hunk, note that 799# new versions of GNU diff omit the leading space on completely 800# blank context lines so we need to count that too. 801 if ($line =~ /^( |\+|$)/) { 802 $realline++; 803 $realcnt-- if ($realcnt != 0); 804 805 # Guestimate if this is a continuing comment. Run 806 # the context looking for a comment "edge". If this 807 # edge is a close comment then we must be in a comment 808 # at context start. 809 if ($linenr == $first_line) { 810 my $edge; 811 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) { 812 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@); 813 last if (defined $edge); 814 } 815 if (defined $edge && $edge eq '*/') { 816 $in_comment = 1; 817 } 818 } 819 820 # Guestimate if this is a continuing comment. If this 821 # is the start of a diff block and this line starts 822 # ' *' then it is very likely a comment. 823 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) { 824 $in_comment = 1; 825 } 826 827 # Find the last comment edge on _this_ line. 828 $comment_edge = 0; 829 while (($rawline =~ m@(/\*|\*/)@g)) { 830 if ($1 eq '/*') { 831 $in_comment = 1; 832 } else { 833 $in_comment = 0; 834 } 835 $comment_edge = 1; 836 } 837 838 # Measure the line length and indent. 839 ($length, $indent) = line_stats($rawline); 840 841 # Track the previous line. 842 ($prevline, $stashline) = ($stashline, $line); 843 ($previndent, $stashindent) = ($stashindent, $indent); 844 ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 845 846 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n"; 847 848 } elsif ($realcnt == 1) { 849 $realcnt--; 850 } 851 852#make up the handle for any error we report on this line 853 $here = "#$linenr: " if (!$file); 854 $here = "#$realline: " if ($file); 855 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 856 857 my $hereline = "$here\n$rawline\n"; 858 my $herecurr = "$here\n$rawline\n"; 859 my $hereprev = "$here\n$prevrawline\n$rawline\n"; 860 861 $prefix = "$filename:$realline: " if ($emacs && $file); 862 $prefix = "$filename:$linenr: " if ($emacs && !$file); 863 $cnt_lines++ if ($realcnt != 0); 864 865#check the patch for a signoff: 866 if ($line =~ /^\s*signed-off-by:/i) { 867 # This is a signoff, if ugly, so do not double report. 868 $signoff++; 869 if (!($line =~ /^\s*Signed-off-by:/)) { 870 WARN("Signed-off-by: is the preferred form\n" . 871 $herecurr); 872 } 873 if ($line =~ /^\s*signed-off-by:\S/i) { 874 WARN("need space after Signed-off-by:\n" . 875 $herecurr); 876 } 877 } 878 879# Check for wrappage within a valid hunk of the file 880 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 881 ERROR("patch seems to be corrupt (line wrapped?)\n" . 882 $herecurr) if (!$emitted_corrupt++); 883 } 884 885# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 886 if (($realfile =~ /^$/ || $line =~ /^\+/) && 887 !($rawline =~ m/^( 888 [\x09\x0A\x0D\x20-\x7E] # ASCII 889 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 890 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 891 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 892 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 893 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 894 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 895 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 896 )*$/x )) { 897 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr); 898 } 899 900#ignore lines being removed 901 if ($line=~/^-/) {next;} 902 903# check we are in a valid source file if not then ignore this hunk 904 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 905 906#trailing whitespace 907 if ($line =~ /^\+.*\015/) { 908 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 909 ERROR("DOS line endings\n" . $herevet); 910 911 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 912 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 913 ERROR("trailing whitespace\n" . $herevet); 914 } 915#80 column limit 916 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) { 917 WARN("line over 80 characters\n" . $herecurr); 918 } 919 920# check for adding lines without a newline. 921 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 922 WARN("adding a line without newline at end of file\n" . $herecurr); 923 } 924 925# check we are in a valid source file *.[hc] if not then ignore this hunk 926 next if ($realfile !~ /\.[hc]$/); 927 928# at the beginning of a line any tabs must come first and anything 929# more than 8 must use tabs. 930 if ($rawline =~ /^\+\s* \t\s*\S/ || 931 $rawline =~ /^\+\s* \s*/) { 932 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 933 ERROR("use tabs not spaces\n" . $herevet); 934 } 935 936# check for RCS/CVS revision markers 937 if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) { 938 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 939 } 940 941# The rest of our checks refer specifically to C style 942# only apply those _outside_ comments. Only skip 943# lines in the middle of comments. 944 next if (!$comment_edge && $in_comment); 945 946# Check for potential 'bare' types 947 if ($realcnt) { 948 # Ignore goto labels. 949 if ($line =~ /$Ident:\*$/) { 950 951 # Ignore functions being called 952 } elsif ($line =~ /^.\s*$Ident\s*\(/) { 953 954 # definitions in global scope can only start with types 955 } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { 956 possible($1); 957 958 # declarations always start with types 959 } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=)/) { 960 possible($1); 961 } 962 963 # any (foo ... *) is a pointer cast, and foo is a type 964 while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { 965 possible($1); 966 } 967 968 # Check for any sort of function declaration. 969 # int foo(something bar, other baz); 970 # void (*store_gdt)(x86_descr_ptr *); 971 if ($prev_values eq 'N' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { 972 my ($name_len) = length($1); 973 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); 974 my $ctx = join("\n", @ctx); 975 976 $ctx =~ s/\n.//; 977 substr($ctx, 0, $name_len + 1) = ''; 978 $ctx =~ s/\)[^\)]*$//; 979 for my $arg (split(/\s*,\s*/, $ctx)) { 980 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) { 981 982 possible($1); 983 } 984 } 985 } 986 987 } 988 989# 990# Checks which may be anchored in the context. 991# 992 993# Check for switch () and associated case and default 994# statements should be at the same indent. 995 if ($line=~/\bswitch\s*\(.*\)/) { 996 my $err = ''; 997 my $sep = ''; 998 my @ctx = ctx_block_outer($linenr, $realcnt); 999 shift(@ctx); 1000 for my $ctx (@ctx) { 1001 my ($clen, $cindent) = line_stats($ctx); 1002 if ($ctx =~ /^\+\s*(case\s+|default:)/ && 1003 $indent != $cindent) { 1004 $err .= "$sep$ctx\n"; 1005 $sep = ''; 1006 } else { 1007 $sep = "[...]\n"; 1008 } 1009 } 1010 if ($err ne '') { 1011 ERROR("switch and case should be at the same indent\n$hereline$err"); 1012 } 1013 } 1014 1015# if/while/etc brace do not go on next line, unless defining a do while loop, 1016# or if that brace on the next line is for something else 1017 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { 1018 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1019 my $ctx_ln = $linenr + $#ctx + 1; 1020 my $ctx_cnt = $realcnt - $#ctx - 1; 1021 my $ctx = join("\n", @ctx); 1022 1023 # Skip over any removed lines in the context following statement. 1024 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { 1025 $ctx_ln++; 1026 $ctx_cnt--; 1027 } 1028 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; 1029 1030 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1031 ERROR("That open brace { should be on the previous line\n" . 1032 "$here\n$ctx\n$lines[$ctx_ln - 1]"); 1033 } 1034 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) { 1035 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 1036 if ($nindent > $indent) { 1037 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" . 1038 "$here\n$ctx\n$lines[$ctx_ln - 1]"); 1039 } 1040 } 1041 } 1042 1043 # Track the 'values' across context and added lines. 1044 my $opline = $line; $opline =~ s/^./ /; 1045 my $curr_values = annotate_values($opline . "\n", $prev_values); 1046 $curr_values = $prev_values . $curr_values; 1047 if ($dbg_values) { 1048 my $outline = $opline; $outline =~ s/\t/ /g; 1049 warn "--> .$outline\n"; 1050 warn "--> $curr_values\n"; 1051 } 1052 $prev_values = substr($curr_values, -1); 1053 1054#ignore lines not being added 1055 if ($line=~/^[^\+]/) {next;} 1056 1057# TEST: allow direct testing of the type matcher. 1058 if ($tst_type && $line =~ /^.$Declare$/) { 1059 ERROR("TEST: is type $Declare\n" . $herecurr); 1060 next; 1061 } 1062 1063# check for initialisation to aggregates open brace on the next line 1064 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 1065 $line =~ /^.\s*{/) { 1066 ERROR("That open brace { should be on the previous line\n" . $hereprev); 1067 } 1068 1069# 1070# Checks which are anchored on the added line. 1071# 1072 1073# check for malformed paths in #include statements (uses RAW line) 1074 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { 1075 my $path = $1; 1076 if ($path =~ m{//}) { 1077 ERROR("malformed #include filename\n" . 1078 $herecurr); 1079 } 1080 } 1081 1082# no C99 // comments 1083 if ($line =~ m{//}) { 1084 ERROR("do not use C99 // comments\n" . $herecurr); 1085 } 1086 # Remove C99 comments. 1087 $line =~ s@//.*@@; 1088 $opline =~ s@//.*@@; 1089 1090#EXPORT_SYMBOL should immediately follow its function closing }. 1091 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 1092 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 1093 my $name = $1; 1094 if (($prevline !~ /^}/) && 1095 ($prevline !~ /^\+}/) && 1096 ($prevline !~ /^ }/) && 1097 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { 1098 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 1099 } 1100 } 1101 1102# check for external initialisers. 1103 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { 1104 ERROR("do not initialise externals to 0 or NULL\n" . 1105 $herecurr); 1106 } 1107# check for static initialisers. 1108 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { 1109 ERROR("do not initialise statics to 0 or NULL\n" . 1110 $herecurr); 1111 } 1112 1113# check for new typedefs, only function parameters and sparse annotations 1114# make sense. 1115 if ($line =~ /\btypedef\s/ && 1116 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && 1117 $line !~ /\b__bitwise(?:__|)\b/) { 1118 WARN("do not add new typedefs\n" . $herecurr); 1119 } 1120 1121# * goes on variable not on type 1122 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { 1123 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . 1124 $herecurr); 1125 1126 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { 1127 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . 1128 $herecurr); 1129 1130 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) { 1131 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . 1132 $herecurr); 1133 1134 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) { 1135 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . 1136 $herecurr); 1137 } 1138 1139# # no BUG() or BUG_ON() 1140# if ($line =~ /\b(BUG|BUG_ON)\b/) { 1141# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 1142# print "$herecurr"; 1143# $clean = 0; 1144# } 1145 1146 if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1147 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 1148 } 1149 1150# printk should use KERN_* levels. Note that follow on printk's on the 1151# same line do not need a level, so we use the current block context 1152# to try and find and validate the current printk. In summary the current 1153# printk includes all preceeding printk's which have no newline on the end. 1154# we assume the first bad printk is the one to report. 1155 if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 1156 my $ok = 0; 1157 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 1158 #print "CHECK<$lines[$ln - 1]\n"; 1159 # we have a preceeding printk if it ends 1160 # with "\n" ignore it, else it is to blame 1161 if ($lines[$ln - 1] =~ m{\bprintk\(}) { 1162 if ($rawlines[$ln - 1] !~ m{\\n"}) { 1163 $ok = 1; 1164 } 1165 last; 1166 } 1167 } 1168 if ($ok == 0) { 1169 WARN("printk() should include KERN_ facility level\n" . $herecurr); 1170 } 1171 } 1172 1173# function brace can't be on same line, except for #defines of do while, 1174# or if closed on same line 1175 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and 1176 !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 1177 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 1178 } 1179 1180# open braces for enum, union and struct go on the same line. 1181 if ($line =~ /^.\s*{/ && 1182 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 1183 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 1184 } 1185 1186# check for spaces between functions and their parentheses. 1187 while ($line =~ /($Ident)\s+\(/g) { 1188 my $name = $1; 1189 my $ctx = substr($line, 0, $-[1]); 1190 1191 # Ignore those directives where spaces _are_ permitted. 1192 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/) { 1193 1194 # cpp #define statements have non-optional spaces, ie 1195 # if there is a space between the name and the open 1196 # parenthesis it is simply not a parameter group. 1197 } elsif ($ctx =~ /^.\#\s*define\s*$/) { 1198 1199 # If this whole things ends with a type its most 1200 # likely a typedef for a function. 1201 } elsif ("$ctx$name" =~ /$Type$/) { 1202 1203 } else { 1204 WARN("no space between function name and open parenthesis '('\n" . $herecurr); 1205 } 1206 } 1207# Check operator spacing. 1208 if (!($line=~/\#\s*include/)) { 1209 my $ops = qr{ 1210 <<=|>>=|<=|>=|==|!=| 1211 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 1212 =>|->|<<|>>|<|>|=|!|~| 1213 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1214 }x; 1215 my @elements = split(/($ops|;)/, $opline); 1216 my $off = 0; 1217 1218 my $blank = copy_spacing($opline); 1219 1220 for (my $n = 0; $n < $#elements; $n += 2) { 1221 $off += length($elements[$n]); 1222 1223 my $a = ''; 1224 $a = 'V' if ($elements[$n] ne ''); 1225 $a = 'W' if ($elements[$n] =~ /\s$/); 1226 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 1227 $a = 'O' if ($elements[$n] eq ''); 1228 $a = 'E' if ($elements[$n] eq '' && $n == 0); 1229 1230 my $op = $elements[$n + 1]; 1231 1232 my $c = ''; 1233 if (defined $elements[$n + 2]) { 1234 $c = 'V' if ($elements[$n + 2] ne ''); 1235 $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1236 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 1237 $c = 'O' if ($elements[$n + 2] eq ''); 1238 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); 1239 } else { 1240 $c = 'E'; 1241 } 1242 1243 # Pick up the preceeding and succeeding characters. 1244 my $ca = substr($opline, 0, $off); 1245 my $cc = ''; 1246 if (length($opline) >= ($off + length($elements[$n + 1]))) { 1247 $cc = substr($opline, $off + length($elements[$n + 1])); 1248 } 1249 my $cb = "$ca$;$cc"; 1250 1251 my $ctx = "${a}x${c}"; 1252 1253 my $at = "(ctx:$ctx)"; 1254 1255 my $ptr = substr($blank, 0, $off) . "^"; 1256 my $hereptr = "$hereline$ptr\n"; 1257 1258 # Classify operators into binary, unary, or 1259 # definitions (* only) where they have more 1260 # than one mode. 1261 my $op_type = substr($curr_values, $off + 1, 1); 1262 my $op_left = substr($curr_values, $off, 1); 1263 my $is_unary; 1264 if ($op_type eq 'T') { 1265 $is_unary = 2; 1266 } elsif ($op_left eq 'V') { 1267 $is_unary = 0; 1268 } else { 1269 $is_unary = 1; 1270 } 1271 #if ($op eq '-' || $op eq '&' || $op eq '*') { 1272 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; 1273 #} 1274 1275 # ; should have either the end of line or a space or \ after it 1276 if ($op eq ';') { 1277 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && 1278 $cc !~ /^;/) { 1279 ERROR("need space after that '$op' $at\n" . $hereptr); 1280 } 1281 1282 # // is a comment 1283 } elsif ($op eq '//') { 1284 1285 # -> should have no spaces 1286 } elsif ($op eq '->') { 1287 if ($ctx =~ /Wx.|.xW/) { 1288 ERROR("no spaces around that '$op' $at\n" . $hereptr); 1289 } 1290 1291 # , must have a space on the right. 1292 } elsif ($op eq ',') { 1293 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { 1294 ERROR("need space after that '$op' $at\n" . $hereptr); 1295 } 1296 1297 # '*' as part of a type definition -- reported already. 1298 } elsif ($op eq '*' && $is_unary == 2) { 1299 #warn "'*' is part of type\n"; 1300 1301 # unary operators should have a space before and 1302 # none after. May be left adjacent to another 1303 # unary operator, or a cast 1304 } elsif ($op eq '!' || $op eq '~' || 1305 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { 1306 if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1307 ERROR("need space before that '$op' $at\n" . $hereptr); 1308 } 1309 if ($ctx =~ /.xW/) { 1310 ERROR("no space after that '$op' $at\n" . $hereptr); 1311 } 1312 1313 # unary ++ and unary -- are allowed no space on one side. 1314 } elsif ($op eq '++' or $op eq '--') { 1315 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { 1316 ERROR("need space one side of that '$op' $at\n" . $hereptr); 1317 } 1318 if ($ctx =~ /Wx./ && $cc =~ /^;/) { 1319 ERROR("no space before that '$op' $at\n" . $hereptr); 1320 } 1321 1322 # << and >> may either have or not have spaces both sides 1323 } elsif ($op eq '<<' or $op eq '>>' or 1324 $op eq '&' or $op eq '^' or $op eq '|' or 1325 $op eq '+' or $op eq '-' or 1326 $op eq '*' or $op eq '/' or 1327 $op eq '%') 1328 { 1329 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { 1330 ERROR("need consistent spacing around '$op' $at\n" . 1331 $hereptr); 1332 } 1333 1334 # All the others need spaces both sides. 1335 } elsif ($ctx !~ /[EW]x[WE]/) { 1336 # Ignore email addresses <foo@bar> 1337 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && 1338 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { 1339 ERROR("need spaces around that '$op' $at\n" . $hereptr); 1340 } 1341 } 1342 $off += length($elements[$n + 1]); 1343 } 1344 } 1345 1346# check for multiple assignments 1347 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 1348 CHK("multiple assignments should be avoided\n" . $herecurr); 1349 } 1350 1351## # check for multiple declarations, allowing for a function declaration 1352## # continuation. 1353## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 1354## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 1355## 1356## # Remove any bracketed sections to ensure we do not 1357## # falsly report the parameters of functions. 1358## my $ln = $line; 1359## while ($ln =~ s/\([^\(\)]*\)//g) { 1360## } 1361## if ($ln =~ /,/) { 1362## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 1363## } 1364## } 1365 1366#need space before brace following if, while, etc 1367 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 1368 $line =~ /do{/) { 1369 ERROR("need a space before the open brace '{'\n" . $herecurr); 1370 } 1371 1372# closing brace should have a space following it when it has anything 1373# on the line 1374 if ($line =~ /}(?!(?:,|;|\)))\S/) { 1375 ERROR("need a space after that close brace '}'\n" . $herecurr); 1376 } 1377 1378# check spacing on square brackets 1379 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 1380 ERROR("no space after that open square bracket '['\n" . $herecurr); 1381 } 1382 if ($line =~ /\s\]/) { 1383 ERROR("no space before that close square bracket ']'\n" . $herecurr); 1384 } 1385 1386# check spacing on paretheses 1387 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 1388 $line !~ /for\s*\(\s+;/) { 1389 ERROR("no space after that open parenthesis '('\n" . $herecurr); 1390 } 1391 if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && 1392 $line !~ /for\s*\(.*;\s+\)/) { 1393 ERROR("no space before that close parenthesis ')'\n" . $herecurr); 1394 } 1395 1396#goto labels aren't indented, allow a single space however 1397 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 1398 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 1399 WARN("labels should not be indented\n" . $herecurr); 1400 } 1401 1402# Need a space before open parenthesis after if, while etc 1403 if ($line=~/\b(if|while|for|switch)\(/) { 1404 ERROR("need a space before the open parenthesis '('\n" . $herecurr); 1405 } 1406 1407# Check for illegal assignment in if conditional. 1408 if ($line =~ /\bif\s*\(/) { 1409 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 1410 1411 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { 1412 ERROR("do not use assignment in if condition\n" . $herecurr); 1413 } 1414 1415 # Find out what is on the end of the line after the 1416 # conditional. 1417 substr($s, 0, length($c)) = ''; 1418 $s =~ s/\n.*//g; 1419 1420 if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) { 1421 ERROR("trailing statements should be on next line\n" . $herecurr); 1422 } 1423 } 1424 1425# if and else should not have general statements after it 1426 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && 1427 $1 !~ /^\s*(?:\sif|{|\\|$)/) { 1428 ERROR("trailing statements should be on next line\n" . $herecurr); 1429 } 1430 1431 # Check for }<nl>else {, these must be at the same 1432 # indent level to be relevant to each other. 1433 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 1434 $previndent == $indent) { 1435 ERROR("else should follow close brace '}'\n" . $hereprev); 1436 } 1437 1438 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 1439 $previndent == $indent) { 1440 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 1441 1442 # Find out what is on the end of the line after the 1443 # conditional. 1444 substr($s, 0, length($c)) = ''; 1445 $s =~ s/\n.*//g; 1446 1447 if ($s =~ /^\s*;/) { 1448 ERROR("while should follow close brace '}'\n" . $hereprev); 1449 } 1450 } 1451 1452#studly caps, commented out until figure out how to distinguish between use of existing and adding new 1453# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 1454# print "No studly caps, use _\n"; 1455# print "$herecurr"; 1456# $clean = 0; 1457# } 1458 1459#no spaces allowed after \ in define 1460 if ($line=~/\#define.*\\\s$/) { 1461 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 1462 } 1463 1464#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 1465 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 1466 my $checkfile = "$root/include/linux/$1.h"; 1467 if (-f $checkfile && $1 ne 'irq.h') { 1468 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . 1469 $herecurr); 1470 } 1471 } 1472 1473# multi-statement macros should be enclosed in a do while loop, grab the 1474# first statement and ensure its the whole macro if its not enclosed 1475# in a known goot container 1476 if ($prevline =~ /\#define.*\\/ && 1477 $prevline !~/(?:do\s+{|\(\{|\{)/ && 1478 $line !~ /(?:do\s+{|\(\{|\{)/ && 1479 $line !~ /^.\s*$Declare\s/) { 1480 # Grab the first statement, if that is the entire macro 1481 # its ok. This may start either on the #define line 1482 # or the one below. 1483 my $ln = $linenr; 1484 my $cnt = $realcnt; 1485 my $off = 0; 1486 1487 # If the macro starts on the define line start 1488 # grabbing the statement after the identifier 1489 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; 1490 ##print "1<$1> 2<$2>\n"; 1491 if (defined $2 && $2 ne '') { 1492 $off = length($1); 1493 $ln--; 1494 $cnt++; 1495 while ($lines[$ln - 1] =~ /^-/) { 1496 $ln--; 1497 $cnt++; 1498 } 1499 } 1500 my @ctx = ctx_statement($ln, $cnt, $off); 1501 my $ctx_ln = $ln + $#ctx + 1; 1502 my $ctx = join("\n", @ctx); 1503 1504 # Pull in any empty extension lines. 1505 while ($ctx =~ /\\$/ && 1506 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { 1507 $ctx .= $lines[$ctx_ln - 1]; 1508 $ctx_ln++; 1509 } 1510 1511 if ($ctx =~ /\\$/) { 1512 if ($ctx =~ /;/) { 1513 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 1514 } else { 1515 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 1516 } 1517 } 1518 } 1519 1520# check for redundant bracing round if etc 1521 if ($line =~ /\b(if|while|for|else)\b/) { 1522 # Locate the end of the opening statement. 1523 my @control = ctx_statement($linenr, $realcnt, 0); 1524 my $nr = $linenr + (scalar(@control) - 1); 1525 my $cnt = $realcnt - (scalar(@control) - 1); 1526 1527 my $off = $realcnt - $cnt; 1528 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; 1529 1530 # If this is is a braced statement group check it 1531 if ($lines[$nr - 1] =~ /{\s*$/) { 1532 my ($lvl, @block) = ctx_block_level($nr, $cnt); 1533 1534 my $stmt = join("\n", @block); 1535 # Drop the diff line leader. 1536 $stmt =~ s/\n./\n/g; 1537 # Drop the code outside the block. 1538 $stmt =~ s/(^[^{]*){\s*//; 1539 my $before = $1; 1540 $stmt =~ s/\s*}([^}]*$)//; 1541 my $after = $1; 1542 1543 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; 1544 #print "stmt<$stmt>\n\n"; 1545 1546 # Count the newlines, if there is only one 1547 # then the block should not have {}'s. 1548 my @lines = ($stmt =~ /\n/g); 1549 my @statements = ($stmt =~ /;/g); 1550 #print "lines<" . scalar(@lines) . ">\n"; 1551 #print "statements<" . scalar(@statements) . ">\n"; 1552 if ($lvl == 0 && scalar(@lines) == 0 && 1553 scalar(@statements) < 2 && 1554 $stmt !~ /{/ && $stmt !~ /\bif\b/ && 1555 $before !~ /}/ && $after !~ /{/) { 1556 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; 1557 shift(@block); 1558 WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 1559 } 1560 } 1561 } 1562 1563# don't include deprecated include files (uses RAW line) 1564 for my $inc (@dep_includes) { 1565 if ($rawline =~ m@\#\s*include\s*\<$inc>@) { 1566 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 1567 } 1568 } 1569 1570# don't use deprecated functions 1571 for my $func (@dep_functions) { 1572 if ($line =~ /\b$func\b/) { 1573 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 1574 } 1575 } 1576 1577# no volatiles please 1578 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 1579 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 1580 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 1581 } 1582 1583# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 1584 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 1585 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 1586 } 1587 1588# warn about #if 0 1589 if ($line =~ /^.#\s*if\s+0\b/) { 1590 CHK("if this code is redundant consider removing it\n" . 1591 $herecurr); 1592 } 1593 1594# check for needless kfree() checks 1595 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 1596 my $expr = $1; 1597 if ($line =~ /\bkfree\(\Q$expr\E\);/) { 1598 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); 1599 } 1600 } 1601 1602# warn about #ifdefs in C files 1603# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 1604# print "#ifdef in C files should be avoided\n"; 1605# print "$herecurr"; 1606# $clean = 0; 1607# } 1608 1609# warn about spacing in #ifdefs 1610 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { 1611 ERROR("exactly one space required after that #$1\n" . $herecurr); 1612 } 1613 1614# check for spinlock_t definitions without a comment. 1615 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { 1616 my $which = $1; 1617 if (!ctx_has_comment($first_line, $linenr)) { 1618 CHK("$1 definition without comment\n" . $herecurr); 1619 } 1620 } 1621# check for memory barriers without a comment. 1622 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 1623 if (!ctx_has_comment($first_line, $linenr)) { 1624 CHK("memory barrier without comment\n" . $herecurr); 1625 } 1626 } 1627# check of hardware specific defines 1628 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 1629 CHK("architecture specific defines should be avoided\n" . $herecurr); 1630 } 1631 1632# check the location of the inline attribute, that it is between 1633# storage class and type. 1634 if ($line =~ /\b$Type\s+$Inline\b/ || 1635 $line =~ /\b$Inline\s+$Storage\b/) { 1636 ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 1637 } 1638 1639# Check for __inline__ and __inline, prefer inline 1640 if ($line =~ /\b(__inline__|__inline)\b/) { 1641 WARN("plain inline is preferred over $1\n" . $herecurr); 1642 } 1643 1644# check for new externs in .c files. 1645 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) { 1646 WARN("externs should be avoided in .c files\n" . $herecurr); 1647 } 1648 1649# checks for new __setup's 1650 if ($rawline =~ /\b__setup\("([^"]*)"/) { 1651 my $name = $1; 1652 1653 if (!grep(/$name/, @setup_docs)) { 1654 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 1655 } 1656 } 1657 1658# check for pointless casting of kmalloc return 1659 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 1660 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 1661 } 1662 } 1663 1664 # In mailback mode only produce a report in the negative, for 1665 # things that appear to be patches. 1666 if ($mailback && ($clean == 1 || !$is_patch)) { 1667 exit(0); 1668 } 1669 1670 # This is not a patch, and we are are in 'no-patch' mode so 1671 # just keep quiet. 1672 if (!$chk_patch && !$is_patch) { 1673 exit(0); 1674 } 1675 1676 if (!$is_patch) { 1677 ERROR("Does not appear to be a unified-diff format patch\n"); 1678 } 1679 if ($is_patch && $chk_signoff && $signoff == 0) { 1680 ERROR("Missing Signed-off-by: line(s)\n"); 1681 } 1682 1683 print report_dump(); 1684 if ($summary) { 1685 print "total: $cnt_error errors, $cnt_warn warnings, " . 1686 (($check)? "$cnt_chk checks, " : "") . 1687 "$cnt_lines lines checked\n"; 1688 print "\n" if ($quiet == 0); 1689 } 1690 1691 if ($clean == 1 && $quiet == 0) { 1692 print "$vname has no obvious style problems and is ready for submission.\n" 1693 } 1694 if ($clean == 0 && $quiet == 0) { 1695 print "$vname has style problems, please review. If any of these errors\n"; 1696 print "are false positives report them to the maintainer, see\n"; 1697 print "CHECKPATCH in MAINTAINERS.\n"; 1698 } 1699 return $clean; 1700} 1701