checkSpace.pl 6.8 KB

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