checkSpace.pl 4.4 KB

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