bison: add command line option to map file prefixes

Teaches bison about a new command line option, --file-prefix-map OLD=NEW
(based on the -ffile-prefix-map option from GCC) which causes it to
replace and file path of OLD in the text of the output file with NEW,
mainly for header guards and comments. The primary use of this is to
make builds reproducible with different input paths, and in particular
the debugging information produced when the source code is compiled. For
example, a distro may know that the bison source code will be located at
"/usr/src/bison" and thus can generate bison files that are reproducible
with the following command:

    bison --output=/build/bison/parse.c -d --file-prefix-map=/build/bison/=/usr/src/bison/ parse.y

Importantly, this will change the header guards and #line directives
from:

    #ifndef YY_BUILD_BISON_PARSE_H
    #line 100 "/build/bison/parse.h"

to

    #ifndef YY_USR_SRC_BISON_PARSE_H
    #line 100 "/usr/src/bison/parse.h"

which is reproducible.

See https://lists.gnu.org/r/bison-patches/2020-05/msg00016.html
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>

* src/files.h, src/files.c (spec_mapped_header_file)
(mapped_dir_prefix, map_file_name, add_prefix_map): New.
* src/getargs.c (-M, --file-prefix-map): New option.
* src/output.c (prepare): Define b4_mapped_dir_prefix and
b4_spec_header_file.
* src/scan-skel.l (@ofile@): Output the mapped file name.
* data/skeletons/glr.c, data/skeletons/glr.cc,
* data/skeletons/lalr1.cc, data/skeletons/location.cc,
* data/skeletons/yacc.c:
Adjust.
* doc/bison.texi: Document.
* tests/input.at, tests/output.at: Check.
This commit is contained in:
Joshua Watt
2020-05-23 20:26:17 -05:00
committed by Akim Demaille
parent 6ed813c122
commit dd878d1851
14 changed files with 294 additions and 28 deletions

View File

@@ -189,9 +189,9 @@ b4_glr_cc_if([],
[b4_output_begin([b4_spec_header_file])
b4_copyright([Skeleton interface for Bison GLR parsers in C],
[2002-2015, 2018-2020])[
]b4_cpp_guard_open([b4_spec_header_file])[
]b4_cpp_guard_open([b4_spec_mapped_header_file])[
]b4_shared_declarations[
]b4_cpp_guard_close([b4_spec_header_file])[
]b4_cpp_guard_close([b4_spec_mapped_header_file])[
]b4_output_end[
]])])

View File

@@ -370,9 +370,9 @@ b4_copyright([Skeleton interface for Bison GLR parsers in C++],
// C++ GLR parser skeleton written by Akim Demaille.
]b4_disclaimer[
]b4_cpp_guard_open([b4_spec_header_file])[
]b4_cpp_guard_open([b4_spec_mapped_header_file])[
]b4_shared_declarations[
]b4_cpp_guard_close([b4_spec_header_file])[
]b4_cpp_guard_close([b4_spec_mapped_header_file])[
]b4_output_end])
# Let glr.c (and b4_shared_declarations) believe that the user

View File

@@ -513,16 +513,16 @@ b4_defines_if(
b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++])
[
/**
** \file ]b4_spec_header_file[
** \file ]b4_spec_mapped_header_file[
** Define the ]b4_namespace_ref[::parser class.
*/
// C++ LALR(1) parser skeleton written by Akim Demaille.
]b4_disclaimer[
]b4_cpp_guard_open([b4_spec_header_file])[
]b4_cpp_guard_open([b4_spec_mapped_header_file])[
]b4_shared_declarations(hh)[
]b4_cpp_guard_close([b4_spec_header_file])[
]b4_cpp_guard_close([b4_spec_mapped_header_file])[
]b4_output_end[
]])

View File

@@ -48,7 +48,7 @@ m4_ifdef([b4_location_file],
["b4_location_file"])])
m4_define([b4_location_path],
b4_percent_define_get([[api.location.include]],
["b4_dir_prefix[]b4_location_file"]))
["b4_mapped_dir_prefix[]b4_location_file"]))
m4_define([b4_location_path],
m4_substr(m4_defn([b4_location_path]), 1, m4_eval(m4_len(m4_defn([b4_location_path])) - 2)))
])

View File

@@ -313,14 +313,14 @@ m4_define([b4_declare_yyparse],
# Declaration that might either go into the header (if --defines)
# or open coded in the parser body.
m4_define([b4_shared_declarations],
[b4_cpp_guard_open([b4_spec_header_file])[
[b4_cpp_guard_open([b4_spec_mapped_header_file])[
]b4_declare_yydebug[
]b4_percent_code_get([[requires]])[
]b4_token_enums_defines[
]b4_declare_yylstype[
]b4_declare_yyparse[
]b4_percent_code_get([[provides]])[
]b4_cpp_guard_close([b4_spec_header_file])[]dnl
]b4_cpp_guard_close([b4_spec_mapped_header_file])[]dnl
])