r17097@catbus: nickm | 2007-12-11 18:13:48 -0500
[tor.git] / contrib / checkSpace.pl
blob686427f73d458da570bf3a00f71e0fc2e4ce0471
1 #!/usr/bin/perl -w
3 if ($ARGV[0] =~ /^-/) {
4 $lang = shift @ARGV;
5 $C = ($lang eq '-C');
6 # $TXT = ($lang eq '-txt');
9 for $fn (@ARGV) {
10 open(F, "$fn");
11 $lastnil = 0;
12 $incomment = 0;
13 while (<F>) {
14 ## Warn about windows-style newlines.
15 if (/\r/) {
16 print " CR:$fn:$.\n";
18 ## Warn about tabs.
19 if (/\t/) {
20 print " TAB:$fn:$.\n";
22 ## Warn about trailing whitespace.
23 if (/ +$/) {
24 print "Space\@EOL:$fn:$.\n";
26 ## Warn about control keywords without following space.
27 if ($C && /\s(?:if|while|for|switch)\(/) {
28 print " KW(:$fn:$.\n";
30 ## Warn about multiple empty lines.
31 if ($lastnil && /^$/) {
32 print " DoubleNL:$fn:$.\n";
33 } elsif (/^$/) {
34 $lastnil = 1;
35 } else {
36 $lastnil = 0;
38 ## Terminals are still 80 columns wide in my world. I refuse to
39 ## accept double-line lines. Except, of course, svn Id tags
40 ## can make us go long.
41 if (/^.{80}/ && !/\$Id: /) {
42 print " Wide:$fn:$.\n";
44 ### Juju to skip over comments and strings, since the tests
45 ### we're about to do are okay there.
46 if ($C) {
47 if ($incomment) {
48 if (m!\*/!) {
49 s!.*?\*/!!;
50 $incomment = 0;
51 } else {
52 next;
55 if (m!/\*.*?\*/!) {
56 s!\s*/\*.*?\*/!!;
57 } elsif (m!/\*!) {
58 s!\s*/\*!!;
59 $incomment = 1;
60 next;
62 s!"(?:[^\"]+|\\.)*"!"X"!g;
63 next if /^\#/;
64 ## Warn about C++-style comments.
65 if (m!//!) {
66 # print " //:$fn:$.\n";
67 s!//.*!!;
69 ## Warn about braces preceded by non-space.
70 if (/([^\s])\{/) {
71 print " $1\{:$fn:$.\n";
73 ## Warn about multiple internal spaces.
74 #if (/[^\s,:]\s{2,}[^\s\\=]/) {
75 # print " X X:$fn:$.\n";
77 ## Warn about { with stuff after.
78 #s/\s+$//;
79 #if (/\{[^\}\\]+$/) {
80 # print " {X:$fn:$.\n";
82 ## Warn about function calls with space before parens.
83 if (/(\w+)\s\(([A-Z]*)/) {
84 if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
85 $1 ne "switch" and $1 ne "return" and $1 ne "int" and
86 $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
87 $1 ne "void" and $1 ne "__attribute__") {
88 print " fn ():$fn:$.\n";
91 ## Warn about functions not declared at start of line.
92 if ($in_func_head ||
93 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
94 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
95 ! /= *\{$/ && ! /;$/)) {
96 if (/.\{$/){
97 print "fn() {:$fn:$.\n";
98 $in_func_head = 0;
99 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
100 $in_func_head = -1; # started with tp fn
101 } elsif (/;$/) {
102 $in_func_head = 0;
103 } elsif (/\{/) {
104 if ($in_func_head == -1) {
105 print "tp fn():$fn:$.\n";
107 $in_func_head = 0;
112 if (! $lastnil) {
113 print " EOL\@EOF:$fn:$.\n";
115 close(F);