forward-port the 0.2.2.38 changelog
[tor.git] / contrib / checkSpace.pl
blob682dbced00436c84795402afad7f54ba5cd6061d
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 $lastline = "";
13 $incomment = 0;
14 while (<F>) {
15 ## Warn about windows-style newlines.
16 if (/\r/) {
17 print " CR:$fn:$.\n";
19 ## Warn about tabs.
20 if (/\t/) {
21 print " TAB:$fn:$.\n";
23 ## Warn about markers that don't have a space in front of them
24 if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
25 print "nosplabel:$fn:$.\n";
27 ## Warn about trailing whitespace.
28 if (/ +$/) {
29 print "Space\@EOL:$fn:$.\n";
31 ## Warn about control keywords without following space.
32 if ($C && /\s(?:if|while|for|switch)\(/) {
33 print " KW(:$fn:$.\n";
35 ## Warn about #else #if instead of #elif.
36 if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
37 print " #else#if:$fn:$.\n";
39 ## Warn about some K&R violations
40 if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
41 $lastline !~ /\{$/) {
42 print "non-K&R {:$fn:$.\n";
44 if (/^\s*else/ and $lastline =~ /\}$/) {
45 print " }\\nelse:$fn:$.\n";
47 $lastline = $_;
48 ## Warn about unnecessary empty lines.
49 if ($lastnil && /^\s*}\n/) {
50 print " UnnecNL:$fn:$.\n";
52 ## Warn about multiple empty lines.
53 if ($lastnil && /^$/) {
54 print " DoubleNL:$fn:$.\n";
55 } elsif (/^$/) {
56 $lastnil = 1;
57 } else {
58 $lastnil = 0;
60 ## Terminals are still 80 columns wide in my world. I refuse to
61 ## accept double-line lines.
62 if (/^.{80}/) {
63 print " Wide:$fn:$.\n";
65 ### Juju to skip over comments and strings, since the tests
66 ### we're about to do are okay there.
67 if ($C) {
68 if ($incomment) {
69 if (m!\*/!) {
70 s!.*?\*/!!;
71 $incomment = 0;
72 } else {
73 next;
76 if (m!/\*.*?\*/!) {
77 s!\s*/\*.*?\*/!!;
78 } elsif (m!/\*!) {
79 s!\s*/\*!!;
80 $incomment = 1;
81 next;
83 s!"(?:[^\"]+|\\.)*"!"X"!g;
84 next if /^\#/;
85 ## Warn about C++-style comments.
86 if (m!//!) {
87 # print " //:$fn:$.\n";
88 s!//.*!!;
90 ## Warn about unquoted braces preceded by non-space.
91 if (/([^\s'])\{/) {
92 print " $1\{:$fn:$.\n";
94 ## Warn about multiple internal spaces.
95 #if (/[^\s,:]\s{2,}[^\s\\=]/) {
96 # print " X X:$fn:$.\n";
98 ## Warn about { with stuff after.
99 #s/\s+$//;
100 #if (/\{[^\}\\]+$/) {
101 # print " {X:$fn:$.\n";
103 ## Warn about function calls with space before parens.
104 if (/(\w+)\s\(([A-Z]*)/) {
105 if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
106 $1 ne "switch" and $1 ne "return" and $1 ne "int" and
107 $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
108 $1 ne "void" and $1 ne "__attribute__" and $1 ne "op") {
109 print " fn ():$fn:$.\n";
112 ## Warn about functions not declared at start of line.
113 if ($in_func_head ||
114 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
115 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
116 ! /= *\{$/ && ! /;$/)) {
117 if (/.\{$/){
118 print "fn() {:$fn:$.\n";
119 $in_func_head = 0;
120 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
121 $in_func_head = -1; # started with tp fn
122 } elsif (/;$/) {
123 $in_func_head = 0;
124 } elsif (/\{/) {
125 if ($in_func_head == -1) {
126 print "tp fn():$fn:$.\n";
128 $in_func_head = 0;
133 if (! $lastnil) {
134 print " EOL\@EOF:$fn:$.\n";
136 close(F);