checkSpace.pl 3.2 KB

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