2
0

clean-header-guards.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/perl -w
  2. #
  3. # Clean up include guards in headers
  4. #
  5. # Copyright (C) 2016 Red Hat, Inc.
  6. #
  7. # Authors:
  8. # Markus Armbruster <armbru@redhat.com>
  9. #
  10. # This work is licensed under the terms of the GNU GPL, version 2 or
  11. # (at your option) any later version. See the COPYING file in the
  12. # top-level directory.
  13. #
  14. # Usage: scripts/clean-header-guards.pl [OPTION]... [FILE]...
  15. # -c CC Use a compiler other than cc
  16. # -n Suppress actual cleanup
  17. # -v Show which files are cleaned up, and which are skipped
  18. #
  19. # Does the following:
  20. # - Header files without a recognizable header guard are skipped.
  21. # - Clean up any untidy header guards in-place. Warn if the cleanup
  22. # renames guard symbols, and explain how to find occurences of these
  23. # symbols that may have to be updated manually.
  24. # - Warn about duplicate header guard symbols. To make full use of
  25. # this warning, you should clean up *all* headers in one run.
  26. # - Warn when preprocessing a header with its guard symbol defined
  27. # produces anything but whitespace. The preprocessor is run like
  28. # "cc -E -DGUARD_H -c -P -", and fed the test program on stdin.
  29. use strict;
  30. use Getopt::Std;
  31. # Stuff we don't want to clean because we import it into our tree:
  32. my $exclude = qr,^(disas/libvixl/|include/standard-headers/
  33. |linux-headers/|pc-bios/|tests/tcg/|tests/multiboot/),x;
  34. # Stuff that is expected to fail the preprocessing test:
  35. my $exclude_cpp = qr,^include/libdecnumber/decNumberLocal.h,;
  36. my %guarded = ();
  37. my %old_guard = ();
  38. our $opt_c = "cc";
  39. our $opt_n = 0;
  40. our $opt_v = 0;
  41. getopts("c:nv");
  42. sub skipping {
  43. my ($fname, $msg, $line1, $line2) = @_;
  44. return if !$opt_v or $fname =~ $exclude;
  45. print "$fname skipped: $msg\n";
  46. print " $line1" if defined $line1;
  47. print " $line2" if defined $line2;
  48. }
  49. sub gripe {
  50. my ($fname, $msg) = @_;
  51. return if $fname =~ $exclude;
  52. print STDERR "$fname: warning: $msg\n";
  53. }
  54. sub slurp {
  55. my ($fname) = @_;
  56. local $/; # slurp
  57. open(my $in, "<", $fname)
  58. or die "can't open $fname for reading: $!";
  59. return <$in>;
  60. }
  61. sub unslurp {
  62. my ($fname, $contents) = @_;
  63. open (my $out, ">", $fname)
  64. or die "can't open $fname for writing: $!";
  65. print $out $contents
  66. or die "error writing $fname: $!";
  67. close $out
  68. or die "error writing $fname: $!";
  69. }
  70. sub fname2guard {
  71. my ($fname) = @_;
  72. $fname =~ tr/a-z/A-Z/;
  73. $fname =~ tr/A-Z0-9/_/cs;
  74. return $fname;
  75. }
  76. sub preprocess {
  77. my ($fname, $guard) = @_;
  78. open(my $pipe, "-|", "$opt_c -E -D$guard -c -P - <$fname")
  79. or die "can't run $opt_c: $!";
  80. while (<$pipe>) {
  81. if ($_ =~ /\S/) {
  82. gripe($fname, "not blank after preprocessing");
  83. last;
  84. }
  85. }
  86. close $pipe
  87. or gripe($fname, "preprocessing failed ($opt_c exit status $?)");
  88. }
  89. for my $fname (@ARGV) {
  90. my $text = slurp($fname);
  91. $text =~ m,\A(\s*\n|\s*//\N*\n|\s*/\*.*?\*/\s*\n)*|,msg;
  92. my $pre = $&;
  93. unless ($text =~ /\G(.*\n)/g) {
  94. $text =~ /\G.*/;
  95. skipping($fname, "no recognizable header guard", "$&\n");
  96. next;
  97. }
  98. my $line1 = $1;
  99. unless ($text =~ /\G(.*\n)/g) {
  100. $text =~ /\G.*/;
  101. skipping($fname, "no recognizable header guard", "$&\n");
  102. next;
  103. }
  104. my $line2 = $1;
  105. my $body = substr($text, pos($text));
  106. unless ($line1 =~ /^\s*\#\s*(if\s*\!\s*defined(\s*\()?|ifndef)\s*
  107. ([A-Za-z0-9_]+)/x) {
  108. skipping($fname, "no recognizable header guard", $line1, $line2);
  109. next;
  110. }
  111. my $guard = $3;
  112. unless ($line2 =~ /^\s*\#\s*define\s+([A-Za-z0-9_]+)/) {
  113. skipping($fname, "no recognizable header guard", $line1, $line2);
  114. next;
  115. }
  116. my $guard2 = $1;
  117. unless ($guard2 eq $guard) {
  118. skipping($fname, "mismatched header guard ($guard vs. $guard2) ",
  119. $line1, $line2);
  120. next;
  121. }
  122. unless ($body =~ m,\A((.*\n)*)
  123. (\s*\#\s*endif\s*(/\*\s*.*\s*\*/\s*)?\n?)
  124. (\n|\s)*\Z,x) {
  125. skipping($fname, "can't find end of header guard");
  126. next;
  127. }
  128. $body = $1;
  129. my $line3 = $3;
  130. my $endif_comment = $4;
  131. my $oldg = $guard;
  132. unless ($fname =~ $exclude) {
  133. my @issues = ();
  134. $guard =~ tr/a-z/A-Z/
  135. and push @issues, "contains lowercase letters";
  136. $guard =~ s/^_+//
  137. and push @issues, "is a reserved identifier";
  138. $guard =~ s/(_H)?_*$/_H/
  139. and $& ne "_H" and push @issues, "doesn't end with _H";
  140. unless ($guard =~ /^[A-Z][A-Z0-9_]*_H/) {
  141. skipping($fname, "can't clean up odd guard symbol $oldg\n",
  142. $line1, $line2);
  143. next;
  144. }
  145. my $exp = fname2guard($fname =~ s,.*/,,r);
  146. unless ($guard =~ /\Q$exp\E\Z/) {
  147. $guard = fname2guard($fname =~ s,^include/,,r);
  148. push @issues, "doesn't match the file name";
  149. }
  150. if (@issues and $opt_v) {
  151. print "$fname guard $oldg needs cleanup:\n ",
  152. join(", ", @issues), "\n";
  153. }
  154. }
  155. $old_guard{$guard} = $oldg
  156. if $guard ne $oldg;
  157. if (exists $guarded{$guard}) {
  158. gripe($fname, "guard $guard also used by $guarded{$guard}");
  159. } else {
  160. $guarded{$guard} = $fname;
  161. }
  162. unless ($fname =~ $exclude) {
  163. my $newl1 = "#ifndef $guard\n";
  164. my $newl2 = "#define $guard\n";
  165. my $newl3 = "#endif\n";
  166. $newl3 =~ s,\Z, /* $guard */, if defined $endif_comment;
  167. if ($line1 ne $newl1 or $line2 ne $newl2 or $line3 ne $newl3) {
  168. $pre =~ s/\n*\Z/\n\n/ if $pre =~ /\N/;
  169. $body =~ s/\A\n*/\n/;
  170. if ($opt_n) {
  171. print "$fname would be cleaned up\n" if $opt_v;
  172. } else {
  173. unslurp($fname, "$pre$newl1$newl2$body$newl3");
  174. print "$fname cleaned up\n" if $opt_v;
  175. }
  176. }
  177. }
  178. preprocess($fname, $opt_n ? $oldg : $guard)
  179. unless $fname =~ $exclude or $fname =~ $exclude_cpp;
  180. }
  181. if (%old_guard) {
  182. print STDERR "warning: guard symbol renaming may break things\n";
  183. for my $guard (sort keys %old_guard) {
  184. print STDERR " $old_guard{$guard} -> $guard\n";
  185. }
  186. print STDERR "To find uses that may have to be updated try:\n";
  187. print STDERR " git grep -Ew '", join("|", sort values %old_guard),
  188. "'\n";
  189. }