Merge branch 'bug26924_029' into bug26924_032
[tor.git] / scripts / maint / checkSpace.pl
bloba12370f21ff3a60595380bf8f74a8dbb5db27442
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 my $found = 0;
7 sub msg {
8 $found = 1;
9 print "$_[0]";
12 my $C = 0;
14 if ($ARGV[0] =~ /^-/) {
15 my $lang = shift @ARGV;
16 $C = ($lang eq '-C');
19 for my $fn (@ARGV) {
20 open(F, "$fn");
21 my $lastnil = 0;
22 my $lastline = "";
23 my $incomment = 0;
24 my $in_func_head = 0;
25 while (<F>) {
26 ## Warn about windows-style newlines.
27 # (We insist on lines that end with a single LF character, not
28 # CR LF.)
29 if (/\r/) {
30 msg " CR:$fn:$.\n";
32 ## Warn about tabs.
33 # (We only use spaces)
34 if (/\t/) {
35 msg " TAB:$fn:$.\n";
37 ## Warn about labels that don't have a space in front of them
38 # (We indent every label at least one space)
39 if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
40 msg "nosplabel:$fn:$.\n";
42 ## Warn about trailing whitespace.
43 # (We don't allow whitespace at the end of the line; make your
44 # editor highlight it for you so you can stop adding it in.)
45 if (/ +$/) {
46 msg "Space\@EOL:$fn:$.\n";
48 ## Warn about control keywords without following space.
49 # (We put a space after every 'if', 'while', 'for', 'switch', etc)
50 if ($C && /\s(?:if|while|for|switch)\(/) {
51 msg " KW(:$fn:$.\n";
53 ## Warn about #else #if instead of #elif.
54 # (We only allow #elif)
55 if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
56 msg " #else#if:$fn:$.\n";
58 ## Warn about some K&R violations
59 # (We use K&R-style C, where open braces go on the same line as
60 # the statement that introduces them. In other words:
61 # if (a) {
62 # stuff;
63 # } else {
64 # other stuff;
65 # }
66 if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
67 $lastline !~ /\{$/) {
68 msg "non-K&R {:$fn:$.\n";
70 if (/^\s*else/ and $lastline =~ /\}$/) {
71 msg " }\\nelse:$fn:$.\n";
73 $lastline = $_;
74 ## Warn about unnecessary empty lines.
75 # (Don't put an empty line before a line that contains nothing
76 # but a closing brace.)
77 if ($lastnil && /^\s*}\n/) {
78 msg " UnnecNL:$fn:$.\n";
80 ## Warn about multiple empty lines.
81 # (At most one blank line in a row.)
82 if ($lastnil && /^$/) {
83 msg " DoubleNL:$fn:$.\n";
84 } elsif (/^$/) {
85 $lastnil = 1;
86 } else {
87 $lastnil = 0;
89 ## Terminals are still 80 columns wide in my world. I refuse to
90 ## accept double-line lines.
91 # (Don't make lines wider than 80 characters, including newline.)
92 if (/^.{80}/) {
93 msg " Wide:$fn:$.\n";
95 ### Juju to skip over comments and strings, since the tests
96 ### we're about to do are okay there.
97 if ($C) {
98 if ($incomment) {
99 if (m!\*/!) {
100 s!.*?\*/!!;
101 $incomment = 0;
102 } else {
103 next;
106 if (m!/\*.*?\*/!) {
107 s!\s*/\*.*?\*/!!;
108 } elsif (m!/\*!) {
109 s!\s*/\*!!;
110 $incomment = 1;
111 next;
113 s!"(?:[^\"]+|\\.)*"!"X"!g;
114 next if /^\#/;
115 ## Warn about C++-style comments.
116 # (Use C style comments only.)
117 if (m!//!) {
118 # msg " //:$fn:$.\n";
119 s!//.*!!;
121 ## Warn about unquoted braces preceded by non-space.
122 # (No character except a space should come before a {)
123 if (/([^\s'])\{/) {
124 msg " $1\{:$fn:$.\n";
126 ## Warn about double semi-colons at the end of a line.
127 if (/;;$/) {
128 msg " double semi-colons at the end of $. in $fn\n"
130 ## Warn about multiple internal spaces.
131 #if (/[^\s,:]\s{2,}[^\s\\=]/) {
132 # msg " X X:$fn:$.\n";
134 ## Warn about { with stuff after.
135 #s/\s+$//;
136 #if (/\{[^\}\\]+$/) {
137 # msg " {X:$fn:$.\n";
139 ## Warn about function calls with space before parens.
140 # (Don't put a space between the name of a function and its
141 # arguments.)
142 if (/(\w+)\s\(([A-Z]*)/) {
143 if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
144 $1 ne "switch" and $1 ne "return" and $1 ne "int" and
145 $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
146 $1 ne "void" and $1 ne "__attribute__" and $1 ne "op" and
147 $1 ne "size_t" and $1 ne "double" and $1 ne "uint64_t" and
148 $1 ne "workqueue_reply_t") {
149 msg " fn ():$fn:$.\n";
152 ## Warn about functions not declared at start of line.
153 # (When you're declaring functions, put "static" and "const"
154 # and the return type on one line, and the function name at
155 # the start of a new line.)
156 if ($in_func_head ||
157 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
158 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
159 ! /= *\{$/ && ! /;$/)) {
160 if (/.\{$/){
161 msg "fn() {:$fn:$.\n";
162 $in_func_head = 0;
163 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
164 $in_func_head = -1; # started with tp fn
165 } elsif (/;$/) {
166 $in_func_head = 0;
167 } elsif (/\{/) {
168 if ($in_func_head == -1) {
169 msg "tp fn():$fn:$.\n";
171 $in_func_head = 0;
175 ## Check for forbidden functions except when they are
176 # explicitly permitted
177 if (/\bassert\(/ && not /assert OK/) {
178 msg "assert :$fn:$. (use tor_assert)\n";
180 if (/\bmemcmp\(/ && not /memcmp OK/) {
181 msg "memcmp :$fn:$. (use {tor,fast}_mem{eq,neq,cmp}\n";
183 # always forbidden.
184 if (not /\ OVERRIDE\ /) {
185 if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
186 msg "$& :$fn:$.\n";
188 if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
189 /\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
190 msg "$& :$fn:$. (use tor_malloc, tor_free, etc)\n";
195 close(F);
198 exit $found;