2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 2011 - 2013, 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 http://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 ###########################################################################
34 my ($num, $col, $file, $line, $msg, $error) = @_;
36 my $w=$error?"error":"warning";
46 print "$file:$num:$col: $w: $msg\n";
50 my $pref = (' ' x $col);
59 if($file =~ /-D(.*)/) {
64 elsif($file =~ /-W(.*)/) {
74 print "checksrc.pl [option] <file1> [file2] ...\n";
76 print " -D[DIR] Directory to prepend file names\n";
77 print " -W[file] Whitelist the given file - ignore all its flaws\n";
82 if("$wlist" !~ / $file /) {
84 $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
98 open(R, "<$file") || die "failed to open $file";
107 # check for a copyright statement
108 if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
113 if(length($l) > $max_column) {
114 checkwarn($line, length($l), $file, $l, "Longer than $max_column columns");
116 # detect TAB characters
117 if($l =~ /^(.*)\t/) {
118 checkwarn($line, length($1), $file, $l, "Contains TAB character", 1);
120 # detect trailing white space
121 if($l =~ /^(.*)[ \t]+\z/) {
122 checkwarn($line, length($1), $file, $l, "Trailing whitespace");
125 # check spaces after for/if/while
126 if($l =~ /^(.*)(for|if|while) \(/) {
128 # this is a #if, treat it differently
131 checkwarn($line, length($1)+length($2), $file, $l,
136 # check spaces after open paren after for/if/while
137 if($l =~ /^(.*)(for|if|while)\( /) {
139 # this is a #if, treat it differently
142 checkwarn($line, length($1)+length($2)+1, $file, $l,
143 "$2 with space first in condition");
148 if($l =~ /^(.*)\} *else/) {
149 checkwarn($line, length($1), $file, $l, "else after closing brace on same line");
152 if($l =~ /^(.*)\)\{/) {
153 checkwarn($line, length($1)+1, $file, $l, "missing space after close paren");
156 # scan for use of banned functions
157 if($l =~ /^(.*\W)(sprintf|vsprintf|strcat|strncat|gets)\s*\(/) {
158 checkwarn($line, length($1), $file, $l,
159 "use of $2 is banned");
162 # check for open brace first on line but not first column
163 # only alert if previous line ended with a close paren and wasn't a cpp
165 if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
166 checkwarn($line, length($1), $file, $l, "badly placed open brace");
169 # if the previous line starts with if/while/for AND ends with an open
170 # brace, check that this line is indented $indent more steps, if not
172 if($prevl =~ /^( *)(if|while|for)\(.*\{\z/) {
173 my $first = length($1);
175 # this line has some character besides spaces
176 if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
177 my $second = length($1);
178 my $expect = $first+$indent;
179 if($expect != $second) {
180 my $diff = $second - $first;
181 checkwarn($line, length($1), $file, $l,
182 "not indented $indent steps, uses $diff)");
193 checkwarn(1, 0, $file, "", "Missing copyright statement", 1);
201 if($errors || $warnings) {
202 printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
203 exit 5; # return failure