checkSpace.pl 8.1 KB

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