checkSpace.pl 3.9 KB

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