2018-01-07 09:34:46 +03:00
|
|
|
#!/usr/bin/env perl
|
2011-04-23 00:58:17 +04:00
|
|
|
#***************************************************************************
|
|
|
|
# _ _ ____ _
|
|
|
|
# Project ___| | | | _ \| |
|
|
|
|
# / __| | | | |_) | |
|
|
|
|
# | (__| |_| | _ <| |___
|
|
|
|
# \___|\___/|_| \_\_____|
|
|
|
|
#
|
2017-07-27 02:13:19 +03:00
|
|
|
# Copyright (C) 2011 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-04-23 00:58:17 +04:00
|
|
|
#
|
|
|
|
# This software is licensed as described in the file COPYING, which
|
|
|
|
# you should have received as part of this distribution. The terms
|
2016-02-03 02:19:02 +03:00
|
|
|
# are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-04-23 00:58:17 +04:00
|
|
|
#
|
|
|
|
# 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 17:17:42 +04:00
|
|
|
|
|
|
|
my $max_column = 79;
|
|
|
|
my $indent = 2;
|
|
|
|
|
2011-04-21 00:44:08 +04:00
|
|
|
my $warnings;
|
|
|
|
my $errors;
|
2015-03-17 15:53:12 +03:00
|
|
|
my $supressed; # whitelisted problems
|
2011-04-23 00:58:17 +04:00
|
|
|
my $file;
|
|
|
|
my $dir=".";
|
2011-04-26 00:43:02 +04:00
|
|
|
my $wlist;
|
2015-03-30 23:22:58 +03:00
|
|
|
my $windows_os = $^O eq 'MSWin32' || $^O eq 'msys' || $^O eq 'cygwin';
|
2016-04-03 17:03:40 +03:00
|
|
|
my $verbose;
|
2015-03-17 15:53:12 +03:00
|
|
|
my %whitelist;
|
|
|
|
|
2016-04-03 12:56:10 +03:00
|
|
|
my %warnings = (
|
|
|
|
'LONGLINE' => "Line longer than $max_column",
|
|
|
|
'TABS' => 'TAB characters not allowed',
|
|
|
|
'TRAILINGSPACE' => 'Trailing white space on the line',
|
|
|
|
'CPPCOMMENTS' => '// comment detected',
|
2016-04-03 21:28:20 +03:00
|
|
|
'SPACEBEFOREPAREN' => 'space before an open parenthesis',
|
|
|
|
'SPACEAFTERPAREN' => 'space after open parenthesis',
|
|
|
|
'SPACEBEFORECLOSE' => 'space before a close parenthesis',
|
|
|
|
'SPACEBEFORECOMMA' => 'space before a comma',
|
2016-04-03 12:56:10 +03:00
|
|
|
'RETURNNOSPACE' => 'return without space',
|
|
|
|
'COMMANOSPACE' => 'comma without following space',
|
|
|
|
'BRACEELSE' => '} else on the same line',
|
|
|
|
'PARENBRACE' => '){ without sufficient space',
|
|
|
|
'SPACESEMILCOLON' => 'space before semicolon',
|
|
|
|
'BANNEDFUNC' => 'a banned function was used',
|
|
|
|
'FOPENMODE' => 'fopen needs a macro for the mode string',
|
|
|
|
'BRACEPOS' => 'wrong position for an open brace',
|
|
|
|
'INDENTATION' => 'wrong start column for code',
|
|
|
|
'COPYRIGHT' => 'file missing a copyright statement',
|
2016-04-03 17:03:40 +03:00
|
|
|
'BADCOMMAND' => 'bad !checksrc! instruction',
|
2016-04-19 09:30:13 +03:00
|
|
|
'UNUSEDIGNORE' => 'a warning ignore was not used',
|
2016-11-23 09:52:38 +03:00
|
|
|
'OPENCOMMENT' => 'file ended with a /* comment still "open"',
|
2016-11-23 10:29:42 +03:00
|
|
|
'ASTERISKSPACE' => 'pointer declared with space after asterisk',
|
2016-12-19 10:31:59 +03:00
|
|
|
'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
|
2017-09-09 12:57:17 +03:00
|
|
|
'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
|
|
|
|
'EQUALSNOSPACE' => 'equals sign without following space',
|
|
|
|
'NOSPACEEQUALS' => 'equals sign without preceeding space',
|
2017-09-11 10:25:31 +03:00
|
|
|
'SEMINOSPACE' => 'semicolon without following space',
|
2017-09-12 10:28:37 +03:00
|
|
|
'MULTISPACE' => 'multiple spaces used when not suitable',
|
2016-04-03 17:03:40 +03:00
|
|
|
);
|
2016-04-03 12:56:10 +03:00
|
|
|
|
2015-03-17 15:53:12 +03:00
|
|
|
sub readwhitelist {
|
2015-03-18 01:26:48 +03:00
|
|
|
open(W, "<$dir/checksrc.whitelist");
|
2015-03-17 15:53:12 +03:00
|
|
|
my @all=<W>;
|
2015-03-30 23:22:58 +03:00
|
|
|
for(@all) {
|
|
|
|
$windows_os ? $_ =~ s/\r?\n$// : chomp;
|
2015-03-17 15:53:12 +03:00
|
|
|
$whitelist{$_}=1;
|
|
|
|
}
|
|
|
|
close(W);
|
|
|
|
}
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
sub checkwarn {
|
2016-04-03 12:56:10 +03:00
|
|
|
my ($name, $num, $col, $file, $line, $msg, $error) = @_;
|
|
|
|
|
|
|
|
my $w=$error?"error":"warning";
|
2016-04-03 17:03:40 +03:00
|
|
|
my $nowarn=0;
|
|
|
|
|
|
|
|
#if(!$warnings{$name}) {
|
|
|
|
# print STDERR "Dev! there's no description for $name!\n";
|
|
|
|
#}
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2016-04-03 17:03:40 +03:00
|
|
|
# checksrc.whitelist
|
2015-03-17 15:53:12 +03:00
|
|
|
if($whitelist{$line}) {
|
2016-04-03 17:03:40 +03:00
|
|
|
$nowarn = 1;
|
|
|
|
}
|
|
|
|
# !checksrc! controlled
|
|
|
|
elsif($ignore{$name}) {
|
|
|
|
$ignore{$name}--;
|
|
|
|
$ignore_used{$name}++;
|
|
|
|
$nowarn = 1;
|
|
|
|
if(!$ignore{$name}) {
|
|
|
|
# reached zero, enable again
|
|
|
|
enable_warn($name, $line, $file, $l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($nowarn) {
|
2015-03-17 15:53:12 +03:00
|
|
|
$supressed++;
|
2016-04-03 12:56:10 +03:00
|
|
|
if($w) {
|
|
|
|
$swarnings++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$serrors++;
|
|
|
|
}
|
2015-03-17 15:53:12 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-04-03 12:29:14 +03:00
|
|
|
|
2011-04-21 00:44:08 +04:00
|
|
|
if($w) {
|
|
|
|
$warnings++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$errors++;
|
|
|
|
}
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
$col++;
|
2016-04-03 12:56:10 +03:00
|
|
|
print "$file:$num:$col: $w: $msg ($name)\n";
|
2011-04-20 17:17:42 +04:00
|
|
|
print " $line\n";
|
|
|
|
|
|
|
|
if($col < 80) {
|
|
|
|
my $pref = (' ' x $col);
|
|
|
|
print "${pref}^\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
$file = shift @ARGV;
|
|
|
|
|
2011-04-26 00:43:02 +04:00
|
|
|
while(1) {
|
|
|
|
|
|
|
|
if($file =~ /-D(.*)/) {
|
|
|
|
$dir = $1;
|
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
elsif($file =~ /-W(.*)/) {
|
2011-08-05 23:01:39 +04:00
|
|
|
$wlist .= " $1 ";
|
2011-04-26 00:43:02 +04:00
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
2016-04-03 12:56:10 +03:00
|
|
|
elsif($file =~ /^(-h|--help)/) {
|
|
|
|
undef $file;
|
|
|
|
last;
|
|
|
|
}
|
2011-04-26 00:43:02 +04:00
|
|
|
|
|
|
|
last;
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
if(!$file) {
|
2011-04-23 00:58:17 +04:00
|
|
|
print "checksrc.pl [option] <file1> [file2] ...\n";
|
|
|
|
print " Options:\n";
|
|
|
|
print " -D[DIR] Directory to prepend file names\n";
|
2016-04-03 17:03:40 +03:00
|
|
|
print " -h Show help output\n";
|
2011-04-26 00:43:02 +04:00
|
|
|
print " -W[file] Whitelist the given file - ignore all its flaws\n";
|
2016-04-03 12:56:10 +03:00
|
|
|
print "\nDetects and warns for these problems:\n";
|
|
|
|
for(sort keys %warnings) {
|
|
|
|
printf (" %-18s: %s\n", $_, $warnings{$_});
|
|
|
|
}
|
2011-04-20 17:17:42 +04:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-03-17 15:53:12 +03:00
|
|
|
readwhitelist();
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
do {
|
2011-08-05 23:24:59 +04:00
|
|
|
if("$wlist" !~ / $file /) {
|
2011-04-27 23:42:15 +04:00
|
|
|
my $fullname = $file;
|
2011-05-26 21:17:10 +04:00
|
|
|
$fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
|
2011-04-27 23:42:15 +04:00
|
|
|
scanfile($fullname);
|
2011-04-26 00:43:02 +04:00
|
|
|
}
|
2011-04-23 00:58:17 +04:00
|
|
|
$file = shift @ARGV;
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
} while($file);
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2016-04-03 17:03:40 +03:00
|
|
|
sub checksrc_clear {
|
|
|
|
undef %ignore;
|
|
|
|
undef %ignore_set;
|
|
|
|
undef @ignore_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub checksrc_endoffile {
|
|
|
|
my ($file) = @_;
|
|
|
|
for(keys %ignore_set) {
|
|
|
|
if($ignore_set{$_} && !$ignore_used{$_}) {
|
|
|
|
checkwarn("UNUSEDIGNORE", $ignore_set{$_},
|
|
|
|
length($_)+11, $file,
|
|
|
|
$ignore_line[$ignore_set{$_}],
|
|
|
|
"Unused ignore: $_");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub enable_warn {
|
|
|
|
my ($what, $line, $file, $l) = @_;
|
|
|
|
|
|
|
|
# switch it back on, but warn if not triggered!
|
|
|
|
if(!$ignore_used{$what}) {
|
|
|
|
checkwarn("UNUSEDIGNORE",
|
|
|
|
$line, length($what) + 11, $file, $l,
|
|
|
|
"No warning was inhibited!");
|
|
|
|
}
|
|
|
|
$ignore_set{$what}=0;
|
|
|
|
$ignore_used{$what}=0;
|
|
|
|
$ignore{$what}=0;
|
|
|
|
}
|
|
|
|
sub checksrc {
|
|
|
|
my ($cmd, $line, $file, $l) = @_;
|
|
|
|
if($cmd =~ / *([^ ]*) *(.*)/) {
|
|
|
|
my ($enable, $what) = ($1, $2);
|
|
|
|
$what =~ s: *\*/$::; # cut off end of C comment
|
|
|
|
# print "ENABLE $enable WHAT $what\n";
|
|
|
|
if($enable eq "disable") {
|
|
|
|
my ($warn, $scope)=($1, $2);
|
|
|
|
if($what =~ /([^ ]*) +(.*)/) {
|
|
|
|
($warn, $scope)=($1, $2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$warn = $what;
|
|
|
|
$scope = 1;
|
|
|
|
}
|
|
|
|
# print "IGNORE $warn for SCOPE $scope\n";
|
|
|
|
if($scope eq "all") {
|
|
|
|
$scope=999999;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($ignore_set{$warn}) {
|
|
|
|
checkwarn("BADCOMMAND",
|
|
|
|
$line, 0, $file, $l,
|
|
|
|
"$warn already disabled from line $ignore_set{$warn}");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ignore{$warn}=$scope;
|
|
|
|
$ignore_set{$warn}=$line;
|
|
|
|
$ignore_line[$line]=$l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif($enable eq "enable") {
|
|
|
|
enable_warn($what, $line, $file, $l);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkwarn("BADCOMMAND",
|
|
|
|
$line, 0, $file, $l,
|
|
|
|
"Illegal !checksrc! command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2016-12-14 01:34:59 +03:00
|
|
|
sub nostrings {
|
|
|
|
my ($str) = @_;
|
|
|
|
$str =~ s/\".*\"//g;
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
sub scanfile {
|
|
|
|
my ($file) = @_;
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
my $line = 1;
|
|
|
|
my $prevl;
|
|
|
|
my $l;
|
|
|
|
open(R, "<$file") || die "failed to open $file";
|
|
|
|
|
2016-04-19 09:30:13 +03:00
|
|
|
my $incomment=0;
|
2011-04-23 00:58:17 +04:00
|
|
|
my $copyright=0;
|
2016-04-03 17:03:40 +03:00
|
|
|
checksrc_clear(); # for file based ignores
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
while(<R>) {
|
2015-03-30 23:22:58 +03:00
|
|
|
$windows_os ? $_ =~ s/\r?\n$// : chomp;
|
2011-04-23 00:58:17 +04:00
|
|
|
my $l = $_;
|
2016-04-19 09:30:13 +03:00
|
|
|
my $ol = $l; # keep the unmodified line for error reporting
|
2011-04-23 00:58:17 +04:00
|
|
|
my $column = 0;
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2016-04-03 17:03:40 +03:00
|
|
|
# check for !checksrc! commands
|
|
|
|
if($l =~ /\!checksrc\! (.*)/) {
|
|
|
|
my $cmd = $1;
|
|
|
|
checksrc($cmd, $line, $file, $l)
|
|
|
|
}
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
# check for a copyright statement
|
|
|
|
if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
|
|
|
|
$copyright=1;
|
2011-04-20 17:17:42 +04:00
|
|
|
}
|
2011-04-23 00:58:17 +04:00
|
|
|
|
|
|
|
# detect long lines
|
|
|
|
if(length($l) > $max_column) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("LONGLINE", $line, length($l), $file, $l,
|
|
|
|
"Longer than $max_column columns");
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
|
|
|
# detect TAB characters
|
|
|
|
if($l =~ /^(.*)\t/) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("TABS",
|
|
|
|
$line, length($1), $file, $l, "Contains TAB character", 1);
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
|
|
|
# detect trailing white space
|
2011-05-16 17:21:04 +04:00
|
|
|
if($l =~ /^(.*)[ \t]+\z/) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("TRAILINGSPACE",
|
|
|
|
$line, length($1), $file, $l, "Trailing whitespace");
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
|
|
|
|
2016-04-19 09:30:13 +03:00
|
|
|
# ------------------------------------------------------------
|
|
|
|
# Above this marker, the checks were done on lines *including*
|
|
|
|
# comments
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
# strip off C89 comments
|
|
|
|
|
|
|
|
comment:
|
|
|
|
if(!$incomment) {
|
|
|
|
if($l =~ s/\/\*.*\*\// /g) {
|
|
|
|
# full /* comments */ were removed!
|
|
|
|
}
|
|
|
|
if($l =~ s/\/\*.*//) {
|
|
|
|
# start of /* comment was removed
|
|
|
|
$incomment = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if($l =~ s/.*\*\///) {
|
|
|
|
# end of comment */ was removed
|
|
|
|
$incomment = 0;
|
|
|
|
goto comment;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# still within a comment
|
|
|
|
$l="";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
# Below this marker, the checks were done on lines *without*
|
|
|
|
# comments
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
2015-10-21 14:46:03 +03:00
|
|
|
# crude attempt to detect // comments without too many false
|
|
|
|
# positives
|
|
|
|
if($l =~ /^([^"\*]*)[^:"]\/\//) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("CPPCOMMENTS",
|
|
|
|
$line, length($1), $file, $l, "\/\/ comment");
|
2015-10-21 14:46:03 +03:00
|
|
|
}
|
2016-04-19 09:30:13 +03:00
|
|
|
|
2016-12-14 01:34:59 +03:00
|
|
|
my $nostr = nostrings($l);
|
|
|
|
# check spaces after for/if/while/function call
|
|
|
|
if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
|
2011-04-23 00:58:17 +04:00
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
2016-12-14 01:34:59 +03:00
|
|
|
elsif($3 eq "return") {
|
|
|
|
# return must have a space
|
|
|
|
}
|
2017-09-02 14:58:55 +03:00
|
|
|
elsif($3 eq "case") {
|
|
|
|
# case must have a space
|
|
|
|
}
|
2016-12-14 01:34:59 +03:00
|
|
|
elsif($4 eq "*") {
|
|
|
|
# (* beginning makes the space OK!
|
|
|
|
}
|
|
|
|
elsif($1 =~ / *typedef/) {
|
|
|
|
# typedefs can use space-paren
|
|
|
|
}
|
2011-04-23 00:58:17 +04:00
|
|
|
else {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l,
|
2011-04-23 00:58:17 +04:00
|
|
|
"$2 with space");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:29:44 +03:00
|
|
|
if($nostr =~ /^((.*)(if) *\()(.*)\)/) {
|
|
|
|
my $pos = length($1);
|
|
|
|
if($4 =~ / = /) {
|
|
|
|
checkwarn("ASSIGNWITHINCONDITION",
|
|
|
|
$line, $pos+1, $file, $l,
|
|
|
|
"assignment within conditional expression");
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 21:28:20 +03:00
|
|
|
# check spaces after open parentheses
|
|
|
|
if($l =~ /^(.*[a-z])\( /i) {
|
|
|
|
checkwarn("SPACEAFTERPAREN",
|
|
|
|
$line, length($1)+1, $file, $l,
|
|
|
|
"space after open parenthesis");
|
|
|
|
}
|
|
|
|
|
|
|
|
# check spaces before close parentheses, unless it was a space or a
|
|
|
|
# close parenthesis!
|
|
|
|
if($l =~ /(.*[^\) ]) \)/) {
|
|
|
|
checkwarn("SPACEBEFORECLOSE",
|
|
|
|
$line, length($1)+1, $file, $l,
|
|
|
|
"space before close parenthesis");
|
|
|
|
}
|
|
|
|
|
|
|
|
# check spaces before comma!
|
|
|
|
if($l =~ /(.*[^ ]) ,/) {
|
|
|
|
checkwarn("SPACEBEFORECOMMA",
|
|
|
|
$line, length($1)+1, $file, $l,
|
|
|
|
"space before comma");
|
2011-04-20 17:17:42 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 15:05:01 +03:00
|
|
|
# check for "return(" without space
|
|
|
|
if($l =~ /^(.*)return\(/) {
|
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
|
|
|
else {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
|
2015-03-17 15:05:01 +03:00
|
|
|
"return without space before paren");
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 15:41:49 +03: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) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
|
2015-03-17 15:41:49 +03:00
|
|
|
"comma without following space");
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 12:29:14 +03:00
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
# check for "} else"
|
2011-09-08 00:45:43 +04:00
|
|
|
if($l =~ /^(.*)\} *else/) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("BRACEELSE",
|
|
|
|
$line, length($1), $file, $l, "else after closing brace on same line");
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
2011-07-05 00:08:14 +04:00
|
|
|
# check for "){"
|
|
|
|
if($l =~ /^(.*)\)\{/) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("PARENBRACE",
|
|
|
|
$line, length($1)+1, $file, $l, "missing space after close paren");
|
2011-07-05 00:08:14 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 16:06:48 +03:00
|
|
|
# check for space before the semicolon last in a line
|
|
|
|
if($l =~ /^(.*[^ ].*) ;$/) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("SPACESEMILCOLON",
|
2016-04-19 09:30:13 +03:00
|
|
|
$line, length($1), $file, $ol, "space before last semicolon");
|
2015-03-17 16:06:48 +03:00
|
|
|
}
|
|
|
|
|
2013-03-06 16:27:51 +04:00
|
|
|
# scan for use of banned functions
|
2016-06-06 04:07:03 +03:00
|
|
|
if($l =~ /^(.*\W)
|
|
|
|
(gets|
|
2016-09-07 10:26:00 +03:00
|
|
|
strtok|
|
2016-06-06 04:07:03 +03:00
|
|
|
v?sprintf|
|
|
|
|
(str|_mbs|_tcs|_wcs)n?cat|
|
|
|
|
LoadLibrary(Ex)?(A|W)?)
|
|
|
|
\s*\(
|
|
|
|
/x) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("BANNEDFUNC",
|
2016-04-19 09:30:13 +03:00
|
|
|
$line, length($1), $file, $ol,
|
2013-03-06 16:27:51 +04:00
|
|
|
"use of $2 is banned");
|
|
|
|
}
|
|
|
|
|
2015-06-02 09:28:10 +03:00
|
|
|
# scan for use of non-binary fopen without the macro
|
2016-04-03 12:29:14 +03:00
|
|
|
if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
|
2015-06-02 09:28:10 +03:00
|
|
|
my $mode = $2;
|
|
|
|
if($mode !~ /b/) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("FOPENMODE",
|
2016-04-19 09:30:13 +03:00
|
|
|
$line, length($1), $file, $ol,
|
2016-04-03 12:29:14 +03:00
|
|
|
"use of non-binary fopen without FOPEN_* macro: $mode");
|
2015-06-02 09:28:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 00:58:17 +04: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 =~ /^( +)\{/)) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("BRACEPOS",
|
2016-04-19 09:30:13 +03:00
|
|
|
$line, length($1), $file, $ol, "badly placed open brace");
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2011-04-23 00:58:17 +04: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;
|
2016-04-19 09:30:13 +03:00
|
|
|
checkwarn("INDENTATION", $line, length($1), $file, $ol,
|
2011-04-23 00:58:17 +04:00
|
|
|
"not indented $indent steps, uses $diff)");
|
|
|
|
|
|
|
|
}
|
2011-04-20 17:17:42 +04:00
|
|
|
}
|
|
|
|
}
|
2011-04-23 00:58:17 +04:00
|
|
|
|
2016-11-23 09:52:38 +03:00
|
|
|
# check for 'char * name'
|
2016-11-23 10:29:42 +03:00
|
|
|
if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost) *(\*+)) (\w+)/) && ($4 ne "const")) {
|
|
|
|
checkwarn("ASTERISKNOSPACE",
|
2016-11-23 09:52:38 +03:00
|
|
|
$line, length($1), $file, $ol,
|
|
|
|
"no space after declarative asterisk");
|
|
|
|
}
|
2016-11-23 10:29:42 +03:00
|
|
|
# check for 'char*'
|
|
|
|
if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
|
|
|
|
checkwarn("ASTERISKNOSPACE",
|
|
|
|
$line, length($1)-1, $file, $ol,
|
|
|
|
"no space before asterisk");
|
|
|
|
}
|
2016-11-23 10:48:42 +03:00
|
|
|
|
|
|
|
# check for 'void func() {', but avoid false positives by requiring
|
|
|
|
# both an open and closed parentheses before the open brace
|
2017-07-27 02:13:19 +03:00
|
|
|
if($l =~ /^((\w).*)\{\z/) {
|
2016-11-23 10:48:42 +03:00
|
|
|
my $k = $1;
|
|
|
|
$k =~ s/const *//;
|
|
|
|
$k =~ s/static *//;
|
|
|
|
if($k =~ /\(.*\)/) {
|
|
|
|
checkwarn("BRACEPOS",
|
|
|
|
$line, length($l)-1, $file, $ol,
|
|
|
|
"wrongly placed open brace");
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 12:57:17 +03:00
|
|
|
|
|
|
|
# check for equals sign without spaces next to it
|
|
|
|
if($nostr =~ /(.*)\=[a-z0-9]/i) {
|
|
|
|
checkwarn("EQUALSNOSPACE",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"no space after equals sign");
|
|
|
|
}
|
|
|
|
# check for equals sign without spaces before it
|
|
|
|
elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
|
|
|
|
checkwarn("NOSPACEEQUALS",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"no space before equals sign");
|
|
|
|
}
|
|
|
|
|
2017-09-10 00:54:47 +03:00
|
|
|
# check for plus signs without spaces next to it
|
|
|
|
if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
|
|
|
|
checkwarn("PLUSNOSPACE",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"no space after plus sign");
|
|
|
|
}
|
|
|
|
# check for plus sign without spaces before it
|
|
|
|
elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
|
|
|
|
checkwarn("NOSPACEPLUS",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"no space before plus sign");
|
|
|
|
}
|
|
|
|
|
2017-09-11 10:25:31 +03:00
|
|
|
# check for semicolons without space next to it
|
|
|
|
if($nostr =~ /(.*)\;[a-z0-9]/i) {
|
|
|
|
checkwarn("SEMINOSPACE",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"no space after semilcolon");
|
|
|
|
}
|
|
|
|
|
2017-09-12 10:28:37 +03:00
|
|
|
# check for more than one consecutive space before open brace or
|
|
|
|
# question mark. Skip lines containing strings since they make it hard
|
|
|
|
# due to artificially getting multiple spaces
|
|
|
|
if(($l eq $nostr) &&
|
|
|
|
$nostr =~ /^(.*(\S)) + [{?]/i) {
|
|
|
|
checkwarn("MULTISPACE",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"multiple space");
|
|
|
|
print STDERR "L: $l\n";
|
|
|
|
print STDERR "nostr: $nostr\n";
|
|
|
|
}
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
$line++;
|
2016-04-19 09:30:13 +03:00
|
|
|
$prevl = $ol;
|
2011-04-20 17:17:42 +04:00
|
|
|
}
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
if(!$copyright) {
|
2016-04-03 12:56:10 +03:00
|
|
|
checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
|
2011-04-23 00:58:17 +04:00
|
|
|
}
|
2016-04-19 09:30:13 +03:00
|
|
|
if($incomment) {
|
|
|
|
checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
|
|
|
|
}
|
2011-04-20 17:17:42 +04:00
|
|
|
|
2016-04-03 17:03:40 +03:00
|
|
|
checksrc_endoffile($file);
|
|
|
|
|
2011-04-23 00:58:17 +04:00
|
|
|
close(R);
|
2011-04-20 17:17:42 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-21 00:44:08 +04:00
|
|
|
|
2016-04-03 12:56:10 +03:00
|
|
|
if($errors || $warnings || $verbose) {
|
2011-04-23 00:58:17 +04:00
|
|
|
printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
|
2016-04-03 12:56:10 +03:00
|
|
|
if($supressed) {
|
|
|
|
printf "checksrc: %d errors and %d warnings suppressed\n",
|
|
|
|
$serrors,
|
|
|
|
$swarnings;
|
|
|
|
}
|
2011-04-21 00:44:08 +04:00
|
|
|
exit 5; # return failure
|
|
|
|
}
|