checkSpace.pl 3.0 KB

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