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