checkSpace.pl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 double semi-colons at the end of a line.
  122. if (/;;$/) {
  123. msg " double semi-colons at the end of $. in $fn\n"
  124. }
  125. ## Warn about multiple internal spaces.
  126. #if (/[^\s,:]\s{2,}[^\s\\=]/) {
  127. # msg " X X:$fn:$.\n";
  128. #}
  129. ## Warn about { with stuff after.
  130. #s/\s+$//;
  131. #if (/\{[^\}\\]+$/) {
  132. # msg " {X:$fn:$.\n";
  133. #}
  134. ## Warn about function calls with space before parens.
  135. # (Don't put a space between the name of a function and its
  136. # arguments.)
  137. if (/(\w+)\s\(([A-Z]*)/) {
  138. if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
  139. $1 ne "switch" and $1 ne "return" and $1 ne "int" and
  140. $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
  141. $1 ne "void" and $1 ne "__attribute__" and $1 ne "op" and
  142. $1 ne "size_t" and $1 ne "double" and $1 ne "uint64_t" and
  143. $1 ne "workqueue_reply_t") {
  144. msg " fn ():$fn:$.\n";
  145. }
  146. }
  147. ## Warn about functions not declared at start of line.
  148. # (When you're declaring functions, put "static" and "const"
  149. # and the return type on one line, and the function name at
  150. # the start of a new line.)
  151. if ($in_func_head ||
  152. ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
  153. ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
  154. ! /= *\{$/ && ! /;$/)) {
  155. if (/.\{$/){
  156. msg "fn() {:$fn:$.\n";
  157. $in_func_head = 0;
  158. } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
  159. $in_func_head = -1; # started with tp fn
  160. } elsif (/;$/) {
  161. $in_func_head = 0;
  162. } elsif (/\{/) {
  163. if ($in_func_head == -1) {
  164. msg "tp fn():$fn:$.\n";
  165. }
  166. $in_func_head = 0;
  167. }
  168. }
  169. ## Check for forbidden functions except when they are
  170. # explicitly permitted
  171. if (/\bassert\(/ && not /assert OK/) {
  172. msg "assert :$fn:$. (use tor_assert)\n";
  173. }
  174. if (/\bmemcmp\(/ && not /memcmp OK/) {
  175. msg "memcmp :$fn:$. (use {tor,fast}_mem{eq,neq,cmp}\n";
  176. }
  177. # always forbidden.
  178. if (not /\ OVERRIDE\ /) {
  179. if (/\bstrcat\(/ or /\bstrcpy\(/ or /\bsprintf\(/) {
  180. msg "$& :$fn:$.\n";
  181. }
  182. if (/\bmalloc\(/ or /\bfree\(/ or /\brealloc\(/ or
  183. /\bstrdup\(/ or /\bstrndup\(/ or /\bcalloc\(/) {
  184. msg "$& :$fn:$. (use tor_malloc, tor_free, etc)\n";
  185. }
  186. }
  187. }
  188. }
  189. ## Warn if the file doesn't end with a blank line.
  190. # (End each file with a single blank line.)
  191. if (! $lastnil) {
  192. msg " EOL\@EOF:$fn:$.\n";
  193. }
  194. close(F);
  195. }
  196. exit $found;