checkSpace.pl 6.8 KB

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