summaryrefslogtreecommitdiffstats
path: root/src/tools/fix-old-flex-code.pl
blob: 8059cb56be0bb5a7234accd148e7242a0e25c453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl
#----------------------------------------------------------------------
#
# fix-old-flex-code.pl
#
# flex versions before 2.5.36, with certain option combinations, produce
# code that causes an "unused variable" warning.  That's annoying, so
# let's suppress it by inserting a dummy reference to the variable.
# (That's exactly what 2.5.36 and later do ...)
#
# Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
# src/tools/fix-old-flex-code.pl
#
#----------------------------------------------------------------------

use strict;
use warnings;

# Get command line argument.
usage() if $#ARGV != 0;
my $filename = shift;

# Suck in the whole file.
local $/ = undef;
my $cfile;
open($cfile, '<', $filename) || die "opening $filename for reading: $!";
my $ccode = <$cfile>;
close($cfile);

# No need to do anything if it's not flex 2.5.x for x < 36.
exit 0 if $ccode !~ m/^#define YY_FLEX_MAJOR_VERSION 2$/m;
exit 0 if $ccode !~ m/^#define YY_FLEX_MINOR_VERSION 5$/m;
exit 0 if $ccode !~ m/^#define YY_FLEX_SUBMINOR_VERSION (\d+)$/m;
exit 0 if $1 >= 36;

# Apply the desired patch.
$ccode =~
  s|(struct yyguts_t \* yyg = \(struct yyguts_t\*\)yyscanner; /\* This var may be unused depending upon options. \*/
.*?)
	return yy_is_jam \? 0 : yy_current_state;
|$1
	(void) yyg;
	return yy_is_jam ? 0 : yy_current_state;
|s;

# Write the modified file back out.
open($cfile, '>', $filename) || die "opening $filename for writing: $!";
print $cfile $ccode;
close($cfile);

exit 0;


sub usage
{
	die <<EOM;
Usage: fix-old-flex-code.pl c-file-name

fix-old-flex-code.pl modifies a flex output file to suppress
an unused-variable warning that occurs with older flex versions.

Report bugs to <pgsql-bugs\@lists.postgresql.org>.
EOM
}