2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 2011 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
22 ###########################################################################
29 my $supressed; # whitelisted problems
33 my $windows_os = $^O eq 'MSWin32' || $^O eq 'msys' || $^O eq 'cygwin';
38 open(W, "<$dir/checksrc.whitelist");
41 $windows_os ? $_ =~ s/\r?\n$// : chomp;
48 my ($num, $col, $file, $line, $msg, $error) = @_;
50 if($whitelist{$line}) {
55 my $w=$error?"error":"warning";
65 print "$file:$num:$col: $w: $msg\n";
69 my $pref = (' ' x $col);
78 if($file =~ /-D(.*)/) {
83 elsif($file =~ /-W(.*)/) {
93 print "checksrc.pl [option] <file1> [file2] ...\n";
95 print " -D[DIR] Directory to prepend file names\n";
96 print " -W[file] Whitelist the given file - ignore all its flaws\n";
103 if("$wlist" !~ / $file /) {
104 my $fullname = $file;
105 $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
119 open(R, "<$file") || die "failed to open $file";
124 $windows_os ? $_ =~ s/\r?\n$// : chomp;
128 # check for a copyright statement
129 if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
134 if(length($l) > $max_column) {
135 checkwarn($line, length($l), $file, $l, "Longer than $max_column columns");
137 # detect TAB characters
138 if($l =~ /^(.*)\t/) {
139 checkwarn($line, length($1), $file, $l, "Contains TAB character", 1);
141 # detect trailing white space
142 if($l =~ /^(.*)[ \t]+\z/) {
143 checkwarn($line, length($1), $file, $l, "Trailing whitespace");
146 # crude attempt to detect // comments without too many false
148 if($l =~ /^([^"\*]*)[^:"]\/\//) {
149 checkwarn($line, length($1), $file, $l, "\/\/ comment");
151 # check spaces after for/if/while
152 if($l =~ /^(.*)(for|if|while) \(/) {
154 # this is a #if, treat it differently
157 checkwarn($line, length($1)+length($2), $file, $l,
162 # check spaces after open paren after for/if/while
163 if($l =~ /^(.*)(for|if|while)\( /) {
165 # this is a #if, treat it differently
168 checkwarn($line, length($1)+length($2)+1, $file, $l,
169 "$2 with space first in condition");
173 # check for "return(" without space
174 if($l =~ /^(.*)return\(/) {
176 # this is a #if, treat it differently
179 checkwarn($line, length($1)+6, $file, $l,
180 "return without space before paren");
184 # check for comma without space
185 if($l =~ /^(.*),[^ \n]/) {
188 if($pref =~ / *\#/) {
189 # this is a #if, treat it differently
192 elsif($pref =~ /\/\*/) {
196 elsif($pref =~ /[\"\']/) {
198 # There is a quote here, figure out whether the comma is
199 # within a string or '' or not.
203 elsif($pref =~ /\'$/) {
211 checkwarn($line, length($pref)+1, $file, $l,
212 "comma without following space");
217 if($l =~ /^(.*)\} *else/) {
218 checkwarn($line, length($1), $file, $l, "else after closing brace on same line");
221 if($l =~ /^(.*)\)\{/) {
222 checkwarn($line, length($1)+1, $file, $l, "missing space after close paren");
225 # check for space before the semicolon last in a line
226 if($l =~ /^(.*[^ ].*) ;$/) {
227 checkwarn($line, length($1), $file, $l, "space before last semicolon");
230 # scan for use of banned functions
231 if($l =~ /^(.*\W)(sprintf|vsprintf|strcat|strncat|gets)\s*\(/) {
232 checkwarn($line, length($1), $file, $l,
233 "use of $2 is banned");
236 # scan for use of non-binary fopen without the macro
237 if($l =~ /^(.*\W)fopen\s*\([^"]*\"([^"]*)/) {
240 checkwarn($line, length($1), $file, $l,
241 "use of non-binary fopen without FOPEN_* macro");
245 # check for open brace first on line but not first column
246 # only alert if previous line ended with a close paren and wasn't a cpp
248 if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
249 checkwarn($line, length($1), $file, $l, "badly placed open brace");
252 # if the previous line starts with if/while/for AND ends with an open
253 # brace, check that this line is indented $indent more steps, if not
255 if($prevl =~ /^( *)(if|while|for)\(.*\{\z/) {
256 my $first = length($1);
258 # this line has some character besides spaces
259 if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
260 my $second = length($1);
261 my $expect = $first+$indent;
262 if($expect != $second) {
263 my $diff = $second - $first;
264 checkwarn($line, length($1), $file, $l,
265 "not indented $indent steps, uses $diff)");
276 checkwarn(1, 0, $file, "", "Missing copyright statement", 1);
284 if($errors || $warnings) {
285 printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
286 exit 5; # return failure