123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- use strict;
- use warnings;
- my $found = 0;
- sub msg {
- $found = 1;
- print "$_[0]";
- }
- my $C = 0;
- if ($ARGV[0] =~ /^-/) {
- my $lang = shift @ARGV;
- $C = ($lang eq '-C');
- }
- our %basenames = ();
- for my $fn (@ARGV) {
- open(F, "$fn");
- my $lastnil = 0;
- my $lastline = "";
- my $incomment = 0;
- my $in_func_head = 0;
- my $basename = $fn;
- $basename =~ s
- if ($basenames{$basename}) {
- msg "Duplicate fnames: $fn and $basenames{$basename}.\n";
- } else {
- $basenames{$basename} = $fn;
- }
- while (<F>) {
-
-
-
- if (/\r/) {
- msg " CR:$fn:$.\n";
- }
-
-
- if (/\t/) {
- msg " TAB:$fn:$.\n";
- }
-
-
- if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
- msg "nosplabel:$fn:$.\n";
- }
-
-
-
- if (/ +$/) {
- msg "Space\@EOL:$fn:$.\n";
- }
-
-
- if ($C && /\s(?:if|while|for|switch)\(/) {
- msg " KW(:$fn:$.\n";
- }
-
-
- if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
- msg " #else#if:$fn:$.\n";
- }
-
-
-
-
-
-
-
-
- if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
- $lastline !~ /\{$/) {
- msg "non-K&R {:$fn:$.\n";
- }
- if (/^\s*else/ and $lastline =~ /\}$/) {
- msg " }\\nelse:$fn:$.\n";
- }
- $lastline = $_;
-
-
-
- if ($lastnil && /^\s*}\n/) {
- msg " UnnecNL:$fn:$.\n";
- }
-
-
- if ($lastnil && /^$/) {
- msg " DoubleNL:$fn:$.\n";
- } elsif (/^$/) {
- $lastnil = 1;
- } else {
- $lastnil = 0;
- }
-
-
-
- if (/^.{80}/) {
- msg " Wide:$fn:$.\n";
- }
-
-
- if ($C) {
- 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 /^\
-
-
- if (m!//!) {
-
- s!//.*!!;
- }
-
-
- if (/([^\s'])\{/) {
- msg " $1\{:$fn:$.\n";
- }
-
- if (/;;$/) {
- msg " double semi-colons at the end of $. in $fn\n"
- }
-
-
-
-
-
-
-
-
-
-
-
-
- if (/(\w+)\s\(([A-Z]*)/) {
- 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
- $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
- $1 ne "void" and $1 ne "__attribute__" and $1 ne "op" and
- $1 ne "size_t" and $1 ne "double" and $1 ne "uint64_t" and
- $1 ne "workqueue_reply_t") {
- msg " fn ():$fn:$.\n";
- }
- }
-
-
-
-
- if ($in_func_head ||
- ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
- ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
- ! /= *\{$/ && ! /;$/)) {
- if (/.\{$/){
- msg "fn() {:$fn:$.\n";
- $in_func_head = 0;
- } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
- $in_func_head = -1;
- } elsif (/;$/) {
- $in_func_head = 0;
- } elsif (/\{/) {
- if ($in_func_head == -1) {
- msg "tp fn():$fn:$.\n";
- }
- $in_func_head = 0;
- }
- }
-
-
- if (/\bassert\(/ && not /assert OK/) {
- msg "assert :$fn:$. (use tor_assert)\n";
- }
- if (/\bmemcmp\(/ && not /memcmp OK/) {
- msg "memcmp :$fn:$. (use {tor,fast}_mem{eq,neq,cmp}\n";
- }
-
- if (not /\ OVERRIDE\ /) {
- if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
- msg "$& :$fn:$.\n";
- }
- if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
- /\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
- msg "$& :$fn:$. (use tor_malloc, tor_free, etc)\n";
- }
- }
- }
- }
- close(F);
- }
- exit $found;
|