Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/scripts/checksrc.pl
2066 views
1
#!/usr/bin/env perl
2
#***************************************************************************
3
# _ _ ____ _
4
# Project ___| | | | _ \| |
5
# / __| | | | |_) | |
6
# | (__| |_| | _ <| |___
7
# \___|\___/|_| \_\_____|
8
#
9
# Copyright (C) Daniel Stenberg, <[email protected]>, 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
26
use strict;
27
use warnings;
28
29
my $max_column = 79;
30
my $indent = 2;
31
32
my $warnings = 0;
33
my $swarnings = 0;
34
my $errors = 0;
35
my $serrors = 0;
36
my $suppressed; # skipped problems
37
my $file;
38
my $dir=".";
39
my $wlist="";
40
my @alist;
41
my $windows_os = $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys';
42
my $verbose = 0;
43
my %skiplist;
44
45
my %ignore;
46
my %ignore_set;
47
my %ignore_used;
48
my @ignore_line;
49
50
my %banfunc = (
51
"gmtime" => 1,
52
"localtime" => 1,
53
"gets" => 1,
54
"strtok" => 1,
55
"sprintf" => 1,
56
"vsprintf" => 1,
57
"strcat" => 1,
58
"strncat" => 1,
59
"_mbscat" => 1,
60
"_mbsncat" => 1,
61
"_tcscat" => 1,
62
"_tcsncat" => 1,
63
"_wcscat" => 1,
64
"_wcsncat" => 1,
65
"LoadLibrary" => 1,
66
"LoadLibraryA" => 1,
67
"LoadLibraryW" => 1,
68
"LoadLibraryEx" => 1,
69
"LoadLibraryExA" => 1,
70
"LoadLibraryExW" => 1,
71
"_waccess" => 1,
72
"_access" => 1,
73
"access" => 1,
74
);
75
76
my %warnings_extended = (
77
'COPYRIGHTYEAR' => 'copyright year incorrect',
78
'STDERR', => 'stderr detected',
79
);
80
81
my %warnings = (
82
'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
83
'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
84
'ASTERISKSPACE' => 'pointer declared with space after asterisk',
85
'BADCOMMAND' => 'bad !checksrc! instruction',
86
'BANNEDFUNC' => 'a banned function was used',
87
'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line',
88
'BRACEELSE' => '} else on the same line',
89
'BRACEPOS' => 'wrong position for an open brace',
90
'BRACEWHILE' => 'A single space between open brace and while',
91
'COMMANOSPACE' => 'comma without following space',
92
'COMMENTNOSPACEEND' => 'no space before */',
93
'COMMENTNOSPACESTART' => 'no space following /*',
94
'COPYRIGHT' => 'file missing a copyright statement',
95
'CPPCOMMENTS' => '// comment detected',
96
'DOBRACE' => 'A single space between do and open brace',
97
'EMPTYLINEBRACE' => 'Empty line before the open brace',
98
'EQUALSNOSPACE' => 'equals sign without following space',
99
'EQUALSNULL' => 'if/while comparison with == NULL',
100
'ERRNOVAR' => 'use of bare errno define',
101
'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression',
102
'FOPENMODE' => 'fopen needs a macro for the mode string',
103
'INCLUDEDUP', => 'same file is included again',
104
'INDENTATION' => 'wrong start column for code',
105
'LONGLINE' => "Line longer than $max_column",
106
'SPACEBEFORELABEL' => 'labels not at the start of the line',
107
'MULTISPACE' => 'multiple spaces used when not suitable',
108
'NOSPACEAND' => 'missing space around Logical AND operator',
109
'NOSPACEC' => 'missing space around ternary colon operator',
110
'NOSPACEEQUALS' => 'equals sign without preceding space',
111
'NOSPACEQ' => 'missing space around ternary question mark operator',
112
'NOSPACETHAN' => 'missing space around less or greater than',
113
'NOTEQUALSZERO', => 'if/while comparison with != 0',
114
'ONELINECONDITION' => 'conditional block on the same line as the if()',
115
'OPENCOMMENT' => 'file ended with a /* comment still "open"',
116
'PARENBRACE' => '){ without sufficient space',
117
'RETURNNOSPACE' => 'return without space',
118
'SEMINOSPACE' => 'semicolon without following space',
119
'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
120
'SPACEAFTERPAREN' => 'space after open parenthesis',
121
'SPACEBEFORECLOSE' => 'space before a close parenthesis',
122
'SPACEBEFORECOMMA' => 'space before a comma',
123
'SPACEBEFOREPAREN' => 'space before an open parenthesis',
124
'SPACESEMICOLON' => 'space before semicolon',
125
'SPACESWITCHCOLON' => 'space before colon of switch label',
126
'TABS' => 'TAB characters not allowed',
127
'TRAILINGSPACE' => 'Trailing whitespace on the line',
128
'TYPEDEFSTRUCT' => 'typedefed struct',
129
'UNUSEDIGNORE' => 'a warning ignore was not used',
130
);
131
132
sub readskiplist {
133
open(my $W, '<', "$dir/checksrc.skip") or return;
134
my @all=<$W>;
135
for(@all) {
136
$windows_os ? $_ =~ s/\r?\n$// : chomp;
137
$skiplist{$_}=1;
138
}
139
close($W);
140
}
141
142
# Reads the .checksrc in $dir for any extended warnings to enable locally.
143
# Currently there is no support for disabling warnings from the standard set,
144
# and since that's already handled via !checksrc! commands there is probably
145
# little use to add it.
146
sub readlocalfile {
147
my ($file) = @_;
148
my $i = 0;
149
my $rcfile;
150
151
if(($dir eq ".") && $file =~ /\//) {
152
my $ldir;
153
if($file =~ /(.*)\//) {
154
$ldir = $1;
155
open($rcfile, "<", "$dir/$ldir/.checksrc") or return;
156
}
157
}
158
else {
159
open($rcfile, "<", "$dir/.checksrc") or return;
160
}
161
162
while(<$rcfile>) {
163
$windows_os ? $_ =~ s/\r?\n$// : chomp;
164
$i++;
165
166
# Lines starting with '#' are considered comments
167
if (/^\s*(#.*)/) {
168
next;
169
}
170
elsif (/^enable ([A-Z]+)$/) {
171
if(!defined($warnings_extended{$1})) {
172
print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
173
next;
174
}
175
$warnings{$1} = $warnings_extended{$1};
176
}
177
elsif (/^disable ([A-Z]+)$/) {
178
if(!defined($warnings{$1})) {
179
print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
180
next;
181
}
182
# Accept-list
183
push @alist, $1;
184
}
185
elsif (/^banfunc ([^ ]*)/) {
186
$banfunc{$1} = $1;
187
}
188
elsif (/^allowfunc ([^ ]*)/) {
189
undef $banfunc{$1};
190
}
191
else {
192
die "Invalid format in $dir/.checksrc on line $i: $_\n";
193
}
194
}
195
close($rcfile);
196
}
197
198
sub checkwarn {
199
my ($name, $num, $col, $file, $line, $msg, $error) = @_;
200
201
my $w=$error?"error":"warning";
202
my $nowarn=0;
203
204
#if(!$warnings{$name}) {
205
# print STDERR "Dev! there's no description for $name!\n";
206
#}
207
208
# checksrc.skip
209
if($skiplist{$line}) {
210
$nowarn = 1;
211
}
212
# !checksrc! controlled
213
elsif($ignore{$name}) {
214
$ignore{$name}--;
215
$ignore_used{$name}++;
216
$nowarn = 1;
217
if(!$ignore{$name}) {
218
# reached zero, enable again
219
enable_warn($name, $num, $file, $line);
220
}
221
}
222
223
if($nowarn) {
224
$suppressed++;
225
if($w) {
226
$swarnings++;
227
}
228
else {
229
$serrors++;
230
}
231
return;
232
}
233
234
if($w) {
235
$warnings++;
236
}
237
else {
238
$errors++;
239
}
240
241
$col++;
242
print "$file:$num:$col: $w: $msg ($name)\n";
243
print " $line\n";
244
245
if($col < 80) {
246
my $pref = (' ' x $col);
247
print "${pref}^\n";
248
}
249
}
250
251
$file = shift @ARGV;
252
253
while(defined $file) {
254
255
if($file =~ /^-D(.*)/) {
256
$dir = $1;
257
$file = shift @ARGV;
258
next;
259
}
260
elsif($file =~ /^-W(.*)/) {
261
$wlist .= " $1 ";
262
$file = shift @ARGV;
263
next;
264
}
265
elsif($file =~ /^-b(.*)/) {
266
$banfunc{$1} = $1;
267
print STDERR "ban use of \"$1\"\n";
268
$file = shift @ARGV;
269
next;
270
}
271
elsif($file =~ /^-a(.*)/) {
272
undef $banfunc{$1};
273
$file = shift @ARGV;
274
next;
275
}
276
elsif($file =~ /^-A(.+)/) {
277
push @alist, $1;
278
$file = shift @ARGV;
279
next;
280
}
281
elsif($file =~ /^-i([1-9])/) {
282
$indent = $1 + 0;
283
$file = shift @ARGV;
284
next;
285
}
286
elsif($file =~ /^-m([0-9]+)/) {
287
$max_column = $1 + 0;
288
$file = shift @ARGV;
289
next;
290
}
291
elsif($file =~ /^-v/) {
292
$verbose = 1;
293
$file = shift @ARGV;
294
next;
295
}
296
elsif($file =~ /^(-h|--help)/) {
297
undef $file;
298
last;
299
}
300
301
last;
302
}
303
304
if(!$file) {
305
print "checksrc.pl [option] <file1> [file2] ...\n";
306
print " Options:\n";
307
print " -A[rule] Accept this violation, can be used multiple times\n";
308
print " -a[func] Allow use of this function\n";
309
print " -b[func] Ban use of this function\n";
310
print " -D[DIR] Directory to prepend file names\n";
311
print " -h Show help output\n";
312
print " -W[file] Skip the given file - ignore all its flaws\n";
313
print " -i<n> Indent spaces. Default: 2\n";
314
print " -m<n> Maximum line length. Default: 79\n";
315
print " -v Verbose\n";
316
print "\nDetects and warns for these problems:\n";
317
my @allw = keys %warnings;
318
push @allw, keys %warnings_extended;
319
for my $w (sort @allw) {
320
if($warnings{$w}) {
321
printf (" %-18s: %s\n", $w, $warnings{$w});
322
}
323
else {
324
printf (" %-18s: %s[*]\n", $w, $warnings_extended{$w});
325
}
326
}
327
print " [*] = disabled by default\n";
328
329
print "\nDetects and bans use of these functions:\n";
330
for my $f (sort keys %banfunc) {
331
printf (" %-18s\n", $f);
332
}
333
exit;
334
}
335
336
readskiplist();
337
readlocalfile($file);
338
339
do {
340
if("$wlist" !~ / $file /) {
341
my $fullname = $file;
342
$fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
343
scanfile($fullname);
344
}
345
$file = shift @ARGV;
346
347
} while($file);
348
349
sub accept_violations {
350
for my $r (@alist) {
351
if(!$warnings{$r}) {
352
print "'$r' is not a warning to accept!\n";
353
exit;
354
}
355
$ignore{$r}=999999;
356
$ignore_used{$r}=0;
357
}
358
}
359
360
sub checksrc_clear {
361
undef %ignore;
362
undef %ignore_set;
363
undef @ignore_line;
364
}
365
366
sub checksrc_endoffile {
367
my ($file) = @_;
368
for(keys %ignore_set) {
369
if($ignore_set{$_} && !$ignore_used{$_}) {
370
checkwarn("UNUSEDIGNORE", $ignore_set{$_},
371
length($_)+11, $file,
372
$ignore_line[$ignore_set{$_}],
373
"Unused ignore: $_");
374
}
375
}
376
}
377
378
sub enable_warn {
379
my ($what, $line, $file, $l) = @_;
380
381
# switch it back on, but warn if not triggered!
382
if(!$ignore_used{$what}) {
383
checkwarn("UNUSEDIGNORE",
384
$line, length($what) + 11, $file, $l,
385
"No warning was inhibited!");
386
}
387
$ignore_set{$what}=0;
388
$ignore_used{$what}=0;
389
$ignore{$what}=0;
390
}
391
sub checksrc {
392
my ($cmd, $line, $file, $l) = @_;
393
if($cmd =~ / *([^ ]*) *(.*)/) {
394
my ($enable, $what) = ($1, $2);
395
$what =~ s: *\*/$::; # cut off end of C comment
396
# print "ENABLE $enable WHAT $what\n";
397
if($enable eq "disable") {
398
my ($warn, $scope)=($1, $2);
399
if($what =~ /([^ ]*) +(.*)/) {
400
($warn, $scope)=($1, $2);
401
}
402
else {
403
$warn = $what;
404
$scope = 1;
405
}
406
# print "IGNORE $warn for SCOPE $scope\n";
407
if($scope eq "all") {
408
$scope=999999;
409
}
410
411
# Comparing for a literal zero rather than the scalar value zero
412
# covers the case where $scope contains the ending '*' from the
413
# comment. If we use a scalar comparison (==) we induce warnings
414
# on non-scalar contents.
415
if($scope eq "0") {
416
checkwarn("BADCOMMAND",
417
$line, 0, $file, $l,
418
"Disable zero not supported, did you mean to enable?");
419
}
420
elsif($ignore_set{$warn}) {
421
checkwarn("BADCOMMAND",
422
$line, 0, $file, $l,
423
"$warn already disabled from line $ignore_set{$warn}");
424
}
425
else {
426
$ignore{$warn}=$scope;
427
$ignore_set{$warn}=$line;
428
$ignore_line[$line]=$l;
429
}
430
}
431
elsif($enable eq "enable") {
432
enable_warn($what, $line, $file, $l);
433
}
434
else {
435
checkwarn("BADCOMMAND",
436
$line, 0, $file, $l,
437
"Illegal !checksrc! command");
438
}
439
}
440
}
441
442
sub nostrings {
443
my ($str) = @_;
444
$str =~ s/\".*\"//g;
445
return $str;
446
}
447
448
sub scanfile {
449
my ($file) = @_;
450
451
my $line = 1;
452
my $prevl="";
453
my $prevpl="";
454
my $l = "";
455
my $prep = 0;
456
my $prevp = 0;
457
458
if($verbose) {
459
printf "Checking file: $file\n";
460
}
461
462
open(my $R, '<', $file) || die "failed to open $file";
463
464
my $incomment=0;
465
my @copyright=();
466
my %includes;
467
checksrc_clear(); # for file based ignores
468
accept_violations();
469
470
while(<$R>) {
471
$windows_os ? $_ =~ s/\r?\n$// : chomp;
472
my $l = $_;
473
my $ol = $l; # keep the unmodified line for error reporting
474
my $column = 0;
475
476
# check for !checksrc! commands
477
if($l =~ /\!checksrc\! (.*)/) {
478
my $cmd = $1;
479
checksrc($cmd, $line, $file, $l)
480
}
481
482
if($l =~ /^#line (\d+) \"([^\"]*)\"/) {
483
# a #line instruction
484
$file = $2;
485
$line = $1;
486
next;
487
}
488
489
# check for a copyright statement and save the years
490
if($l =~ /\* +copyright .* (\d\d\d\d|)/i) {
491
my $count = 0;
492
while($l =~ /([\d]{4})/g) {
493
push @copyright, {
494
year => $1,
495
line => $line,
496
col => index($l, $1),
497
code => $l
498
};
499
$count++;
500
}
501
if(!$count) {
502
# year-less
503
push @copyright, {
504
year => -1,
505
line => $line,
506
col => index($l, $1),
507
code => $l
508
};
509
}
510
}
511
512
# detect long lines
513
if(length($l) > $max_column) {
514
checkwarn("LONGLINE", $line, length($l), $file, $l,
515
"Longer than $max_column columns");
516
}
517
# detect TAB characters
518
if($l =~ /^(.*)\t/) {
519
checkwarn("TABS",
520
$line, length($1), $file, $l, "Contains TAB character", 1);
521
}
522
# detect trailing whitespace
523
if($l =~ /^(.*)[ \t]+\z/) {
524
checkwarn("TRAILINGSPACE",
525
$line, length($1), $file, $l, "Trailing whitespace");
526
}
527
528
# no space after comment start
529
if($l =~ /^(.*)\/\*\w/) {
530
checkwarn("COMMENTNOSPACESTART",
531
$line, length($1) + 2, $file, $l,
532
"Missing space after comment start");
533
}
534
# no space at comment end
535
if($l =~ /^(.*)\w\*\//) {
536
checkwarn("COMMENTNOSPACEEND",
537
$line, length($1) + 1, $file, $l,
538
"Missing space end comment end");
539
}
540
541
if($l =~ /(.*)(FIXME|TODO)/) {
542
checkwarn("FIXME",
543
$line, length($1), $file, $l,
544
"Avoid $2 comments. Add to documentation instead");
545
}
546
# ------------------------------------------------------------
547
# Above this marker, the checks were done on lines *including*
548
# comments
549
# ------------------------------------------------------------
550
551
# strip off C89 comments
552
553
comment:
554
if(!$incomment) {
555
if($l =~ s/\/\*.*\*\// /g) {
556
# full /* comments */ were removed!
557
}
558
if($l =~ s/\/\*.*//) {
559
# start of /* comment was removed
560
$incomment = 1;
561
}
562
}
563
else {
564
if($l =~ s/.*\*\///) {
565
# end of comment */ was removed
566
$incomment = 0;
567
goto comment;
568
}
569
else {
570
# still within a comment
571
$l="";
572
}
573
}
574
575
# ------------------------------------------------------------
576
# Below this marker, the checks were done on lines *without*
577
# comments
578
# ------------------------------------------------------------
579
580
# prev line was a preprocessor **and** ended with a backslash
581
if($prep && ($prevpl =~ /\\ *\z/)) {
582
# this is still a preprocessor line
583
$prep = 1;
584
goto preproc;
585
}
586
$prep = 0;
587
588
# crude attempt to detect // comments without too many false
589
# positives
590
if($l =~ /^(([^"\*]*)[^:"]|)\/\//) {
591
checkwarn("CPPCOMMENTS",
592
$line, length($1), $file, $l, "\/\/ comment");
593
}
594
595
if($l =~ /^(\#\s*include\s+)([\">].*[>}"])/) {
596
my ($pre, $path) = ($1, $2);
597
if($includes{$path}) {
598
checkwarn("INCLUDEDUP",
599
$line, length($1), $file, $l, "duplicated include");
600
}
601
$includes{$path} = $l;
602
}
603
604
# detect and strip preprocessor directives
605
if($l =~ /^[ \t]*\#/) {
606
# preprocessor line
607
$prep = 1;
608
goto preproc;
609
}
610
611
my $nostr = nostrings($l);
612
# check spaces after for/if/while/function call
613
if($nostr =~ /^(.*)(for|if|while|switch| ([a-zA-Z0-9_]+)) \((.)/) {
614
my ($leading, $word, $extra, $first)=($1,$2,$3,$4);
615
if($1 =~ / *\#/) {
616
# this is a #if, treat it differently
617
}
618
elsif(defined $3 && $3 eq "return") {
619
# return must have a space
620
}
621
elsif(defined $3 && $3 eq "case") {
622
# case must have a space
623
}
624
elsif(($first eq "*") && ($word !~ /(for|if|while|switch)/)) {
625
# A "(*" beginning makes the space OK because it wants to
626
# allow function pointer declared
627
}
628
elsif($1 =~ / *typedef/) {
629
# typedefs can use space-paren
630
}
631
else {
632
checkwarn("SPACEBEFOREPAREN", $line, length($leading)+length($word), $file, $l,
633
"$word with space");
634
}
635
}
636
# check for '== NULL' in if/while conditions but not if the thing on
637
# the left of it is a function call
638
if($nostr =~ /^(.*)(if|while)(\(.*?)([!=]= NULL|NULL [!=]=)/) {
639
checkwarn("EQUALSNULL", $line,
640
length($1) + length($2) + length($3),
641
$file, $l, "we prefer !variable instead of \"== NULL\" comparisons");
642
}
643
644
# check for '!= 0' in if/while conditions but not if the thing on
645
# the left of it is a function call
646
if($nostr =~ /^(.*)(if|while)(\(.*[^)]) != 0[^x]/) {
647
checkwarn("NOTEQUALSZERO", $line,
648
length($1) + length($2) + length($3),
649
$file, $l, "we prefer if(rc) instead of \"rc != 0\" comparisons");
650
}
651
652
# check spaces in 'do {'
653
if($nostr =~ /^( *)do( *)\{/ && length($2) != 1) {
654
checkwarn("DOBRACE", $line, length($1) + 2, $file, $l, "one space after do before brace");
655
}
656
# check spaces in 'do {'
657
elsif($nostr =~ /^( *)\}( *)while/ && length($2) != 1) {
658
checkwarn("BRACEWHILE", $line, length($1) + 2, $file, $l, "one space between brace and while");
659
}
660
if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
661
my $pos = length($1);
662
my $postparen = $5;
663
my $cond = $4;
664
if($cond =~ / = /) {
665
checkwarn("ASSIGNWITHINCONDITION",
666
$line, $pos+1, $file, $l,
667
"assignment within conditional expression");
668
}
669
my $temp = $cond;
670
$temp =~ s/\(//g; # remove open parens
671
my $openc = length($cond) - length($temp);
672
673
$temp = $cond;
674
$temp =~ s/\)//g; # remove close parens
675
my $closec = length($cond) - length($temp);
676
my $even = $openc == $closec;
677
678
if($l =~ / *\#/) {
679
# this is a #if, treat it differently
680
}
681
elsif($even && $postparen &&
682
($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
683
checkwarn("ONELINECONDITION",
684
$line, length($l)-length($postparen), $file, $l,
685
"conditional block on the same line");
686
}
687
}
688
# check spaces after open parentheses
689
if($l =~ /^(.*[a-z])\( /i) {
690
checkwarn("SPACEAFTERPAREN",
691
$line, length($1)+1, $file, $l,
692
"space after open parenthesis");
693
}
694
695
# check spaces before Logical AND operator
696
if($nostr =~ /^(.*)\w&&/i) {
697
checkwarn("NOSPACEAND",
698
$line, length($1)+1, $file, $l,
699
"missing space before Logical AND");
700
}
701
702
# check spaces after Logical AND operator
703
if($nostr =~ /^(.*&&)\w/i) {
704
checkwarn("NOSPACEAND",
705
$line, length($1), $file, $l,
706
"missing space after Logical AND");
707
}
708
709
# check spaces before colon
710
if($nostr =~ /^(.*[^']\?[^'].*)(\w|\)|\]|')\:/i) {
711
my $m = $1;
712
my $e = $nostr;
713
$e =~ s/'(.)':'(.)'/$1:$2/g; # eliminate chars quotes that surround colon
714
$e =~ s/':'//g; # ignore these
715
if($e =~ /^(.*[^']\?[^'].*)(\w|\)|\]|')\:/i) {
716
checkwarn("NOSPACEC",
717
$line, length($m)+1, $file, $l,
718
"missing space before colon");
719
}
720
}
721
# check spaces after colon
722
if($nostr =~ /^(.*[^'"]\?[^'"].*)\:(\w|\)|\]|')/i) {
723
my $m = $1;
724
my $e = $nostr;
725
$e =~ s/'(.)':'(.)'/$1:$2/g; # eliminate chars quotes that surround colon
726
$e =~ s/':'//g; # ignore these
727
if($e =~ /^(.*[^'"]\?[^'"].*)\:(\w|\)|\]|')/i) {
728
checkwarn("NOSPACEC",
729
$line, length($m)+1, $file, $l,
730
"missing space after colon");
731
}
732
}
733
734
# check spaces before question mark
735
if($nostr =~ /^(.*)(\w|\)|\]|')\?/i) {
736
my $m = $1;
737
my $e = $nostr;
738
$e =~ s/'?'//g; # ignore these
739
if($e =~ /^(.*)(\w|\)|\]|')\?/i) {
740
checkwarn("NOSPACEQ",
741
$line, length($m)+1, $file, $l,
742
"missing space before question mark");
743
}
744
}
745
# check spaces after question mark
746
if($nostr =~ /^(.*)\?\w/i) {
747
checkwarn("NOSPACEQ",
748
$line, length($1)+1, $file, $l,
749
"missing space after question mark");
750
}
751
752
# check spaces before less or greater than
753
if($nostr =~ /^(.*)(\w|\)|\])[<>]/) {
754
checkwarn("NOSPACETHAN",
755
$line, length($1)+1, $file, $l,
756
"missing space before less or greater than");
757
}
758
# check spaces after less or greater than
759
if($nostr =~ /^(.*)[^-][<>](\w|\(|\[)/) {
760
checkwarn("NOSPACETHAN",
761
$line, length($1)+1, $file, $l,
762
"missing space after less or greater than");
763
}
764
765
# check spaces before close parentheses, unless it was a space or a
766
# close parenthesis!
767
if($l =~ /(.*[^\) ]) \)/) {
768
checkwarn("SPACEBEFORECLOSE",
769
$line, length($1)+1, $file, $l,
770
"space before close parenthesis");
771
}
772
773
# check spaces before comma!
774
if($l =~ /(.*[^ ]) ,/) {
775
checkwarn("SPACEBEFORECOMMA",
776
$line, length($1)+1, $file, $l,
777
"space before comma");
778
}
779
780
# check for "return(" without space
781
if($l =~ /^(.*\W)return\(/) {
782
if($1 =~ / *\#/) {
783
# this is a #if, treat it differently
784
}
785
else {
786
checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
787
"return without space before paren");
788
}
789
}
790
791
# check for "return" with parentheses around just a value/name
792
if($l =~ /^(.*\W)return \(\w*\);/) {
793
checkwarn("RETURNPAREN", $line, length($1)+7, $file, $l,
794
"return with paren");
795
}
796
797
# check for "sizeof" without parenthesis
798
if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
799
if($1 =~ / *\#/) {
800
# this is a #if, treat it differently
801
}
802
else {
803
checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
804
"sizeof without parenthesis");
805
}
806
}
807
808
# check for comma without space
809
if($l =~ /^(.*),[^ \n]/) {
810
my $pref=$1;
811
my $ign=0;
812
if($pref =~ / *\#/) {
813
# this is a #if, treat it differently
814
$ign=1;
815
}
816
elsif($pref =~ /\/\*/) {
817
# this is a comment
818
$ign=1;
819
}
820
elsif($pref =~ /[\"\']/) {
821
$ign = 1;
822
# There is a quote here, figure out whether the comma is
823
# within a string or '' or not.
824
if($pref =~ /\"/) {
825
# within a string
826
}
827
elsif($pref =~ /\'$/) {
828
# a single letter
829
}
830
else {
831
$ign = 0;
832
}
833
}
834
if(!$ign) {
835
checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
836
"comma without following space");
837
}
838
}
839
840
# check for "} else"
841
if($l =~ /^(.*)\} *else/) {
842
checkwarn("BRACEELSE",
843
$line, length($1), $file, $l, "else after closing brace on same line");
844
}
845
# check for "){"
846
if($l =~ /^(.*)\)\{/) {
847
checkwarn("PARENBRACE",
848
$line, length($1)+1, $file, $l, "missing space after close paren");
849
}
850
# check for "^{" with an empty line before it
851
if(($l =~ /^\{/) && ($prevl =~ /^[ \t]*\z/)) {
852
checkwarn("EMPTYLINEBRACE",
853
$line, 0, $file, $l, "empty line before open brace");
854
}
855
856
# check for space before the semicolon last in a line
857
if($l =~ /^(.*[^ ].*) ;$/) {
858
checkwarn("SPACESEMICOLON",
859
$line, length($1), $file, $ol, "no space before semicolon");
860
}
861
862
# check for space before the colon in a switch label
863
if($l =~ /^( *(case .+|default)) :/) {
864
checkwarn("SPACESWITCHCOLON",
865
$line, length($1), $file, $ol, "no space before colon of switch label");
866
}
867
868
if($prevl !~ /\?\z/ && $l =~ /^ +([A-Za-z_][A-Za-z0-9_]*):$/ && $1 ne 'default') {
869
checkwarn("SPACEBEFORELABEL",
870
$line, length($1), $file, $ol, "no space before label");
871
}
872
873
# scan for use of banned functions
874
my $bl = $l;
875
again:
876
if(($l =~ /^(.*?\W)(\w+)(\s*\()/x) && $banfunc{$2}) {
877
my $bad = $2;
878
my $prefix = $1;
879
my $suff = $3;
880
checkwarn("BANNEDFUNC",
881
$line, length($prefix), $file, $ol,
882
"use of $bad is banned");
883
my $replace = 'x' x (length($bad) + 1);
884
$prefix =~ s/\*/\\*/;
885
$suff =~ s/\(/\\(/;
886
$l =~ s/$prefix$bad$suff/$prefix$replace/;
887
goto again;
888
}
889
$l = $bl; # restore to pre-bannedfunc content
890
891
if($warnings{"STDERR"}) {
892
# scan for use of banned stderr. This is not a BANNEDFUNC to
893
# allow for individual enable/disable of this warning.
894
if($l =~ /^([^\"-]*\W)(stderr)[^\"_]/x) {
895
if($1 !~ /^ *\#/) {
896
# skip preprocessor lines
897
checkwarn("STDERR",
898
$line, length($1), $file, $ol,
899
"use of $2 is banned (use tool_stderr instead)");
900
}
901
}
902
}
903
904
# scan for use of non-binary fopen without the macro
905
if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
906
my $mode = $2;
907
if($mode !~ /b/) {
908
checkwarn("FOPENMODE",
909
$line, length($1), $file, $ol,
910
"use of non-binary fopen without FOPEN_* macro: $mode");
911
}
912
}
913
914
# check for open brace first on line but not first column only alert
915
# if previous line ended with a close paren and it wasn't a cpp line
916
if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
917
checkwarn("BRACEPOS",
918
$line, length($1), $file, $ol, "badly placed open brace");
919
}
920
921
# if the previous line starts with if/while/for AND ends with an open
922
# brace, or an else statement, check that this line is indented $indent
923
# more steps, if not a cpp line
924
if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
925
my $first = length($1);
926
# this line has some character besides spaces
927
if($l =~ /^( *)[^ ]/) {
928
my $second = length($1);
929
my $expect = $first+$indent;
930
if($expect != $second) {
931
my $diff = $second - $first;
932
checkwarn("INDENTATION", $line, length($1), $file, $ol,
933
"not indented $indent steps (uses $diff)");
934
935
}
936
}
937
}
938
939
# if the previous line starts with if/while/for AND ends with a closed
940
# parenthesis and there's an equal number of open and closed
941
# parentheses, check that this line is indented $indent more steps, if
942
# not a cpp line
943
elsif(!$prevp && ($prevl =~ /^( *)(if|while|for)(\(.*\))\z/)) {
944
my $first = length($1);
945
my $op = $3;
946
my $cl = $3;
947
948
$op =~ s/[^(]//g;
949
$cl =~ s/[^)]//g;
950
951
if(length($op) == length($cl)) {
952
# this line has some character besides spaces
953
if($l =~ /^( *)[^ ]/) {
954
my $second = length($1);
955
my $expect = $first+$indent;
956
if($expect != $second) {
957
my $diff = $second - $first;
958
checkwarn("INDENTATION", $line, length($1), $file, $ol,
959
"not indented $indent steps (uses $diff)");
960
}
961
}
962
}
963
}
964
965
# check for 'char * name'
966
if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
967
checkwarn("ASTERISKSPACE",
968
$line, length($1), $file, $ol,
969
"space after declarative asterisk");
970
}
971
# check for 'char*'
972
if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
973
checkwarn("ASTERISKNOSPACE",
974
$line, length($1)-1, $file, $ol,
975
"no space before asterisk");
976
}
977
978
# check for 'void func() {', but avoid false positives by requiring
979
# both an open and closed parentheses before the open brace
980
if($l =~ /^((\w).*)\{\z/) {
981
my $k = $1;
982
$k =~ s/const *//;
983
$k =~ s/static *//;
984
if($k =~ /\(.*\)/) {
985
checkwarn("BRACEPOS",
986
$line, length($l)-1, $file, $ol,
987
"wrongly placed open brace");
988
}
989
}
990
991
# check for equals sign without spaces next to it
992
if($nostr =~ /(.*)\=[a-z0-9]/i) {
993
checkwarn("EQUALSNOSPACE",
994
$line, length($1)+1, $file, $ol,
995
"no space after equals sign");
996
}
997
# check for equals sign without spaces before it
998
elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
999
checkwarn("NOSPACEEQUALS",
1000
$line, length($1)+1, $file, $ol,
1001
"no space before equals sign");
1002
}
1003
1004
# check for plus signs without spaces next to it
1005
if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
1006
checkwarn("PLUSNOSPACE",
1007
$line, length($1)+1, $file, $ol,
1008
"no space after plus sign");
1009
}
1010
# check for plus sign without spaces before it
1011
elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
1012
checkwarn("NOSPACEPLUS",
1013
$line, length($1)+1, $file, $ol,
1014
"no space before plus sign");
1015
}
1016
1017
# check for semicolons without space next to it
1018
if($nostr =~ /(.*)\;[a-z0-9]/i) {
1019
checkwarn("SEMINOSPACE",
1020
$line, length($1)+1, $file, $ol,
1021
"no space after semicolon");
1022
}
1023
1024
# typedef struct ... {
1025
if($nostr =~ /^(.*)typedef struct.*{/) {
1026
checkwarn("TYPEDEFSTRUCT",
1027
$line, length($1)+1, $file, $ol,
1028
"typedef'ed struct");
1029
}
1030
1031
if($nostr =~ /(.*)! +(\w|\()/) {
1032
checkwarn("EXCLAMATIONSPACE",
1033
$line, length($1)+1, $file, $ol,
1034
"space after exclamation mark");
1035
}
1036
1037
if($nostr =~ /(.*)\b(EACCES|EADDRINUSE|EADDRNOTAVAIL|EAFNOSUPPORT|EBADF|ECONNREFUSED|ECONNRESET|EINPROGRESS|EINTR|EINVAL|EISCONN|EMSGSIZE|ENOMEM|ETIMEDOUT|EWOULDBLOCK)\b/) {
1038
checkwarn("ERRNOVAR",
1039
$line, length($1), $file, $ol,
1040
"use of bare errno define $2, use SOCK$2");
1041
}
1042
1043
# check for more than one consecutive space before open brace or
1044
# question mark. Skip lines containing strings since they make it hard
1045
# due to artificially getting multiple spaces
1046
if(($l eq $nostr) &&
1047
$nostr =~ /^(.*(\S)) + [{?]/i) {
1048
checkwarn("MULTISPACE",
1049
$line, length($1)+1, $file, $ol,
1050
"multiple spaces");
1051
}
1052
preproc:
1053
if($prep) {
1054
# scan for use of banned symbols on a preprocessor line
1055
if($l =~ /^(^|.*\W)
1056
(WIN32)
1057
(\W|$)
1058
/x) {
1059
checkwarn("BANNEDPREPROC",
1060
$line, length($1), $file, $ol,
1061
"use of $2 is banned from preprocessor lines" .
1062
(($2 eq "WIN32") ? ", use _WIN32 instead" : ""));
1063
}
1064
}
1065
$line++;
1066
$prevp = $prep;
1067
$prevl = $ol if(!$prep);
1068
$prevpl = $ol if($prep);
1069
}
1070
1071
if(!scalar(@copyright)) {
1072
checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
1073
}
1074
1075
# COPYRIGHTYEAR is an extended warning so we must first see if it has been
1076
# enabled in .checksrc
1077
if(defined($warnings{"COPYRIGHTYEAR"})) {
1078
# The check for updated copyrightyear is overly complicated in order to
1079
# not punish current hacking for past sins. The copyright years are
1080
# right now a bit behind, so enforcing copyright year checking on all
1081
# files would cause hundreds of errors. Instead we only look at files
1082
# which are tracked in the Git repo and edited in the workdir, or
1083
# committed locally on the branch without being in upstream master.
1084
#
1085
# The simple and naive test is to simply check for the current year,
1086
# but updating the year even without an edit is against project policy
1087
# (and it would fail every file on January 1st).
1088
#
1089
# A rather more interesting, and correct, check would be to not test
1090
# only locally committed files but inspect all files wrt the year of
1091
# their last commit. Removing the `git rev-list origin/master..HEAD`
1092
# condition below will enforce copyright year checks against the year
1093
# the file was last committed (and thus edited to some degree).
1094
my $commityear = undef;
1095
@copyright = sort {$$b{year} cmp $$a{year}} @copyright;
1096
1097
# if the file is modified, assume commit year this year
1098
if(`git status -s -- "$file"` =~ /^ [MARCU]/) {
1099
$commityear = (localtime(time))[5] + 1900;
1100
}
1101
else {
1102
# min-parents=1 to ignore wrong initial commit in truncated repos
1103
my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- "$file"`;
1104
if($grl) {
1105
chomp $grl;
1106
$commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
1107
}
1108
}
1109
1110
if(defined($commityear) && scalar(@copyright) &&
1111
$copyright[0]{year} != $commityear) {
1112
checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
1113
$file, $copyright[0]{code},
1114
"Copyright year out of date, should be $commityear, " .
1115
"is $copyright[0]{year}", 1);
1116
}
1117
}
1118
1119
if($incomment) {
1120
checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
1121
}
1122
1123
checksrc_endoffile($file);
1124
1125
close($R);
1126
1127
}
1128
1129
1130
if($errors || $warnings || $verbose) {
1131
printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
1132
if($suppressed) {
1133
printf "checksrc: %d errors and %d warnings suppressed\n",
1134
$serrors,
1135
$swarnings;
1136
}
1137
if($errors || $warnings) {
1138
exit 5; # return failure
1139
}
1140
}
1141
1142