Introspection fixes
[gnumeric.git] / tools / check-header-guards
blob946a9a80bfb19a93e8c032ab196ddbb1436182d4
1 #!/usr/bin/perl -w
3 # Gnumeric
5 # Copyright (C) 2001 Morten Welinder.
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this library; if not, see <https://www.gnu.org/licenses/>.
20 # Author: Morten Welinder <terra@gnome.org>
22 use strict;
24 my $exitcode = 0;
25 my $verbose = 0;
27 warn "$0: should be run from top-level directory.\n"
28 unless -r "configure.ac" && -r 'ChangeLog';
30 my %base_exceptions =
31 ('acconfig.h' => 1,
34 my %exceptions =
35 ('gnumeric-config.h' => 1,
36 'stamp.h' => 1,
39 my %guard_to_file = ();
42 local (*FIND);
43 open (*FIND, "find . '(' -type f -name '*.[hH]' -print ')' -o '(' -type d '(' -name intl -o -name macros -o -name .git -o -name win32 ')' -prune ')' |")
44 or die "$0: cannot execute find: $!\n";
45 FILE:
46 foreach my $filename (<FIND>) {
47 chomp $filename;
48 $filename =~ s|^\./||;
50 next if $exceptions{$filename};
51 next if $filename =~ /\.glade\.h$/;
52 next if $filename =~ /\.xml\.h$/;
53 my $basename = $filename;
54 $basename =~ s|^.*/||;
55 next if $base_exceptions{$basename};
57 local (*FIL);
58 if (open (*FIL, "< $filename")) {
59 my $state = 1;
60 my $guard = undef;
61 LINE:
62 while (<FIL>) {
63 if ($state == 1 && /^\s*\#\s*ifndef\s+([A-Za-z0-9_]+)/) {
64 $guard = $1;
65 $state = 2;
66 print "$0: File `$filename' ok.\n" if $verbose;
67 next LINE;
68 } elsif ($state == 2 && /^\s*\#\s*define\s+([A-Za-z0-9_]+)/) {
69 if ($guard ne $1) {
70 print STDERR "$0: File `$filename' is improperly guarded.\n";
71 $exitcode = 1;
72 next FILE;
74 if (exists $guard_to_file{$guard}) {
75 my $otherfilename = $guard_to_file{$guard};
76 print STDERR "$0: Files `$filename' and $otherfilename have same guard.\n";
77 $exitcode = 1;
78 next FILE;
81 $guard_to_file{$guard} = $filename;
82 next FILE;
83 } elsif (/^\s*\#/) {
84 last LINE;
87 close (*FIL);
88 print STDERR "$0: File `$filename' is not guarded.\n";
89 $exitcode = 1;
90 } else {
91 print STDERR "$0: Cannot read `$filename': $!\b";
92 $exitcode = 1;
95 close (*FIND);
98 exit $exitcode;