1#!/usr/bin/perl -w
2#----------------------------------------------------------------------
3#
4# fix-old-flex-code.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-old-flex-code.pl
15#
16#----------------------------------------------------------------------
17
18use strict;
19use warnings;
20
21# Get command line argument.
22usage() if $#ARGV != 0;
23my $filename = shift;
24
25# Suck in the whole file.
26local $/ = undef;
27my $cfile;
28open($cfile, '<', $filename) || die "opening $filename for reading: $!";
29my $ccode = <$cfile>;
30close($cfile);
31
32# No need to do anything if it's not flex 2.5.x for x < 36.
33exit 0 if $ccode !~ m/^#define YY_FLEX_MAJOR_VERSION 2$/m;
34exit 0 if $ccode !~ m/^#define YY_FLEX_MINOR_VERSION 5$/m;
35exit 0 if $ccode !~ m/^#define YY_FLEX_SUBMINOR_VERSION (\d+)$/m;
36exit 0 if $1 >= 36;
37
38# Apply the desired patch.
39$ccode =~
40s|(struct yyguts_t \* yyg = \(struct yyguts_t\*\)yyscanner; /\* This var may be unused depending upon options. \*/
41.*?)
42	return yy_is_jam \? 0 : yy_current_state;
43|$1
44	(void) yyg;
45	return yy_is_jam ? 0 : yy_current_state;
46|s;
47
48# Write the modified file back out.
49open($cfile, '>', $filename) || die "opening $filename for writing: $!";
50print $cfile $ccode;
51close($cfile);
52
53exit 0;
54
55
56sub usage
57{
58	die <<EOM;
59Usage: fix-old-flex-code.pl c-file-name
60
61fix-old-flex-code.pl modifies a flex output file to suppress
62an unused-variable warning that occurs with older flex versions.
63
64Report bugs to <pgsql-bugs\@postgresql.org>.
65EOM
66}
67