2018-01-07 14:34:46 +08:00
|
|
|
#!/usr/bin/env perl
|
2011-04-23 04:58:17 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# _ _ ____ _
|
|
|
|
# Project ___| | | | _ \| |
|
|
|
|
# / __| | | | |_) | |
|
|
|
|
# | (__| |_| | _ <| |___
|
|
|
|
# \___|\___/|_| \_\_____|
|
|
|
|
#
|
2023-01-02 20:51:48 +08:00
|
|
|
# Copyright (C) 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
|
2020-11-04 21:02:01 +08:00
|
|
|
# are also available at https://curl.se/docs/copyright.html.
|
2011-04-23 04:58:17 +08: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.
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
#
|
2011-04-23 04:58:17 +08:00
|
|
|
###########################################################################
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2018-10-05 19:29:37 +08:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
my $max_column = 79;
|
|
|
|
my $indent = 2;
|
|
|
|
|
2018-10-05 19:29:37 +08:00
|
|
|
my $warnings = 0;
|
|
|
|
my $swarnings = 0;
|
|
|
|
my $errors = 0;
|
|
|
|
my $serrors = 0;
|
2020-06-09 22:08:11 +08:00
|
|
|
my $suppressed; # skipped problems
|
2011-04-23 04:58:17 +08:00
|
|
|
my $file;
|
|
|
|
my $dir=".";
|
2018-10-05 19:29:37 +08:00
|
|
|
my $wlist="";
|
2018-11-22 16:00:57 +08:00
|
|
|
my @alist;
|
2020-03-06 18:13:57 +08:00
|
|
|
my $windows_os = $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys';
|
2016-04-03 22:03:40 +08:00
|
|
|
my $verbose;
|
2020-06-09 22:08:11 +08:00
|
|
|
my %skiplist;
|
2015-03-17 20:53:12 +08:00
|
|
|
|
2018-10-05 19:29:37 +08:00
|
|
|
my %ignore;
|
|
|
|
my %ignore_set;
|
|
|
|
my %ignore_used;
|
|
|
|
my @ignore_line;
|
|
|
|
|
2018-12-04 06:13:40 +08:00
|
|
|
my %warnings_extended = (
|
|
|
|
'COPYRIGHTYEAR' => 'copyright year incorrect',
|
2021-09-08 17:27:49 +08:00
|
|
|
'STRERROR', => 'strerror() detected',
|
2023-09-28 18:50:07 +08:00
|
|
|
'STDERR', => 'stderr detected',
|
2018-12-04 06:13:40 +08:00
|
|
|
);
|
|
|
|
|
2016-04-03 17:56:10 +08:00
|
|
|
my %warnings = (
|
2023-01-02 20:51:48 +08:00
|
|
|
'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
|
|
|
|
'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
|
|
|
|
'ASTERISKSPACE' => 'pointer declared with space after asterisk',
|
|
|
|
'BADCOMMAND' => 'bad !checksrc! instruction',
|
2016-04-03 17:56:10 +08:00
|
|
|
'BANNEDFUNC' => 'a banned function was used',
|
2023-11-22 00:54:49 +08:00
|
|
|
'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line',
|
2023-01-02 20:51:48 +08:00
|
|
|
'BRACEELSE' => '} else on the same line',
|
2016-04-03 17:56:10 +08:00
|
|
|
'BRACEPOS' => 'wrong position for an open brace',
|
2023-01-02 20:51:48 +08:00
|
|
|
'BRACEWHILE' => 'A single space between open brace and while',
|
|
|
|
'COMMANOSPACE' => 'comma without following space',
|
|
|
|
'COMMENTNOSPACEEND' => 'no space before */',
|
|
|
|
'COMMENTNOSPACESTART' => 'no space following /*',
|
2016-04-03 17:56:10 +08:00
|
|
|
'COPYRIGHT' => 'file missing a copyright statement',
|
2023-01-02 20:51:48 +08:00
|
|
|
'CPPCOMMENTS' => '// comment detected',
|
|
|
|
'DOBRACE' => 'A single space between do and open brace',
|
|
|
|
'EMPTYLINEBRACE' => 'Empty line before the open brace',
|
2017-09-09 17:57:17 +08:00
|
|
|
'EQUALSNOSPACE' => 'equals sign without following space',
|
2023-01-02 20:51:48 +08:00
|
|
|
'EQUALSNULL' => 'if/while comparison with == NULL',
|
|
|
|
'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression',
|
|
|
|
'FOPENMODE' => 'fopen needs a macro for the mode string',
|
|
|
|
'INCLUDEDUP', => 'same file is included again',
|
|
|
|
'INDENTATION' => 'wrong start column for code',
|
|
|
|
'LONGLINE' => "Line longer than $max_column",
|
2023-05-18 12:54:18 +08:00
|
|
|
'SPACEBEFORELABEL' => 'labels not at the start of the line',
|
2023-01-02 20:51:48 +08:00
|
|
|
'MULTISPACE' => 'multiple spaces used when not suitable',
|
2018-02-24 07:29:01 +08:00
|
|
|
'NOSPACEEQUALS' => 'equals sign without preceding space',
|
2023-01-02 20:51:48 +08:00
|
|
|
'NOTEQUALSZERO', => 'if/while comparison with != 0',
|
|
|
|
'ONELINECONDITION' => 'conditional block on the same line as the if()',
|
|
|
|
'OPENCOMMENT' => 'file ended with a /* comment still "open"',
|
|
|
|
'PARENBRACE' => '){ without sufficient space',
|
|
|
|
'RETURNNOSPACE' => 'return without space',
|
2017-09-11 15:25:31 +08:00
|
|
|
'SEMINOSPACE' => 'semicolon without following space',
|
2018-05-12 05:40:58 +08:00
|
|
|
'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
|
2018-11-22 16:00:57 +08:00
|
|
|
'SNPRINTF' => 'use of snprintf',
|
2023-01-02 20:51:48 +08:00
|
|
|
'SPACEAFTERPAREN' => 'space after open parenthesis',
|
|
|
|
'SPACEBEFORECLOSE' => 'space before a close parenthesis',
|
|
|
|
'SPACEBEFORECOMMA' => 'space before a comma',
|
|
|
|
'SPACEBEFOREPAREN' => 'space before an open parenthesis',
|
|
|
|
'SPACESEMICOLON' => 'space before semicolon',
|
2023-04-28 01:01:28 +08:00
|
|
|
'SPACESWITCHCOLON' => 'space before colon of switch label',
|
2023-01-02 20:51:48 +08:00
|
|
|
'TABS' => 'TAB characters not allowed',
|
|
|
|
'TRAILINGSPACE' => 'Trailing whitespace on the line',
|
2020-05-14 06:05:04 +08:00
|
|
|
'TYPEDEFSTRUCT' => 'typedefed struct',
|
2023-01-02 20:51:48 +08:00
|
|
|
'UNUSEDIGNORE' => 'a warning ignore was not used',
|
2016-04-03 22:03:40 +08:00
|
|
|
);
|
2016-04-03 17:56:10 +08:00
|
|
|
|
2020-06-09 22:08:11 +08:00
|
|
|
sub readskiplist {
|
2023-06-21 16:50:27 +08:00
|
|
|
open(my $W, '<', "$dir/checksrc.skip") or return;
|
|
|
|
my @all=<$W>;
|
2015-03-31 04:22:58 +08:00
|
|
|
for(@all) {
|
|
|
|
$windows_os ? $_ =~ s/\r?\n$// : chomp;
|
2020-06-09 22:08:11 +08:00
|
|
|
$skiplist{$_}=1;
|
2015-03-17 20:53:12 +08:00
|
|
|
}
|
2023-06-21 16:50:27 +08:00
|
|
|
close($W);
|
2015-03-17 20:53:12 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 06:13:40 +08:00
|
|
|
# Reads the .checksrc in $dir for any extended warnings to enable locally.
|
|
|
|
# Currently there is no support for disabling warnings from the standard set,
|
|
|
|
# and since that's already handled via !checksrc! commands there is probably
|
|
|
|
# little use to add it.
|
|
|
|
sub readlocalfile {
|
|
|
|
my $i = 0;
|
|
|
|
|
|
|
|
open(my $rcfile, "<", "$dir/.checksrc") or return;
|
|
|
|
|
|
|
|
while(<$rcfile>) {
|
2024-02-18 14:36:27 +08:00
|
|
|
$windows_os ? $_ =~ s/\r?\n$// : chomp;
|
2018-12-04 06:13:40 +08:00
|
|
|
$i++;
|
|
|
|
|
|
|
|
# Lines starting with '#' are considered comments
|
|
|
|
if (/^\s*(#.*)/) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
elsif (/^\s*enable ([A-Z]+)$/) {
|
|
|
|
if(!defined($warnings_extended{$1})) {
|
|
|
|
print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$warnings{$1} = $warnings_extended{$1};
|
|
|
|
}
|
2020-05-14 06:05:04 +08:00
|
|
|
elsif (/^\s*disable ([A-Z]+)$/) {
|
|
|
|
if(!defined($warnings{$1})) {
|
|
|
|
print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
# Accept-list
|
|
|
|
push @alist, $1;
|
|
|
|
}
|
2018-12-04 06:13:40 +08:00
|
|
|
else {
|
|
|
|
die "Invalid format in $dir/.checksrc on line $i\n";
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 16:25:38 +08:00
|
|
|
close($rcfile);
|
2018-12-04 06:13:40 +08:00
|
|
|
}
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
sub checkwarn {
|
2016-04-03 17:56:10 +08:00
|
|
|
my ($name, $num, $col, $file, $line, $msg, $error) = @_;
|
|
|
|
|
|
|
|
my $w=$error?"error":"warning";
|
2016-04-03 22:03:40 +08:00
|
|
|
my $nowarn=0;
|
|
|
|
|
|
|
|
#if(!$warnings{$name}) {
|
|
|
|
# print STDERR "Dev! there's no description for $name!\n";
|
|
|
|
#}
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2020-06-09 22:08:11 +08:00
|
|
|
# checksrc.skip
|
|
|
|
if($skiplist{$line}) {
|
2016-04-03 22:03:40 +08:00
|
|
|
$nowarn = 1;
|
|
|
|
}
|
|
|
|
# !checksrc! controlled
|
|
|
|
elsif($ignore{$name}) {
|
|
|
|
$ignore{$name}--;
|
|
|
|
$ignore_used{$name}++;
|
|
|
|
$nowarn = 1;
|
|
|
|
if(!$ignore{$name}) {
|
|
|
|
# reached zero, enable again
|
2018-10-05 19:29:37 +08:00
|
|
|
enable_warn($name, $num, $file, $line);
|
2016-04-03 22:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($nowarn) {
|
2018-02-24 07:29:01 +08:00
|
|
|
$suppressed++;
|
2016-04-03 17:56:10 +08:00
|
|
|
if($w) {
|
|
|
|
$swarnings++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$serrors++;
|
|
|
|
}
|
2015-03-17 20:53:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-04-03 17:29:14 +08:00
|
|
|
|
2011-04-21 04:44:08 +08:00
|
|
|
if($w) {
|
|
|
|
$warnings++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$errors++;
|
|
|
|
}
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
$col++;
|
2016-04-03 17:56:10 +08:00
|
|
|
print "$file:$num:$col: $w: $msg ($name)\n";
|
2011-04-20 21:17:42 +08:00
|
|
|
print " $line\n";
|
|
|
|
|
|
|
|
if($col < 80) {
|
|
|
|
my $pref = (' ' x $col);
|
|
|
|
print "${pref}^\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
$file = shift @ARGV;
|
|
|
|
|
2019-10-01 06:32:26 +08:00
|
|
|
while(defined $file) {
|
2011-04-26 04:43:02 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-11-22 16:00:57 +08:00
|
|
|
elsif($file =~ /-A(.+)/) {
|
|
|
|
push @alist, $1;
|
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
2018-03-13 02:19:24 +08:00
|
|
|
elsif($file =~ /-i([1-9])/) {
|
|
|
|
$indent = $1 + 0;
|
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
elsif($file =~ /-m([0-9]+)/) {
|
|
|
|
$max_column = $1 + 0;
|
|
|
|
$file = shift @ARGV;
|
|
|
|
next;
|
|
|
|
}
|
2016-04-03 17:56:10 +08:00
|
|
|
elsif($file =~ /^(-h|--help)/) {
|
|
|
|
undef $file;
|
|
|
|
last;
|
|
|
|
}
|
2011-04-26 04:43:02 +08:00
|
|
|
|
|
|
|
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";
|
2018-11-22 16:00:57 +08:00
|
|
|
print " -A[rule] Accept this violation, can be used multiple times\n";
|
2011-04-23 04:58:17 +08:00
|
|
|
print " -D[DIR] Directory to prepend file names\n";
|
2016-04-03 22:03:40 +08:00
|
|
|
print " -h Show help output\n";
|
2020-06-09 22:08:11 +08:00
|
|
|
print " -W[file] Skip the given file - ignore all its flaws\n";
|
2018-03-13 02:19:24 +08:00
|
|
|
print " -i<n> Indent spaces. Default: 2\n";
|
|
|
|
print " -m<n> Maximum line length. Default: 79\n";
|
2016-04-03 17:56:10 +08:00
|
|
|
print "\nDetects and warns for these problems:\n";
|
2021-09-08 17:27:49 +08:00
|
|
|
my @allw = keys %warnings;
|
|
|
|
push @allw, keys %warnings_extended;
|
|
|
|
for my $w (sort @allw) {
|
|
|
|
if($warnings{$w}) {
|
|
|
|
printf (" %-18s: %s\n", $w, $warnings{$w});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf (" %-18s: %s[*]\n", $w, $warnings_extended{$w});
|
|
|
|
}
|
2016-04-03 17:56:10 +08:00
|
|
|
}
|
2021-09-08 17:27:49 +08:00
|
|
|
print " [*] = disabled by default\n";
|
2011-04-20 21:17:42 +08:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:08:11 +08:00
|
|
|
readskiplist();
|
2018-12-04 06:13:40 +08:00
|
|
|
readlocalfile();
|
2015-03-17 20:53:12 +08:00
|
|
|
|
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
|
|
|
|
2018-11-22 16:00:57 +08:00
|
|
|
sub accept_violations {
|
|
|
|
for my $r (@alist) {
|
|
|
|
if(!$warnings{$r}) {
|
|
|
|
print "'$r' is not a warning to accept!\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$ignore{$r}=999999;
|
|
|
|
$ignore_used{$r}=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 22:03:40 +08: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;
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:33:21 +08:00
|
|
|
# Comparing for a literal zero rather than the scalar value zero
|
|
|
|
# covers the case where $scope contains the ending '*' from the
|
|
|
|
# comment. If we use a scalar comparison (==) we induce warnings
|
|
|
|
# on non-scalar contents.
|
|
|
|
if($scope eq "0") {
|
|
|
|
checkwarn("BADCOMMAND",
|
|
|
|
$line, 0, $file, $l,
|
|
|
|
"Disable zero not supported, did you mean to enable?");
|
|
|
|
}
|
|
|
|
elsif($ignore_set{$warn}) {
|
2016-04-03 22:03:40 +08:00
|
|
|
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 21:17:42 +08:00
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
sub nostrings {
|
|
|
|
my ($str) = @_;
|
|
|
|
$str =~ s/\".*\"//g;
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
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;
|
2018-10-05 19:29:37 +08:00
|
|
|
my $prevl="";
|
2021-10-16 19:50:25 +08:00
|
|
|
my $prevpl="";
|
|
|
|
my $l = "";
|
|
|
|
my $prep = 0;
|
|
|
|
my $prevp = 0;
|
2023-06-21 16:50:27 +08:00
|
|
|
open(my $R, '<', $file) || die "failed to open $file";
|
2011-04-23 04:58:17 +08:00
|
|
|
|
2016-04-19 14:30:13 +08:00
|
|
|
my $incomment=0;
|
2018-12-04 06:13:40 +08:00
|
|
|
my @copyright=();
|
2022-10-25 05:26:56 +08:00
|
|
|
my %includes;
|
2016-04-03 22:03:40 +08:00
|
|
|
checksrc_clear(); # for file based ignores
|
2018-11-22 16:00:57 +08:00
|
|
|
accept_violations();
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2023-06-21 16:50:27 +08:00
|
|
|
while(<$R>) {
|
2015-03-31 04:22:58 +08:00
|
|
|
$windows_os ? $_ =~ s/\r?\n$// : chomp;
|
2011-04-23 04:58:17 +08:00
|
|
|
my $l = $_;
|
2016-04-19 14:30:13 +08:00
|
|
|
my $ol = $l; # keep the unmodified line for error reporting
|
2011-04-23 04:58:17 +08:00
|
|
|
my $column = 0;
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2016-04-03 22:03:40 +08:00
|
|
|
# check for !checksrc! commands
|
|
|
|
if($l =~ /\!checksrc\! (.*)/) {
|
|
|
|
my $cmd = $1;
|
|
|
|
checksrc($cmd, $line, $file, $l)
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:50:42 +08:00
|
|
|
if($l =~ /^#line (\d+) \"([^\"]*)\"/) {
|
|
|
|
# a #line instruction
|
|
|
|
$file = $2;
|
|
|
|
$line = $1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:13:40 +08:00
|
|
|
# check for a copyright statement and save the years
|
2023-01-02 20:51:48 +08:00
|
|
|
if($l =~ /\* +copyright .* (\d\d\d\d|)/i) {
|
|
|
|
my $count = 0;
|
2018-12-04 06:13:40 +08:00
|
|
|
while($l =~ /([\d]{4})/g) {
|
|
|
|
push @copyright, {
|
|
|
|
year => $1,
|
|
|
|
line => $line,
|
|
|
|
col => index($l, $1),
|
|
|
|
code => $l
|
|
|
|
};
|
2023-01-02 20:51:48 +08:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
if(!$count) {
|
|
|
|
# year-less
|
|
|
|
push @copyright, {
|
|
|
|
year => -1,
|
|
|
|
line => $line,
|
|
|
|
col => index($l, $1),
|
|
|
|
code => $l
|
|
|
|
};
|
2018-12-04 06:13:40 +08:00
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
|
|
|
|
# detect long lines
|
|
|
|
if(length($l) > $max_column) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("LONGLINE", $line, length($l), $file, $l,
|
|
|
|
"Longer than $max_column columns");
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
|
|
|
# detect TAB characters
|
|
|
|
if($l =~ /^(.*)\t/) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("TABS",
|
|
|
|
$line, length($1), $file, $l, "Contains TAB character", 1);
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
2020-10-01 03:10:14 +08:00
|
|
|
# detect trailing whitespace
|
2011-05-16 21:21:04 +08:00
|
|
|
if($l =~ /^(.*)[ \t]+\z/) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("TRAILINGSPACE",
|
|
|
|
$line, length($1), $file, $l, "Trailing whitespace");
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
|
|
|
|
2022-10-31 00:38:16 +08:00
|
|
|
# no space after comment start
|
|
|
|
if($l =~ /^(.*)\/\*\w/) {
|
|
|
|
checkwarn("COMMENTNOSPACESTART",
|
|
|
|
$line, length($1) + 2, $file, $l,
|
|
|
|
"Missing space after comment start");
|
|
|
|
}
|
|
|
|
# no space at comment end
|
|
|
|
if($l =~ /^(.*)\w\*\//) {
|
|
|
|
checkwarn("COMMENTNOSPACEEND",
|
|
|
|
$line, length($1) + 1, $file, $l,
|
|
|
|
"Missing space end comment end");
|
|
|
|
}
|
2016-04-19 14:30:13 +08: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
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
2021-10-16 19:50:25 +08:00
|
|
|
# prev line was a preprocessor **and** ended with a backslash
|
|
|
|
if($prep && ($prevpl =~ /\\ *\z/)) {
|
|
|
|
# this is still a preprocessor line
|
|
|
|
$prep = 1;
|
|
|
|
goto preproc;
|
|
|
|
}
|
|
|
|
$prep = 0;
|
|
|
|
|
2015-10-21 19:46:03 +08:00
|
|
|
# crude attempt to detect // comments without too many false
|
|
|
|
# positives
|
2020-10-07 15:11:03 +08:00
|
|
|
if($l =~ /^(([^"\*]*)[^:"]|)\/\//) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("CPPCOMMENTS",
|
|
|
|
$line, length($1), $file, $l, "\/\/ comment");
|
2015-10-21 19:46:03 +08:00
|
|
|
}
|
2016-04-19 14:30:13 +08:00
|
|
|
|
2022-10-25 05:26:56 +08:00
|
|
|
if($l =~ /^(\#\s*include\s+)([\">].*[>}"])/) {
|
|
|
|
my ($pre, $path) = ($1, $2);
|
|
|
|
if($includes{$path}) {
|
|
|
|
checkwarn("INCLUDEDUP",
|
|
|
|
$line, length($1), $file, $l, "duplicated include");
|
|
|
|
}
|
|
|
|
$includes{$path} = $l;
|
|
|
|
}
|
|
|
|
|
2021-10-16 19:50:25 +08:00
|
|
|
# detect and strip preprocessor directives
|
|
|
|
if($l =~ /^[ \t]*\#/) {
|
|
|
|
# preprocessor line
|
|
|
|
$prep = 1;
|
|
|
|
goto preproc;
|
|
|
|
}
|
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
my $nostr = nostrings($l);
|
|
|
|
# check spaces after for/if/while/function call
|
2023-04-27 22:29:45 +08:00
|
|
|
if($nostr =~ /^(.*)(for|if|while|switch| ([a-zA-Z0-9_]+)) \((.)/) {
|
|
|
|
my ($leading, $word, $extra, $first)=($1,$2,$3,$4);
|
2011-04-23 04:58:17 +08:00
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
2018-10-05 19:29:37 +08:00
|
|
|
elsif(defined $3 && $3 eq "return") {
|
2016-12-14 06:34:59 +08:00
|
|
|
# return must have a space
|
|
|
|
}
|
2018-10-05 19:29:37 +08:00
|
|
|
elsif(defined $3 && $3 eq "case") {
|
2017-09-02 19:58:55 +08:00
|
|
|
# case must have a space
|
|
|
|
}
|
2023-04-27 22:29:45 +08:00
|
|
|
elsif(($first eq "*") && ($word !~ /(for|if|while|switch)/)) {
|
|
|
|
# A "(*" beginning makes the space OK because it wants to
|
2023-08-01 17:19:58 +08:00
|
|
|
# allow function pointer declared
|
2016-12-14 06:34:59 +08:00
|
|
|
}
|
|
|
|
elsif($1 =~ / *typedef/) {
|
|
|
|
# typedefs can use space-paren
|
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
else {
|
2023-04-27 22:29:45 +08:00
|
|
|
checkwarn("SPACEBEFOREPAREN", $line, length($leading)+length($word), $file, $l,
|
|
|
|
"$word with space");
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 16:45:29 +08:00
|
|
|
# check for '== NULL' in if/while conditions but not if the thing on
|
|
|
|
# the left of it is a function call
|
2021-12-26 04:41:14 +08:00
|
|
|
if($nostr =~ /^(.*)(if|while)(\(.*?)([!=]= NULL|NULL [!=]=)/) {
|
2021-04-19 16:45:29 +08:00
|
|
|
checkwarn("EQUALSNULL", $line,
|
|
|
|
length($1) + length($2) + length($3),
|
|
|
|
$file, $l, "we prefer !variable instead of \"== NULL\" comparisons");
|
|
|
|
}
|
|
|
|
|
|
|
|
# check for '!= 0' in if/while conditions but not if the thing on
|
|
|
|
# the left of it is a function call
|
|
|
|
if($nostr =~ /^(.*)(if|while)(\(.*[^)]) != 0[^x]/) {
|
|
|
|
checkwarn("NOTEQUALSZERO", $line,
|
|
|
|
length($1) + length($2) + length($3),
|
|
|
|
$file, $l, "we prefer if(rc) instead of \"rc != 0\" comparisons");
|
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
|
2020-08-24 15:31:39 +08:00
|
|
|
# check spaces in 'do {'
|
|
|
|
if($nostr =~ /^( *)do( *)\{/ && length($2) != 1) {
|
|
|
|
checkwarn("DOBRACE", $line, length($1) + 2, $file, $l, "one space after do before brace");
|
|
|
|
}
|
|
|
|
# check spaces in 'do {'
|
|
|
|
elsif($nostr =~ /^( *)\}( *)while/ && length($2) != 1) {
|
|
|
|
checkwarn("BRACEWHILE", $line, length($1) + 2, $file, $l, "one space between brace and while");
|
|
|
|
}
|
2020-03-30 16:52:48 +08:00
|
|
|
if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
|
2016-12-14 08:29:44 +08:00
|
|
|
my $pos = length($1);
|
2020-03-30 16:52:48 +08:00
|
|
|
my $postparen = $5;
|
|
|
|
my $cond = $4;
|
|
|
|
if($cond =~ / = /) {
|
2016-12-14 08:29:44 +08:00
|
|
|
checkwarn("ASSIGNWITHINCONDITION",
|
|
|
|
$line, $pos+1, $file, $l,
|
|
|
|
"assignment within conditional expression");
|
|
|
|
}
|
2020-03-30 16:52:48 +08:00
|
|
|
my $temp = $cond;
|
|
|
|
$temp =~ s/\(//g; # remove open parens
|
|
|
|
my $openc = length($cond) - length($temp);
|
|
|
|
|
|
|
|
$temp = $cond;
|
|
|
|
$temp =~ s/\)//g; # remove close parens
|
|
|
|
my $closec = length($cond) - length($temp);
|
|
|
|
my $even = $openc == $closec;
|
|
|
|
|
|
|
|
if($l =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
|
|
|
elsif($even && $postparen &&
|
|
|
|
($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
|
|
|
|
checkwarn("ONELINECONDITION",
|
|
|
|
$line, length($l)-length($postparen), $file, $l,
|
|
|
|
"conditional block on the same line");
|
|
|
|
}
|
2016-12-14 08:29:44 +08:00
|
|
|
}
|
2016-04-04 02:28:20 +08: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 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 {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
|
2015-03-17 20:05:01 +08:00
|
|
|
"return without space before paren");
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 20:41:49 +08:00
|
|
|
|
2018-05-12 05:40:58 +08:00
|
|
|
# check for "sizeof" without parenthesis
|
|
|
|
if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
|
|
|
|
if($1 =~ / *\#/) {
|
|
|
|
# this is a #if, treat it differently
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
|
|
|
|
"sizeof without parenthesis");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 =~ /\"/) {
|
2018-02-24 07:29:01 +08:00
|
|
|
# within a string
|
2015-03-17 20:41:49 +08:00
|
|
|
}
|
|
|
|
elsif($pref =~ /\'$/) {
|
|
|
|
# a single letter
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ign = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$ign) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
|
2015-03-17 20:41:49 +08:00
|
|
|
"comma without following space");
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:29:14 +08:00
|
|
|
|
2011-04-23 04:58:17 +08:00
|
|
|
# check for "} else"
|
2011-09-08 04:45:43 +08:00
|
|
|
if($l =~ /^(.*)\} *else/) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("BRACEELSE",
|
|
|
|
$line, length($1), $file, $l, "else after closing brace on same line");
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
2011-07-05 04:08:14 +08:00
|
|
|
# check for "){"
|
|
|
|
if($l =~ /^(.*)\)\{/) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("PARENBRACE",
|
|
|
|
$line, length($1)+1, $file, $l, "missing space after close paren");
|
2011-07-05 04:08:14 +08:00
|
|
|
}
|
2020-10-15 21:30:55 +08:00
|
|
|
# check for "^{" with an empty line before it
|
|
|
|
if(($l =~ /^\{/) && ($prevl =~ /^[ \t]*\z/)) {
|
|
|
|
checkwarn("EMPTYLINEBRACE",
|
|
|
|
$line, 0, $file, $l, "empty line before open brace");
|
|
|
|
}
|
2011-07-05 04:08:14 +08:00
|
|
|
|
2015-03-17 21:06:48 +08:00
|
|
|
# check for space before the semicolon last in a line
|
|
|
|
if($l =~ /^(.*[^ ].*) ;$/) {
|
2018-04-15 06:47:36 +08:00
|
|
|
checkwarn("SPACESEMICOLON",
|
2021-10-18 16:46:04 +08:00
|
|
|
$line, length($1), $file, $ol, "no space before semicolon");
|
2015-03-17 21:06:48 +08:00
|
|
|
}
|
|
|
|
|
2023-04-28 01:01:28 +08:00
|
|
|
# check for space before the colon in a switch label
|
|
|
|
if($l =~ /^( *(case .+|default)) :/) {
|
|
|
|
checkwarn("SPACESWITCHCOLON",
|
|
|
|
$line, length($1), $file, $ol, "no space before colon of switch label");
|
|
|
|
}
|
|
|
|
|
2023-05-18 12:54:18 +08:00
|
|
|
if($prevl !~ /\?\z/ && $l =~ /^ +([A-Za-z_][A-Za-z0-9_]*):$/ && $1 ne 'default') {
|
|
|
|
checkwarn("SPACEBEFORELABEL",
|
|
|
|
$line, length($1), $file, $ol, "no space before label");
|
|
|
|
}
|
|
|
|
|
2013-03-06 20:27:51 +08:00
|
|
|
# scan for use of banned functions
|
2016-06-06 09:07:03 +08:00
|
|
|
if($l =~ /^(.*\W)
|
2020-07-27 20:28:37 +08:00
|
|
|
(gmtime|localtime|
|
|
|
|
gets|
|
2018-04-15 06:47:36 +08:00
|
|
|
strtok|
|
2016-06-06 09:07:03 +08:00
|
|
|
v?sprintf|
|
|
|
|
(str|_mbs|_tcs|_wcs)n?cat|
|
2024-04-29 19:49:03 +08:00
|
|
|
LoadLibrary(Ex)?(A|W)?|
|
2024-05-04 07:31:00 +08:00
|
|
|
_?w?access)
|
2016-06-06 09:07:03 +08:00
|
|
|
\s*\(
|
|
|
|
/x) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("BANNEDFUNC",
|
2018-11-22 16:00:57 +08:00
|
|
|
$line, length($1), $file, $ol,
|
|
|
|
"use of $2 is banned");
|
|
|
|
}
|
2021-09-08 17:27:49 +08:00
|
|
|
if($warnings{"STRERROR"}) {
|
|
|
|
# scan for use of banned strerror. This is not a BANNEDFUNC to
|
|
|
|
# allow for individual enable/disable of this warning.
|
|
|
|
if($l =~ /^(.*\W)(strerror)\s*\(/x) {
|
|
|
|
if($1 !~ /^ *\#/) {
|
|
|
|
# skip preprocessor lines
|
|
|
|
checkwarn("STRERROR",
|
|
|
|
$line, length($1), $file, $ol,
|
|
|
|
"use of $2 is banned");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 18:50:07 +08:00
|
|
|
if($warnings{"STDERR"}) {
|
|
|
|
# scan for use of banned stderr. This is not a BANNEDFUNC to
|
|
|
|
# allow for individual enable/disable of this warning.
|
|
|
|
if($l =~ /^([^\"-]*\W)(stderr)[^\"_]/x) {
|
|
|
|
if($1 !~ /^ *\#/) {
|
|
|
|
# skip preprocessor lines
|
|
|
|
checkwarn("STDERR",
|
|
|
|
$line, length($1), $file, $ol,
|
|
|
|
"use of $2 is banned (use tool_stderr instead)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-22 16:00:57 +08:00
|
|
|
# scan for use of snprintf for curl-internals reasons
|
|
|
|
if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) {
|
|
|
|
checkwarn("SNPRINTF",
|
2016-04-19 14:30:13 +08:00
|
|
|
$line, length($1), $file, $ol,
|
2013-03-06 20:27:51 +08:00
|
|
|
"use of $2 is banned");
|
|
|
|
}
|
|
|
|
|
2015-06-02 14:28:10 +08:00
|
|
|
# scan for use of non-binary fopen without the macro
|
2016-04-03 17:29:14 +08:00
|
|
|
if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
|
2015-06-02 14:28:10 +08:00
|
|
|
my $mode = $2;
|
|
|
|
if($mode !~ /b/) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("FOPENMODE",
|
2016-04-19 14:30:13 +08:00
|
|
|
$line, length($1), $file, $ol,
|
2016-04-03 17:29:14 +08:00
|
|
|
"use of non-binary fopen without FOPEN_* macro: $mode");
|
2015-06-02 14:28:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 19:50:25 +08:00
|
|
|
# check for open brace first on line but not first column only alert
|
|
|
|
# if previous line ended with a close paren and it wasn't a cpp line
|
|
|
|
if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("BRACEPOS",
|
2016-04-19 14:30:13 +08:00
|
|
|
$line, length($1), $file, $ol, "badly placed open brace");
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
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
|
2018-04-26 03:53:27 +08:00
|
|
|
# brace, or an else statement, check that this line is indented $indent
|
|
|
|
# more steps, if not a cpp line
|
2021-10-16 19:50:25 +08:00
|
|
|
if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
|
2011-04-23 04:58:17 +08:00
|
|
|
my $first = length($1);
|
|
|
|
# this line has some character besides spaces
|
2021-10-16 19:50:25 +08:00
|
|
|
if($l =~ /^( *)[^ ]/) {
|
2011-04-23 04:58:17 +08:00
|
|
|
my $second = length($1);
|
|
|
|
my $expect = $first+$indent;
|
|
|
|
if($expect != $second) {
|
|
|
|
my $diff = $second - $first;
|
2016-04-19 14:30:13 +08:00
|
|
|
checkwarn("INDENTATION", $line, length($1), $file, $ol,
|
2018-04-26 03:53:27 +08:00
|
|
|
"not indented $indent steps (uses $diff)");
|
2011-04-23 04:58:17 +08:00
|
|
|
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
|
2023-04-29 00:07:33 +08:00
|
|
|
# if the previous line starts with if/while/for AND ends with a closed
|
|
|
|
# parenthesis and there's an equal number of open and closed
|
|
|
|
# parentheses, check that this line is indented $indent more steps, if
|
|
|
|
# not a cpp line
|
|
|
|
elsif(!$prevp && ($prevl =~ /^( *)(if|while|for)(\(.*\))\z/)) {
|
|
|
|
my $first = length($1);
|
|
|
|
my $op = $3;
|
|
|
|
my $cl = $3;
|
|
|
|
|
|
|
|
$op =~ s/[^(]//g;
|
|
|
|
$cl =~ s/[^)]//g;
|
|
|
|
|
|
|
|
if(length($op) == length($cl)) {
|
|
|
|
# this line has some character besides spaces
|
|
|
|
if($l =~ /^( *)[^ ]/) {
|
|
|
|
my $second = length($1);
|
|
|
|
my $expect = $first+$indent;
|
|
|
|
if($expect != $second) {
|
|
|
|
my $diff = $second - $first;
|
|
|
|
checkwarn("INDENTATION", $line, length($1), $file, $ol,
|
|
|
|
"not indented $indent steps (uses $diff)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:52:38 +08:00
|
|
|
# check for 'char * name'
|
2020-05-13 18:56:53 +08:00
|
|
|
if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
|
|
|
|
checkwarn("ASTERISKSPACE",
|
2016-11-23 14:52:38 +08:00
|
|
|
$line, length($1), $file, $ol,
|
2020-05-13 18:56:53 +08:00
|
|
|
"space after declarative asterisk");
|
2016-11-23 14:52:38 +08:00
|
|
|
}
|
2016-11-23 15:29:42 +08: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 15:48:42 +08:00
|
|
|
|
|
|
|
# check for 'void func() {', but avoid false positives by requiring
|
|
|
|
# both an open and closed parentheses before the open brace
|
2017-07-27 07:13:19 +08:00
|
|
|
if($l =~ /^((\w).*)\{\z/) {
|
2016-11-23 15:48:42 +08: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 17:57:17 +08: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 05:54:47 +08: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 15:25:31 +08:00
|
|
|
# check for semicolons without space next to it
|
|
|
|
if($nostr =~ /(.*)\;[a-z0-9]/i) {
|
|
|
|
checkwarn("SEMINOSPACE",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
2018-04-15 06:47:36 +08:00
|
|
|
"no space after semicolon");
|
2017-09-11 15:25:31 +08:00
|
|
|
}
|
|
|
|
|
2020-05-14 06:05:04 +08:00
|
|
|
# typedef struct ... {
|
|
|
|
if($nostr =~ /^(.*)typedef struct.*{/) {
|
|
|
|
checkwarn("TYPEDEFSTRUCT",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"typedef'ed struct");
|
|
|
|
}
|
|
|
|
|
2020-10-02 16:58:52 +08:00
|
|
|
if($nostr =~ /(.*)! +(\w|\()/) {
|
|
|
|
checkwarn("EXCLAMATIONSPACE",
|
|
|
|
$line, length($1)+1, $file, $ol,
|
|
|
|
"space after exclamation mark");
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:28:37 +08: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,
|
2021-10-18 16:46:04 +08:00
|
|
|
"multiple spaces");
|
2017-09-12 15:28:37 +08:00
|
|
|
}
|
2021-10-16 19:50:25 +08:00
|
|
|
preproc:
|
2023-11-22 00:54:49 +08:00
|
|
|
if($prep) {
|
|
|
|
# scan for use of banned symbols on a preprocessor line
|
|
|
|
if($l =~ /^(^|.*\W)
|
|
|
|
(WIN32)
|
|
|
|
(\W|$)
|
|
|
|
/x) {
|
|
|
|
checkwarn("BANNEDPREPROC",
|
|
|
|
$line, length($1), $file, $ol,
|
|
|
|
"use of $2 is banned from preprocessor lines" .
|
|
|
|
(($2 eq "WIN32") ? ", use _WIN32 instead" : ""));
|
|
|
|
}
|
|
|
|
}
|
2011-04-23 04:58:17 +08:00
|
|
|
$line++;
|
2021-10-16 19:50:25 +08:00
|
|
|
$prevp = $prep;
|
|
|
|
$prevl = $ol if(!$prep);
|
|
|
|
$prevpl = $ol if($prep);
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 06:13:40 +08:00
|
|
|
if(!scalar(@copyright)) {
|
2016-04-03 17:56:10 +08:00
|
|
|
checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
|
2011-04-23 04:58:17 +08:00
|
|
|
}
|
2018-12-04 06:13:40 +08:00
|
|
|
|
2024-04-10 15:36:10 +08:00
|
|
|
# COPYRIGHTYEAR is an extended warning so we must first see if it has been
|
2018-12-04 06:13:40 +08:00
|
|
|
# enabled in .checksrc
|
|
|
|
if(defined($warnings{"COPYRIGHTYEAR"})) {
|
|
|
|
# The check for updated copyrightyear is overly complicated in order to
|
|
|
|
# not punish current hacking for past sins. The copyright years are
|
|
|
|
# right now a bit behind, so enforcing copyright year checking on all
|
|
|
|
# files would cause hundreds of errors. Instead we only look at files
|
|
|
|
# which are tracked in the Git repo and edited in the workdir, or
|
|
|
|
# committed locally on the branch without being in upstream master.
|
|
|
|
#
|
|
|
|
# The simple and naive test is to simply check for the current year,
|
|
|
|
# but updating the year even without an edit is against project policy
|
|
|
|
# (and it would fail every file on January 1st).
|
|
|
|
#
|
|
|
|
# A rather more interesting, and correct, check would be to not test
|
|
|
|
# only locally committed files but inspect all files wrt the year of
|
|
|
|
# their last commit. Removing the `git rev-list origin/master..HEAD`
|
2022-01-15 14:56:27 +08:00
|
|
|
# condition below will enforce copyright year checks against the year
|
2018-12-04 06:13:40 +08:00
|
|
|
# the file was last committed (and thus edited to some degree).
|
|
|
|
my $commityear = undef;
|
|
|
|
@copyright = sort {$$b{year} cmp $$a{year}} @copyright;
|
|
|
|
|
2019-11-01 20:58:27 +08:00
|
|
|
# if the file is modified, assume commit year this year
|
2023-07-13 19:50:31 +08:00
|
|
|
if(`git status -s -- "$file"` =~ /^ [MARCU]/) {
|
2018-12-04 06:13:40 +08:00
|
|
|
$commityear = (localtime(time))[5] + 1900;
|
|
|
|
}
|
2019-11-01 20:58:27 +08:00
|
|
|
else {
|
|
|
|
# min-parents=1 to ignore wrong initial commit in truncated repos
|
2023-07-13 19:50:31 +08:00
|
|
|
my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- "$file"`;
|
2019-11-01 20:58:27 +08:00
|
|
|
if($grl) {
|
|
|
|
chomp $grl;
|
|
|
|
$commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
|
|
|
|
}
|
2018-12-04 06:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(defined($commityear) && scalar(@copyright) &&
|
|
|
|
$copyright[0]{year} != $commityear) {
|
|
|
|
checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
|
|
|
|
$file, $copyright[0]{code},
|
|
|
|
"Copyright year out of date, should be $commityear, " .
|
|
|
|
"is $copyright[0]{year}", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 14:30:13 +08:00
|
|
|
if($incomment) {
|
|
|
|
checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
|
2016-04-03 22:03:40 +08:00
|
|
|
checksrc_endoffile($file);
|
|
|
|
|
2023-06-21 16:50:27 +08:00
|
|
|
close($R);
|
2011-04-20 21:17:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-21 04:44:08 +08:00
|
|
|
|
2016-04-03 17:56:10 +08:00
|
|
|
if($errors || $warnings || $verbose) {
|
2011-04-23 04:58:17 +08:00
|
|
|
printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
|
2018-02-24 07:29:01 +08:00
|
|
|
if($suppressed) {
|
2016-04-03 17:56:10 +08:00
|
|
|
printf "checksrc: %d errors and %d warnings suppressed\n",
|
|
|
|
$serrors,
|
|
|
|
$swarnings;
|
|
|
|
}
|
2011-04-21 04:44:08 +08:00
|
|
|
exit 5; # return failure
|
|
|
|
}
|