2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 2011, 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($file ne "$wlist") {
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 =~ /^(\S+)[ \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");
151 # check for open brace first on line but not first column
152 # only alert if previous line ended with a close paren and wasn't a cpp
154 if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
155 checkwarn($line, length($1), $file, $l, "badly placed open brace");
158 # if the previous line starts with if/while/for AND ends with an open
159 # brace, check that this line is indented $indent more steps, if not
161 if($prevl =~ /^( *)(if|while|for)\(.*\{\z/) {
162 my $first = length($1);
164 # this line has some character besides spaces
165 if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
166 my $second = length($1);
167 my $expect = $first+$indent;
168 if($expect != $second) {
169 my $diff = $second - $first;
170 checkwarn($line, length($1), $file, $l,
171 "not indented $indent steps, uses $diff)");
182 checkwarn(1, 0, $file, "", "Missing copyright statement", 1);
190 if($errors || $warnings) {
191 printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
192 exit 5; # return failure