checkSpace.pl 4.1 KB

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