checkSpace.pl 6.8 KB

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