2004-11-09 21:04:00 +01:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2005-06-11 20:52:37 +02:00
|
|
|
if ($ARGV[0] =~ /^-/) {
|
|
|
|
$lang = shift @ARGV;
|
|
|
|
$C = ($lang eq '-C');
|
|
|
|
# $TXT = ($lang eq '-txt');
|
|
|
|
}
|
|
|
|
|
2004-11-09 21:04:00 +01:00
|
|
|
for $fn (@ARGV) {
|
|
|
|
open(F, "$fn");
|
|
|
|
$lastnil = 0;
|
2008-01-06 04:16:08 +01:00
|
|
|
$lastline = "";
|
2004-11-23 00:28:54 +01:00
|
|
|
$incomment = 0;
|
2004-11-09 21:04:00 +01:00
|
|
|
while (<F>) {
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about windows-style newlines.
|
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.
|
2004-11-10 02:20:17 +01:00
|
|
|
if (/\t/) {
|
|
|
|
print " TAB:$fn:$.\n";
|
|
|
|
}
|
2010-08-16 00:28:39 +02:00
|
|
|
## Warn about markers that don't have a space in front of them
|
|
|
|
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.
|
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.
|
|
|
|
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.
|
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
|
|
|
|
if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
|
|
|
|
$lastline !~ /\{$/) {
|
|
|
|
print "non-K&R {:$fn:$.\n";
|
|
|
|
}
|
|
|
|
if (/^\s*else/ and $lastline =~ /\}$/) {
|
|
|
|
print " }\\nelse:$fn:$.\n";
|
|
|
|
}
|
2010-02-20 22:44:15 +01:00
|
|
|
$lastline = $_;
|
|
|
|
## Warn about unnecessary empty lines.
|
|
|
|
if ($lastnil && /^\s*}\n/) {
|
|
|
|
print " UnnecNL:$fn:$.\n";
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
## Warn about multiple empty lines.
|
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.
|
|
|
|
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.
|
|
|
|
if (m!//!) {
|
|
|
|
# print " //:$fn:$.\n";
|
|
|
|
s!//.*!!;
|
|
|
|
}
|
2008-09-25 22:21:35 +02:00
|
|
|
## Warn about unquoted braces preceded by non-space.
|
|
|
|
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.
|
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
|
2011-11-24 09:24:59 +01:00
|
|
|
$1 ne "void" and $1 ne "__attribute__" and $1 ne "op") {
|
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.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-06-11 20:52:37 +02:00
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
}
|
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);
|
|
|
|
}
|
2006-07-26 00:33:57 +02:00
|
|
|
|