1#!/usr/bin/env perl 2#*************************************************************************** 3# _ _ ____ _ 4# Project ___| | | | _ \| | 5# / __| | | | |_) | | 6# | (__| |_| | _ <| |___ 7# \___|\___/|_| \_\_____| 8# 9# Copyright (C) 2011 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. 10# 11# This software is licensed as described in the file COPYING, which 12# you should have received as part of this distribution. The terms 13# are also available at https://curl.haxx.se/docs/copyright.html. 14# 15# You may opt to use, copy, modify, merge, publish, distribute and/or sell 16# copies of the Software, and permit persons to whom the Software is 17# furnished to do so, under the terms of the COPYING file. 18# 19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 20# KIND, either express or implied. 21# 22########################################################################### 23 24use strict; 25use warnings; 26 27my $max_column = 79; 28my $indent = 2; 29 30my $warnings = 0; 31my $swarnings = 0; 32my $errors = 0; 33my $serrors = 0; 34my $suppressed; # skipped problems 35my $file; 36my $dir="."; 37my $wlist=""; 38my @alist; 39my $windows_os = $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys'; 40my $verbose; 41my %skiplist; 42 43my %ignore; 44my %ignore_set; 45my %ignore_used; 46my @ignore_line; 47 48my %warnings_extended = ( 49 'COPYRIGHTYEAR' => 'copyright year incorrect', 50 ); 51 52my %warnings = ( 53 'LONGLINE' => "Line longer than $max_column", 54 'TABS' => 'TAB characters not allowed', 55 'TRAILINGSPACE' => 'Trailing white space on the line', 56 'CPPCOMMENTS' => '// comment detected', 57 'SPACEBEFOREPAREN' => 'space before an open parenthesis', 58 'SPACEAFTERPAREN' => 'space after open parenthesis', 59 'SPACEBEFORECLOSE' => 'space before a close parenthesis', 60 'SPACEBEFORECOMMA' => 'space before a comma', 61 'RETURNNOSPACE' => 'return without space', 62 'COMMANOSPACE' => 'comma without following space', 63 'BRACEELSE' => '} else on the same line', 64 'PARENBRACE' => '){ without sufficient space', 65 'SPACESEMICOLON' => 'space before semicolon', 66 'BANNEDFUNC' => 'a banned function was used', 67 'FOPENMODE' => 'fopen needs a macro for the mode string', 68 'BRACEPOS' => 'wrong position for an open brace', 69 'INDENTATION' => 'wrong start column for code', 70 'COPYRIGHT' => 'file missing a copyright statement', 71 'BADCOMMAND' => 'bad !checksrc! instruction', 72 'UNUSEDIGNORE' => 'a warning ignore was not used', 73 'OPENCOMMENT' => 'file ended with a /* comment still "open"', 74 'ASTERISKSPACE' => 'pointer declared with space after asterisk', 75 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk', 76 'ASSIGNWITHINCONDITION' => 'assignment within conditional expression', 77 'EQUALSNOSPACE' => 'equals sign without following space', 78 'NOSPACEEQUALS' => 'equals sign without preceding space', 79 'SEMINOSPACE' => 'semicolon without following space', 80 'MULTISPACE' => 'multiple spaces used when not suitable', 81 'SIZEOFNOPAREN' => 'use of sizeof without parentheses', 82 'SNPRINTF' => 'use of snprintf', 83 'ONELINECONDITION' => 'conditional block on the same line as the if()', 84 'TYPEDEFSTRUCT' => 'typedefed struct', 85 ); 86 87sub readskiplist { 88 open(W, "<$dir/checksrc.skip") or return; 89 my @all=<W>; 90 for(@all) { 91 $windows_os ? $_ =~ s/\r?\n$// : chomp; 92 $skiplist{$_}=1; 93 } 94 close(W); 95} 96 97# Reads the .checksrc in $dir for any extended warnings to enable locally. 98# Currently there is no support for disabling warnings from the standard set, 99# and since that's already handled via !checksrc! commands there is probably 100# little use to add it. 101sub readlocalfile { 102 my $i = 0; 103 104 open(my $rcfile, "<", "$dir/.checksrc") or return; 105 106 while(<$rcfile>) { 107 $i++; 108 109 # Lines starting with '#' are considered comments 110 if (/^\s*(#.*)/) { 111 next; 112 } 113 elsif (/^\s*enable ([A-Z]+)$/) { 114 if(!defined($warnings_extended{$1})) { 115 print STDERR "invalid warning specified in .checksrc: \"$1\"\n"; 116 next; 117 } 118 $warnings{$1} = $warnings_extended{$1}; 119 } 120 elsif (/^\s*disable ([A-Z]+)$/) { 121 if(!defined($warnings{$1})) { 122 print STDERR "invalid warning specified in .checksrc: \"$1\"\n"; 123 next; 124 } 125 # Accept-list 126 push @alist, $1; 127 } 128 else { 129 die "Invalid format in $dir/.checksrc on line $i\n"; 130 } 131 } 132 close($rcfile); 133} 134 135sub checkwarn { 136 my ($name, $num, $col, $file, $line, $msg, $error) = @_; 137 138 my $w=$error?"error":"warning"; 139 my $nowarn=0; 140 141 #if(!$warnings{$name}) { 142 # print STDERR "Dev! there's no description for $name!\n"; 143 #} 144 145 # checksrc.skip 146 if($skiplist{$line}) { 147 $nowarn = 1; 148 } 149 # !checksrc! controlled 150 elsif($ignore{$name}) { 151 $ignore{$name}--; 152 $ignore_used{$name}++; 153 $nowarn = 1; 154 if(!$ignore{$name}) { 155 # reached zero, enable again 156 enable_warn($name, $num, $file, $line); 157 } 158 } 159 160 if($nowarn) { 161 $suppressed++; 162 if($w) { 163 $swarnings++; 164 } 165 else { 166 $serrors++; 167 } 168 return; 169 } 170 171 if($w) { 172 $warnings++; 173 } 174 else { 175 $errors++; 176 } 177 178 $col++; 179 print "$file:$num:$col: $w: $msg ($name)\n"; 180 print " $line\n"; 181 182 if($col < 80) { 183 my $pref = (' ' x $col); 184 print "${pref}^\n"; 185 } 186} 187 188$file = shift @ARGV; 189 190while(defined $file) { 191 192 if($file =~ /-D(.*)/) { 193 $dir = $1; 194 $file = shift @ARGV; 195 next; 196 } 197 elsif($file =~ /-W(.*)/) { 198 $wlist .= " $1 "; 199 $file = shift @ARGV; 200 next; 201 } 202 elsif($file =~ /-A(.+)/) { 203 push @alist, $1; 204 $file = shift @ARGV; 205 next; 206 } 207 elsif($file =~ /-i([1-9])/) { 208 $indent = $1 + 0; 209 $file = shift @ARGV; 210 next; 211 } 212 elsif($file =~ /-m([0-9]+)/) { 213 $max_column = $1 + 0; 214 $file = shift @ARGV; 215 next; 216 } 217 elsif($file =~ /^(-h|--help)/) { 218 undef $file; 219 last; 220 } 221 222 last; 223} 224 225if(!$file) { 226 print "checksrc.pl [option] <file1> [file2] ...\n"; 227 print " Options:\n"; 228 print " -A[rule] Accept this violation, can be used multiple times\n"; 229 print " -D[DIR] Directory to prepend file names\n"; 230 print " -h Show help output\n"; 231 print " -W[file] Skip the given file - ignore all its flaws\n"; 232 print " -i<n> Indent spaces. Default: 2\n"; 233 print " -m<n> Maximum line length. Default: 79\n"; 234 print "\nDetects and warns for these problems:\n"; 235 for(sort keys %warnings) { 236 printf (" %-18s: %s\n", $_, $warnings{$_}); 237 } 238 exit; 239} 240 241readskiplist(); 242readlocalfile(); 243 244do { 245 if("$wlist" !~ / $file /) { 246 my $fullname = $file; 247 $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/'); 248 scanfile($fullname); 249 } 250 $file = shift @ARGV; 251 252} while($file); 253 254sub accept_violations { 255 for my $r (@alist) { 256 if(!$warnings{$r}) { 257 print "'$r' is not a warning to accept!\n"; 258 exit; 259 } 260 $ignore{$r}=999999; 261 $ignore_used{$r}=0; 262 } 263} 264 265sub checksrc_clear { 266 undef %ignore; 267 undef %ignore_set; 268 undef @ignore_line; 269} 270 271sub checksrc_endoffile { 272 my ($file) = @_; 273 for(keys %ignore_set) { 274 if($ignore_set{$_} && !$ignore_used{$_}) { 275 checkwarn("UNUSEDIGNORE", $ignore_set{$_}, 276 length($_)+11, $file, 277 $ignore_line[$ignore_set{$_}], 278 "Unused ignore: $_"); 279 } 280 } 281} 282 283sub enable_warn { 284 my ($what, $line, $file, $l) = @_; 285 286 # switch it back on, but warn if not triggered! 287 if(!$ignore_used{$what}) { 288 checkwarn("UNUSEDIGNORE", 289 $line, length($what) + 11, $file, $l, 290 "No warning was inhibited!"); 291 } 292 $ignore_set{$what}=0; 293 $ignore_used{$what}=0; 294 $ignore{$what}=0; 295} 296sub checksrc { 297 my ($cmd, $line, $file, $l) = @_; 298 if($cmd =~ / *([^ ]*) *(.*)/) { 299 my ($enable, $what) = ($1, $2); 300 $what =~ s: *\*/$::; # cut off end of C comment 301 # print "ENABLE $enable WHAT $what\n"; 302 if($enable eq "disable") { 303 my ($warn, $scope)=($1, $2); 304 if($what =~ /([^ ]*) +(.*)/) { 305 ($warn, $scope)=($1, $2); 306 } 307 else { 308 $warn = $what; 309 $scope = 1; 310 } 311 # print "IGNORE $warn for SCOPE $scope\n"; 312 if($scope eq "all") { 313 $scope=999999; 314 } 315 316 # Comparing for a literal zero rather than the scalar value zero 317 # covers the case where $scope contains the ending '*' from the 318 # comment. If we use a scalar comparison (==) we induce warnings 319 # on non-scalar contents. 320 if($scope eq "0") { 321 checkwarn("BADCOMMAND", 322 $line, 0, $file, $l, 323 "Disable zero not supported, did you mean to enable?"); 324 } 325 elsif($ignore_set{$warn}) { 326 checkwarn("BADCOMMAND", 327 $line, 0, $file, $l, 328 "$warn already disabled from line $ignore_set{$warn}"); 329 } 330 else { 331 $ignore{$warn}=$scope; 332 $ignore_set{$warn}=$line; 333 $ignore_line[$line]=$l; 334 } 335 } 336 elsif($enable eq "enable") { 337 enable_warn($what, $line, $file, $l); 338 } 339 else { 340 checkwarn("BADCOMMAND", 341 $line, 0, $file, $l, 342 "Illegal !checksrc! command"); 343 } 344 } 345} 346 347sub nostrings { 348 my ($str) = @_; 349 $str =~ s/\".*\"//g; 350 return $str; 351} 352 353sub scanfile { 354 my ($file) = @_; 355 356 my $line = 1; 357 my $prevl=""; 358 my $l; 359 open(R, "<$file") || die "failed to open $file"; 360 361 my $incomment=0; 362 my @copyright=(); 363 checksrc_clear(); # for file based ignores 364 accept_violations(); 365 366 while(<R>) { 367 $windows_os ? $_ =~ s/\r?\n$// : chomp; 368 my $l = $_; 369 my $ol = $l; # keep the unmodified line for error reporting 370 my $column = 0; 371 372 # check for !checksrc! commands 373 if($l =~ /\!checksrc\! (.*)/) { 374 my $cmd = $1; 375 checksrc($cmd, $line, $file, $l) 376 } 377 378 # check for a copyright statement and save the years 379 if($l =~ /\* +copyright .* \d\d\d\d/i) { 380 while($l =~ /([\d]{4})/g) { 381 push @copyright, { 382 year => $1, 383 line => $line, 384 col => index($l, $1), 385 code => $l 386 }; 387 } 388 } 389 390 # detect long lines 391 if(length($l) > $max_column) { 392 checkwarn("LONGLINE", $line, length($l), $file, $l, 393 "Longer than $max_column columns"); 394 } 395 # detect TAB characters 396 if($l =~ /^(.*)\t/) { 397 checkwarn("TABS", 398 $line, length($1), $file, $l, "Contains TAB character", 1); 399 } 400 # detect trailing white space 401 if($l =~ /^(.*)[ \t]+\z/) { 402 checkwarn("TRAILINGSPACE", 403 $line, length($1), $file, $l, "Trailing whitespace"); 404 } 405 406 # ------------------------------------------------------------ 407 # Above this marker, the checks were done on lines *including* 408 # comments 409 # ------------------------------------------------------------ 410 411 # strip off C89 comments 412 413 comment: 414 if(!$incomment) { 415 if($l =~ s/\/\*.*\*\// /g) { 416 # full /* comments */ were removed! 417 } 418 if($l =~ s/\/\*.*//) { 419 # start of /* comment was removed 420 $incomment = 1; 421 } 422 } 423 else { 424 if($l =~ s/.*\*\///) { 425 # end of comment */ was removed 426 $incomment = 0; 427 goto comment; 428 } 429 else { 430 # still within a comment 431 $l=""; 432 } 433 } 434 435 # ------------------------------------------------------------ 436 # Below this marker, the checks were done on lines *without* 437 # comments 438 # ------------------------------------------------------------ 439 440 # crude attempt to detect // comments without too many false 441 # positives 442 if($l =~ /^([^"\*]*)[^:"]\/\//) { 443 checkwarn("CPPCOMMENTS", 444 $line, length($1), $file, $l, "\/\/ comment"); 445 } 446 447 my $nostr = nostrings($l); 448 # check spaces after for/if/while/function call 449 if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) { 450 if($1 =~ / *\#/) { 451 # this is a #if, treat it differently 452 } 453 elsif(defined $3 && $3 eq "return") { 454 # return must have a space 455 } 456 elsif(defined $3 && $3 eq "case") { 457 # case must have a space 458 } 459 elsif($4 eq "*") { 460 # (* beginning makes the space OK! 461 } 462 elsif($1 =~ / *typedef/) { 463 # typedefs can use space-paren 464 } 465 else { 466 checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l, 467 "$2 with space"); 468 } 469 } 470 471 if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) { 472 my $pos = length($1); 473 my $postparen = $5; 474 my $cond = $4; 475 if($cond =~ / = /) { 476 checkwarn("ASSIGNWITHINCONDITION", 477 $line, $pos+1, $file, $l, 478 "assignment within conditional expression"); 479 } 480 my $temp = $cond; 481 $temp =~ s/\(//g; # remove open parens 482 my $openc = length($cond) - length($temp); 483 484 $temp = $cond; 485 $temp =~ s/\)//g; # remove close parens 486 my $closec = length($cond) - length($temp); 487 my $even = $openc == $closec; 488 489 if($l =~ / *\#/) { 490 # this is a #if, treat it differently 491 } 492 elsif($even && $postparen && 493 ($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) { 494 print STDERR "5: '$postparen'\n"; 495 checkwarn("ONELINECONDITION", 496 $line, length($l)-length($postparen), $file, $l, 497 "conditional block on the same line"); 498 } 499 } 500 # check spaces after open parentheses 501 if($l =~ /^(.*[a-z])\( /i) { 502 checkwarn("SPACEAFTERPAREN", 503 $line, length($1)+1, $file, $l, 504 "space after open parenthesis"); 505 } 506 507 # check spaces before close parentheses, unless it was a space or a 508 # close parenthesis! 509 if($l =~ /(.*[^\) ]) \)/) { 510 checkwarn("SPACEBEFORECLOSE", 511 $line, length($1)+1, $file, $l, 512 "space before close parenthesis"); 513 } 514 515 # check spaces before comma! 516 if($l =~ /(.*[^ ]) ,/) { 517 checkwarn("SPACEBEFORECOMMA", 518 $line, length($1)+1, $file, $l, 519 "space before comma"); 520 } 521 522 # check for "return(" without space 523 if($l =~ /^(.*)return\(/) { 524 if($1 =~ / *\#/) { 525 # this is a #if, treat it differently 526 } 527 else { 528 checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l, 529 "return without space before paren"); 530 } 531 } 532 533 # check for "sizeof" without parenthesis 534 if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) { 535 if($1 =~ / *\#/) { 536 # this is a #if, treat it differently 537 } 538 else { 539 checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l, 540 "sizeof without parenthesis"); 541 } 542 } 543 544 # check for comma without space 545 if($l =~ /^(.*),[^ \n]/) { 546 my $pref=$1; 547 my $ign=0; 548 if($pref =~ / *\#/) { 549 # this is a #if, treat it differently 550 $ign=1; 551 } 552 elsif($pref =~ /\/\*/) { 553 # this is a comment 554 $ign=1; 555 } 556 elsif($pref =~ /[\"\']/) { 557 $ign = 1; 558 # There is a quote here, figure out whether the comma is 559 # within a string or '' or not. 560 if($pref =~ /\"/) { 561 # within a string 562 } 563 elsif($pref =~ /\'$/) { 564 # a single letter 565 } 566 else { 567 $ign = 0; 568 } 569 } 570 if(!$ign) { 571 checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l, 572 "comma without following space"); 573 } 574 } 575 576 # check for "} else" 577 if($l =~ /^(.*)\} *else/) { 578 checkwarn("BRACEELSE", 579 $line, length($1), $file, $l, "else after closing brace on same line"); 580 } 581 # check for "){" 582 if($l =~ /^(.*)\)\{/) { 583 checkwarn("PARENBRACE", 584 $line, length($1)+1, $file, $l, "missing space after close paren"); 585 } 586 587 # check for space before the semicolon last in a line 588 if($l =~ /^(.*[^ ].*) ;$/) { 589 checkwarn("SPACESEMICOLON", 590 $line, length($1), $file, $ol, "space before last semicolon"); 591 } 592 593 # scan for use of banned functions 594 if($l =~ /^(.*\W) 595 (gets| 596 strtok| 597 v?sprintf| 598 (str|_mbs|_tcs|_wcs)n?cat| 599 LoadLibrary(Ex)?(A|W)?) 600 \s*\( 601 /x) { 602 checkwarn("BANNEDFUNC", 603 $line, length($1), $file, $ol, 604 "use of $2 is banned"); 605 } 606 607 # scan for use of snprintf for curl-internals reasons 608 if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) { 609 checkwarn("SNPRINTF", 610 $line, length($1), $file, $ol, 611 "use of $2 is banned"); 612 } 613 614 # scan for use of non-binary fopen without the macro 615 if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) { 616 my $mode = $2; 617 if($mode !~ /b/) { 618 checkwarn("FOPENMODE", 619 $line, length($1), $file, $ol, 620 "use of non-binary fopen without FOPEN_* macro: $mode"); 621 } 622 } 623 624 # check for open brace first on line but not first column 625 # only alert if previous line ended with a close paren and wasn't a cpp 626 # line 627 if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) { 628 checkwarn("BRACEPOS", 629 $line, length($1), $file, $ol, "badly placed open brace"); 630 } 631 632 # if the previous line starts with if/while/for AND ends with an open 633 # brace, or an else statement, check that this line is indented $indent 634 # more steps, if not a cpp line 635 if($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/) { 636 my $first = length($1); 637 638 # this line has some character besides spaces 639 if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) { 640 my $second = length($1); 641 my $expect = $first+$indent; 642 if($expect != $second) { 643 my $diff = $second - $first; 644 checkwarn("INDENTATION", $line, length($1), $file, $ol, 645 "not indented $indent steps (uses $diff)"); 646 647 } 648 } 649 } 650 651 # check for 'char * name' 652 if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) { 653 checkwarn("ASTERISKSPACE", 654 $line, length($1), $file, $ol, 655 "space after declarative asterisk"); 656 } 657 # check for 'char*' 658 if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) { 659 checkwarn("ASTERISKNOSPACE", 660 $line, length($1)-1, $file, $ol, 661 "no space before asterisk"); 662 } 663 664 # check for 'void func() {', but avoid false positives by requiring 665 # both an open and closed parentheses before the open brace 666 if($l =~ /^((\w).*)\{\z/) { 667 my $k = $1; 668 $k =~ s/const *//; 669 $k =~ s/static *//; 670 if($k =~ /\(.*\)/) { 671 checkwarn("BRACEPOS", 672 $line, length($l)-1, $file, $ol, 673 "wrongly placed open brace"); 674 } 675 } 676 677 # check for equals sign without spaces next to it 678 if($nostr =~ /(.*)\=[a-z0-9]/i) { 679 checkwarn("EQUALSNOSPACE", 680 $line, length($1)+1, $file, $ol, 681 "no space after equals sign"); 682 } 683 # check for equals sign without spaces before it 684 elsif($nostr =~ /(.*)[a-z0-9]\=/i) { 685 checkwarn("NOSPACEEQUALS", 686 $line, length($1)+1, $file, $ol, 687 "no space before equals sign"); 688 } 689 690 # check for plus signs without spaces next to it 691 if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) { 692 checkwarn("PLUSNOSPACE", 693 $line, length($1)+1, $file, $ol, 694 "no space after plus sign"); 695 } 696 # check for plus sign without spaces before it 697 elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) { 698 checkwarn("NOSPACEPLUS", 699 $line, length($1)+1, $file, $ol, 700 "no space before plus sign"); 701 } 702 703 # check for semicolons without space next to it 704 if($nostr =~ /(.*)\;[a-z0-9]/i) { 705 checkwarn("SEMINOSPACE", 706 $line, length($1)+1, $file, $ol, 707 "no space after semicolon"); 708 } 709 710 # typedef struct ... { 711 if($nostr =~ /^(.*)typedef struct.*{/) { 712 checkwarn("TYPEDEFSTRUCT", 713 $line, length($1)+1, $file, $ol, 714 "typedef'ed struct"); 715 } 716 717 # check for more than one consecutive space before open brace or 718 # question mark. Skip lines containing strings since they make it hard 719 # due to artificially getting multiple spaces 720 if(($l eq $nostr) && 721 $nostr =~ /^(.*(\S)) + [{?]/i) { 722 checkwarn("MULTISPACE", 723 $line, length($1)+1, $file, $ol, 724 "multiple space"); 725 print STDERR "L: $l\n"; 726 print STDERR "nostr: $nostr\n"; 727 } 728 729 $line++; 730 $prevl = $ol; 731 } 732 733 if(!scalar(@copyright)) { 734 checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1); 735 } 736 737 # COPYRIGHTYEAR is a extended warning so we must first see if it has been 738 # enabled in .checksrc 739 if(defined($warnings{"COPYRIGHTYEAR"})) { 740 # The check for updated copyrightyear is overly complicated in order to 741 # not punish current hacking for past sins. The copyright years are 742 # right now a bit behind, so enforcing copyright year checking on all 743 # files would cause hundreds of errors. Instead we only look at files 744 # which are tracked in the Git repo and edited in the workdir, or 745 # committed locally on the branch without being in upstream master. 746 # 747 # The simple and naive test is to simply check for the current year, 748 # but updating the year even without an edit is against project policy 749 # (and it would fail every file on January 1st). 750 # 751 # A rather more interesting, and correct, check would be to not test 752 # only locally committed files but inspect all files wrt the year of 753 # their last commit. Removing the `git rev-list origin/master..HEAD` 754 # condition below will enfore copyright year checks against the year 755 # the file was last committed (and thus edited to some degree). 756 my $commityear = undef; 757 @copyright = sort {$$b{year} cmp $$a{year}} @copyright; 758 759 # if the file is modified, assume commit year this year 760 if(`git status -s -- $file` =~ /^ [MARCU]/) { 761 $commityear = (localtime(time))[5] + 1900; 762 } 763 else { 764 # min-parents=1 to ignore wrong initial commit in truncated repos 765 my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`; 766 if($grl) { 767 chomp $grl; 768 $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900; 769 } 770 } 771 772 if(defined($commityear) && scalar(@copyright) && 773 $copyright[0]{year} != $commityear) { 774 checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col}, 775 $file, $copyright[0]{code}, 776 "Copyright year out of date, should be $commityear, " . 777 "is $copyright[0]{year}", 1); 778 } 779 } 780 781 if($incomment) { 782 checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1); 783 } 784 785 checksrc_endoffile($file); 786 787 close(R); 788 789} 790 791 792if($errors || $warnings || $verbose) { 793 printf "checksrc: %d errors and %d warnings\n", $errors, $warnings; 794 if($suppressed) { 795 printf "checksrc: %d errors and %d warnings suppressed\n", 796 $serrors, 797 $swarnings; 798 } 799 exit 5; # return failure 800} 801