2004-11-09 21:04:00 +01:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2016-12-15 11:47:48 +01:00
|
|
|
my $C = 0;
|
|
|
|
|
2005-06-11 20:52:37 +02:00
|
|
|
if ($ARGV[0] =~ /^-/) {
|
2016-12-15 11:47:48 +01:00
|
|
|
my $lang = shift @ARGV;
|
2005-06-11 20:52:37 +02:00
|
|
|
$C = ($lang eq '-C');
|
|
|
|
}
|
|
|
|
|
2016-12-15 11:47:48 +01:00
|
|
|
for my $fn (@ARGV) {
|
2004-11-09 21:04:00 +01:00
|
|
|
open(F, "$fn");
|
2016-12-15 11:47:48 +01:00
|
|
|
my $lastnil = 0;
|
|
|
|
my $lastline = "";
|
|
|
|
my $incomment = 0;
|
|
|
|
my $in_func_head = 0;
|
2004-11-09 21:04:00 +01:00
|
|
|
while (<F>) {
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about windows-style newlines.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (We insist on lines that end with a single LF character, not
|
|
|
|
# CR LF.)
|
2004-11-10 02:20:17 +01:00
|
|
|
if (/\r/) {
|
|
|
|
print " CR:$fn:$.\n";
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about tabs.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (We only use spaces)
|
2004-11-10 02:20:17 +01:00
|
|
|
if (/\t/) {
|
|
|
|
print " TAB:$fn:$.\n";
|
|
|
|
}
|
2014-08-18 16:03:14 +02:00
|
|
|
## Warn about labels that don't have a space in front of them
|
2016-12-15 11:40:38 +01:00
|
|
|
# (We indent every label at least one space)
|
2010-08-16 00:28:39 +02:00
|
|
|
if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
|
|
|
|
print "nosplabel:$fn:$.\n";
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about trailing whitespace.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (We don't allow whitespace at the end of the line; make your
|
|
|
|
# editor highlight it for you so you can stop adding it in.)
|
2004-11-10 02:20:17 +01:00
|
|
|
if (/ +$/) {
|
|
|
|
print "Space\@EOL:$fn:$.\n";
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about control keywords without following space.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (We put a space after every 'if', 'while', 'for', 'switch', etc)
|
2006-07-26 00:33:57 +02:00
|
|
|
if ($C && /\s(?:if|while|for|switch)\(/) {
|
|
|
|
print " KW(:$fn:$.\n";
|
|
|
|
}
|
2012-06-23 21:51:48 +02:00
|
|
|
## Warn about #else #if instead of #elif.
|
2014-08-18 16:03:14 +02:00
|
|
|
# (We only allow #elif)
|
2010-02-20 22:44:15 +01:00
|
|
|
if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
|
2008-01-06 04:16:08 +01:00
|
|
|
print " #else#if:$fn:$.\n";
|
2010-02-20 22:44:15 +01:00
|
|
|
}
|
2012-06-23 21:51:48 +02:00
|
|
|
## Warn about some K&R violations
|
2014-08-18 16:03:14 +02:00
|
|
|
# (We use K&R-style C, where open braces go on the same line as
|
|
|
|
# the statement that introduces them. In other words:
|
|
|
|
# if (a) {
|
|
|
|
# stuff;
|
|
|
|
# } else {
|
|
|
|
# other stuff;
|
|
|
|
# }
|
2012-06-23 21:51:48 +02:00
|
|
|
if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
|
2016-12-15 11:40:38 +01:00
|
|
|
$lastline !~ /\{$/) {
|
2012-06-23 21:51:48 +02:00
|
|
|
print "non-K&R {:$fn:$.\n";
|
2016-12-15 11:40:38 +01:00
|
|
|
}
|
2012-06-23 21:51:48 +02:00
|
|
|
if (/^\s*else/ and $lastline =~ /\}$/) {
|
2016-12-15 11:40:38 +01:00
|
|
|
print " }\\nelse:$fn:$.\n";
|
|
|
|
}
|
2010-02-20 22:44:15 +01:00
|
|
|
$lastline = $_;
|
|
|
|
## Warn about unnecessary empty lines.
|
2014-08-18 16:03:14 +02:00
|
|
|
# (Don't put an empty line before a line that contains nothing
|
|
|
|
# but a closing brace.)
|
2010-02-20 22:44:15 +01:00
|
|
|
if ($lastnil && /^\s*}\n/) {
|
|
|
|
print " UnnecNL:$fn:$.\n";
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about multiple empty lines.
|
2014-08-18 16:03:14 +02:00
|
|
|
# (At most one blank line in a row.)
|
2004-11-10 02:20:17 +01:00
|
|
|
if ($lastnil && /^$/) {
|
|
|
|
print " DoubleNL:$fn:$.\n";
|
|
|
|
} elsif (/^$/) {
|
|
|
|
$lastnil = 1;
|
|
|
|
} else {
|
|
|
|
$lastnil = 0;
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Terminals are still 80 columns wide in my world. I refuse to
|
2009-05-05 15:55:32 +02:00
|
|
|
## accept double-line lines.
|
2014-08-18 16:03:14 +02:00
|
|
|
# (Don't make lines wider than 80 characters, including newline.)
|
2009-05-05 15:55:32 +02:00
|
|
|
if (/^.{80}/) {
|
2006-07-26 00:33:57 +02:00
|
|
|
print " Wide:$fn:$.\n";
|
|
|
|
}
|
|
|
|
### Juju to skip over comments and strings, since the tests
|
|
|
|
### we're about to do are okay there.
|
|
|
|
if ($C) {
|
2005-06-11 20:52:37 +02:00
|
|
|
if ($incomment) {
|
|
|
|
if (m!\*/!) {
|
|
|
|
s!.*?\*/!!;
|
|
|
|
$incomment = 0;
|
|
|
|
} else {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m!/\*.*?\*/!) {
|
|
|
|
s!\s*/\*.*?\*/!!;
|
|
|
|
} elsif (m!/\*!) {
|
|
|
|
s!\s*/\*!!;
|
|
|
|
$incomment = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
s!"(?:[^\"]+|\\.)*"!"X"!g;
|
|
|
|
next if /^\#/;
|
|
|
|
## Warn about C++-style comments.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (Use C style comments only.)
|
2005-06-11 20:52:37 +02:00
|
|
|
if (m!//!) {
|
|
|
|
# print " //:$fn:$.\n";
|
|
|
|
s!//.*!!;
|
|
|
|
}
|
2008-09-25 22:21:35 +02:00
|
|
|
## Warn about unquoted braces preceded by non-space.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (No character except a space should come before a {)
|
2008-09-25 22:21:35 +02:00
|
|
|
if (/([^\s'])\{/) {
|
2005-06-11 20:52:37 +02:00
|
|
|
print " $1\{:$fn:$.\n";
|
|
|
|
}
|
|
|
|
## Warn about multiple internal spaces.
|
|
|
|
#if (/[^\s,:]\s{2,}[^\s\\=]/) {
|
|
|
|
# print " X X:$fn:$.\n";
|
|
|
|
#}
|
|
|
|
## Warn about { with stuff after.
|
|
|
|
#s/\s+$//;
|
|
|
|
#if (/\{[^\}\\]+$/) {
|
|
|
|
# print " {X:$fn:$.\n";
|
|
|
|
#}
|
|
|
|
## Warn about function calls with space before parens.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (Don't put a space between the name of a function and its
|
|
|
|
# arguments.)
|
2006-10-09 17:46:12 +02:00
|
|
|
if (/(\w+)\s\(([A-Z]*)/) {
|
2005-06-11 20:52:37 +02:00
|
|
|
if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
|
|
|
|
$1 ne "switch" and $1 ne "return" and $1 ne "int" and
|
2006-10-09 17:46:12 +02:00
|
|
|
$1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
|
2013-11-05 07:40:24 +01:00
|
|
|
$1 ne "void" and $1 ne "__attribute__" and $1 ne "op" and
|
2015-08-20 20:42:19 +02:00
|
|
|
$1 ne "size_t" and $1 ne "double" and
|
|
|
|
$1 ne "workqueue_reply_t") {
|
2005-06-11 20:52:37 +02:00
|
|
|
print " fn ():$fn:$.\n";
|
|
|
|
}
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about functions not declared at start of line.
|
2016-12-15 11:40:38 +01:00
|
|
|
# (When you're declaring functions, put "static" and "const"
|
|
|
|
# and the return type on one line, and the function name at
|
|
|
|
# the start of a new line.)
|
2006-07-26 00:33:57 +02:00
|
|
|
if ($in_func_head ||
|
|
|
|
($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
|
2006-08-10 11:02:12 +02:00
|
|
|
! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
|
2006-07-26 00:33:57 +02:00
|
|
|
! /= *\{$/ && ! /;$/)) {
|
|
|
|
if (/.\{$/){
|
|
|
|
print "fn() {:$fn:$.\n";
|
|
|
|
$in_func_head = 0;
|
|
|
|
} elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
|
|
|
|
$in_func_head = -1; # started with tp fn
|
|
|
|
} elsif (/;$/) {
|
|
|
|
$in_func_head = 0;
|
|
|
|
} elsif (/\{/) {
|
|
|
|
if ($in_func_head == -1) {
|
|
|
|
print "tp fn():$fn:$.\n";
|
|
|
|
}
|
|
|
|
$in_func_head = 0;
|
|
|
|
}
|
|
|
|
}
|
checkSpace.pl now forbids more identifiers.
The functions it warns about are:
assert, memcmp, strcat, strcpy, sprintf, malloc, free, realloc,
strdup, strndup, calloc.
Also, fix a few lingering instances of these in the code. Use other
conventions to indicate _intended_ use of assert and
malloc/realloc/etc.
2016-09-06 18:35:37 +02:00
|
|
|
|
2016-12-15 11:40:38 +01:00
|
|
|
## Check for forbidden functions except when they are
|
|
|
|
# explicitly permitted
|
|
|
|
if (/\bassert\(/ && not /assert OK/) {
|
|
|
|
print "assert :$fn:$. (use tor_assert)\n";
|
|
|
|
}
|
|
|
|
if (/\bmemcmp\(/ && not /memcmp OK/) {
|
|
|
|
print "memcmp :$fn:$. (use {tor,fast}_mem{eq,neq,cmp}\n";
|
|
|
|
}
|
|
|
|
# always forbidden.
|
|
|
|
if (not /\ OVERRIDE\ /) {
|
|
|
|
if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
|
|
|
|
print "$& :$fn:$.\n";
|
|
|
|
}
|
|
|
|
if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
|
|
|
|
/\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
|
|
|
|
print "$& :$fn:$. (use tor_malloc, tor_free, etc)\n";
|
|
|
|
}
|
|
|
|
}
|
2005-06-11 20:52:37 +02:00
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
}
|
2014-08-18 16:03:14 +02:00
|
|
|
## Warn if the file doesn't end with a blank line.
|
|
|
|
# (End each file with a single blank line.)
|
2005-06-09 21:03:31 +02:00
|
|
|
if (! $lastnil) {
|
2005-06-09 18:46:51 +02:00
|
|
|
print " EOL\@EOF:$fn:$.\n";
|
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
close(F);
|
|
|
|
}
|