2020# src/tools/pginclude/pgcheckdefines
2121#
2222
23+ use strict;
24+
2325use Cwd;
2426use File::Basename;
2527
26- $topdir = cwd();
28+ my $topdir = cwd();
2729
2830# Programs to use
29- $FIND =" find" ;
30- $MAKE =" make" ;
31+ my $FIND =" find" ;
32+ my $MAKE =" make" ;
3133
3234#
3335# Build arrays of all the .c and .h files in the tree
@@ -38,6 +40,8 @@ $MAKE = "make";
3840# Including these .h files would clutter the list of define'd symbols and
3941# cause a lot of false-positive results.
4042#
43+ my (@cfiles ,@hfiles );
44+
4145open PIPE," $FIND * -type f -name '*.c' |"
4246or die " can't fork:$! " ;
4347while (<PIPE>)
@@ -63,7 +67,9 @@ close PIPE or die "$FIND failed: $!";
6367# a hash table. To cover the possibility of multiple .h files defining
6468# the same symbol, we make each hash entry a hash of filenames.
6569#
66- foreach $hfile (@hfiles )
70+ my %defines ;
71+
72+ foreach my $hfile (@hfiles )
6773{
6874open HFILE,$hfile
6975or die " can't open$hfile :$! " ;
@@ -82,9 +88,9 @@ foreach $hfile (@hfiles)
8288# files it #include's. Then extract all the symbols it tests for defined-ness,
8389# and check each one against the previously built hashtable.
8490#
85- foreach $file (@hfiles ,@cfiles )
91+ foreach my $file (@hfiles ,@cfiles )
8692{
87- ($fname ,$fpath ) = fileparse($file );
93+ my ($fname ,$fpath ) = fileparse($file );
8894chdir $fpath or die " can't chdir to$fpath :$! " ;
8995
9096#
@@ -96,23 +102,28 @@ foreach $file (@hfiles, @cfiles)
96102# hence printing multiple definitions --- we keep the last one, which
97103# should come from the current Makefile.
98104#
105+ my $MAKECMD ;
106+
99107if (-f " Makefile" ||-f " GNUmakefile" )
100108{
101109$MAKECMD =" $MAKE -qp" ;
102110}
103111else
104112{
105- $subdir =$fpath ;
113+ my $subdir =$fpath ;
106114chop $subdir ;
107- $top_builddir =" .." ;
108- $tmp =$fpath ;
115+ my $top_builddir =" .." ;
116+ my $tmp =$fpath ;
109117while (($tmp = dirname($tmp ))ne ' .' )
110118{
111119$top_builddir =$top_builddir ." /.." ;
112120}
113121$MAKECMD =
114122" $MAKE -qp 'subdir=$subdir ' 'top_builddir=$top_builddir ' -f '$top_builddir /src/Makefile.global'" ;
115123}
124+
125+ my ($CPPFLAGS ,$CFLAGS ,$CFLAGS_SL ,$PTHREAD_CFLAGS ,$CC );
126+
116127open PIPE," $MAKECMD |"
117128or die " can't fork:$! " ;
118129while (<PIPE>)
@@ -153,15 +164,15 @@ foreach $file (@hfiles, @cfiles)
153164# "gcc -H" reports inclusions on stderr as "... filename" where the
154165# number of dots varies according to nesting depth.
155166#
156- @includes = ();
157- $COMPILE =" $CC $CPPFLAGS $CFLAGS -H -E$fname " ;
167+ my @includes = ();
168+ my $COMPILE =" $CC $CPPFLAGS $CFLAGS -H -E$fname " ;
158169open PIPE," $COMPILE 2>&1 >/dev/null |"
159170or die " can't fork:$! " ;
160171while (<PIPE>)
161172{
162173if (m / ^\. + (.*)/ )
163174{
164- $include =$1 ;
175+ my $include =$1 ;
165176
166177# Ignore system headers (absolute paths); but complain if a
167178# .c file includes a system header before any PG header.
@@ -176,7 +187,7 @@ foreach $file (@hfiles, @cfiles)
176187$include =~s | ^\. /|| ;
177188
178189# Make path relative to top of tree
179- $ipath =$fpath ;
190+ my $ipath =$fpath ;
180191while ($include =~s | ^\.\. /|| )
181192{
182193$ipath = dirname($ipath ) ." /" ;
@@ -202,19 +213,17 @@ foreach $file (@hfiles, @cfiles)
202213#
203214open FILE,$fname
204215or die " can't open$file :$! " ;
205- $inif = 0;
216+ my $inif = 0;
206217while (<FILE>)
207218{
208- $line =$_ ;
219+ my $line =$_ ;
209220if ($line =~m / ^\s *#\s *ifdef\s +(\w +)/ )
210221{
211- $symbol =$1 ;
212- &checkit;
222+ checkit($file ,$1 ,@includes );
213223}
214224if ($line =~m / ^\s *#\s *ifndef\s +(\w +)/ )
215225{
216- $symbol =$1 ;
217- &checkit;
226+ checkit($file ,$1 ,@includes );
218227}
219228if ($line =~m / ^\s *#\s *if\s +/ )
220229{
@@ -224,8 +233,7 @@ foreach $file (@hfiles, @cfiles)
224233{
225234while ($line =~s /\b defined(\s +|\s *\(\s *)(\w +)// )
226235{
227- $symbol =$2 ;
228- &checkit;
236+ checkit($file ,$2 ,@includes );
229237}
230238if (!($line =~m /\\ $ / ))
231239{
@@ -243,6 +251,7 @@ exit 0;
243251# Check an is-defined reference
244252sub checkit
245253{
254+ my ($file ,$symbol ,@includes ) =@_ ;
246255
247256# Ignore if symbol isn't defined in any PG include files
248257if (!defined $defines {$symbol })
@@ -258,10 +267,10 @@ sub checkit
258267# occur after the use of the symbol. Given our normal file layout,
259268# however, the risk is minimal.
260269#
261- foreach $deffile (keys %{$defines {$symbol } })
270+ foreach my $deffile (keys %{$defines {$symbol } })
262271{
263272return if $deffile eq $file ;
264- foreach $reffile (@includes )
273+ foreach my $reffile (@includes )
265274{
266275return if $deffile eq $reffile ;
267276}
@@ -273,7 +282,7 @@ sub checkit
273282#
274283if ($file =~m /\. h$ / )
275284{
276- foreach $deffile (keys %{$defines {$symbol } })
285+ foreach my $deffile (keys %{$defines {$symbol } })
277286{
278287return if $deffile eq ' src/include/c.h' ;
279288return if $deffile eq ' src/include/postgres.h' ;
@@ -284,7 +293,7 @@ sub checkit
284293}
285294
286295#
287- @places =keys %{$defines {$symbol } };
296+ my @places =keys %{$defines {$symbol } };
288297print " $file references$symbol , defined in@places \n " ;
289298
290299# print "includes: @includes\n";