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