checkSpace.pl 5.8 KB

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