3 # Clean up include guards in headers
5 # Copyright (C) 2016 Red Hat, Inc.
8 # Markus Armbruster <armbru@redhat.com>
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.
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
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 occurrences 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.
34 # Stuff we don't want to clean because we import it into our tree:
35 my $exclude = qr
,^(include
/standard-headers/|linux
-headers
/
36 |pc
-bios
/|tests
/tcg
/|tests
/multiboot/),x
;
37 # Stuff that is expected to fail the preprocessing test:
38 my $exclude_cpp = qr
,^include
/libdecnumber/decNumberLocal
.h
,;
49 my ($fname, $msg, $line1, $line2) = @_;
51 return if !$opt_v or $fname =~ $exclude;
52 print "$fname skipped: $msg\n";
53 print " $line1" if defined $line1;
54 print " $line2" if defined $line2;
58 my ($fname, $msg) = @_;
59 return if $fname =~ $exclude;
60 print STDERR
"$fname: warning: $msg\n";
66 open(my $in, "<", $fname)
67 or die "can't open $fname for reading: $!";
72 my ($fname, $contents) = @_;
73 open (my $out, ">", $fname)
74 or die "can't open $fname for writing: $!";
76 or die "error writing $fname: $!";
78 or die "error writing $fname: $!";
83 $fname =~ tr/a-z/A-Z/;
84 $fname =~ tr/A-Z0-9/_/cs;
89 my ($fname, $guard) = @_;
91 open(my $pipe, "-|", "$opt_c -E -D$guard -c -P - <$fname")
92 or die "can't run $opt_c: $!";
95 gripe
($fname, "not blank after preprocessing");
100 or gripe
($fname, "preprocessing failed ($opt_c exit status $?)");
103 for my $fname (@ARGV) {
104 my $text = slurp
($fname);
106 $text =~ m
,\A
(\s
*\n|\s
*//\N
*\n|\s
*/\*.*?\*/\s
*\n)*|,sg
;
108 unless ($text =~ /\G(.*\n)/g) {
110 skipping
($fname, "no recognizable header guard", "$&\n");
114 unless ($text =~ /\G(.*\n)/g) {
116 skipping
($fname, "no recognizable header guard", "$&\n");
120 my $body = substr($text, pos($text));
122 unless ($line1 =~ /^\s
*\#\s
*(if\s
*\
!\s
*defined(\s
*\
()?
|ifndef
)\s
*
124 skipping
($fname, "no recognizable header guard", $line1, $line2);
128 unless ($line2 =~ /^\s*\#\s*define\s+([A-Za-z0-9_]+)/) {
129 skipping
($fname, "no recognizable header guard", $line1, $line2);
133 unless ($guard2 eq $guard) {
134 skipping
($fname, "mismatched header guard ($guard vs. $guard2) ",
139 unless ($body =~ m
,\A
((.*\n)*)
140 ([ \t]*\#
[ \t]*endif
([ \t]*\N
*)\n)
141 ((?s
)(\s
*\n|\s
*//\N
*\n|\s
*/\*.*?\*/\s
*\n)*)
143 skipping
($fname, "can't find end of header guard");
148 my $endif_comment = $4;
153 unless ($fname =~ $exclude) {
155 $guard =~ tr/a-z/A-Z/
156 and push @issues, "contains lowercase letters";
158 and push @issues, "is a reserved identifier";
159 $guard =~ s/(_H)?_*$/_H/
160 and $& ne "_H" and push @issues, "doesn't end with _H";
161 unless ($guard =~ /^[A-Z][A-Z0-9_]*_H/) {
162 skipping
($fname, "can't clean up odd guard symbol $oldg\n",
167 my $exp = fname2guard
($fname =~ s
,.*/,,r
);
168 unless ($guard =~ /\Q$exp\E\Z/) {
169 $guard = fname2guard
($fname =~ s
,^include
/,,r
);
170 push @issues, "doesn't match the file name";
172 if (@issues and $opt_v) {
173 print "$fname guard $oldg needs cleanup:\n ",
174 join(", ", @issues), "\n";
178 $old_guard{$guard} = $oldg
181 if (exists $guarded{$guard}) {
182 gripe
($fname, "guard $guard also used by $guarded{$guard}");
184 $guarded{$guard} = $fname;
187 unless ($fname =~ $exclude) {
188 my $newl1 = "#ifndef $guard\n";
189 my $newl2 = "#define $guard\n";
190 my $newl3 = "#endif\n";
191 $newl3 =~ s
,\Z
, /* $guard */, if $endif_comment;
192 if ($line1 ne $newl1 or $line2 ne $newl2 or $line3 ne $newl3) {
193 $pre =~ s/\n*\Z/\n\n/ if $pre =~ /\N/;
194 $body =~ s/\A\n*/\n/;
196 print "$fname would be cleaned up\n" if $opt_v;
198 unslurp
($fname, "$pre$newl1$newl2$body$newl3$post");
199 print "$fname cleaned up\n" if $opt_v;
204 preprocess
($fname, $opt_n ?
$oldg : $guard)
205 unless $fname =~ $exclude or $fname =~ $exclude_cpp;
209 print STDERR
"warning: guard symbol renaming may break things\n";
210 for my $guard (sort keys %old_guard) {
211 print STDERR
" $old_guard{$guard} -> $guard\n";
213 print STDERR
"To find uses that may have to be updated try:\n";
214 print STDERR
" git grep -Ew '", join("|", sort values %old_guard),