14 if ($ARGV[0] =~ /^-/) {
15 my $lang = shift @ARGV;
26 ## Warn about windows-style newlines.
27 # (We insist on lines that end with a single LF character, not
33 # (We only use spaces)
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.)
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)\(/) {
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:
66 if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
68 msg
"non-K&R {:$fn:$.\n";
70 if (/^\s*else/ and $lastline =~ /\}$/) {
71 msg
" }\\nelse:$fn:$.\n";
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";
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.)
95 ### Juju to skip over comments and strings, since the tests
96 ### we're about to do are okay there.
113 s!"(?:[^\"]+|\\.)*"!"X"!g;
115 ## Warn about C++-style comments.
116 # (Use C style comments only.)
118 # msg " //:$fn:$.\n";
121 ## Warn about unquoted braces preceded by non-space.
122 # (No character except a space should come before a {)
124 msg
" $1\{:$fn:$.\n";
126 ## Warn about double semi-colons at the end of a line.
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.
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
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.)
157 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
158 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
159 ! /= *\{$/ && ! /;$/)) {
161 msg
"fn() {:$fn:$.\n";
163 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
164 $in_func_head = -1; # started with tp fn
168 if ($in_func_head == -1) {
169 msg
"tp fn():$fn:$.\n";
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";
184 if (not /\ OVERRIDE\ /) {
185 if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
188 if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
189 /\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
190 msg
"$& :$fn:$. (use tor_malloc, tor_free, etc)\n";