2
#***************************************************************************
4
# Project ___| | | | _ \| |
6
# | (__| |_| | _ <| |___
7
# \___|\___/|_| \_\_____|
9
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
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.se/docs/copyright.html.
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.
19
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20
# KIND, either express or implied.
22
# SPDX-License-Identifier: curl
24
###########################################################################
36
my $suppressed; # skipped problems
41
my $windows_os = $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys';
50
my %warnings_extended = (
51
'COPYRIGHTYEAR' => 'copyright year incorrect',
52
'STRERROR', => 'strerror() detected',
53
'STDERR', => 'stderr detected',
57
'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
58
'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
59
'ASTERISKSPACE' => 'pointer declared with space after asterisk',
60
'BADCOMMAND' => 'bad !checksrc! instruction',
61
'BANNEDFUNC' => 'a banned function was used',
62
'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line',
63
'BRACEELSE' => '} else on the same line',
64
'BRACEPOS' => 'wrong position for an open brace',
65
'BRACEWHILE' => 'A single space between open brace and while',
66
'COMMANOSPACE' => 'comma without following space',
67
'COMMENTNOSPACEEND' => 'no space before */',
68
'COMMENTNOSPACESTART' => 'no space following /*',
69
'COPYRIGHT' => 'file missing a copyright statement',
70
'CPPCOMMENTS' => '// comment detected',
71
'DOBRACE' => 'A single space between do and open brace',
72
'EMPTYLINEBRACE' => 'Empty line before the open brace',
73
'EQUALSNOSPACE' => 'equals sign without following space',
74
'EQUALSNULL' => 'if/while comparison with == NULL',
75
'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression',
76
'FOPENMODE' => 'fopen needs a macro for the mode string',
77
'INCLUDEDUP', => 'same file is included again',
78
'INDENTATION' => 'wrong start column for code',
79
'LONGLINE' => "Line longer than $max_column",
80
'SPACEBEFORELABEL' => 'labels not at the start of the line',
81
'MULTISPACE' => 'multiple spaces used when not suitable',
82
'NOSPACEEQUALS' => 'equals sign without preceding space',
83
'NOTEQUALSZERO', => 'if/while comparison with != 0',
84
'ONELINECONDITION' => 'conditional block on the same line as the if()',
85
'OPENCOMMENT' => 'file ended with a /* comment still "open"',
86
'PARENBRACE' => '){ without sufficient space',
87
'RETURNNOSPACE' => 'return without space',
88
'SEMINOSPACE' => 'semicolon without following space',
89
'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
90
'SNPRINTF' => 'use of snprintf',
91
'SPACEAFTERPAREN' => 'space after open parenthesis',
92
'SPACEBEFORECLOSE' => 'space before a close parenthesis',
93
'SPACEBEFORECOMMA' => 'space before a comma',
94
'SPACEBEFOREPAREN' => 'space before an open parenthesis',
95
'SPACESEMICOLON' => 'space before semicolon',
96
'SPACESWITCHCOLON' => 'space before colon of switch label',
97
'TABS' => 'TAB characters not allowed',
98
'TRAILINGSPACE' => 'Trailing whitespace on the line',
99
'TYPEDEFSTRUCT' => 'typedefed struct',
100
'UNUSEDIGNORE' => 'a warning ignore was not used',
104
open(my $W, '<', "$dir/checksrc.skip") or return;
107
$windows_os ? $_ =~ s/\r?\n$// : chomp;
113
# Reads the .checksrc in $dir for any extended warnings to enable locally.
114
# Currently there is no support for disabling warnings from the standard set,
115
# and since that's already handled via !checksrc! commands there is probably
116
# little use to add it.
120
open(my $rcfile, "<", "$dir/.checksrc") or return;
125
# Lines starting with '#' are considered comments
129
elsif (/^\s*enable ([A-Z]+)$/) {
130
if(!defined($warnings_extended{$1})) {
131
print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
134
$warnings{$1} = $warnings_extended{$1};
136
elsif (/^\s*disable ([A-Z]+)$/) {
137
if(!defined($warnings{$1})) {
138
print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
145
die "Invalid format in $dir/.checksrc on line $i\n";
152
my ($name, $num, $col, $file, $line, $msg, $error) = @_;
154
my $w=$error?"error":"warning";
157
#if(!$warnings{$name}) {
158
# print STDERR "Dev! there's no description for $name!\n";
162
if($skiplist{$line}) {
165
# !checksrc! controlled
166
elsif($ignore{$name}) {
168
$ignore_used{$name}++;
170
if(!$ignore{$name}) {
171
# reached zero, enable again
172
enable_warn($name, $num, $file, $line);
195
print "$file:$num:$col: $w: $msg ($name)\n";
199
my $pref = (' ' x $col);
206
while(defined $file) {
208
if($file =~ /-D(.*)/) {
213
elsif($file =~ /-W(.*)/) {
218
elsif($file =~ /-A(.+)/) {
223
elsif($file =~ /-i([1-9])/) {
228
elsif($file =~ /-m([0-9]+)/) {
229
$max_column = $1 + 0;
233
elsif($file =~ /^(-h|--help)/) {
242
print "checksrc.pl [option] <file1> [file2] ...\n";
244
print " -A[rule] Accept this violation, can be used multiple times\n";
245
print " -D[DIR] Directory to prepend file names\n";
246
print " -h Show help output\n";
247
print " -W[file] Skip the given file - ignore all its flaws\n";
248
print " -i<n> Indent spaces. Default: 2\n";
249
print " -m<n> Maximum line length. Default: 79\n";
250
print "\nDetects and warns for these problems:\n";
251
my @allw = keys %warnings;
252
push @allw, keys %warnings_extended;
253
for my $w (sort @allw) {
255
printf (" %-18s: %s\n", $w, $warnings{$w});
258
printf (" %-18s: %s[*]\n", $w, $warnings_extended{$w});
261
print " [*] = disabled by default\n";
269
if("$wlist" !~ / $file /) {
270
my $fullname = $file;
271
$fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
278
sub accept_violations {
281
print "'$r' is not a warning to accept!\n";
295
sub checksrc_endoffile {
297
for(keys %ignore_set) {
298
if($ignore_set{$_} && !$ignore_used{$_}) {
299
checkwarn("UNUSEDIGNORE", $ignore_set{$_},
300
length($_)+11, $file,
301
$ignore_line[$ignore_set{$_}],
302
"Unused ignore: $_");
308
my ($what, $line, $file, $l) = @_;
310
# switch it back on, but warn if not triggered!
311
if(!$ignore_used{$what}) {
312
checkwarn("UNUSEDIGNORE",
313
$line, length($what) + 11, $file, $l,
314
"No warning was inhibited!");
316
$ignore_set{$what}=0;
317
$ignore_used{$what}=0;
321
my ($cmd, $line, $file, $l) = @_;
322
if($cmd =~ / *([^ ]*) *(.*)/) {
323
my ($enable, $what) = ($1, $2);
324
$what =~ s: *\*/$::; # cut off end of C comment
325
# print "ENABLE $enable WHAT $what\n";
326
if($enable eq "disable") {
327
my ($warn, $scope)=($1, $2);
328
if($what =~ /([^ ]*) +(.*)/) {
329
($warn, $scope)=($1, $2);
335
# print "IGNORE $warn for SCOPE $scope\n";
336
if($scope eq "all") {
340
# Comparing for a literal zero rather than the scalar value zero
341
# covers the case where $scope contains the ending '*' from the
342
# comment. If we use a scalar comparison (==) we induce warnings
343
# on non-scalar contents.
345
checkwarn("BADCOMMAND",
347
"Disable zero not supported, did you mean to enable?");
349
elsif($ignore_set{$warn}) {
350
checkwarn("BADCOMMAND",
352
"$warn already disabled from line $ignore_set{$warn}");
355
$ignore{$warn}=$scope;
356
$ignore_set{$warn}=$line;
357
$ignore_line[$line]=$l;
360
elsif($enable eq "enable") {
361
enable_warn($what, $line, $file, $l);
364
checkwarn("BADCOMMAND",
366
"Illegal !checksrc! command");
386
open(my $R, '<', $file) || die "failed to open $file";
391
checksrc_clear(); # for file based ignores
395
$windows_os ? $_ =~ s/\r?\n$// : chomp;
397
my $ol = $l; # keep the unmodified line for error reporting
400
# check for !checksrc! commands
401
if($l =~ /\!checksrc\! (.*)/) {
403
checksrc($cmd, $line, $file, $l)
406
# check for a copyright statement and save the years
407
if($l =~ /\* +copyright .* (\d\d\d\d|)/i) {
409
while($l =~ /([\d]{4})/g) {
413
col => index($l, $1),
423
col => index($l, $1),
430
if(length($l) > $max_column) {
431
checkwarn("LONGLINE", $line, length($l), $file, $l,
432
"Longer than $max_column columns");
434
# detect TAB characters
435
if($l =~ /^(.*)\t/) {
437
$line, length($1), $file, $l, "Contains TAB character", 1);
439
# detect trailing whitespace
440
if($l =~ /^(.*)[ \t]+\z/) {
441
checkwarn("TRAILINGSPACE",
442
$line, length($1), $file, $l, "Trailing whitespace");
445
# no space after comment start
446
if($l =~ /^(.*)\/\*\w/) {
447
checkwarn("COMMENTNOSPACESTART",
448
$line, length($1) + 2, $file, $l,
449
"Missing space after comment start");
451
# no space at comment end
452
if($l =~ /^(.*)\w\*\//) {
453
checkwarn("COMMENTNOSPACEEND",
454
$line, length($1) + 1, $file, $l,
455
"Missing space end comment end");
457
# ------------------------------------------------------------
458
# Above this marker, the checks were done on lines *including*
460
# ------------------------------------------------------------
462
# strip off C89 comments
466
if($l =~ s/\/\*.*\*\// /g) {
467
# full /* comments */ were removed!
469
if($l =~ s/\/\*.*//) {
470
# start of /* comment was removed
475
if($l =~ s/.*\*\///) {
476
# end of comment */ was removed
481
# still within a comment
486
# ------------------------------------------------------------
487
# Below this marker, the checks were done on lines *without*
489
# ------------------------------------------------------------
491
# prev line was a preprocessor **and** ended with a backslash
492
if($prep && ($prevpl =~ /\\ *\z/)) {
493
# this is still a preprocessor line
499
# crude attempt to detect // comments without too many false
501
if($l =~ /^(([^"\*]*)[^:"]|)\/\//) {
502
checkwarn("CPPCOMMENTS",
503
$line, length($1), $file, $l, "\/\/ comment");
506
if($l =~ /^(\#\s*include\s+)([\">].*[>}"])/) {
507
my ($pre, $path) = ($1, $2);
508
if($includes{$path}) {
509
checkwarn("INCLUDEDUP",
510
$line, length($1), $file, $l, "duplicated include");
512
$includes{$path} = $l;
515
# detect and strip preprocessor directives
516
if($l =~ /^[ \t]*\#/) {
522
my $nostr = nostrings($l);
523
# check spaces after for/if/while/function call
524
if($nostr =~ /^(.*)(for|if|while|switch| ([a-zA-Z0-9_]+)) \((.)/) {
525
my ($leading, $word, $extra, $first)=($1,$2,$3,$4);
527
# this is a #if, treat it differently
529
elsif(defined $3 && $3 eq "return") {
530
# return must have a space
532
elsif(defined $3 && $3 eq "case") {
533
# case must have a space
535
elsif(($first eq "*") && ($word !~ /(for|if|while|switch)/)) {
536
# A "(*" beginning makes the space OK because it wants to
537
# allow function pointer declared
539
elsif($1 =~ / *typedef/) {
540
# typedefs can use space-paren
543
checkwarn("SPACEBEFOREPAREN", $line, length($leading)+length($word), $file, $l,
547
# check for '== NULL' in if/while conditions but not if the thing on
548
# the left of it is a function call
549
if($nostr =~ /^(.*)(if|while)(\(.*?)([!=]= NULL|NULL [!=]=)/) {
550
checkwarn("EQUALSNULL", $line,
551
length($1) + length($2) + length($3),
552
$file, $l, "we prefer !variable instead of \"== NULL\" comparisons");
555
# check for '!= 0' in if/while conditions but not if the thing on
556
# the left of it is a function call
557
if($nostr =~ /^(.*)(if|while)(\(.*[^)]) != 0[^x]/) {
558
checkwarn("NOTEQUALSZERO", $line,
559
length($1) + length($2) + length($3),
560
$file, $l, "we prefer if(rc) instead of \"rc != 0\" comparisons");
563
# check spaces in 'do {'
564
if($nostr =~ /^( *)do( *)\{/ && length($2) != 1) {
565
checkwarn("DOBRACE", $line, length($1) + 2, $file, $l, "one space after do before brace");
567
# check spaces in 'do {'
568
elsif($nostr =~ /^( *)\}( *)while/ && length($2) != 1) {
569
checkwarn("BRACEWHILE", $line, length($1) + 2, $file, $l, "one space between brace and while");
571
if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
572
my $pos = length($1);
576
checkwarn("ASSIGNWITHINCONDITION",
577
$line, $pos+1, $file, $l,
578
"assignment within conditional expression");
581
$temp =~ s/\(//g; # remove open parens
582
my $openc = length($cond) - length($temp);
585
$temp =~ s/\)//g; # remove close parens
586
my $closec = length($cond) - length($temp);
587
my $even = $openc == $closec;
590
# this is a #if, treat it differently
592
elsif($even && $postparen &&
593
($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
594
checkwarn("ONELINECONDITION",
595
$line, length($l)-length($postparen), $file, $l,
596
"conditional block on the same line");
599
# check spaces after open parentheses
600
if($l =~ /^(.*[a-z])\( /i) {
601
checkwarn("SPACEAFTERPAREN",
602
$line, length($1)+1, $file, $l,
603
"space after open parenthesis");
606
# check spaces before close parentheses, unless it was a space or a
608
if($l =~ /(.*[^\) ]) \)/) {
609
checkwarn("SPACEBEFORECLOSE",
610
$line, length($1)+1, $file, $l,
611
"space before close parenthesis");
614
# check spaces before comma!
615
if($l =~ /(.*[^ ]) ,/) {
616
checkwarn("SPACEBEFORECOMMA",
617
$line, length($1)+1, $file, $l,
618
"space before comma");
621
# check for "return(" without space
622
if($l =~ /^(.*)return\(/) {
624
# this is a #if, treat it differently
627
checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
628
"return without space before paren");
632
# check for "sizeof" without parenthesis
633
if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
635
# this is a #if, treat it differently
638
checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
639
"sizeof without parenthesis");
643
# check for comma without space
644
if($l =~ /^(.*),[^ \n]/) {
647
if($pref =~ / *\#/) {
648
# this is a #if, treat it differently
651
elsif($pref =~ /\/\*/) {
655
elsif($pref =~ /[\"\']/) {
657
# There is a quote here, figure out whether the comma is
658
# within a string or '' or not.
662
elsif($pref =~ /\'$/) {
670
checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
671
"comma without following space");
676
if($l =~ /^(.*)\} *else/) {
677
checkwarn("BRACEELSE",
678
$line, length($1), $file, $l, "else after closing brace on same line");
681
if($l =~ /^(.*)\)\{/) {
682
checkwarn("PARENBRACE",
683
$line, length($1)+1, $file, $l, "missing space after close paren");
685
# check for "^{" with an empty line before it
686
if(($l =~ /^\{/) && ($prevl =~ /^[ \t]*\z/)) {
687
checkwarn("EMPTYLINEBRACE",
688
$line, 0, $file, $l, "empty line before open brace");
691
# check for space before the semicolon last in a line
692
if($l =~ /^(.*[^ ].*) ;$/) {
693
checkwarn("SPACESEMICOLON",
694
$line, length($1), $file, $ol, "no space before semicolon");
697
# check for space before the colon in a switch label
698
if($l =~ /^( *(case .+|default)) :/) {
699
checkwarn("SPACESWITCHCOLON",
700
$line, length($1), $file, $ol, "no space before colon of switch label");
703
if($prevl !~ /\?\z/ && $l =~ /^ +([A-Za-z_][A-Za-z0-9_]*):$/ && $1 ne 'default') {
704
checkwarn("SPACEBEFORELABEL",
705
$line, length($1), $file, $ol, "no space before label");
708
# scan for use of banned functions
714
(str|_mbs|_tcs|_wcs)n?cat|
715
LoadLibrary(Ex)?(A|W)?)
718
checkwarn("BANNEDFUNC",
719
$line, length($1), $file, $ol,
720
"use of $2 is banned");
722
if($warnings{"STRERROR"}) {
723
# scan for use of banned strerror. This is not a BANNEDFUNC to
724
# allow for individual enable/disable of this warning.
725
if($l =~ /^(.*\W)(strerror)\s*\(/x) {
727
# skip preprocessor lines
728
checkwarn("STRERROR",
729
$line, length($1), $file, $ol,
730
"use of $2 is banned");
734
if($warnings{"STDERR"}) {
735
# scan for use of banned stderr. This is not a BANNEDFUNC to
736
# allow for individual enable/disable of this warning.
737
if($l =~ /^([^\"-]*\W)(stderr)[^\"_]/x) {
739
# skip preprocessor lines
741
$line, length($1), $file, $ol,
742
"use of $2 is banned (use tool_stderr instead)");
746
# scan for use of snprintf for curl-internals reasons
747
if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) {
748
checkwarn("SNPRINTF",
749
$line, length($1), $file, $ol,
750
"use of $2 is banned");
753
# scan for use of non-binary fopen without the macro
754
if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
757
checkwarn("FOPENMODE",
758
$line, length($1), $file, $ol,
759
"use of non-binary fopen without FOPEN_* macro: $mode");
763
# check for open brace first on line but not first column only alert
764
# if previous line ended with a close paren and it wasn't a cpp line
765
if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
766
checkwarn("BRACEPOS",
767
$line, length($1), $file, $ol, "badly placed open brace");
770
# if the previous line starts with if/while/for AND ends with an open
771
# brace, or an else statement, check that this line is indented $indent
772
# more steps, if not a cpp line
773
if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
774
my $first = length($1);
775
# this line has some character besides spaces
776
if($l =~ /^( *)[^ ]/) {
777
my $second = length($1);
778
my $expect = $first+$indent;
779
if($expect != $second) {
780
my $diff = $second - $first;
781
checkwarn("INDENTATION", $line, length($1), $file, $ol,
782
"not indented $indent steps (uses $diff)");
788
# if the previous line starts with if/while/for AND ends with a closed
789
# parenthesis and there's an equal number of open and closed
790
# parentheses, check that this line is indented $indent more steps, if
792
elsif(!$prevp && ($prevl =~ /^( *)(if|while|for)(\(.*\))\z/)) {
793
my $first = length($1);
800
if(length($op) == length($cl)) {
801
# this line has some character besides spaces
802
if($l =~ /^( *)[^ ]/) {
803
my $second = length($1);
804
my $expect = $first+$indent;
805
if($expect != $second) {
806
my $diff = $second - $first;
807
checkwarn("INDENTATION", $line, length($1), $file, $ol,
808
"not indented $indent steps (uses $diff)");
814
# check for 'char * name'
815
if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
816
checkwarn("ASTERISKSPACE",
817
$line, length($1), $file, $ol,
818
"space after declarative asterisk");
821
if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
822
checkwarn("ASTERISKNOSPACE",
823
$line, length($1)-1, $file, $ol,
824
"no space before asterisk");
827
# check for 'void func() {', but avoid false positives by requiring
828
# both an open and closed parentheses before the open brace
829
if($l =~ /^((\w).*)\{\z/) {
834
checkwarn("BRACEPOS",
835
$line, length($l)-1, $file, $ol,
836
"wrongly placed open brace");
840
# check for equals sign without spaces next to it
841
if($nostr =~ /(.*)\=[a-z0-9]/i) {
842
checkwarn("EQUALSNOSPACE",
843
$line, length($1)+1, $file, $ol,
844
"no space after equals sign");
846
# check for equals sign without spaces before it
847
elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
848
checkwarn("NOSPACEEQUALS",
849
$line, length($1)+1, $file, $ol,
850
"no space before equals sign");
853
# check for plus signs without spaces next to it
854
if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
855
checkwarn("PLUSNOSPACE",
856
$line, length($1)+1, $file, $ol,
857
"no space after plus sign");
859
# check for plus sign without spaces before it
860
elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
861
checkwarn("NOSPACEPLUS",
862
$line, length($1)+1, $file, $ol,
863
"no space before plus sign");
866
# check for semicolons without space next to it
867
if($nostr =~ /(.*)\;[a-z0-9]/i) {
868
checkwarn("SEMINOSPACE",
869
$line, length($1)+1, $file, $ol,
870
"no space after semicolon");
873
# typedef struct ... {
874
if($nostr =~ /^(.*)typedef struct.*{/) {
875
checkwarn("TYPEDEFSTRUCT",
876
$line, length($1)+1, $file, $ol,
877
"typedef'ed struct");
880
if($nostr =~ /(.*)! +(\w|\()/) {
881
checkwarn("EXCLAMATIONSPACE",
882
$line, length($1)+1, $file, $ol,
883
"space after exclamation mark");
886
# check for more than one consecutive space before open brace or
887
# question mark. Skip lines containing strings since they make it hard
888
# due to artificially getting multiple spaces
890
$nostr =~ /^(.*(\S)) + [{?]/i) {
891
checkwarn("MULTISPACE",
892
$line, length($1)+1, $file, $ol,
897
# scan for use of banned symbols on a preprocessor line
902
checkwarn("BANNEDPREPROC",
903
$line, length($1), $file, $ol,
904
"use of $2 is banned from preprocessor lines" .
905
(($2 eq "WIN32") ? ", use _WIN32 instead" : ""));
910
$prevl = $ol if(!$prep);
911
$prevpl = $ol if($prep);
914
if(!scalar(@copyright)) {
915
checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
918
# COPYRIGHTYEAR is a extended warning so we must first see if it has been
919
# enabled in .checksrc
920
if(defined($warnings{"COPYRIGHTYEAR"})) {
921
# The check for updated copyrightyear is overly complicated in order to
922
# not punish current hacking for past sins. The copyright years are
923
# right now a bit behind, so enforcing copyright year checking on all
924
# files would cause hundreds of errors. Instead we only look at files
925
# which are tracked in the Git repo and edited in the workdir, or
926
# committed locally on the branch without being in upstream master.
928
# The simple and naive test is to simply check for the current year,
929
# but updating the year even without an edit is against project policy
930
# (and it would fail every file on January 1st).
932
# A rather more interesting, and correct, check would be to not test
933
# only locally committed files but inspect all files wrt the year of
934
# their last commit. Removing the `git rev-list origin/master..HEAD`
935
# condition below will enforce copyright year checks against the year
936
# the file was last committed (and thus edited to some degree).
937
my $commityear = undef;
938
@copyright = sort {$$b{year} cmp $$a{year}} @copyright;
940
# if the file is modified, assume commit year this year
941
if(`git status -s -- "$file"` =~ /^ [MARCU]/) {
942
$commityear = (localtime(time))[5] + 1900;
945
# min-parents=1 to ignore wrong initial commit in truncated repos
946
my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- "$file"`;
949
$commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
953
if(defined($commityear) && scalar(@copyright) &&
954
$copyright[0]{year} != $commityear) {
955
checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
956
$file, $copyright[0]{code},
957
"Copyright year out of date, should be $commityear, " .
958
"is $copyright[0]{year}", 1);
963
checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
966
checksrc_endoffile($file);
973
if($errors || $warnings || $verbose) {
974
printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
976
printf "checksrc: %d errors and %d warnings suppressed\n",
980
exit 5; # return failure