checkSpace.pl 3.8 KB

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