20
20
# src/tools/pginclude/pgcheckdefines
21
21
#
22
22
23
+ use strict;
24
+
23
25
use Cwd;
24
26
use File::Basename;
25
27
26
- $topdir = cwd();
28
+ my $topdir = cwd();
27
29
28
30
# Programs to use
29
- $FIND = " find" ;
30
- $MAKE = " make" ;
31
+ my $FIND = " find" ;
32
+ my $MAKE = " make" ;
31
33
32
34
#
33
35
# Build arrays of all the .c and .h files in the tree
@@ -38,6 +40,8 @@ $MAKE = "make";
38
40
# Including these .h files would clutter the list of define'd symbols and
39
41
# cause a lot of false-positive results.
40
42
#
43
+ my (@cfiles , @hfiles );
44
+
41
45
open PIPE, " $FIND * -type f -name '*.c' |"
42
46
or die " can't fork: $! " ;
43
47
while (<PIPE>)
@@ -63,7 +67,9 @@ close PIPE or die "$FIND failed: $!";
63
67
# a hash table. To cover the possibility of multiple .h files defining
64
68
# the same symbol, we make each hash entry a hash of filenames.
65
69
#
66
- foreach $hfile (@hfiles )
70
+ my %defines ;
71
+
72
+ foreach my $hfile (@hfiles )
67
73
{
68
74
open HFILE, $hfile
69
75
or die " can't open $hfile : $! " ;
@@ -82,9 +88,9 @@ foreach $hfile (@hfiles)
82
88
# files it #include's. Then extract all the symbols it tests for defined-ness,
83
89
# and check each one against the previously built hashtable.
84
90
#
85
- foreach $file (@hfiles , @cfiles )
91
+ foreach my $file (@hfiles , @cfiles )
86
92
{
87
- ($fname , $fpath ) = fileparse($file );
93
+ my ($fname , $fpath ) = fileparse($file );
88
94
chdir $fpath or die " can't chdir to $fpath : $! " ;
89
95
90
96
#
@@ -96,23 +102,28 @@ foreach $file (@hfiles, @cfiles)
96
102
# hence printing multiple definitions --- we keep the last one, which
97
103
# should come from the current Makefile.
98
104
#
105
+ my $MAKECMD ;
106
+
99
107
if (-f " Makefile" || -f " GNUmakefile" )
100
108
{
101
109
$MAKECMD = " $MAKE -qp" ;
102
110
}
103
111
else
104
112
{
105
- $subdir = $fpath ;
113
+ my $subdir = $fpath ;
106
114
chop $subdir ;
107
- $top_builddir = " .." ;
108
- $tmp = $fpath ;
115
+ my $top_builddir = " .." ;
116
+ my $tmp = $fpath ;
109
117
while (($tmp = dirname($tmp )) ne ' .' )
110
118
{
111
119
$top_builddir = $top_builddir . " /.." ;
112
120
}
113
121
$MAKECMD =
114
122
" $MAKE -qp 'subdir=$subdir ' 'top_builddir=$top_builddir ' -f '$top_builddir /src/Makefile.global'" ;
115
123
}
124
+
125
+ my ($CPPFLAGS , $CFLAGS , $CFLAGS_SL , $PTHREAD_CFLAGS , $CC );
126
+
116
127
open PIPE, " $MAKECMD |"
117
128
or die " can't fork: $! " ;
118
129
while (<PIPE>)
@@ -153,15 +164,15 @@ foreach $file (@hfiles, @cfiles)
153
164
# "gcc -H" reports inclusions on stderr as "... filename" where the
154
165
# number of dots varies according to nesting depth.
155
166
#
156
- @includes = ();
157
- $COMPILE = " $CC $CPPFLAGS $CFLAGS -H -E $fname " ;
167
+ my @includes = ();
168
+ my $COMPILE = " $CC $CPPFLAGS $CFLAGS -H -E $fname " ;
158
169
open PIPE, " $COMPILE 2>&1 >/dev/null |"
159
170
or die " can't fork: $! " ;
160
171
while (<PIPE>)
161
172
{
162
173
if (m / ^\. + (.*)/ )
163
174
{
164
- $include = $1 ;
175
+ my $include = $1 ;
165
176
166
177
# Ignore system headers (absolute paths); but complain if a
167
178
# .c file includes a system header before any PG header.
@@ -176,7 +187,7 @@ foreach $file (@hfiles, @cfiles)
176
187
$include =~ s | ^\. /|| ;
177
188
178
189
# Make path relative to top of tree
179
- $ipath = $fpath ;
190
+ my $ipath = $fpath ;
180
191
while ($include =~ s | ^\.\. /|| )
181
192
{
182
193
$ipath = dirname($ipath ) . " /" ;
@@ -202,19 +213,17 @@ foreach $file (@hfiles, @cfiles)
202
213
#
203
214
open FILE, $fname
204
215
or die " can't open $file : $! " ;
205
- $inif = 0;
216
+ my $inif = 0;
206
217
while (<FILE>)
207
218
{
208
- $line = $_ ;
219
+ my $line = $_ ;
209
220
if ($line =~ m / ^\s *#\s *ifdef\s +(\w +)/ )
210
221
{
211
- $symbol = $1 ;
212
- &checkit;
222
+ checkit($file , $1 , @includes );
213
223
}
214
224
if ($line =~ m / ^\s *#\s *ifndef\s +(\w +)/ )
215
225
{
216
- $symbol = $1 ;
217
- &checkit;
226
+ checkit($file , $1 , @includes );
218
227
}
219
228
if ($line =~ m / ^\s *#\s *if\s +/ )
220
229
{
@@ -224,8 +233,7 @@ foreach $file (@hfiles, @cfiles)
224
233
{
225
234
while ($line =~ s /\b defined(\s +|\s *\(\s *)(\w +)// )
226
235
{
227
- $symbol = $2 ;
228
- &checkit;
236
+ checkit($file , $2 , @includes );
229
237
}
230
238
if (!($line =~ m /\\ $ / ))
231
239
{
@@ -243,6 +251,7 @@ exit 0;
243
251
# Check an is-defined reference
244
252
sub checkit
245
253
{
254
+ my ($file , $symbol , @includes ) = @_ ;
246
255
247
256
# Ignore if symbol isn't defined in any PG include files
248
257
if (!defined $defines {$symbol })
@@ -258,10 +267,10 @@ sub checkit
258
267
# occur after the use of the symbol. Given our normal file layout,
259
268
# however, the risk is minimal.
260
269
#
261
- foreach $deffile (keys %{ $defines {$symbol } })
270
+ foreach my $deffile (keys %{ $defines {$symbol } })
262
271
{
263
272
return if $deffile eq $file ;
264
- foreach $reffile (@includes )
273
+ foreach my $reffile (@includes )
265
274
{
266
275
return if $deffile eq $reffile ;
267
276
}
@@ -273,7 +282,7 @@ sub checkit
273
282
#
274
283
if ($file =~ m /\. h$ / )
275
284
{
276
- foreach $deffile (keys %{ $defines {$symbol } })
285
+ foreach my $deffile (keys %{ $defines {$symbol } })
277
286
{
278
287
return if $deffile eq ' src/include/c.h' ;
279
288
return if $deffile eq ' src/include/postgres.h' ;
@@ -284,7 +293,7 @@ sub checkit
284
293
}
285
294
286
295
#
287
- @places = keys %{ $defines {$symbol } };
296
+ my @places = keys %{ $defines {$symbol } };
288
297
print " $file references $symbol , defined in @places \n " ;
289
298
290
299
# print "includes: @includes\n";
0 commit comments