checkSpace.pl 7.1 KB

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