checkSpace.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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!/\*.*?\*/!!;
  43. } elsif (m!/\*!) {
  44. s!/\*!!;
  45. $incomment = 1;
  46. next;
  47. }
  48. s!"(?:[^\"]+|\\.)*"!!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 function calls with space before parens.
  60. if (/(\w+)\s\(/) {
  61. if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
  62. $1 ne "switch" and $1 ne "return" and $1 ne "int" and
  63. $1 ne "void" and $1 ne "__attribute__") {
  64. print " fn ():$fn:$.\n";
  65. }
  66. }
  67. ## Warn about multiple internal spaces.
  68. #if (/\S\s{2,}[^\s\\]/) {
  69. # print " X X:$fn:$.\n";
  70. #}
  71. ## Warn about { with stuff after.
  72. #s/\s+$//;
  73. #if (/\{[^\}\\]+$/) {
  74. # print " {X:$fn:$.\n";
  75. #}
  76. }
  77. close(F);
  78. }