checkSpace.pl 4.0 KB

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