12 $v =~ /^\s*([^:]+):(.*)$/;
13 chomp(my $errtype = $1);
15 my $padding = ' ' x
($COLON_POS - length $errtype);
16 print "$padding$errtype:$rest\n";
21 if ($ARGV[0] =~ /^-/) {
22 my $lang = shift @ARGV;
26 # hashmap of things where we allow spaces between them and (.
27 our %allow_space_after= map {$_, 1} qw{
28 if while for switch
return int unsigned
elsif WINAPI
29 void __attribute__ op size_t double uint64_t
31 workqueue_reply_t hs_desc_decode_status_t
33 SMARTLIST_FOREACH_BEGIN SMARTLIST_FOREACH_END
35 DIGESTMAP_FOREACH_MODIFY DIGESTMAP_FOREACH
36 DIGEST256MAP_FOREACH_MODIFY DIGEST256MAP_FOREACH
37 STRMAP_FOREACH_MODIFY STRMAP_FOREACH
38 SDMAP_FOREACH EIMAP_FOREACH RIMAP_FOREACH
39 MAP_FOREACH_MODIFY MAP_FOREACH
40 TOR_SIMPLEQ_FOREACH TOR_SIMPLEQ_FOREACH_SAFE
41 TOR_LIST_FOREACH TOR_LIST_FOREACH_SAFE
42 TOR_SLIST_FOREACH TOR_SLIST_FOREACH_SAFE
57 if ($basenames{$basename}) {
58 msg
"dup fname:$fn (same as $basenames{$basename}).\n";
60 $basenames{$basename} = $fn;
62 my $isheader = ($fn =~ /\.h/);
64 my $guardname = "<none>";
67 ## Warn about windows-style newlines.
68 # (We insist on lines that end with a single LF character, not
74 # (We only use spaces)
78 ## Warn about labels that don't have a space in front of them
79 # (We indent every label at least one space)
80 #if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
81 # msg "nosplabel:$fn:$.\n";
83 ## Warn about trailing whitespace.
84 # (We don't allow whitespace at the end of the line; make your
85 # editor highlight it for you so you can stop adding it in.)
87 msg
"Space\@EOL:$fn:$.\n";
89 ## Warn about control keywords without following space.
90 # (We put a space after every 'if', 'while', 'for', 'switch', etc)
91 if ($C && /\s(?:if|while|for|switch)\(/) {
94 ## Warn about #else #if instead of #elif.
95 # (We only allow #elif)
96 if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
97 msg
"#else#if:$fn:$.\n";
99 ## Warn about some K&R violations
100 # (We use K&R-style C, where open braces go on the same line as
101 # the statement that introduces them. In other words:
107 if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
108 $lastline !~ /\{$/) {
109 msg
"non-K&R {:$fn:$.\n";
111 if (/^\s*else/ and $lastline =~ /\}$/) {
112 msg
"}\\nelse:$fn:$.\n";
115 ## Warn about unnecessary empty lines.
116 # (Don't put an empty line before a line that contains nothing
117 # but a closing brace.)
118 if ($lastnil && /^\s*}\n/) {
119 msg
"UnnecNL:$fn:$.\n";
121 ## Warn about multiple empty lines.
122 # (At most one blank line in a row.)
123 if ($lastnil && /^$/) {
124 msg
"DoubleNL:$fn:$.\n";
130 ## Terminals are still 80 columns wide in my world. I refuse to
131 ## accept double-line lines.
132 # (Don't make lines wider than 80 characters, including newline.)
133 if (/^.{80}/ and not /LCOV_EXCL/) {
136 ### Juju to skip over comments and strings, since the tests
137 ### we're about to do are okay there.
149 if ($seenguard == 0) {
150 if (/^\s*\#\s*ifndef\s+(\S+)/) {
154 } elsif ($seenguard == 1) {
155 if (/^\s*\#\s*define (\S+)/) {
157 if ($1 ne $guardname) {
158 msg
"GUARD:$fn:$.: Header guard macro mismatch.\n";
171 s!"(?:[^\"]+|\\.)*"!"X"!g;
173 ## Skip C++-style comments.
178 ## Warn about unquoted braces preceded by unexpected character.
179 if (/([^\s'\)\(\{])\{/) {
182 ## Warn about double semi-colons at the end of a line.
186 ## Warn about multiple internal spaces.
187 #if (/[^\s,:]\s{2,}[^\s\\=]/) {
188 # msg "X X:$fn:$.\n";
190 ## Warn about { with stuff after.
192 #if (/\{[^\}\\]+$/) {
195 ## Warn about function calls with space before parens.
196 # (Don't put a space between the name of a function and its
198 if (/(\w+)\s\(([A-Z]*)/) {
199 if (! $allow_space_after{$1} && $2 ne 'WINAPI') {
200 msg
"fn ():$fn:$.\n";
203 ## Warn about functions not declared at start of line.
204 # (When you're declaring functions, put "static" and "const"
205 # and the return type on one line, and the function name at
206 # the start of a new line.)
208 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
209 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
210 ! /= *\{$/ && ! /;$/) && ! /^[a-zA-Z0-9_]+\s*:/) {
212 msg
"fn() {:$fn:$.\n";
214 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
215 $in_func_head = -1; # started with tp fn
219 if ($in_func_head == -1) {
220 msg
"tp fn():$fn:$.\n";
226 ## Check for forbidden functions except when they are
227 # explicitly permitted
228 if (/\bassert\(/ && not /assert OK/) {
229 msg
"assert:$fn:$. (use tor_assert)\n";
231 if (/\bmemcmp\(/ && not /memcmp OK/) {
232 msg
"memcmp:$fn:$. (use {tor,fast}_mem{eq,neq,cmp}\n";
235 if (not /\ OVERRIDE\ /) {
236 if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
239 if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
240 /\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
241 msg
"$&:$fn:$. (use tor_malloc, tor_free, etc)\n";
246 if ($isheader && $C) {
247 if ($seenguard < 2) {
248 msg
"noguard:$fn (No #ifndef/#define header guard pair found)\n";
249 } elsif ($guardnames{$guardname}) {
250 msg
"dupguard:$fn (Guard macro $guardname also used in $guardnames{$guardname})\n";
252 $guardnames{$guardname} = $fn;