checkSpace.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/perl -w
  2. for $fn (@ARGV) {
  3. open(F, "$fn");
  4. $lastnil = 0;
  5. $incomment = 0;
  6. while (<F>) {
  7. ## Warn about windows-style newlines.
  8. if (/\r/) {
  9. print " CR:$fn:$.\n";
  10. }
  11. ## Warn about tabs.
  12. if (/\t/) {
  13. print " TAB:$fn:$.\n";
  14. }
  15. ## Warn about trailing whitespace.
  16. if (/ +$/) {
  17. print "Space\@EOL:$fn:$.\n";
  18. }
  19. ## Warn about control keywords without following space.
  20. if (/\s(?:if|while|for|switch)\(/) {
  21. print " KW(:$fn:$.\n";
  22. }
  23. ## Warn about multiple empty lines.
  24. if ($lastnil && /^$/) {
  25. print " DoubleNL:$fn:$.\n";
  26. } elsif (/^$/) {
  27. $lastnil = 1;
  28. } else {
  29. $lastnil = 0;
  30. }
  31. ### Juju to skip over comments and strings, since the tests
  32. ### we're about to do are okay there.
  33. if ($incomment) {
  34. if (m!\*/!) {
  35. s!.*?\*/!!;
  36. $incomment = 0;
  37. } else {
  38. next;
  39. }
  40. }
  41. if (m!/\*.*?\*/!) {
  42. s!\s*/\*.*?\*/!!;
  43. } elsif (m!/\*!) {
  44. s!\s*/\*!!;
  45. $incomment = 1;
  46. next;
  47. }
  48. s!"(?:[^\"]+|\\.)*"!"X"!g;
  49. next if /^\#/;
  50. ## Warn about C++-style comments.
  51. if (m!//!) {
  52. # print " //:$fn:$.\n";
  53. s!//.*!!;
  54. }
  55. ## Warn about braces preceded by non-space.
  56. if (/([^\s])\{/) {
  57. print " $1\{:$fn:$.\n";
  58. }
  59. ## Warn about multiple internal spaces.
  60. #if (/[^\s,:]\s{2,}[^\s\\=]/) {
  61. # print " X X:$fn:$.\n";
  62. #}
  63. ## Warn about { with stuff after.
  64. #s/\s+$//;
  65. #if (/\{[^\}\\]+$/) {
  66. # print " {X:$fn:$.\n";
  67. #}
  68. ## Warn about function calls with space before parens.
  69. if (/(\w+)\s\(/) {
  70. if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
  71. $1 ne "switch" and $1 ne "return" and $1 ne "int" and
  72. $1 ne "void" and $1 ne "__attribute__") {
  73. print " fn ():$fn:$.\n";
  74. }
  75. }
  76. }
  77. if (! $lastnil) {
  78. print " EOL\@EOF:$fn:$.\n";
  79. }
  80. close(F);
  81. }