~aglenyoung/+junk/postgres-9.3-dtrace

« back to all changes in this revision

Viewing changes to src/tools/pginclude/pgcheckdefines

  • Committer: Package Import Robot
  • Author(s): Martin Pitt, Christoph Berg, Martin Pitt
  • Date: 2013-06-26 15:13:32 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130626151332-p34yjpn0txbdsdzd
Tags: 9.3~beta2-1
[ Christoph Berg ]
* hurd-i386: Ignore testsuite failures so we have a working libpq5 (they
  don't implement semaphores so the server won't even start).
* Mark postgresql-9.3 as beta in the description, suggested by Joshua D.
  Drake.

[ Martin Pitt ]
* New upstream release 9.3 beta2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
# cause a lot of false-positive results.
40
40
#
41
41
open PIPE, "$FIND * -type f -name '*.c' |"
42
 
    or die "can't fork: $!";
43
 
while (<PIPE>) {
44
 
    chomp;
45
 
    push @cfiles, $_;
 
42
  or die "can't fork: $!";
 
43
while (<PIPE>)
 
44
{
 
45
        chomp;
 
46
        push @cfiles, $_;
46
47
}
47
48
close PIPE or die "$FIND failed: $!";
48
49
 
49
50
open PIPE, "$FIND * -type f -name '*.h' |"
50
 
    or die "can't fork: $!";
51
 
while (<PIPE>) {
52
 
    chomp;
53
 
    push @hfiles, $_ unless
54
 
        m|^src/include/port/| ||
55
 
        m|^src/backend/port/\w+/|;
 
51
  or die "can't fork: $!";
 
52
while (<PIPE>)
 
53
{
 
54
        chomp;
 
55
        push @hfiles, $_
 
56
          unless m|^src/include/port/|
 
57
                  || m|^src/backend/port/\w+/|;
56
58
}
57
59
close PIPE or die "$FIND failed: $!";
58
60
 
61
63
# a hash table.  To cover the possibility of multiple .h files defining
62
64
# the same symbol, we make each hash entry a hash of filenames.
63
65
#
64
 
foreach $hfile (@hfiles) {
65
 
    open HFILE, $hfile
66
 
        or die "can't open $hfile: $!";
67
 
    while (<HFILE>) {
68
 
        if (m/^\s*#\s*define\s+(\w+)/) {
69
 
            $defines{$1}{$hfile} = 1;
 
66
foreach $hfile (@hfiles)
 
67
{
 
68
        open HFILE, $hfile
 
69
          or die "can't open $hfile: $!";
 
70
        while (<HFILE>)
 
71
        {
 
72
                if (m/^\s*#\s*define\s+(\w+)/)
 
73
                {
 
74
                        $defines{$1}{$hfile} = 1;
 
75
                }
70
76
        }
71
 
    }
72
 
    close HFILE;
 
77
        close HFILE;
73
78
}
74
79
 
75
80
#
77
82
# files it #include's.  Then extract all the symbols it tests for defined-ness,
78
83
# and check each one against the previously built hashtable.
79
84
#
80
 
foreach $file (@hfiles, @cfiles) {
81
 
    ($fname, $fpath) = fileparse($file);
82
 
    chdir $fpath or die "can't chdir to $fpath: $!";
83
 
    #
84
 
    # Ask 'make' to parse the makefile so we can get the correct flags to
85
 
    # use.  CPPFLAGS in particular varies for each subdirectory.  If we are
86
 
    # processing a .h file, we might be in a subdirectory that has no
87
 
    # Makefile, in which case we have to fake it.  Note that there seems
88
 
    # no easy way to prevent make from recursing into subdirectories and
89
 
    # hence printing multiple definitions --- we keep the last one, which
90
 
    # should come from the current Makefile.
91
 
    #
92
 
    if (-f "Makefile" || -f "GNUmakefile") {
93
 
        $MAKECMD = "$MAKE -qp";
94
 
    } else {
95
 
        $subdir = $fpath;
96
 
        chop $subdir;
97
 
        $top_builddir = "..";
98
 
        $tmp = $fpath;
99
 
        while (($tmp = dirname($tmp)) ne '.') {
100
 
            $top_builddir = $top_builddir . "/..";
101
 
        }
102
 
        $MAKECMD = "$MAKE -qp 'subdir=$subdir' 'top_builddir=$top_builddir' -f '$top_builddir/src/Makefile.global'";
103
 
    }
104
 
    open PIPE, "$MAKECMD |"
105
 
        or die "can't fork: $!";
106
 
    while (<PIPE>) {
107
 
        if (m/^CPPFLAGS :?= (.*)/) {
108
 
            $CPPFLAGS = $1;
109
 
        } elsif (m/^CFLAGS :?= (.*)/) {
110
 
            $CFLAGS = $1;
111
 
        } elsif (m/^CFLAGS_SL :?= (.*)/) {
112
 
            $CFLAGS_SL = $1;
113
 
        } elsif (m/^PTHREAD_CFLAGS :?= (.*)/) {
114
 
            $PTHREAD_CFLAGS = $1;
115
 
        } elsif (m/^CC :?= (.*)/) {
116
 
            $CC = $1;
117
 
        }
118
 
    }
119
 
    # If make exits with status 1, it's not an error, it just means make
120
 
    # thinks some files may not be up-to-date.  Only complain on status 2.
121
 
    close PIPE;
122
 
    die "$MAKE failed in $fpath\n" if $? != 0 && $? != 256;
123
 
 
124
 
    # Expand out stuff that might be referenced in CFLAGS
125
 
    $CFLAGS =~ s/\$\(CFLAGS_SL\)/$CFLAGS_SL/;
126
 
    $CFLAGS =~ s/\$\(PTHREAD_CFLAGS\)/$PTHREAD_CFLAGS/;
127
 
 
128
 
    #
129
 
    # Run the compiler (which had better be gcc) to get the inclusions.
130
 
    # "gcc -H" reports inclusions on stderr as "... filename" where the
131
 
    # number of dots varies according to nesting depth.
132
 
    #
133
 
    @includes = ();
134
 
    $COMPILE = "$CC $CPPFLAGS $CFLAGS -H -E $fname";
135
 
    open PIPE, "$COMPILE 2>&1 >/dev/null |"
136
 
        or die "can't fork: $!";
137
 
    while (<PIPE>) {
138
 
        if (m/^\.+ (.*)/) {
139
 
            $include = $1;
140
 
            # Ignore system headers (absolute paths); but complain if a
141
 
            # .c file includes a system header before any PG header.
142
 
            if ($include =~ m|^/|) {
143
 
                warn "$file includes $include before any Postgres inclusion\n"
144
 
                    if $#includes == -1 && $file =~ m/\.c$/;
145
 
                next;
146
 
            }
147
 
            # Strip any "./" (assume this appears only at front)
148
 
            $include =~ s|^\./||;
149
 
            # Make path relative to top of tree
150
 
            $ipath = $fpath;
151
 
            while ($include =~ s|^\.\./||) {
152
 
                $ipath = dirname($ipath) . "/";
153
 
            }
154
 
            $ipath =~ s|^\./||;
155
 
            push @includes, $ipath . $include;
156
 
        } else {
157
 
            warn "$CC: $_";
158
 
        }
159
 
    }
160
 
    # The compiler might fail, particularly if we are checking a file that's
161
 
    # not supposed to be compiled at all on the current platform, so don't
162
 
    # quit on nonzero status.
163
 
    close PIPE or warn "$COMPILE failed in $fpath\n";
164
 
 
165
 
    #
166
 
    # Scan the file to find #ifdef, #ifndef, and #if defined() constructs
167
 
    # We assume #ifdef isn't continued across lines, and that defined(foo)
168
 
    # isn't split across lines either
169
 
    #
170
 
    open FILE, $fname
171
 
        or die "can't open $file: $!";
172
 
    $inif = 0;
173
 
    while (<FILE>) {
174
 
        $line = $_;
175
 
        if ($line =~ m/^\s*#\s*ifdef\s+(\w+)/) {
176
 
            $symbol = $1;
177
 
            &checkit;
178
 
        }
179
 
        if ($line =~ m/^\s*#\s*ifndef\s+(\w+)/) {
180
 
            $symbol = $1;
181
 
            &checkit;
182
 
        }
183
 
        if ($line =~ m/^\s*#\s*if\s+/) {
184
 
            $inif = 1;
185
 
        }
186
 
        if ($inif) {
187
 
            while ($line =~ s/\bdefined(\s+|\s*\(\s*)(\w+)//) {
188
 
                $symbol = $2;
189
 
                &checkit;
190
 
            }
191
 
            if (!($line =~ m/\\$/)) {
192
 
                $inif = 0;
193
 
            }
194
 
        }
195
 
    }
196
 
    close FILE;
197
 
 
198
 
    chdir $topdir or die "can't chdir to $topdir: $!";
 
85
foreach $file (@hfiles, @cfiles)
 
86
{
 
87
        ($fname, $fpath) = fileparse($file);
 
88
        chdir $fpath or die "can't chdir to $fpath: $!";
 
89
 
 
90
        #
 
91
        # Ask 'make' to parse the makefile so we can get the correct flags to
 
92
        # use.  CPPFLAGS in particular varies for each subdirectory.  If we are
 
93
        # processing a .h file, we might be in a subdirectory that has no
 
94
        # Makefile, in which case we have to fake it.  Note that there seems
 
95
        # no easy way to prevent make from recursing into subdirectories and
 
96
        # hence printing multiple definitions --- we keep the last one, which
 
97
        # should come from the current Makefile.
 
98
        #
 
99
        if (-f "Makefile" || -f "GNUmakefile")
 
100
        {
 
101
                $MAKECMD = "$MAKE -qp";
 
102
        }
 
103
        else
 
104
        {
 
105
                $subdir = $fpath;
 
106
                chop $subdir;
 
107
                $top_builddir = "..";
 
108
                $tmp          = $fpath;
 
109
                while (($tmp = dirname($tmp)) ne '.')
 
110
                {
 
111
                        $top_builddir = $top_builddir . "/..";
 
112
                }
 
113
                $MAKECMD =
 
114
"$MAKE -qp 'subdir=$subdir' 'top_builddir=$top_builddir' -f '$top_builddir/src/Makefile.global'";
 
115
        }
 
116
        open PIPE, "$MAKECMD |"
 
117
          or die "can't fork: $!";
 
118
        while (<PIPE>)
 
119
        {
 
120
                if (m/^CPPFLAGS :?= (.*)/)
 
121
                {
 
122
                        $CPPFLAGS = $1;
 
123
                }
 
124
                elsif (m/^CFLAGS :?= (.*)/)
 
125
                {
 
126
                        $CFLAGS = $1;
 
127
                }
 
128
                elsif (m/^CFLAGS_SL :?= (.*)/)
 
129
                {
 
130
                        $CFLAGS_SL = $1;
 
131
                }
 
132
                elsif (m/^PTHREAD_CFLAGS :?= (.*)/)
 
133
                {
 
134
                        $PTHREAD_CFLAGS = $1;
 
135
                }
 
136
                elsif (m/^CC :?= (.*)/)
 
137
                {
 
138
                        $CC = $1;
 
139
                }
 
140
        }
 
141
 
 
142
        # If make exits with status 1, it's not an error, it just means make
 
143
        # thinks some files may not be up-to-date.  Only complain on status 2.
 
144
        close PIPE;
 
145
        die "$MAKE failed in $fpath\n" if $? != 0 && $? != 256;
 
146
 
 
147
        # Expand out stuff that might be referenced in CFLAGS
 
148
        $CFLAGS =~ s/\$\(CFLAGS_SL\)/$CFLAGS_SL/;
 
149
        $CFLAGS =~ s/\$\(PTHREAD_CFLAGS\)/$PTHREAD_CFLAGS/;
 
150
 
 
151
        #
 
152
        # Run the compiler (which had better be gcc) to get the inclusions.
 
153
        # "gcc -H" reports inclusions on stderr as "... filename" where the
 
154
        # number of dots varies according to nesting depth.
 
155
        #
 
156
        @includes = ();
 
157
        $COMPILE  = "$CC $CPPFLAGS $CFLAGS -H -E $fname";
 
158
        open PIPE, "$COMPILE 2>&1 >/dev/null |"
 
159
          or die "can't fork: $!";
 
160
        while (<PIPE>)
 
161
        {
 
162
                if (m/^\.+ (.*)/)
 
163
                {
 
164
                        $include = $1;
 
165
 
 
166
                        # Ignore system headers (absolute paths); but complain if a
 
167
                        # .c file includes a system header before any PG header.
 
168
                        if ($include =~ m|^/|)
 
169
                        {
 
170
                                warn "$file includes $include before any Postgres inclusion\n"
 
171
                                  if $#includes == -1 && $file =~ m/\.c$/;
 
172
                                next;
 
173
                        }
 
174
 
 
175
                        # Strip any "./" (assume this appears only at front)
 
176
                        $include =~ s|^\./||;
 
177
 
 
178
                        # Make path relative to top of tree
 
179
                        $ipath = $fpath;
 
180
                        while ($include =~ s|^\.\./||)
 
181
                        {
 
182
                                $ipath = dirname($ipath) . "/";
 
183
                        }
 
184
                        $ipath =~ s|^\./||;
 
185
                        push @includes, $ipath . $include;
 
186
                }
 
187
                else
 
188
                {
 
189
                        warn "$CC: $_";
 
190
                }
 
191
        }
 
192
 
 
193
        # The compiler might fail, particularly if we are checking a file that's
 
194
        # not supposed to be compiled at all on the current platform, so don't
 
195
        # quit on nonzero status.
 
196
        close PIPE or warn "$COMPILE failed in $fpath\n";
 
197
 
 
198
        #
 
199
        # Scan the file to find #ifdef, #ifndef, and #if defined() constructs
 
200
        # We assume #ifdef isn't continued across lines, and that defined(foo)
 
201
        # isn't split across lines either
 
202
        #
 
203
        open FILE, $fname
 
204
          or die "can't open $file: $!";
 
205
        $inif = 0;
 
206
        while (<FILE>)
 
207
        {
 
208
                $line = $_;
 
209
                if ($line =~ m/^\s*#\s*ifdef\s+(\w+)/)
 
210
                {
 
211
                        $symbol = $1;
 
212
                        &checkit;
 
213
                }
 
214
                if ($line =~ m/^\s*#\s*ifndef\s+(\w+)/)
 
215
                {
 
216
                        $symbol = $1;
 
217
                        &checkit;
 
218
                }
 
219
                if ($line =~ m/^\s*#\s*if\s+/)
 
220
                {
 
221
                        $inif = 1;
 
222
                }
 
223
                if ($inif)
 
224
                {
 
225
                        while ($line =~ s/\bdefined(\s+|\s*\(\s*)(\w+)//)
 
226
                        {
 
227
                                $symbol = $2;
 
228
                                &checkit;
 
229
                        }
 
230
                        if (!($line =~ m/\\$/))
 
231
                        {
 
232
                                $inif = 0;
 
233
                        }
 
234
                }
 
235
        }
 
236
        close FILE;
 
237
 
 
238
        chdir $topdir or die "can't chdir to $topdir: $!";
199
239
}
200
240
 
201
241
exit 0;
202
242
 
203
243
# Check an is-defined reference
204
 
sub checkit {
205
 
    # Ignore if symbol isn't defined in any PG include files
206
 
    if (! defined $defines{$symbol}) {
207
 
        return;
208
 
    }
209
 
    #
210
 
    # Try to match source(s) of symbol to the inclusions of the current file
211
 
    # (including itself).  We consider it OK if any one matches.
212
 
    #
213
 
    # Note: these tests aren't bulletproof; in theory the inclusion might
214
 
    # occur after the use of the symbol.  Given our normal file layout,
215
 
    # however, the risk is minimal.
216
 
    #
217
 
    foreach $deffile (keys %{ $defines{$symbol} }) {
218
 
        return if $deffile eq $file;
219
 
        foreach $reffile (@includes) {
220
 
            return if $deffile eq $reffile;
221
 
        }
222
 
    }
223
 
    #
224
 
    # If current file is a .h file, it's OK for it to assume that one of the
225
 
    # base headers (postgres.h or postgres_fe.h) has been included.
226
 
    #
227
 
    if ($file =~ m/\.h$/) {
228
 
        foreach $deffile (keys %{ $defines{$symbol} }) {
229
 
            return if $deffile eq 'src/include/c.h';
230
 
            return if $deffile eq 'src/include/postgres.h';
231
 
            return if $deffile eq 'src/include/postgres_fe.h';
232
 
            return if $deffile eq 'src/include/pg_config.h';
233
 
            return if $deffile eq 'src/include/pg_config_manual.h';
234
 
        }
235
 
    }
236
 
    #
237
 
    @places = keys %{ $defines{$symbol} };
238
 
    print "$file references $symbol, defined in @places\n";
239
 
    # print "includes: @includes\n";
 
244
sub checkit
 
245
{
 
246
 
 
247
        # Ignore if symbol isn't defined in any PG include files
 
248
        if (!defined $defines{$symbol})
 
249
        {
 
250
                return;
 
251
        }
 
252
 
 
253
        #
 
254
        # Try to match source(s) of symbol to the inclusions of the current file
 
255
        # (including itself).  We consider it OK if any one matches.
 
256
        #
 
257
        # Note: these tests aren't bulletproof; in theory the inclusion might
 
258
        # occur after the use of the symbol.  Given our normal file layout,
 
259
        # however, the risk is minimal.
 
260
        #
 
261
        foreach $deffile (keys %{ $defines{$symbol} })
 
262
        {
 
263
                return if $deffile eq $file;
 
264
                foreach $reffile (@includes)
 
265
                {
 
266
                        return if $deffile eq $reffile;
 
267
                }
 
268
        }
 
269
 
 
270
        #
 
271
        # If current file is a .h file, it's OK for it to assume that one of the
 
272
        # base headers (postgres.h or postgres_fe.h) has been included.
 
273
        #
 
274
        if ($file =~ m/\.h$/)
 
275
        {
 
276
                foreach $deffile (keys %{ $defines{$symbol} })
 
277
                {
 
278
                        return if $deffile eq 'src/include/c.h';
 
279
                        return if $deffile eq 'src/include/postgres.h';
 
280
                        return if $deffile eq 'src/include/postgres_fe.h';
 
281
                        return if $deffile eq 'src/include/pg_config.h';
 
282
                        return if $deffile eq 'src/include/pg_config_manual.h';
 
283
                }
 
284
        }
 
285
 
 
286
        #
 
287
        @places = keys %{ $defines{$symbol} };
 
288
        print "$file references $symbol, defined in @places\n";
 
289
 
 
290
        # print "includes: @includes\n";
240
291
}