2011-04-20 21:17:42 +08:00
|
|
|
#!/usr/bin/perl
|
2011-04-23 04:58:17 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# _ _ ____ _
|
|
|
|
# Project ___| | | | _ \| |
|
|
|
|
# / __| | | | |_) | |
|
|
|
|
# | (__| |_| | _ <| |___
|
|
|
|
# \___|\___/|_| \_\_____|
|
|
|
|
#
|
2015-03-17 20:05:01 +08:00
|
|
|
# Copyright (C) 2011 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-04-23 04:58:17 +08:00
|
|
|
#
|
|
|
|
# This software is licensed as described in the file COPYING, which
|
|
|
|
# you should have received as part of this distribution. The terms
|
|
|
|
# are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
#
|
|
|
|
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
# copies of the Software, and permit persons to whom the Software is
|
|
|
|
# furnished to do so, under the terms of the COPYING file.
|
|
|
|
#
|
|
|
|
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, either express or implied.
|
|
|
|
#
|
|
|
|
###########################################################################
|
2011-04-20 21:17:42 +08:00
|
|
|
|
|
|
|
my $max_column = 79;
|
|
|
|
my $indent = 2;
|
|
|
|
|
2011-04-21 04:44:08 +08:00
|
|
|
my $warnings;
|
|
|
|
my $errors;
|
2011-04-23 04:58:17 +08:00
|
|
|
my $file;
|
|
|
|
my $dir=".";
|
2011-04-26 04:43:02 +08:00
|
|
|
my $wlist;
|
2011-04-21 04:44:08 +08:00
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
sub checkwarn {
|
|
|
|
my ($num, $col, $file, $line, $msg, $error) = @_;
|
|
|
|
|
|
|
|
my $w=$error?"error":"warning";
|
|
|
|
|
2011-04-21 04:44:08 +08:00
|
|
|
if($w) {
|
|
|
|
$warnings++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$errors++;
|
|
|
|
}
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
$col++;
|
|
|
|
print "$file:$num:$col: $w: $msg\n";
|
|
|
|
print " $line\n";
|
|
|
|
|
|
|
|
if($col < 80) {
|
|
|
|
my $pref = (' ' x $col);
|
|
|
|
print "${pref}^\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
$file = shift @ARGV;
|
|
|
|
|
2011-04-26 04:43:02 +08:00
|
|
|
while(1) {
|
|
|
|
|
|
|
|
if($file =~ /-D(.*)/) {
|
|
|
|
$dir = $1;
|
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
elsif($file =~ /-W(.*)/) {
|
2011-08-06 03:01:39 +08:00
|
|
|
$wlist .= " $1 ";
|
2011-04-26 04:43:02 +08:00
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
last;
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
if(!$file) {
|
2011-04-23 04:58:17 +08:00
|
|
|
print "checksrc.pl [option] <file1> [file2] ...\n";
|
|
|
|
print " Options:\n";
|
|
|
|
print " -D[DIR] Directory to prepend file names\n";
|
2011-04-26 04:43:02 +08:00
|
|
|
print " -W[file] Whitelist the given file - ignore all its flaws\n";
|
2011-04-20 21:17:42 +08:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
do {
|
2011-08-06 03:24:59 +08:00
|
|
|
if("$wlist" !~ / $file /) {
|
2011-04-28 03:42:15 +08:00
|
|
|
my $fullname = $file;
|
2011-05-27 01:17:10 +08:00
|
|
|
$fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
|
2011-04-28 03:42:15 +08:00
|
|
|
scanfile($fullname);
|
2011-04-26 04:43:02 +08:00
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
$file = shift @ARGV;
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
} while($file);
|
2011-04-20 21:17:42 +08:00
|
|
|
|
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
sub scanfile {
|
|
|
|
my ($file) = @_;
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
my $line = 1;
|
|
|
|
my $prevl;
|
|
|
|
my $l;
|
|
|
|
open(R, "<$file") || die "failed to open $file";
|
|
|
|
|
|
|
|
my $copyright=0;
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
while(<R>) {
|
|
|
|
chomp;
|
|
|
|
my $l = $_;
|
|
|
|
my $column = 0;
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
# check for a copyright statement
|
|
|
|
if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
|
|
|
|
$copyright=1;
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
|
|
|
|
# detect long lines
|
|
|
|
if(length($l) > $max_column) {
|
|
|
|
checkwarn($line, length($l), $file, $l, "Longer than $max_column columns");
|
|
|
|
}
|
|
|
|
# detect TAB characters
|
|
|
|
if($l =~ /^(.*)\t/) {
|
|
|
|
checkwarn($line, length($1), $file, $l, "Contains TAB character", 1);
|
|
|
|
}
|
|
|
|
# detect trailing white space
|
2011-05-16 21:21:04 +08:00
|
|
|
if($l =~ /^(.*)[ \t]+\z/) {
|
2011-04-23 04:58:17 +08:00
|
|
|
checkwarn($line, length($1), $file, $l, "Trailing whitespace");
|
|
|
|
}
|
|
|
|
|
|
|
|
# check spaces after for/if/while
|
|
|
|
if($l =~ /^(.*)(for|if|while) \(/) {
|
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkwarn($line, length($1)+length($2), $file, $l,
|
|
|
|
"$2 with space");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# check spaces after open paren after for/if/while
|
|
|
|
if($l =~ /^(.*)(for|if|while)\( /) {
|
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkwarn($line, length($1)+length($2)+1, $file, $l,
|
|
|
|
"$2 with space first in condition");
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 20:05:01 +08:00
|
|
|
# check for "return(" without space
|
|
|
|
if($l =~ /^(.*)return\(/) {
|
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkwarn($line, length($1)+6, $file, $l,
|
|
|
|
"return without space before paren");
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 20:41:49 +08:00
|
|
|
|
|
|
|
# check for comma without space
|
|
|
|
if($l =~ /^(.*),[^ \n]/) {
|
|
|
|
my $pref=$1;
|
|
|
|
my $ign=0;
|
|
|
|
if($pref =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
$ign=1;
|
|
|
|
}
|
|
|
|
elsif($pref =~ /\/\*/) {
|
|
|
|
# this is a comment
|
|
|
|
$ign=1;
|
|
|
|
}
|
|
|
|
elsif($pref =~ /[\"\']/) {
|
|
|
|
$ign = 1;
|
|
|
|
# There is a quote here, figure out whether the comma is
|
|
|
|
# within a string or '' or not.
|
|
|
|
if($pref =~ /\"/) {
|
|
|
|
# withing a string
|
|
|
|
}
|
|
|
|
elsif($pref =~ /\'$/) {
|
|
|
|
# a single letter
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ign = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$ign) {
|
|
|
|
checkwarn($line, length($pref)+1, $file, $l,
|
|
|
|
"comma without following space");
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 20:05:01 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
# check for "} else"
|
2011-09-08 04:45:43 +08:00
|
|
|
if($l =~ /^(.*)\} *else/) {
|
2011-04-23 04:58:17 +08:00
|
|
|
checkwarn($line, length($1), $file, $l, "else after closing brace on same line");
|
|
|
|
}
|
2011-07-05 04:08:14 +08:00
|
|
|
# check for "){"
|
|
|
|
if($l =~ /^(.*)\)\{/) {
|
|
|
|
checkwarn($line, length($1)+1, $file, $l, "missing space after close paren");
|
|
|
|
}
|
|
|
|
|
2013-03-06 20:27:51 +08:00
|
|
|
# scan for use of banned functions
|
|
|
|
if($l =~ /^(.*\W)(sprintf|vsprintf|strcat|strncat|gets)\s*\(/) {
|
|
|
|
checkwarn($line, length($1), $file, $l,
|
|
|
|
"use of $2 is banned");
|
|
|
|
}
|
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
# check for open brace first on line but not first column
|
|
|
|
# only alert if previous line ended with a close paren and wasn't a cpp
|
|
|
|
# line
|
|
|
|
if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
|
|
|
|
checkwarn($line, length($1), $file, $l, "badly placed open brace");
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
# if the previous line starts with if/while/for AND ends with an open
|
|
|
|
# brace, check that this line is indented $indent more steps, if not
|
|
|
|
# a cpp line
|
|
|
|
if($prevl =~ /^( *)(if|while|for)\(.*\{\z/) {
|
|
|
|
my $first = length($1);
|
|
|
|
|
|
|
|
# this line has some character besides spaces
|
|
|
|
if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
|
|
|
|
my $second = length($1);
|
|
|
|
my $expect = $first+$indent;
|
|
|
|
if($expect != $second) {
|
|
|
|
my $diff = $second - $first;
|
|
|
|
checkwarn($line, length($1), $file, $l,
|
|
|
|
"not indented $indent steps, uses $diff)");
|
|
|
|
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
|
|
|
|
$line++;
|
|
|
|
$prevl = $l;
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
if(!$copyright) {
|
|
|
|
checkwarn(1, 0, $file, "", "Missing copyright statement", 1);
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
close(R);
|
2011-04-20 21:17:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-21 04:44:08 +08:00
|
|
|
|
|
|
|
if($errors || $warnings) {
|
2011-04-23 04:58:17 +08:00
|
|
|
printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
|
2011-04-21 04:44:08 +08:00
|
|
|
exit 5; # return failure
|
|
|
|
}
|