brace_style.pl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env perl -w
  2. use strict;
  3. my $filename = $ARGV[0];
  4. if (!$filename) {
  5. print "Usage: modified_otb.pl <filename>\n";
  6. exit(1);
  7. }
  8. my @results = ();
  9. my $line_num = 0;
  10. my ($NONE, $BRACE, $PAREN) = (0, 1, 2);
  11. my $looking_for = $NONE;
  12. my $last_func_name = "";
  13. open(HANDLE, "<", $filename) or die "Cannot open $filename\n";
  14. # Read the file and track the lines with length > $max_length.
  15. while (<HANDLE>) {
  16. $line_num++;
  17. # Subtract one because the newline doesn't count toward the
  18. # length.
  19. chomp;
  20. if (!$looking_for &&
  21. ($_ =~ /^\s*function/) &&
  22. ($_ =~ /\{/)) {
  23. # Done (bad): we found a function whose opening line ends with
  24. # a brace, which goes against the PEAR coding guidelines.
  25. ($last_func_name) = $_ =~ /function\s*(.*)\(/;
  26. push @results, "'$last_func_name' prototype ends with opening ".
  27. "brace, line $line_num";
  28. } elsif (!$looking_for &&
  29. ($_ =~ /^\s*function/) &&
  30. ($_ !~ /\)/)) {
  31. ($last_func_name) = $_ =~ /function\s*(.*)\(/;
  32. $looking_for = $PAREN;
  33. } elsif (($looking_for == $PAREN) &&
  34. ($_ =~ /\)/) &&
  35. ($_ =~ /\{/)) {
  36. # Done (bad): function prototype and brace are on the same
  37. # line.
  38. push @results, "'$last_func_name' prototype ends with with ".
  39. "opening brace, line $line_num";
  40. $looking_for = $NONE;
  41. } elsif (($looking_for == $PAREN) &&
  42. ($_ =~ /\)/) &&
  43. ($_ !~ /\{/)) {
  44. $looking_for = $BRACE;
  45. } elsif (!$looking_for &&
  46. ($_ =~ /^\s*function/) &&
  47. ($_ =~ /\)/) &&
  48. ($_ !~ /\{/)) {
  49. ($last_func_name) = $_ =~ /function\s*(.*)\(/;
  50. $looking_for = $BRACE;
  51. } elsif (($looking_for == $BRACE) &&
  52. ($_ eq "{")) {
  53. $looking_for = $NONE;
  54. # Done (good): the brace was found on the line after the
  55. # function prototype.
  56. } else {
  57. # We got here because we got a line that we're not interested
  58. # in.
  59. $looking_for = $NONE;
  60. }
  61. }
  62. # If any long lines were found, notify and exit(1); otherwise,
  63. # exit(0).
  64. if (@results) {
  65. foreach my $result (@results) {
  66. print "$filename: $result\n";
  67. }
  68. exit(1);
  69. } else {
  70. exit(0);
  71. }