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