checkSpace.pl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $found = 0;
  5. my $COLON_POS = 10;
  6. sub msg {
  7. $found = 1;
  8. my $v = shift;
  9. $v =~ /^\s*([^:]+):(.*)$/;
  10. chomp(my $errtype = $1);
  11. my $rest = $2;
  12. my $padding = ' ' x ($COLON_POS - length $errtype);
  13. print "$padding$errtype:$rest\n";
  14. }
  15. my $C = 0;
  16. if ($ARGV[0] =~ /^-/) {
  17. my $lang = shift @ARGV;
  18. $C = ($lang eq '-C');
  19. }
  20. # hashmap of things where we allow spaces between them and (.
  21. our %allow_space_after= map {$_, 1} qw{
  22. if while for switch return int unsigned elsif WINAPI
  23. void __attribute__ op size_t double uint64_t
  24. bool ssize_t
  25. workqueue_reply_t hs_desc_decode_status_t
  26. PRStatus
  27. SMARTLIST_FOREACH_BEGIN SMARTLIST_FOREACH_END
  28. HT_FOREACH
  29. DIGESTMAP_FOREACH_MODIFY DIGESTMAP_FOREACH
  30. DIGEST256MAP_FOREACH_MODIFY DIGEST256MAP_FOREACH
  31. STRMAP_FOREACH_MODIFY STRMAP_FOREACH
  32. SDMAP_FOREACH EIMAP_FOREACH RIMAP_FOREACH
  33. MAP_FOREACH_MODIFY MAP_FOREACH
  34. TOR_SIMPLEQ_FOREACH TOR_SIMPLEQ_FOREACH_SAFE
  35. TOR_LIST_FOREACH TOR_LIST_FOREACH_SAFE
  36. TOR_SLIST_FOREACH TOR_SLIST_FOREACH_SAFE
  37. };
  38. our %basenames = ();
  39. our %guardnames = ();
  40. for my $fn (@ARGV) {
  41. open(F, "$fn");
  42. my $lastnil = 0;
  43. my $lastline = "";
  44. my $incomment = 0;
  45. my $in_func_head = 0;
  46. my $basename = $fn;
  47. $basename =~ s#.*/##;
  48. if ($basenames{$basename}) {
  49. msg "dup fname:$fn (same as $basenames{$basename}).\n";
  50. } else {
  51. $basenames{$basename} = $fn;
  52. }
  53. my $isheader = ($fn =~ /\.h/);
  54. my $seenguard = 0;
  55. my $guardname = "<none>";
  56. while (<F>) {
  57. ## Warn about windows-style newlines.
  58. # (We insist on lines that end with a single LF character, not
  59. # CR LF.)
  60. if (/\r/) {
  61. msg "CR:$fn:$.\n";
  62. }
  63. ## Warn about tabs.
  64. # (We only use spaces)
  65. if (/\t/) {
  66. msg "TAB:$fn:$.\n";
  67. }
  68. ## Warn about labels that don't have a space in front of them
  69. # (We indent every label at least one space)
  70. #if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
  71. # msg "nosplabel:$fn:$.\n";
  72. #}
  73. ## Warn about trailing whitespace.
  74. # (We don't allow whitespace at the end of the line; make your
  75. # editor highlight it for you so you can stop adding it in.)
  76. if (/ +$/) {
  77. msg "Space\@EOL:$fn:$.\n";
  78. }
  79. ## Warn about control keywords without following space.
  80. # (We put a space after every 'if', 'while', 'for', 'switch', etc)
  81. if ($C && /\s(?:if|while|for|switch)\(/) {
  82. msg "KW(:$fn:$.\n";
  83. }
  84. ## Warn about #else #if instead of #elif.
  85. # (We only allow #elif)
  86. if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
  87. msg "#else#if:$fn:$.\n";
  88. }
  89. ## Warn about some K&R violations
  90. # (We use K&R-style C, where open braces go on the same line as
  91. # the statement that introduces them. In other words:
  92. # if (a) {
  93. # stuff;
  94. # } else {
  95. # other stuff;
  96. # }
  97. if (/^\s+\{/ and $lastline =~ /^\s*(if|while|for|else if)/ and
  98. $lastline !~ /\{$/) {
  99. msg "non-K&R {:$fn:$.\n";
  100. }
  101. if (/^\s*else/ and $lastline =~ /\}$/) {
  102. msg "}\\nelse:$fn:$.\n";
  103. }
  104. $lastline = $_;
  105. ## Warn about unnecessary empty lines.
  106. # (Don't put an empty line before a line that contains nothing
  107. # but a closing brace.)
  108. if ($lastnil && /^\s*}\n/) {
  109. msg "UnnecNL:$fn:$.\n";
  110. }
  111. ## Warn about multiple empty lines.
  112. # (At most one blank line in a row.)
  113. if ($lastnil && /^$/) {
  114. msg "DoubleNL:$fn:$.\n";
  115. } elsif (/^$/) {
  116. $lastnil = 1;
  117. } else {
  118. $lastnil = 0;
  119. }
  120. ## Terminals are still 80 columns wide in my world. I refuse to
  121. ## accept double-line lines.
  122. # (Don't make lines wider than 80 characters, including newline.)
  123. if (/^.{80}/ and not /LCOV_EXCL/) {
  124. msg "Wide:$fn:$.\n";
  125. }
  126. ### Juju to skip over comments and strings, since the tests
  127. ### we're about to do are okay there.
  128. if ($C) {
  129. if ($incomment) {
  130. if (m!\*/!) {
  131. s!.*?\*/!!;
  132. $incomment = 0;
  133. } else {
  134. next;
  135. }
  136. }
  137. if ($isheader) {
  138. if ($seenguard == 0) {
  139. if (/^\s*\#\s*ifndef\s+(\S+)/) {
  140. ++$seenguard;
  141. $guardname = $1;
  142. }
  143. } elsif ($seenguard == 1) {
  144. if (/^\s*\#\s*define (\S+)/) {
  145. ++$seenguard;
  146. if ($1 ne $guardname) {
  147. msg "GUARD:$fn:$.: Header guard macro mismatch.\n";
  148. }
  149. }
  150. }
  151. }
  152. if (m!/\*.*?\*/!) {
  153. s!\s*/\*.*?\*/!!;
  154. } elsif (m!/\*!) {
  155. s!\s*/\*!!;
  156. $incomment = 1;
  157. next;
  158. }
  159. s!"(?:[^\"]+|\\.)*"!"X"!g;
  160. next if /^\#/;
  161. ## Skip C++-style comments.
  162. if (m!//!) {
  163. # msg "//:$fn:$.\n";
  164. s!//.*!!;
  165. }
  166. ## Warn about unquoted braces preceded by unexpected character.
  167. if (/([^\s'\)\(\{])\{/) {
  168. msg "$1\{:$fn:$.\n";
  169. }
  170. ## Warn about double semi-colons at the end of a line.
  171. if (/;;$/) {
  172. msg ";;:$fn:$.\n"
  173. }
  174. ## Warn about multiple internal spaces.
  175. #if (/[^\s,:]\s{2,}[^\s\\=]/) {
  176. # msg "X X:$fn:$.\n";
  177. #}
  178. ## Warn about { with stuff after.
  179. #s/\s+$//;
  180. #if (/\{[^\}\\]+$/) {
  181. # msg "{X:$fn:$.\n";
  182. #}
  183. ## Warn about function calls with space before parens.
  184. # (Don't put a space between the name of a function and its
  185. # arguments.)
  186. if (/(\w+)\s\(([A-Z]*)/) {
  187. if (! $allow_space_after{$1} && $2 ne 'WINAPI') {
  188. msg "fn ():$fn:$.\n";
  189. }
  190. }
  191. ## Warn about functions not declared at start of line.
  192. # (When you're declaring functions, put "static" and "const"
  193. # and the return type on one line, and the function name at
  194. # the start of a new line.)
  195. if ($in_func_head ||
  196. ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
  197. ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
  198. ! /= *\{$/ && ! /;$/) && ! /^[a-zA-Z0-9_]+\s*:/) {
  199. if (/[^,\s]\s*\{$/){
  200. msg "fn() {:$fn:$.\n";
  201. $in_func_head = 0;
  202. } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
  203. $in_func_head = -1; # started with tp fn
  204. } elsif (/;$/) {
  205. $in_func_head = 0;
  206. } elsif (/\{/) {
  207. if ($in_func_head == -1) {
  208. msg "tp fn():$fn:$.\n";
  209. }
  210. $in_func_head = 0;
  211. }
  212. }
  213. ## Check for forbidden functions except when they are
  214. # explicitly permitted
  215. if (/\bassert\(/ && not /assert OK/) {
  216. msg "assert:$fn:$. (use tor_assert)\n";
  217. }
  218. if (/\bmemcmp\(/ && not /memcmp OK/) {
  219. msg "memcmp:$fn:$. (use {tor,fast}_mem{eq,neq,cmp}\n";
  220. }
  221. # always forbidden.
  222. if (not /\ OVERRIDE\ /) {
  223. if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
  224. msg "$&:$fn:$.\n";
  225. }
  226. if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
  227. /\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
  228. msg "$&:$fn:$. (use tor_malloc, tor_free, etc)\n";
  229. }
  230. }
  231. }
  232. }
  233. if ($isheader && $C) {
  234. if ($seenguard < 2) {
  235. msg "noguard:$fn (No #ifndef/#define header guard pair found)\n";
  236. } elsif ($guardnames{$guardname}) {
  237. msg "dupguard:$fn (Guard macro $guardname also used in $guardnames{$guardname})\n";
  238. } else {
  239. $guardnames{$guardname} = $fn;
  240. }
  241. }
  242. close(F);
  243. }
  244. exit $found;