check-includes.pl revision 499b34cea04a46823d003d4c0520c8b03e8513cb
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister#! /usr/bin/perl -ws
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence# Copyright (C) 2000, 2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# purpose with or without fee is hereby granted, provided that the above
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# copyright notice and this permission notice appear in all copies.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister#
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7df0472d8a76c83d2137bd9549414a30787d58cfBrian Wellington
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# $Id: check-includes.pl,v 1.7 2001/01/09 22:00:46 bwelling Exp $
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# Rudimentary, primarily for use by the developers.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# This just evolved with no serious attempt at making it
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# bulletproof or foolproof. Or pretty even. Probably would
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# have done it differently if it were actually designed as opposed
1b6d529cb5ee0ad44f8518e1b8c2cbca54bbdf18David Lawrence# to just growing as a multi-tentacled thing as various messages
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# were either added or selectively silenced.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# XXX many warnings should not be made unless the header will be a public file
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Bristeruse strict;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Bristeruse vars qw($debug $isc_includes $dns_includes $lwres_includes
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister $omapi_includes);
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister$isc_includes = "-Ilib/isc/include -Ilib/isc/unix/include " .
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister "-Ilib/isc/pthreads/include";
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister$dns_includes = "-Ilib/dns/include -Ilib/dns/sec/dst/include";
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence$lwres_includes = "-Ilib/lwres/include";
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister$omapi_includes = "-Ilib/omapi/include";
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister$0 =~ s%.*/%%;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencedie "Usage: $0 [-debug] headerfile ...\n" unless @ARGV > 0;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Bristerunless (-f 'configure.in') {
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister die "$0: run from top of bind9 source tree\n";
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister}
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceundef $/;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Bristermy @files = @ARGV;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister# Outer loop runs once for each file.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Bristerfor (<>) {
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister my ($file, $tmpfile, $objfile);
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister $file = shift @files;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister unless ($file =~ /\.h$/) {
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister print "$0: skipping non-header file $file\n";
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence next;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister }
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister die "$0: $file: no such file\n" unless -f $file;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister # header file fragments; ignore
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister # XXX rdatastruct itself is moderately tricky.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister next if $file =~ m%/rdatastruct(pre|suf)\.h$%;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
efe2f579ba6fbec4229129015780f2d925085b04Michael Graff # From external sources; ignore.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister next if $file =~ m%lib/dns/sec/(dnssafe|openssl)%m;
efe2f579ba6fbec4229129015780f2d925085b04Michael Graff
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister # Totally wrong platform; ignore.
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister next if $file =~ m%lib/isc/win32%;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff ($tmpfile = $file) =~ s%(.*/)?%/tmp/%;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff $tmpfile =~ s/\.h$/.c/;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff ($objfile = $tmpfile) =~ s/\.c$/\.o/;;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister $file =~ m%(.*/)?(.*)/(.*)\.h%;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister my $symbol = uc "\Q$2_$3_H\E";
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister $symbol =~ s/\\-/_/g;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister if (! m%^\#ifndef\ $symbol\n
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister \#define\ $symbol\ 1\n
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister (.*\n)+
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister \#endif\ /\*\ $symbol\ \*/\n
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister \n*\Z%mx) {
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister print "$file has non-conforming wrapper for symbol $symbol\n"
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister unless $file =~ m%confparser_p\.h%;
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister }
ae32d75897b39cf93af7348d101dcda8b4b12a54James Brister
my $nocomment = '^(?!\s+/?\*)';
my $lib = $file =~ /lwres/ ? "lwres" : "isc";
# check use of macros without having included proper header for them.
if (/^(\U$lib\E_LANG_(BEGIN|END)DECLS)$/m &&
! m%^#include <$lib/lang\.h>$%m) {
print "$file has $1 without <$lib/lang.h>\n";
}
if (/$nocomment.*ISC_EVENTCLASS_/m && ! m%^#include <isc/eventclass\.h>%m) {
print "$file has ISC_EVENTCLASS_ without <isc/eventclass.h>\n"
unless $file =~ m%isc/eventclass.h%;
}
if (/$nocomment.*ISC_RESULTCLASS_/m &&
! m%^#include <isc/resultclass\.h>%m) {
print "$file has ISC_RESULTCLASS_ without <isc/resultclass.h>\n"
unless $file =~ m%isc/resultclass.h%;
}
if (/$nocomment.*ISC_(TRUE|FALSE|TF)\W/m &&
! m%^#include <isc/(types|boolean).h>%m) {
print "$file has ISC_TRUE/FALSE/TF without <isc/(boolean|types).h>\n"
unless $file =~ m%isc/boolean.h%;
}
if (/$nocomment.*ISC_PLATFORM_/m &&
! m%^#include <isc/platform.h>%m) {
print "$file has ISC_PLATFORM_ without <isc/platform.h>\n"
unless $file =~ m%isc/platform.h%;
}
if ($file !~ m%isc/magic\.h$% && $lib ne "lwres") {
print "$file has ISC_MAGIC_VALID without <isc/magic.h>\n"
if /$nocomment.*ISC_MAGIC_VALID/m && ! m%^#include <isc/magic.h>%m;
print "$file could use ISC_MAGIC_VALID\n" if /^$nocomment.*->magic ==/m;
}
if (/$nocomment.*(ISC|DNS|DST)_R_/m &&
! m%^#include <\L$1\E/result.h>%m) {
print "$file has $1_R_ without <\L$1\E/result.h>\n"
unless $file =~ m%\L$1\E/result.h%m;
}
if (/^$nocomment(?!#define)[a-z].*([a-zA-Z0-9]\([^;]*\);)/m &&
! m%^#include <$lib/lang.h>%m) {
print "$file has declarations without <$lib/lang.h>\n";
}
#
# First see whether it can be compiled without any additional includes.
# Only bother doing this for files that will be installed as public
# headers (thus weeding out, for example, all of the dns/rdata/*/*.h)
#
if ($file =~ m%/include/% && system("cp $file $tmpfile") == 0) {
if (compile($file, $tmpfile, $objfile) != 0) {
print "$file does not compile stand-alone\n";
}
}
my $prefix = '';
my ($elided, $comment, $prefix_extend, $body);
while (1) {
eval {
# 1 23 4 5 6 78
if (m%(\A\Q$prefix\E((.*\n)*?))(\#include .*(<.*?>)(.*)\n)((.*\n)*)%) {
$elided = $5;
$prefix_extend = $2 . $4;
$comment = $6;
$body = $1 . $7;
} else {
$elided = ""; # stop processing this file.
}
};
if ($@ ne "") {
print "$file processing failed: $@\n";
last;
}
last if $elided eq "";
print STDERR "$file checking $elided\n" if $debug;
# Can mark in the header file when a #include should stay even
# though it might not appear that way otherwise.
next if $comment =~ /require|provide|extend|define|contract|ensure/i;
#
# Special exceptions.
# XXXDCL some of these should be perhaps generalized (ie, look for
# ISC_(LINK|LIST)_ when using <isc/list.h>.
#
if (($file =~ m%isc/log\.h$% && $elided eq "<syslog.h>") ||
($file =~ m%isc/print\.h$% && $elided =~ /^<std(arg|def)\.h>$/) ||
($file =~ m%isc/string\.h$% && $elided eq "<string.h>") ||
($file =~ m%isc/types\.h$% &&
$elided =~ m%^<isc/(boolean|int|offset)\.h>$%) ||
($file =~ m%isc/netdb\.h$% &&
$elided =~ m%^<(netdb|isc/net)\.h>$%)) {
next;
}
if ($elided =~ m%^<(isc|dns|dst)/result.h>$%) {
my $dir = $1;
if (! /$nocomment.*\U$dir\E_R_/m) {
unless ($dir eq "isc" && /$nocomment.*isc_result_t/m) {
# No {foo}_R_, but it is acceptable to include isc/result.h for
# isc_result_t ... but not both isc/result.h and isc/types.h.
# The later check will determine isc/result.h to be redundant,
# so only the ISC_R_ aspect has to be pointed out.
print "$file has <$dir/result.h> without \U$dir\E_R_\n";
next;
}
} else {
# There is an {foo}_R_; this is a necessary include.
next;
}
}
if ($elided eq "<$lib/lang.h>") {
if (! /^\U$lib\E_LANG_BEGINDECLS$/m) {
print "$file includes <$lib/lang.h> but " .
"has no \U$lib\E_LANG_BEGINDECLS\n";
} elsif (! /^\U$lib\E_LANG_ENDDECLS$/m) {
print "$file has \U$lib\E_LANG_BEGINDECLS but " .
"has no \U$lib\E_LANG_ENDDECLS\n";
} elsif (! /^$nocomment(?!#define)[a-z].*([a-zA-Z0-9]\()/m) {
print "$file has <$lib/lang.h> apparently not function declarations\n";
}
next;
}
if ($elided eq "<isc/eventclass.h>") {
if (! /$nocomment.*ISC_EVENTCLASS_/m) {
print "$file has <isc/eventclass.h> without ISC_EVENTCLASS_\n";
}
next;
}
if ($elided eq "<isc/resultclass.h>") {
if (! /$nocomment.*ISC_RESULTCLASS_/m) {
print "$file has <isc/resultclass.h> without ISC_RESULTCLASS_\n";
}
next;
}
if ($elided =~ "<(isc|dns)/types.h>") {
my $dir = $1;
if (! /^$nocomment.*$dir\_\S+\_t\s/m) {
print "$file has <$dir/types.h> but apparently no $dir\_*_t uses\n";
} elsif ($dir ne "isc" && m%^#include <isc/types.h>%m) {
print "$file has <$dir/types.h> and redundant <isc/types.h>\n";
}
# ... otherwise the types.h file is needed for the relevant _t types
# it defines, even if this header file accidentally picks it up by
# including another header that itself included types.h.
# So skip the elision test in any event.
# XXX would be good to test for files that need types.h but don't
# include it.
next;
}
if ($elided eq "<isc/boolean.h>") {
next if /^$nocomment.*ISC_(TRUE|FALSE|TF)\W/m;
}
if ($elided eq "<isc/platform.h>") {
if (! /^$nocomment.*ISC_PLATFORM_/m) {
print "$file has <isc/platform.h> but no ISC_PLATFORM_\n";
}
next;
}
if ($elided eq "<isc/magic.h>") {
if (! /^$nocomment.*ISC_MAGIC_VALID/m) {
print "$file has <isc/magic.h> but no ISC_MAGIC_VALID\n";
}
next;
}
open(TMP, "> $tmpfile");
print TMP "$body";
close(TMP);
print "$file elided $elided, compiling\n" if $debug;
if (compile($file, $tmpfile, $objfile) == 0) {
print "$file does not need $elided\n";
}
} continue {
$prefix .= $prefix_extend;
}
}
sub
compile() {
my ($original, $source, $objfile) = @_;
my $includes;
my $stderr = $debug ? "" : "2>/dev/null";
if ($original =~ m%lib/(isc|tests)/%) {
$includes = $isc_includes;
} elsif ($original =~ m%lib/dns/%) {
$includes = "$isc_includes $dns_includes";
} elsif ($original =~ m%lib/lwres/%) {
$includes = $lwres_includes;
} elsif ($original =~ m%lib/omapi/%) {
$includes = "$isc_includes $dns_includes $omapi_includes";
} else {
$includes = "";
}
system("cc $includes -c $source -o $objfile $stderr");
unlink($source, $objfile);
return ($?);
}