Imported Upstream version 7.53.1
[platform/upstream/curl.git] / lib / checksrc.pl
1 #!/usr/bin/perl
2 #***************************************************************************
3 #                                  _   _ ____  _
4 #  Project                     ___| | | |  _ \| |
5 #                             / __| | | | |_) | |
6 #                            | (__| |_| |  _ <| |___
7 #                             \___|\___/|_| \_\_____|
8 #
9 # Copyright (C) 2011 - 2016, 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
24 my $max_column = 79;
25 my $indent = 2;
26
27 my $warnings;
28 my $errors;
29 my $supressed; # whitelisted problems
30 my $file;
31 my $dir=".";
32 my $wlist;
33 my $windows_os = $^O eq 'MSWin32' || $^O eq 'msys' || $^O eq 'cygwin';
34 my $verbose;
35 my %whitelist;
36
37 my %warnings = (
38     'LONGLINE' =>         "Line longer than $max_column",
39     'TABS' =>             'TAB characters not allowed',
40     'TRAILINGSPACE' =>    'Trailing white space on the line',
41     'CPPCOMMENTS' =>      '// comment detected',
42     'SPACEBEFOREPAREN' => 'space before an open parenthesis',
43     'SPACEAFTERPAREN'  => 'space after open parenthesis',
44     'SPACEBEFORECLOSE' => 'space before a close parenthesis',
45     'SPACEBEFORECOMMA' => 'space before a comma',
46     'RETURNNOSPACE'    => 'return without space',
47     'COMMANOSPACE'     => 'comma without following space',
48     'BRACEELSE'        => '} else on the same line',
49     'PARENBRACE'       => '){ without sufficient space',
50     'SPACESEMILCOLON'  => 'space before semicolon',
51     'BANNEDFUNC'       => 'a banned function was used',
52     'FOPENMODE'        => 'fopen needs a macro for the mode string',
53     'BRACEPOS'         => 'wrong position for an open brace',
54     'INDENTATION'      => 'wrong start column for code',
55     'COPYRIGHT'        => 'file missing a copyright statement',
56     'BADCOMMAND'       => 'bad !checksrc! instruction',
57     'UNUSEDIGNORE'     => 'a warning ignore was not used',
58     'OPENCOMMENT'      => 'file ended with a /* comment still "open"',
59     'ASTERISKSPACE'    => 'pointer declared with space after asterisk',
60     'ASTERISKNOSPACE'  => 'pointer declared without space before asterisk',
61     'ASSIGNWITHINCONDITION'  => 'assignment within conditional expression'
62     );
63
64 sub readwhitelist {
65     open(W, "<$dir/checksrc.whitelist");
66     my @all=<W>;
67     for(@all) {
68         $windows_os ? $_ =~ s/\r?\n$// : chomp;
69         $whitelist{$_}=1;
70     }
71     close(W);
72 }
73
74 sub checkwarn {
75     my ($name, $num, $col, $file, $line, $msg, $error) = @_;
76
77     my $w=$error?"error":"warning";
78     my $nowarn=0;
79
80     #if(!$warnings{$name}) {
81     #    print STDERR "Dev! there's no description for $name!\n";
82     #}
83
84     # checksrc.whitelist
85     if($whitelist{$line}) {
86         $nowarn = 1;
87     }
88     # !checksrc! controlled
89     elsif($ignore{$name}) {
90         $ignore{$name}--;
91         $ignore_used{$name}++;
92         $nowarn = 1;
93         if(!$ignore{$name}) {
94             # reached zero, enable again
95             enable_warn($name, $line, $file, $l);
96         }
97     }
98
99     if($nowarn) {
100         $supressed++;
101         if($w) {
102             $swarnings++;
103         }
104         else {
105             $serrors++;
106         }
107         return;
108     }
109
110     if($w) {
111         $warnings++;
112     }
113     else {
114         $errors++;
115     }
116
117     $col++;
118     print "$file:$num:$col: $w: $msg ($name)\n";
119     print " $line\n";
120
121     if($col < 80) {
122         my $pref = (' ' x $col);
123         print "${pref}^\n";
124     }
125 }
126
127 $file = shift @ARGV;
128
129 while(1) {
130
131     if($file =~ /-D(.*)/) {
132         $dir = $1;
133         $file = shift @ARGV;
134         next;
135     }
136     elsif($file =~ /-W(.*)/) {
137         $wlist .= " $1 ";
138         $file = shift @ARGV;
139         next;
140     }
141     elsif($file =~ /^(-h|--help)/) {
142         undef $file;
143         last;
144     }
145
146     last;
147 }
148
149 if(!$file) {
150     print "checksrc.pl [option] <file1> [file2] ...\n";
151     print " Options:\n";
152     print "  -D[DIR]   Directory to prepend file names\n";
153     print "  -h        Show help output\n";
154     print "  -W[file]  Whitelist the given file - ignore all its flaws\n";
155     print "\nDetects and warns for these problems:\n";
156     for(sort keys %warnings) {
157         printf (" %-18s: %s\n", $_, $warnings{$_});
158     }
159     exit;
160 }
161
162 readwhitelist();
163
164 do {
165     if("$wlist" !~ / $file /) {
166         my $fullname = $file;
167         $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
168         scanfile($fullname);
169     }
170     $file = shift @ARGV;
171
172 } while($file);
173
174 sub checksrc_clear {
175     undef %ignore;
176     undef %ignore_set;
177     undef @ignore_line;
178 }
179
180 sub checksrc_endoffile {
181     my ($file) = @_;
182     for(keys %ignore_set) {
183         if($ignore_set{$_} && !$ignore_used{$_}) {
184             checkwarn("UNUSEDIGNORE", $ignore_set{$_},
185                       length($_)+11, $file,
186                       $ignore_line[$ignore_set{$_}],
187                       "Unused ignore: $_");
188         }
189     }
190 }
191
192 sub enable_warn {
193     my ($what, $line, $file, $l) = @_;
194
195     # switch it back on, but warn if not triggered!
196     if(!$ignore_used{$what}) {
197         checkwarn("UNUSEDIGNORE",
198                   $line, length($what) + 11, $file, $l,
199                   "No warning was inhibited!");
200     }
201     $ignore_set{$what}=0;
202     $ignore_used{$what}=0;
203     $ignore{$what}=0;
204 }
205 sub checksrc {
206     my ($cmd, $line, $file, $l) = @_;
207     if($cmd =~ / *([^ ]*) *(.*)/) {
208         my ($enable, $what) = ($1, $2);
209         $what =~ s: *\*/$::; # cut off end of C comment
210         # print "ENABLE $enable WHAT $what\n";
211         if($enable eq "disable") {
212             my ($warn, $scope)=($1, $2);
213             if($what =~ /([^ ]*) +(.*)/) {
214                 ($warn, $scope)=($1, $2);
215             }
216             else {
217                 $warn = $what;
218                 $scope = 1;
219             }
220             # print "IGNORE $warn for SCOPE $scope\n";
221             if($scope eq "all") {
222                 $scope=999999;
223             }
224
225             if($ignore_set{$warn}) {
226                 checkwarn("BADCOMMAND",
227                           $line, 0, $file, $l,
228                           "$warn already disabled from line $ignore_set{$warn}");
229             }
230             else {
231                 $ignore{$warn}=$scope;
232                 $ignore_set{$warn}=$line;
233                 $ignore_line[$line]=$l;
234             }
235         }
236         elsif($enable eq "enable") {
237             enable_warn($what, $line, $file, $l);
238         }
239         else {
240             checkwarn("BADCOMMAND",
241                       $line, 0, $file, $l,
242                       "Illegal !checksrc! command");
243         }
244     }
245 }
246
247 sub nostrings {
248     my ($str) = @_;
249     $str =~ s/\".*\"//g;
250     return $str;
251 }
252
253 sub scanfile {
254     my ($file) = @_;
255
256     my $line = 1;
257     my $prevl;
258     my $l;
259     open(R, "<$file") || die "failed to open $file";
260
261     my $incomment=0;
262     my $copyright=0;
263     checksrc_clear(); # for file based ignores
264
265     while(<R>) {
266         $windows_os ? $_ =~ s/\r?\n$// : chomp;
267         my $l = $_;
268         my $ol = $l; # keep the unmodified line for error reporting
269         my $column = 0;
270
271         # check for !checksrc! commands
272         if($l =~ /\!checksrc\! (.*)/) {
273             my $cmd = $1;
274             checksrc($cmd, $line, $file, $l)
275         }
276
277         # check for a copyright statement
278         if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
279             $copyright=1;
280         }
281
282         # detect long lines
283         if(length($l) > $max_column) {
284             checkwarn("LONGLINE", $line, length($l), $file, $l,
285                       "Longer than $max_column columns");
286         }
287         # detect TAB characters
288         if($l =~ /^(.*)\t/) {
289             checkwarn("TABS",
290                       $line, length($1), $file, $l, "Contains TAB character", 1);
291         }
292         # detect trailing white space
293         if($l =~ /^(.*)[ \t]+\z/) {
294             checkwarn("TRAILINGSPACE",
295                       $line, length($1), $file, $l, "Trailing whitespace");
296         }
297
298         # ------------------------------------------------------------
299         # Above this marker, the checks were done on lines *including*
300         # comments
301         # ------------------------------------------------------------
302
303         # strip off C89 comments
304
305       comment:
306         if(!$incomment) {
307             if($l =~ s/\/\*.*\*\// /g) {
308                 # full /* comments */ were removed!
309             }
310             if($l =~ s/\/\*.*//) {
311                 # start of /* comment was removed
312                 $incomment = 1;
313             }
314         }
315         else {
316             if($l =~ s/.*\*\///) {
317                 # end of comment */ was removed
318                 $incomment = 0;
319                 goto comment;
320             }
321             else {
322                 # still within a comment
323                 $l="";
324             }
325         }
326
327         # ------------------------------------------------------------
328         # Below this marker, the checks were done on lines *without*
329         # comments
330         # ------------------------------------------------------------
331
332         # crude attempt to detect // comments without too many false
333         # positives
334         if($l =~ /^([^"\*]*)[^:"]\/\//) {
335             checkwarn("CPPCOMMENTS",
336                       $line, length($1), $file, $l, "\/\/ comment");
337         }
338
339         my $nostr = nostrings($l);
340         # check spaces after for/if/while/function call
341         if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
342             if($1 =~ / *\#/) {
343                 # this is a #if, treat it differently
344             }
345             elsif($3 eq "return") {
346                 # return must have a space
347             }
348             elsif($4 eq "*") {
349                 # (* beginning makes the space OK!
350             }
351             elsif($1 =~ / *typedef/) {
352                 # typedefs can use space-paren
353             }
354             else {
355                 checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l,
356                           "$2 with space");
357             }
358         }
359
360         if($nostr =~ /^((.*)(if) *\()(.*)\)/) {
361             my $pos = length($1);
362             if($4 =~ / = /) {
363                 checkwarn("ASSIGNWITHINCONDITION",
364                           $line, $pos+1, $file, $l,
365                           "assignment within conditional expression");
366             }
367         }
368         # check spaces after open parentheses
369         if($l =~ /^(.*[a-z])\( /i) {
370             checkwarn("SPACEAFTERPAREN",
371                       $line, length($1)+1, $file, $l,
372                       "space after open parenthesis");
373         }
374
375         # check spaces before close parentheses, unless it was a space or a
376         # close parenthesis!
377         if($l =~ /(.*[^\) ]) \)/) {
378             checkwarn("SPACEBEFORECLOSE",
379                       $line, length($1)+1, $file, $l,
380                       "space before close parenthesis");
381         }
382
383         # check spaces before comma!
384         if($l =~ /(.*[^ ]) ,/) {
385             checkwarn("SPACEBEFORECOMMA",
386                       $line, length($1)+1, $file, $l,
387                       "space before comma");
388         }
389
390         # check for "return(" without space
391         if($l =~ /^(.*)return\(/) {
392             if($1 =~ / *\#/) {
393                 # this is a #if, treat it differently
394             }
395             else {
396                 checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
397                           "return without space before paren");
398             }
399         }
400
401         # check for comma without space
402         if($l =~ /^(.*),[^ \n]/) {
403             my $pref=$1;
404             my $ign=0;
405             if($pref =~ / *\#/) {
406                 # this is a #if, treat it differently
407                 $ign=1;
408             }
409             elsif($pref =~ /\/\*/) {
410                 # this is a comment
411                 $ign=1;
412             }
413             elsif($pref =~ /[\"\']/) {
414                 $ign = 1;
415                 # There is a quote here, figure out whether the comma is
416                 # within a string or '' or not.
417                 if($pref =~ /\"/) {
418                     # withing a string
419                 }
420                 elsif($pref =~ /\'$/) {
421                     # a single letter
422                 }
423                 else {
424                     $ign = 0;
425                 }
426             }
427             if(!$ign) {
428                 checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
429                           "comma without following space");
430             }
431         }
432
433         # check for "} else"
434         if($l =~ /^(.*)\} *else/) {
435             checkwarn("BRACEELSE",
436                       $line, length($1), $file, $l, "else after closing brace on same line");
437         }
438         # check for "){"
439         if($l =~ /^(.*)\)\{/) {
440             checkwarn("PARENBRACE",
441                       $line, length($1)+1, $file, $l, "missing space after close paren");
442         }
443
444         # check for space before the semicolon last in a line
445         if($l =~ /^(.*[^ ].*) ;$/) {
446             checkwarn("SPACESEMILCOLON",
447                       $line, length($1), $file, $ol, "space before last semicolon");
448         }
449
450         # scan for use of banned functions
451         if($l =~ /^(.*\W)
452                    (gets|
453                     strtok|
454                     v?sprintf|
455                     (str|_mbs|_tcs|_wcs)n?cat|
456                     LoadLibrary(Ex)?(A|W)?)
457                    \s*\(
458                  /x) {
459             checkwarn("BANNEDFUNC",
460                       $line, length($1), $file, $ol,
461                       "use of $2 is banned");
462         }
463
464         # scan for use of non-binary fopen without the macro
465         if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
466             my $mode = $2;
467             if($mode !~ /b/) {
468                 checkwarn("FOPENMODE",
469                           $line, length($1), $file, $ol,
470                           "use of non-binary fopen without FOPEN_* macro: $mode");
471             }
472         }
473
474         # check for open brace first on line but not first column
475         # only alert if previous line ended with a close paren and wasn't a cpp
476         # line
477         if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
478             checkwarn("BRACEPOS",
479                       $line, length($1), $file, $ol, "badly placed open brace");
480         }
481
482         # if the previous line starts with if/while/for AND ends with an open
483         # brace, check that this line is indented $indent more steps, if not
484         # a cpp line
485         if($prevl =~ /^( *)(if|while|for)\(.*\{\z/) {
486             my $first = length($1);
487
488             # this line has some character besides spaces
489             if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
490                 my $second = length($1);
491                 my $expect = $first+$indent;
492                 if($expect != $second) {
493                     my $diff = $second - $first;
494                     checkwarn("INDENTATION", $line, length($1), $file, $ol,
495                               "not indented $indent steps, uses $diff)");
496
497                 }
498             }
499         }
500
501         # check for 'char * name'
502         if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost) *(\*+)) (\w+)/) && ($4 ne "const")) {
503             checkwarn("ASTERISKNOSPACE",
504                       $line, length($1), $file, $ol,
505                       "no space after declarative asterisk");
506         }
507         # check for 'char*'
508         if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
509             checkwarn("ASTERISKNOSPACE",
510                       $line, length($1)-1, $file, $ol,
511                       "no space before asterisk");
512         }
513
514         # check for 'void func() {', but avoid false positives by requiring
515         # both an open and closed parentheses before the open brace
516         if($l =~ /^((\w).*){\z/) {
517             my $k = $1;
518             $k =~ s/const *//;
519             $k =~ s/static *//;
520             if($k =~ /\(.*\)/) {
521                 checkwarn("BRACEPOS",
522                           $line, length($l)-1, $file, $ol,
523                           "wrongly placed open brace");
524             }
525         }
526         $line++;
527         $prevl = $ol;
528     }
529
530     if(!$copyright) {
531         checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
532     }
533     if($incomment) {
534         checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
535     }
536
537     checksrc_endoffile($file);
538
539     close(R);
540
541 }
542
543
544 if($errors || $warnings || $verbose) {
545     printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
546     if($supressed) {
547         printf "checksrc: %d errors and %d warnings suppressed\n",
548         $serrors,
549         $swarnings;
550     }
551     exit 5; # return failure
552 }