checkSpace.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. $incomment = 0;
  11. while (<F>) {
  12. ## Warn about windows-style newlines.
  13. if (/\r/) {
  14. print " CR:$fn:$.\n";
  15. }
  16. ## Warn about tabs.
  17. if (/\t/) {
  18. print " TAB:$fn:$.\n";
  19. }
  20. ## Warn about trailing whitespace.
  21. if (/ +$/) {
  22. print "Space\@EOL:$fn:$.\n";
  23. }
  24. ## Warn about control keywords without following space.
  25. if ($C && /\s(?:if|while|for|switch)\(/) {
  26. print " KW(:$fn:$.\n";
  27. }
  28. ## Warn about multiple empty lines.
  29. if ($lastnil && /^$/) {
  30. print " DoubleNL:$fn:$.\n";
  31. } elsif (/^$/) {
  32. $lastnil = 1;
  33. } else {
  34. $lastnil = 0;
  35. }
  36. ## Terminals are still 80 columns wide in my world. I refuse to
  37. ## accept double-line lines. Except, of course, svn Id tags
  38. ## can make us go long.
  39. if (/^.{80}/ && !/\$Id: /) {
  40. print " Wide:$fn:$.\n";
  41. }
  42. ### Juju to skip over comments and strings, since the tests
  43. ### we're about to do are okay there.
  44. if ($C) {
  45. if ($incomment) {
  46. if (m!\*/!) {
  47. s!.*?\*/!!;
  48. $incomment = 0;
  49. } else {
  50. next;
  51. }
  52. }
  53. if (m!/\*.*?\*/!) {
  54. s!\s*/\*.*?\*/!!;
  55. } elsif (m!/\*!) {
  56. s!\s*/\*!!;
  57. $incomment = 1;
  58. next;
  59. }
  60. s!"(?:[^\"]+|\\.)*"!"X"!g;
  61. next if /^\#/;
  62. ## Warn about C++-style comments.
  63. if (m!//!) {
  64. # print " //:$fn:$.\n";
  65. s!//.*!!;
  66. }
  67. ## Warn about braces preceded by non-space.
  68. if (/([^\s])\{/) {
  69. print " $1\{:$fn:$.\n";
  70. }
  71. ## Warn about multiple internal spaces.
  72. #if (/[^\s,:]\s{2,}[^\s\\=]/) {
  73. # print " X X:$fn:$.\n";
  74. #}
  75. ## Warn about { with stuff after.
  76. #s/\s+$//;
  77. #if (/\{[^\}\\]+$/) {
  78. # print " {X:$fn:$.\n";
  79. #}
  80. ## Warn about function calls with space before parens.
  81. if (/(\w+)\s\(/) {
  82. if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
  83. $1 ne "switch" and $1 ne "return" and $1 ne "int" and
  84. $1 ne "elsif" and
  85. $1 ne "void" and $1 ne "__attribute__") {
  86. print " fn ():$fn:$.\n";
  87. }
  88. }
  89. ## Warn about functions not declared at start of line.
  90. if ($in_func_head ||
  91. ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
  92. ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
  93. ! /= *\{$/ && ! /;$/)) {
  94. if (/.\{$/){
  95. print "fn() {:$fn:$.\n";
  96. $in_func_head = 0;
  97. } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
  98. $in_func_head = -1; # started with tp fn
  99. } elsif (/;$/) {
  100. $in_func_head = 0;
  101. } elsif (/\{/) {
  102. if ($in_func_head == -1) {
  103. print "tp fn():$fn:$.\n";
  104. }
  105. $in_func_head = 0;
  106. }
  107. }
  108. }
  109. }
  110. if (! $lastnil) {
  111. print " EOL\@EOF:$fn:$.\n";
  112. }
  113. close(F);
  114. }