Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 65d508f

Browse files
committed
Suppress "unused variable" warnings with older versions of flex.
Versions of flex before 2.5.36 might generate code that results in an "unused variable" warning, when using %option reentrant. Historically we've worked around that by specifying -Wno-error, but that's an unsatisfying solution. The official "fix" for this was just to insert a dummy reference to the variable, so write a small perl script that edits the generated C code similarly. The MSVC side of this is untested, but the buildfarm should soon reveal if I broke that. Discussion: https://postgr.es/m/25456.1487437842@sss.pgh.pa.us
1 parent a3dc8e4 commit 65d508f

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

src/Makefile.global.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ TAS = @TAS@
627627
ifdef FLEX
628628
$(FLEX) $(if $(FLEX_NO_BACKUP),-b) $(FLEXFLAGS) -o'$@' $<
629629
@$(if $(FLEX_NO_BACKUP),if [ `wc -l <lex.backup` -eq 1 ]; then rm lex.backup; else echo "Scanner requires backup; see lex.backup." 1>&2; exit 1; fi)
630+
$(if $(FLEX_FIX_WARNING),$(PERL) $(top_srcdir)/src/tools/fix-flex-warning.pl '$@')
630631
else
631632
@$(missing) flex $< '$@'
632633
endif

src/backend/parser/Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ OBJS= analyze.o gram.o scan.o parser.o \
2020
include $(top_srcdir)/src/backend/common.mk
2121

2222

23-
# Latest flex causes warnings in this file.
24-
ifeq ($(GCC),yes)
25-
scan.o: CFLAGS += -Wno-error
26-
endif
27-
28-
2923
# There is no correct way to write a rule that generates two files.
3024
# Rules with two targets don't have that meaning, they are merely
3125
# shorthand for two otherwise separate rules. To be safe for parallel
@@ -41,6 +35,7 @@ gram.c: BISON_CHECK_CMD = $(PERL) $(srcdir)/check_keywords.pl $< $(top_srcdir)/s
4135

4236
scan.c: FLEXFLAGS = -CF -p -p
4337
scan.c: FLEX_NO_BACKUP=yes
38+
scan.c: FLEX_FIX_WARNING=yes
4439

4540

4641
# Force these dependencies to be known even without dependency info built:

src/bin/psql/Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
4141

4242
psqlscanslash.c: FLEXFLAGS = -Cfe -p -p
4343
psqlscanslash.c: FLEX_NO_BACKUP=yes
44-
45-
# Latest flex causes warnings in this file.
46-
ifeq ($(GCC),yes)
47-
psqlscanslash.o: CFLAGS += -Wno-error
48-
endif
44+
psqlscanslash.c: FLEX_FIX_WARNING=yes
4945

5046
distprep: sql_help.h psqlscanslash.c
5147

src/fe_utils/Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ libpgfeutils.a: $(OBJS)
2929

3030
psqlscan.c: FLEXFLAGS = -Cfe -p -p
3131
psqlscan.c: FLEX_NO_BACKUP=yes
32-
33-
# Latest flex causes warnings in this file.
34-
ifeq ($(GCC),yes)
35-
psqlscan.o: CFLAGS += -Wno-error
36-
endif
32+
psqlscan.c: FLEX_FIX_WARNING=yes
3733

3834
distprep: psqlscan.c
3935

src/tools/fix-flex-warning.pl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/perl -w
2+
#----------------------------------------------------------------------
3+
#
4+
# fix-flex-warning.pl
5+
#
6+
# flex versions before 2.5.36, with certain option combinations, produce
7+
# code that causes an "unused variable" warning. That's annoying, so
8+
# let's suppress it by inserting a dummy reference to the variable.
9+
# (That's exactly what 2.5.36 and later do ...)
10+
#
11+
# Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
12+
# Portions Copyright (c) 1994, Regents of the University of California
13+
#
14+
# src/tools/fix-flex-warning.pl
15+
#
16+
#----------------------------------------------------------------------
17+
18+
use strict;
19+
use warnings;
20+
21+
# Get command line argument.
22+
usage() if $#ARGV != 0;
23+
my $filename = shift;
24+
25+
# Suck in the whole file.
26+
local $/ = undef;
27+
my $cfile;
28+
open($cfile, $filename) || die "opening $filename for reading: $!";
29+
my $ccode = <$cfile>;
30+
close($cfile);
31+
32+
# No need to do anything if it's not flex 2.5.x for x < 36.
33+
exit 0 if $ccode !~ m/^#define YY_FLEX_MAJOR_VERSION 2$/m;
34+
exit 0 if $ccode !~ m/^#define YY_FLEX_MINOR_VERSION 5$/m;
35+
exit 0 if $ccode !~ m/^#define YY_FLEX_SUBMINOR_VERSION (\d+)$/m;
36+
exit 0 if $1 >= 36;
37+
38+
# Apply the desired patch.
39+
$ccode =~ s|(struct yyguts_t \* yyg = \(struct yyguts_t\*\)yyscanner; /\* This var may be unused depending upon options. \*/
40+
.*?)
41+
return yy_is_jam \? 0 : yy_current_state;
42+
|$1
43+
(void) yyg;
44+
return yy_is_jam ? 0 : yy_current_state;
45+
|s;
46+
47+
# Write the modified file back out.
48+
open($cfile, ">$filename") || die "opening $filename for writing: $!";
49+
print $cfile $ccode;
50+
close($cfile);
51+
52+
exit 0;
53+
54+
55+
sub usage
56+
{
57+
die <<EOM;
58+
Usage: fix-flex-warning.pl c-file-name
59+
60+
fix-flex-warning.pl modifies a flex output file to suppress
61+
an unused-variable warning that occurs with older flex versions.
62+
63+
Report bugs to <pgsql-bugs\@postgresql.org>.
64+
EOM
65+
}

src/tools/msvc/pgflex.pl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,29 @@
5151
system("flex $flexflags -o$output $input");
5252
if ($? == 0)
5353
{
54-
55-
# For non-reentrant scanners we need to fix up the yywrap macro definition
56-
# to keep the MS compiler happy.
57-
# For reentrant scanners (like the core scanner) we do not
58-
# need to (and must not) change the yywrap definition.
54+
# Check for "%option reentrant" in .l file.
5955
my $lfile;
6056
open($lfile, $input) || die "opening $input for reading: $!";
6157
my $lcode = <$lfile>;
6258
close($lfile);
63-
if ($lcode !~ /\%option\sreentrant/)
59+
if ($lcode =~ /\%option\sreentrant/)
60+
{
61+
# Reentrant scanners usually need a fix to prevent
62+
# "unused variable" warnings with older flex versions.
63+
system("perl src\\tools\\fix-flex-warning.pl $output");
64+
}
65+
else
6466
{
67+
# For non-reentrant scanners we need to fix up the yywrap
68+
# macro definition to keep the MS compiler happy.
69+
# For reentrant scanners (like the core scanner) we do not
70+
# need to (and must not) change the yywrap definition.
6571
my $cfile;
6672
open($cfile, $output) || die "opening $output for reading: $!";
6773
my $ccode = <$cfile>;
6874
close($cfile);
6975
$ccode =~ s/yywrap\(n\)/yywrap()/;
70-
open($cfile, ">$output") || die "opening $output for reading: $!";
76+
open($cfile, ">$output") || die "opening $output for writing: $!";
7177
print $cfile $ccode;
7278
close($cfile);
7379
}

0 commit comments

Comments
 (0)