Update copyright for 2022
[pgsql.git] / src / tools / fix-old-flex-code.pl
blob8059cb56be0bb5a7234accd148e7242a0e25c453
1 #!/usr/bin/perl
2 #----------------------------------------------------------------------
4 # fix-old-flex-code.pl
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 ...)
11 # Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
12 # Portions Copyright (c) 1994, Regents of the University of California
14 # src/tools/fix-old-flex-code.pl
16 #----------------------------------------------------------------------
18 use strict;
19 use warnings;
21 # Get command line argument.
22 usage() if $#ARGV != 0;
23 my $filename = shift;
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);
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;
38 # Apply the desired patch.
39 $ccode =~
40 s|(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;
48 # Write the modified file back out.
49 open($cfile, '>', $filename) || die "opening $filename for writing: $!";
50 print $cfile $ccode;
51 close($cfile);
53 exit 0;
56 sub usage
58 die <<EOM;
59 Usage: fix-old-flex-code.pl c-file-name
61 fix-old-flex-code.pl modifies a flex output file to suppress
62 an unused-variable warning that occurs with older flex versions.
64 Report bugs to <pgsql-bugs\@lists.postgresql.org>.
65 EOM