change Bytes to B to fix ticket 1195.
[tor.git] / contrib / checkSpace.pl
blobb694abff6437a43ff42da6f197c144d1af840724
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 trailing whitespace.
24 if (/ +$/) {
25 print "Space\@EOL:$fn:$.\n";
27 ## Warn about control keywords without following space.
28 if ($C && /\s(?:if|while|for|switch)\(/) {
29 print " KW(:$fn:$.\n";
31 ## Warn about #else #if instead of #elif.
32 if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
33 print " #else#if:$fn:$.\n";
35 $lastline = $_;
36 ## Warn about unnecessary empty lines.
37 if ($lastnil && /^\s*}\n/) {
38 print " UnnecNL:$fn:$.\n";
40 ## Warn about multiple empty lines.
41 if ($lastnil && /^$/) {
42 print " DoubleNL:$fn:$.\n";
43 } elsif (/^$/) {
44 $lastnil = 1;
45 } else {
46 $lastnil = 0;
48 ## Terminals are still 80 columns wide in my world. I refuse to
49 ## accept double-line lines.
50 if (/^.{80}/) {
51 print " Wide:$fn:$.\n";
53 ### Juju to skip over comments and strings, since the tests
54 ### we're about to do are okay there.
55 if ($C) {
56 if ($incomment) {
57 if (m!\*/!) {
58 s!.*?\*/!!;
59 $incomment = 0;
60 } else {
61 next;
64 if (m!/\*.*?\*/!) {
65 s!\s*/\*.*?\*/!!;
66 } elsif (m!/\*!) {
67 s!\s*/\*!!;
68 $incomment = 1;
69 next;
71 s!"(?:[^\"]+|\\.)*"!"X"!g;
72 next if /^\#/;
73 ## Warn about C++-style comments.
74 if (m!//!) {
75 # print " //:$fn:$.\n";
76 s!//.*!!;
78 ## Warn about unquoted braces preceded by non-space.
79 if (/([^\s'])\{/) {
80 print " $1\{:$fn:$.\n";
82 ## Warn about multiple internal spaces.
83 #if (/[^\s,:]\s{2,}[^\s\\=]/) {
84 # print " X X:$fn:$.\n";
86 ## Warn about { with stuff after.
87 #s/\s+$//;
88 #if (/\{[^\}\\]+$/) {
89 # print " {X:$fn:$.\n";
91 ## Warn about function calls with space before parens.
92 if (/(\w+)\s\(([A-Z]*)/) {
93 if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
94 $1 ne "switch" and $1 ne "return" and $1 ne "int" and
95 $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
96 $1 ne "void" and $1 ne "__attribute__") {
97 print " fn ():$fn:$.\n";
100 ## Warn about functions not declared at start of line.
101 if ($in_func_head ||
102 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
103 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
104 ! /= *\{$/ && ! /;$/)) {
105 if (/.\{$/){
106 print "fn() {:$fn:$.\n";
107 $in_func_head = 0;
108 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
109 $in_func_head = -1; # started with tp fn
110 } elsif (/;$/) {
111 $in_func_head = 0;
112 } elsif (/\{/) {
113 if ($in_func_head == -1) {
114 print "tp fn():$fn:$.\n";
116 $in_func_head = 0;
121 if (! $lastnil) {
122 print " EOL\@EOF:$fn:$.\n";
124 close(F);