From 7365279fca30371b07e49bfa83a23ddc44cc3860 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 18 Jan 2018 13:17:37 +0000 Subject: [PATCH] Add ability to remap file names in __FILE__, etc (PR other/70268) This commit adds the -fmacro-prefix-map option that allows remapping of file names in __FILE__, __BASE_FILE__, and __builtin_FILE(), similar to how -fdebug-prefix-map allows to do the same for debug information. Additionally, it adds -ffile-prefix-map which can be used to specify both mappings with a single option (and, should we need to add more -f*-prefix-map options in the future, those as well). libcpp/ChangeLog: 2018-01-18 Boris Kolpackov PR other/70268 * include/cpplib.h (cpp_callbacks::remap_filename): New callback. * libcpp/macro.c (_cpp_builtin_macro_text): Call remap_filename for __FILE__ and __BASE_FILE__. gcc/ChangeLog: 2018-01-18 Boris Kolpackov PR other/70268 * common.opt: (-ffile-prefix-map): New option. * opts.c (common_handle_option): Defer it. * opts-global.c (handle_common_deferred_options): Handle it. * debug.h (remap_debug_filename, add_debug_prefix_map): Move to... * file-prefix-map.h: New file. (remap_debug_filename, add_debug_prefix_map): ...here. (add_macro_prefix_map, add_file_prefix_map, remap_macro_filename): New. * final.c (debug_prefix_map, add_debug_prefix_map remap_debug_filename): Move to... * file-prefix-map.c: New file. (file_prefix_map, add_prefix_map, remap_filename) ...here and rename, generalize, get rid of alloca(), use strrchr() instead of strchr(). (add_macro_prefix_map, add_debug_prefix_map, add_file_prefix_map): Implement in terms of add_prefix_map(). (remap_macro_filename, remap_debug_filename): Implement in term of remap_filename(). * Makefile.in (OBJS, PLUGIN_HEADERS): Add new files. * builtins.c (fold_builtin_FILE): Call remap_macro_filename(). * dbxout.c: Include file-prefix-map.h. * varasm.c: Likewise. * vmsdbgout.c: Likewise. * xcoffout.c: Likewise. * dwarf2out.c: Likewise plus omit new options from DW_AT_producer. * doc/cppopts.texi (-fmacro-prefix-map): Document. * doc/invoke.texi (-ffile-prefix-map): Document. (-fdebug-prefix-map): Update description. gcc/c-family/ChangeLog: 2018-01-18 Boris Kolpackov PR other/70268 * c-family/c.opt (-fmacro-prefix-map): New option. * c-family/c-opts.c (c_common_handle_option): Handle it. * c-family/c-lex.c (init_c_lex): Set remap_filename cpp callback. * c-family/c-ppoutput.c (init_pp_output): Likewise. gcc/testsuite/ChangeLog: 2018-01-18 Boris Kolpackov PR other/70268 * c-c++-common/ffile-prefix-map.c: New test. * c-c++-common/fmacro-prefix-map.c: New test. * c-c++-common/cpp/ffile-prefix-map.c: New test. * c-c++-common/cpp/fmacro-prefix-map.c: New test. From-SVN: r256847 --- gcc/ChangeLog | 32 ++++- gcc/Makefile.in | 5 +- gcc/builtins.c | 7 + gcc/c-family/ChangeLog | 16 ++- gcc/c-family/c-lex.c | 2 + gcc/c-family/c-opts.c | 5 + gcc/c-family/c-ppoutput.c | 2 + gcc/c-family/c.opt | 4 + gcc/common.opt | 6 +- gcc/dbxout.c | 1 + gcc/debug.h | 3 - gcc/doc/cppopts.texi | 12 +- gcc/doc/invoke.texi | 34 +++-- gcc/dwarf2out.c | 3 + gcc/file-prefix-map.c | 132 ++++++++++++++++++ gcc/file-prefix-map.h | 28 ++++ gcc/final.c | 72 +--------- gcc/opts-global.c | 5 + gcc/opts.c | 1 + gcc/testsuite/ChangeLog | 8 ++ .../c-c++-common/cpp/ffile-prefix-map.c | 5 + .../c-c++-common/cpp/fmacro-prefix-map.c | 5 + gcc/testsuite/c-c++-common/ffile-prefix-map.c | 12 ++ .../c-c++-common/fmacro-prefix-map.c | 12 ++ gcc/varasm.c | 1 + gcc/vmsdbgout.c | 1 + gcc/xcoffout.c | 1 + libcpp/ChangeLog | 15 +- libcpp/include/cpplib.h | 4 + libcpp/macro.c | 2 + 30 files changed, 340 insertions(+), 96 deletions(-) create mode 100644 gcc/file-prefix-map.c create mode 100644 gcc/file-prefix-map.h create mode 100644 gcc/testsuite/c-c++-common/cpp/ffile-prefix-map.c create mode 100644 gcc/testsuite/c-c++-common/cpp/fmacro-prefix-map.c create mode 100644 gcc/testsuite/c-c++-common/ffile-prefix-map.c create mode 100644 gcc/testsuite/c-c++-common/fmacro-prefix-map.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5eb37fa6f96..19116fb6244 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,33 @@ +2018-01-18 Boris Kolpackov + + PR other/70268 + * common.opt: (-ffile-prefix-map): New option. + * opts.c (common_handle_option): Defer it. + * opts-global.c (handle_common_deferred_options): Handle it. + * debug.h (remap_debug_filename, add_debug_prefix_map): Move to... + * file-prefix-map.h: New file. + (remap_debug_filename, add_debug_prefix_map): ...here. + (add_macro_prefix_map, add_file_prefix_map, remap_macro_filename): New. + * final.c (debug_prefix_map, add_debug_prefix_map + remap_debug_filename): Move to... + * file-prefix-map.c: New file. + (file_prefix_map, add_prefix_map, remap_filename) ...here and rename, + generalize, get rid of alloca(), use strrchr() instead of strchr(). + (add_macro_prefix_map, add_debug_prefix_map, add_file_prefix_map): + Implement in terms of add_prefix_map(). + (remap_macro_filename, remap_debug_filename): Implement in term of + remap_filename(). + * Makefile.in (OBJS, PLUGIN_HEADERS): Add new files. + * builtins.c (fold_builtin_FILE): Call remap_macro_filename(). + * dbxout.c: Include file-prefix-map.h. + * varasm.c: Likewise. + * vmsdbgout.c: Likewise. + * xcoffout.c: Likewise. + * dwarf2out.c: Likewise plus omit new options from DW_AT_producer. + * doc/cppopts.texi (-fmacro-prefix-map): Document. + * doc/invoke.texi (-ffile-prefix-map): Document. + (-fdebug-prefix-map): Update description. + 2018-01-18 Martin Liska * config/i386/i386.c (indirect_thunk_name): Document that also @@ -73,7 +103,7 @@ * config/aarch64/aarch64.md (movti_aarch64): Use Uti constraint. * config/aarch64/aarch64.c (aarch64_mov128_immediate): New function. - (aarch64_legitimate_constant_p): Just support CONST_DOUBLE + (aarch64_legitimate_constant_p): Just support CONST_DOUBLE SF/DF/TF mode to avoid creating illegal CONST_WIDE_INT immediates. * config/aarch64/aarch64-protos.h (aarch64_mov128_immediate): Add declaration. diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 374bf3edeb4..6c37e46f792 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1285,6 +1285,7 @@ OBJS = \ expmed.o \ expr.o \ fibonacci_heap.o \ + file-prefix-map.o \ final.o \ fixed-value.o \ fold-const.o \ @@ -3506,8 +3507,8 @@ PLUGIN_HEADERS = $(TREE_H) $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \ tree-ssa-loop-niter.h tree-ssa-ter.h tree-ssa-threadedge.h \ tree-ssa-threadupdate.h inchash.h wide-int.h signop.h hash-map.h \ hash-set.h dominance.h cfg.h cfgrtl.h cfganal.h cfgbuild.h cfgcleanup.h \ - lcm.h cfgloopmanip.h builtins.def chkp-builtins.def pass-instances.def \ - params.list + lcm.h cfgloopmanip.h file-prefix-map.h builtins.def chkp-builtins.def \ + pass-instances.def params.list # generate the 'build fragment' b-header-vars s-header-vars: Makefile diff --git a/gcc/builtins.c b/gcc/builtins.c index d9f1e476cb0..683c6ec6e5b 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -70,6 +70,7 @@ along with GCC; see the file COPYING3. If not see #include "case-cfn-macros.h" #include "gimple-fold.h" #include "intl.h" +#include "file-prefix-map.h" /* remap_macro_filename() */ struct target_builtins default_target_builtins; #if SWITCHABLE_TARGET @@ -8871,7 +8872,13 @@ static inline tree fold_builtin_FILE (location_t loc) { if (const char *fname = LOCATION_FILE (loc)) + { + /* The documentation says this builtin is equivalent to the preprocessor + __FILE__ macro so it appears appropriate to use the same file prefix + mappings. */ + fname = remap_macro_filename (fname); return build_string_literal (strlen (fname) + 1, fname); + } return build_string_literal (1, ""); } diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index d5012e251b4..7d293826e5d 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,11 @@ +2018-01-18 Boris Kolpackov + + PR other/70268 + * c-family/c.opt (-fmacro-prefix-map): New option. + * c-family/c-opts.c (c_common_handle_option): Handle it. + * c-family/c-lex.c (init_c_lex): Set remap_filename cpp callback. + * c-family/c-ppoutput.c (init_pp_output): Likewise. + 2018-01-17 David Malcolm PR c++/83814 @@ -119,7 +127,7 @@ 2017-12-14 Qing Zhao - PR middle_end/79538 + PR middle_end/79538 * c-cppbuiltin.c (builtin_define_with_hex_fp_value): Adjust the size of buf1 and buf2, add a new buf to avoid format-overflow warning. @@ -463,7 +471,7 @@ (set_std_cxx17): ... this. (c_common_handle_option): Rename OPT_std_c__1z to OPT_std_c__17 and OPT_std_gnu__1z to OPT_std_gnu__17. Adjust set_std_cxx1z - caller. + caller. (c_common_post_options): Use cxx17 instead of cxx1z. Adjust comments. @@ -2430,7 +2438,7 @@ Matthias Klose * c-common.c (get_source_date_epoch): New function, gets the environment - variable SOURCE_DATE_EPOCH and parses it as long long with error + variable SOURCE_DATE_EPOCH and parses it as long long with error handling. * c-common.h (get_source_date_epoch): Prototype. * c-lex.c (c_lex_with_flags): set parse_in->source_date_epoch. @@ -2901,7 +2909,7 @@ 2015-11-12 James Norris Joseph Myers - * c-pragma.c (oacc_pragmas): Add entry for declare directive. + * c-pragma.c (oacc_pragmas): Add entry for declare directive. * c-pragma.h (enum pragma_kind): Add PRAGMA_OACC_DECLARE. (enum pragma_omp_clause): Add PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT and PRAGMA_OACC_CLAUSE_LINK. diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c index e82670f487c..a4ad0a30f38 100644 --- a/gcc/c-family/c-lex.c +++ b/gcc/c-family/c-lex.c @@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see #include "stor-layout.h" #include "c-pragma.h" #include "debug.h" +#include "file-prefix-map.h" /* remap_macro_filename() */ #include "attribs.h" @@ -82,6 +83,7 @@ init_c_lex (void) cb->has_attribute = c_common_has_attribute; cb->get_source_date_epoch = cb_get_source_date_epoch; cb->get_suggestion = cb_get_suggestion; + cb->remap_filename = remap_macro_filename; /* Set the debug callbacks if we can use them. */ if ((debug_info_level == DINFO_LEVEL_VERBOSE diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c index f84ecf4539c..3db01d453a6 100644 --- a/gcc/c-family/c-opts.c +++ b/gcc/c-family/c-opts.c @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3. If not see #include "plugin.h" /* For PLUGIN_INCLUDE_FILE event. */ #include "mkdeps.h" #include "dumpfile.h" +#include "file-prefix-map.h" /* add_*_prefix_map() */ #ifndef DOLLARS_IN_IDENTIFIERS # define DOLLARS_IN_IDENTIFIERS true @@ -448,6 +449,10 @@ c_common_handle_option (size_t scode, const char *arg, int value, cpp_opts->dollars_in_ident = value; break; + case OPT_fmacro_prefix_map_: + add_macro_prefix_map (arg); + break; + case OPT_ffreestanding: value = !value; /* Fall through. */ diff --git a/gcc/c-family/c-ppoutput.c b/gcc/c-family/c-ppoutput.c index 9d08776c187..8c525caf28d 100644 --- a/gcc/c-family/c-ppoutput.c +++ b/gcc/c-family/c-ppoutput.c @@ -22,6 +22,7 @@ #include "c-common.h" /* For flags. */ #include "../libcpp/internal.h" #include "c-pragma.h" /* For parse_in. */ +#include "file-prefix-map.h" /* remap_macro_filename() */ /* Encapsulates state used to convert a stream of tokens into a text file. */ @@ -151,6 +152,7 @@ init_pp_output (FILE *out_stream) cb->has_attribute = c_common_has_attribute; cb->get_source_date_epoch = cb_get_source_date_epoch; + cb->remap_filename = remap_macro_filename; /* Initialize the print structure. */ print.src_line = 1; diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 16a515afd55..9c7172607a7 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1390,6 +1390,10 @@ fdollars-in-identifiers C ObjC C++ ObjC++ Permit '$' as an identifier character. +fmacro-prefix-map= +C ObjC C++ ObjC++ Joined RejectNegative +-fmacro-prefix-map== Map one directory name to another in __FILE__, __BASE_FILE__, and __builtin_FILE(). + fdump-ada-spec C ObjC C++ ObjC++ RejectNegative Var(flag_dump_ada_spec) Write all declarations as Ada code transitively. diff --git a/gcc/common.opt b/gcc/common.opt index b20a9aac82e..874d0c2e72b 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1175,7 +1175,11 @@ Common RejectNegative Joined Var(common_deferred_options) Defer fdebug-prefix-map= Common Joined RejectNegative Var(common_deferred_options) Defer -Map one directory name to another in debug information. +-fdebug-prefix-map== Map one directory name to another in debug information. + +ffile-prefix-map= +Common Joined RejectNegative Var(common_deferred_options) Defer +-ffile-prefix-map== Map one directory name to another in compilation result. fdebug-types-section Common Report Var(flag_debug_types_section) Init(0) diff --git a/gcc/dbxout.c b/gcc/dbxout.c index 507fd41f9ea..a77e652d3d2 100644 --- a/gcc/dbxout.c +++ b/gcc/dbxout.c @@ -91,6 +91,7 @@ along with GCC; see the file COPYING3. If not see #include "common/common-target.h" #include "langhooks.h" #include "expr.h" +#include "file-prefix-map.h" /* remap_debug_filename() */ #ifdef XCOFF_DEBUGGING_INFO #include "xcoffout.h" diff --git a/gcc/debug.h b/gcc/debug.h index dc51182e4c2..e1dfe4befaa 100644 --- a/gcc/debug.h +++ b/gcc/debug.h @@ -249,9 +249,6 @@ extern bool dwarf2out_do_frame (void); extern bool dwarf2out_do_cfi_asm (void); extern void dwarf2out_switch_text_section (void); -const char *remap_debug_filename (const char *); -void add_debug_prefix_map (const char *); - /* For -fdump-go-spec. */ extern const struct gcc_debug_hooks * diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi index 6bfa46e1c32..7af15ddc617 100644 --- a/gcc/doc/cppopts.texi +++ b/gcc/doc/cppopts.texi @@ -287,6 +287,17 @@ When this option is given no argument, the default parameter value is Note that @code{-ftrack-macro-expansion=2} is activated by default. +@item -fmacro-prefix-map=@var{old}=@var{new} +@opindex fmacro-prefix-map +When preprocessing files residing in directory @file{@var{old}}, +expand the @code{__FILE__} and @code{__BASE_FILE__} macros as if the +files resided in directory @file{@var{new}} instead. This can be used +to change an absolute path to a relative path by using @file{.} for +@var{new} which can result in more reproducible builds that are +location independent. This option also affects +@code{__builtin_FILE()} during compilation. See also +@option{-ffile-prefix-map}. + @item -fexec-charset=@var{charset} @opindex fexec-charset @cindex character set, execution @@ -534,4 +545,3 @@ token in the output is preceded by the dump of the map its location belongs to. When used from GCC without @option{-E}, this option has no effect. - diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 98b73db17af..8c1d9b3c652 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -172,7 +172,8 @@ in the following sections. @gccoptlist{-c -S -E -o @var{file} -x @var{language} @gol -v -### --help@r{[}=@var{class}@r{[},@dots{}@r{]]} --target-help --version @gol -pass-exit-codes -pipe -specs=@var{file} -wrapper @gol -@@@var{file} -fplugin=@var{file} -fplugin-arg-@var{name}=@var{arg} @gol +@@@var{file} -ffile-prefix-map=@var{old}=@var{new} @gol +-fplugin=@var{file} -fplugin-arg-@var{name}=@var{arg} @gol -fdump-ada-spec@r{[}-slim@r{]} -fada-spec-parent=@var{unit} -fdump-go-spec=@var{file}} @item C Language Options @@ -481,9 +482,9 @@ Objective-C and Objective-C++ Dialects}. -dD -dI -dM -dN -dU @gol -fdebug-cpp -fdirectives-only -fdollars-in-identifiers @gol -fexec-charset=@var{charset} -fextended-identifiers @gol --finput-charset=@var{charset} -fno-canonical-system-headers @gol --fpch-deps -fpch-preprocess -fpreprocessed @gol --ftabstop=@var{width} -ftrack-macro-expansion @gol +-finput-charset=@var{charset} -fmacro-prefix-map=@var{old}=@var{new} @gol +-fno-canonical-system-headers @gol -fpch-deps -fpch-preprocess @gol +-fpreprocessed -ftabstop=@var{width} -ftrack-macro-expansion @gol -fwide-exec-charset=@var{charset} -fworking-directory @gol -H -imacros @var{file} -include @var{file} @gol -M -MD -MF -MG -MM -MMD -MP -MQ -MT @gol @@ -1660,6 +1661,16 @@ This invokes all subprograms of @command{gcc} under @samp{gdb --args}, thus the invocation of @command{cc1} is @samp{gdb --args cc1 @dots{}}. +@item -ffile-prefix-map=@var{old}=@var{new} +@opindex ffile-prefix-map +When compiling files residing in directory @file{@var{old}}, record +any references to them in the result of the compilation as if the +files resided in directory @file{@var{new}} instead. Specifying this +option is equivalent to specifying all the individual +@option{-f*-prefix-map} options. This can be used to make reproducible +builds that are location independent. See also +@option{-fmacro-prefix-map} and @option{-fdebug-prefix-map}. + @item -fplugin=@var{name}.so @opindex fplugin Load the plugin code in file @var{name}.so, assumed to be a @@ -7103,13 +7114,14 @@ link processing time. Merging is enabled by default. @item -fdebug-prefix-map=@var{old}=@var{new} @opindex fdebug-prefix-map -When compiling files in directory @file{@var{old}}, record debugging -information describing them as in @file{@var{new}} instead. This can be -used to replace a build-time path with an install-time path in the debug info. -It can also be used to change an absolute path to a relative path by using -@file{.} for @var{new}. This can give more reproducible builds, which are -location independent, but may require an extra command to tell GDB where to -find the source files. +When compiling files residing in directory @file{@var{old}}, record +debugging information describing them as if the files resided in +directory @file{@var{new}} instead. This can be used to replace a +build-time path with an install-time path in the debug info. It can +also be used to change an absolute path to a relative path by using +@file{.} for @var{new}. This can give more reproducible builds, which +are location independent, but may require an extra command to tell GDB +where to find the source files. See also @option{-ffile-prefix-map}. @item -fvar-tracking @opindex fvar-tracking diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index 0cc05cb5b87..948b3cbe591 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -95,6 +95,7 @@ along with GCC; see the file COPYING3. If not see #include "rtl-iter.h" #include "stringpool.h" #include "attribs.h" +#include "file-prefix-map.h" /* remap_debug_filename() */ static void dwarf2out_source_line (unsigned int, unsigned int, const char *, int, bool); @@ -23535,6 +23536,8 @@ gen_producer_string (void) case OPT_fltrans_output_list_: case OPT_fresolution_: case OPT_fdebug_prefix_map_: + case OPT_fmacro_prefix_map_: + case OPT_ffile_prefix_map_: case OPT_fcompare_debug: /* Ignore these. */ continue; diff --git a/gcc/file-prefix-map.c b/gcc/file-prefix-map.c new file mode 100644 index 00000000000..88248146ab6 --- /dev/null +++ b/gcc/file-prefix-map.c @@ -0,0 +1,132 @@ +/* Implementation of file prefix remapping support (-f*-prefix-map options). + Copyright (C) 2017 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING3. If not see + . */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "diagnostic.h" +#include "file-prefix-map.h" + +/* Structure recording the mapping from source file and directory names at + compile time to those to be embedded in the compilation result (debug + information, the __FILE__ macro expansion, etc). */ +struct file_prefix_map +{ + const char *old_prefix; + const char *new_prefix; + size_t old_len; + size_t new_len; + struct file_prefix_map *next; +}; + +/* Record a file prefix mapping in the specified map. ARG is the argument to + -f*-prefix-map and must be of the form OLD=NEW. OPT is the option name + for diagnostics. */ +static void +add_prefix_map (file_prefix_map *&maps, const char *arg, const char *opt) +{ + file_prefix_map *map; + const char *p; + + /* Note: looking for the last '='. The thinking is we can control the paths + inside our projects but not where the users build them. */ + p = strrchr (arg, '='); + if (!p) + { + error ("invalid argument %qs to %qs", arg, opt); + return; + } + map = XNEW (file_prefix_map); + map->old_prefix = xstrndup (arg, p - arg); + map->old_len = p - arg; + p++; + map->new_prefix = xstrdup (p); + map->new_len = strlen (p); + map->next = maps; + maps = map; +} + +/* Perform user-specified mapping of filename prefixes. Return the + GC-allocated new name corresponding to FILENAME or FILENAME if no + remapping was performed. */ + +static const char * +remap_filename (file_prefix_map *maps, const char *filename) +{ + file_prefix_map *map; + char *s; + const char *name; + size_t name_len; + + for (map = maps; map; map = map->next) + if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0) + break; + if (!map) + return filename; + name = filename + map->old_len; + name_len = strlen (name) + 1; + + s = (char *) ggc_alloc_atomic (name_len + map->new_len); + memcpy (s, map->new_prefix, map->new_len); + memcpy (s + map->new_len, name, name_len); + return s; +} + +/* NOTE: if adding another -f*-prefix-map option then don't forget to + ignore it in DW_AT_producer (dwarf2out.c). */ + +/* Linked lists of file_prefix_map structures. */ +static file_prefix_map *macro_prefix_maps; /* -fmacro-prefix-map */ +static file_prefix_map *debug_prefix_maps; /* -fdebug-prefix-map */ + +/* Record a file prefix mapping for -fmacro-prefix-map. */ +void +add_macro_prefix_map (const char *arg) +{ + add_prefix_map (macro_prefix_maps, arg, "-fmacro-prefix-map"); +} + +/* Record a file prefix mapping for -fdebug-prefix-map. */ +void +add_debug_prefix_map (const char *arg) +{ + add_prefix_map (debug_prefix_maps, arg, "-fdebug-prefix-map"); +} + +/* Record a file prefix mapping for all -f*-prefix-map. */ +void +add_file_prefix_map (const char *arg) +{ + add_prefix_map (macro_prefix_maps, arg, "-ffile-prefix-map"); + add_prefix_map (debug_prefix_maps, arg, "-ffile-prefix-map"); +} + +/* Remap using -fmacro-prefix-map. Return the GC-allocated new name + corresponding to FILENAME or FILENAME if no remapping was performed. */ +const char * +remap_macro_filename (const char *filename) +{ + return remap_filename (macro_prefix_maps, filename); +} + +/* Remap using -fdebug-prefix-map. Return the GC-allocated new name + corresponding to FILENAME or FILENAME if no remapping was performed. */ +const char * +remap_debug_filename (const char *filename) +{ + return remap_filename (debug_prefix_maps, filename); +} diff --git a/gcc/file-prefix-map.h b/gcc/file-prefix-map.h new file mode 100644 index 00000000000..3309eeed646 --- /dev/null +++ b/gcc/file-prefix-map.h @@ -0,0 +1,28 @@ +/* Declarations for file prefix remapping support (-f*-prefix-map options). + Copyright (C) 2017 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING3. If not see + . */ + +#ifndef GCC_FILE_PREFIX_MAP_H +#define GCC_FILE_PREFIX_MAP_H + +void add_macro_prefix_map (const char *); +void add_debug_prefix_map (const char *); +void add_file_prefix_map (const char *); + +const char *remap_macro_filename (const char *); +const char *remap_debug_filename (const char *); + +#endif /* !GCC_FILE_PREFIX_MAP_H */ diff --git a/gcc/final.c b/gcc/final.c index db3095c5526..578e5d6af4c 100644 --- a/gcc/final.c +++ b/gcc/final.c @@ -196,7 +196,7 @@ static int dialect_number; /* Nonnull if the insn currently being emitted was a COND_EXEC pattern. */ rtx current_insn_predicate; -/* True if printing into -fdump-final-insns= dump. */ +/* True if printing into -fdump-final-insns= dump. */ bool final_insns_dump_p; /* True if profile_function should be called, but hasn't been called yet. */ @@ -760,7 +760,7 @@ compute_alignments (void) if (!has_fallthru && (branch_count > count_threshold || (bb->count > bb->prev_bb->count.apply_scale (10, 1) - && (bb->prev_bb->count + && (bb->prev_bb->count <= ENTRY_BLOCK_PTR_FOR_FN (cfun) ->count.apply_scale (1, 2))))) { @@ -804,7 +804,7 @@ compute_alignments (void) /* Grow the LABEL_ALIGN array after new labels are created. */ -static void +static void grow_label_align (void) { int old = max_labelno; @@ -1507,72 +1507,6 @@ asm_str_count (const char *templ) return count; } -/* ??? This is probably the wrong place for these. */ -/* Structure recording the mapping from source file and directory - names at compile time to those to be embedded in debug - information. */ -struct debug_prefix_map -{ - const char *old_prefix; - const char *new_prefix; - size_t old_len; - size_t new_len; - struct debug_prefix_map *next; -}; - -/* Linked list of such structures. */ -static debug_prefix_map *debug_prefix_maps; - - -/* Record a debug file prefix mapping. ARG is the argument to - -fdebug-prefix-map and must be of the form OLD=NEW. */ - -void -add_debug_prefix_map (const char *arg) -{ - debug_prefix_map *map; - const char *p; - - p = strchr (arg, '='); - if (!p) - { - error ("invalid argument %qs to -fdebug-prefix-map", arg); - return; - } - map = XNEW (debug_prefix_map); - map->old_prefix = xstrndup (arg, p - arg); - map->old_len = p - arg; - p++; - map->new_prefix = xstrdup (p); - map->new_len = strlen (p); - map->next = debug_prefix_maps; - debug_prefix_maps = map; -} - -/* Perform user-specified mapping of debug filename prefixes. Return - the new name corresponding to FILENAME. */ - -const char * -remap_debug_filename (const char *filename) -{ - debug_prefix_map *map; - char *s; - const char *name; - size_t name_len; - - for (map = debug_prefix_maps; map; map = map->next) - if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0) - break; - if (!map) - return filename; - name = filename + map->old_len; - name_len = strlen (name) + 1; - s = (char *) alloca (name_len + map->new_len); - memcpy (s, map->new_prefix, map->new_len); - memcpy (s + map->new_len, name, name_len); - return ggc_strdup (s); -} - /* Return true if DWARF2 debug info can be emitted for DECL. */ static bool diff --git a/gcc/opts-global.c b/gcc/opts-global.c index d2653dde715..f33c9da4c3f 100644 --- a/gcc/opts-global.c +++ b/gcc/opts-global.c @@ -38,6 +38,7 @@ along with GCC; see the file COPYING3. If not see #include "stringpool.h" #include "attribs.h" #include "asan.h" +#include "file-prefix-map.h" /* add_*_prefix_map() */ typedef const char *const_char_p; /* For DEF_VEC_P. */ @@ -365,6 +366,10 @@ handle_common_deferred_options (void) add_debug_prefix_map (opt->arg); break; + case OPT_ffile_prefix_map_: + add_file_prefix_map (opt->arg); + break; + case OPT_fdump_: if (!g->get_dumps ()->dump_switch_p (opt->arg)) error ("unrecognized command line option %<-fdump-%s%>", opt->arg); diff --git a/gcc/opts.c b/gcc/opts.c index b1b6a325d09..f2795f98bf4 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -2103,6 +2103,7 @@ common_handle_option (struct gcc_options *opts, break; case OPT_fdebug_prefix_map_: + case OPT_ffile_prefix_map_: /* Deferred. */ break; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f20400de97b..0ae3871d089 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2018-01-18 Boris Kolpackov + + PR other/70268 + * c-c++-common/ffile-prefix-map.c: New test. + * c-c++-common/fmacro-prefix-map.c: New test. + * c-c++-common/cpp/ffile-prefix-map.c: New test. + * c-c++-common/cpp/fmacro-prefix-map.c: New test. + 2018-01-18 Martin Liska * gcc.target/i386/indirect-thunk-3.c: Remove duplicate options. diff --git a/gcc/testsuite/c-c++-common/cpp/ffile-prefix-map.c b/gcc/testsuite/c-c++-common/cpp/ffile-prefix-map.c new file mode 100644 index 00000000000..294875b717d --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/ffile-prefix-map.c @@ -0,0 +1,5 @@ +/* { dg-do compile } */ +/* { dg-options "-ffile-prefix-map==FILE-PREFIX" } */ + +#pragma message "FILE starts with " __FILE__ /* { dg-message "FILE starts with FILE-PREFIX" } */ +#pragma message "BASE_FILE starts with " __BASE_FILE__ /* { dg-message "BASE_FILE starts with FILE-PREFIX" } */ diff --git a/gcc/testsuite/c-c++-common/cpp/fmacro-prefix-map.c b/gcc/testsuite/c-c++-common/cpp/fmacro-prefix-map.c new file mode 100644 index 00000000000..43dc4bb287e --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/fmacro-prefix-map.c @@ -0,0 +1,5 @@ +/* { dg-do compile } */ +/* { dg-options "-fmacro-prefix-map==MACRO-PREFIX" } */ + +#pragma message "FILE starts with " __FILE__ /* { dg-message "FILE starts with MACRO-PREFIX" } */ +#pragma message "BASE_FILE starts with " __BASE_FILE__ /* { dg-message "BASE_FILE starts with MACRO-PREFIX" } */ diff --git a/gcc/testsuite/c-c++-common/ffile-prefix-map.c b/gcc/testsuite/c-c++-common/ffile-prefix-map.c new file mode 100644 index 00000000000..88c29ca829b --- /dev/null +++ b/gcc/testsuite/c-c++-common/ffile-prefix-map.c @@ -0,0 +1,12 @@ +/* Test __builtin_FILE(). */ +/* { dg-do run } */ +/* { dg-options "-ffile-prefix-map==FILE-PREFIX" } */ + +#include + +int main () +{ + printf ("__builtin_FILE starts with %s\n", __builtin_FILE ()); +} + +/* { dg-output "__builtin_FILE starts with FILE-PREFIX" } */ diff --git a/gcc/testsuite/c-c++-common/fmacro-prefix-map.c b/gcc/testsuite/c-c++-common/fmacro-prefix-map.c new file mode 100644 index 00000000000..db51587cb4f --- /dev/null +++ b/gcc/testsuite/c-c++-common/fmacro-prefix-map.c @@ -0,0 +1,12 @@ +/* Test __builtin_FILE(). */ +/* { dg-do run } */ +/* { dg-options "-fmacro-prefix-map==MACRO-PREFIX" } */ + +#include + +int main () +{ + printf ("__builtin_FILE starts with %s\n", __builtin_FILE ()); +} + +/* { dg-output "__builtin_FILE starts with MACRO-PREFIX" } */ diff --git a/gcc/varasm.c b/gcc/varasm.c index ea79893a547..b045efaabf5 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -55,6 +55,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "asan.h" #include "rtl-iter.h" +#include "file-prefix-map.h" /* remap_debug_filename() */ #ifdef XCOFF_DEBUGGING_INFO #include "xcoffout.h" /* Needed for external data declarations. */ diff --git a/gcc/vmsdbgout.c b/gcc/vmsdbgout.c index a940d32b234..ad0eac2e7d0 100644 --- a/gcc/vmsdbgout.c +++ b/gcc/vmsdbgout.c @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see #include "langhooks.h" #include "function.h" #include "target.h" +#include "file-prefix-map.h" /* remap_debug_filename() */ /* Difference in seconds between the VMS Epoch and the Unix Epoch */ static const long long vms_epoch_offset = 3506716800ll; diff --git a/gcc/xcoffout.c b/gcc/xcoffout.c index 591c5900027..53156a70fcb 100644 --- a/gcc/xcoffout.c +++ b/gcc/xcoffout.c @@ -32,6 +32,7 @@ along with GCC; see the file COPYING3. If not see #include "varasm.h" #include "output.h" #include "debug.h" +#include "file-prefix-map.h" /* remap_debug_filename() */ #ifdef XCOFF_DEBUGGING_INFO diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index b69e3645bad..5d2aa26b7bf 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,10 @@ +2018-01-18 Boris Kolpackov + + PR other/70268 + * include/cpplib.h (cpp_callbacks::remap_filename): New callback. + * libcpp/macro.c (_cpp_builtin_macro_text): Call remap_filename for + __FILE__ and __BASE_FILE__. + 2018-01-10 Kelvin Nilsen * lex.c (search_line_fast): Remove illegal coercion of an @@ -19,7 +26,7 @@ 2017-12-14 Bernd Edlinger * internal.h (maybe_print_line): Change signature. - + 2017-12-05 Jakub Jelinek PR c++/79228 @@ -714,9 +721,9 @@ * init.c (cpp_init_source_date_epoch): New function. * internal.h: Added source_date_epoch variable to struct cpp_reader to store a reproducible date. - * macro.c (_cpp_builtin_macro_text): Set pfile->date timestamp from - pfile->source_date_epoch instead of localtime if source_date_epoch is - set, to be used for __DATE__ and __TIME__ macros to help reproducible + * macro.c (_cpp_builtin_macro_text): Set pfile->date timestamp from + pfile->source_date_epoch instead of localtime if source_date_epoch is + set, to be used for __DATE__ and __TIME__ macros to help reproducible builds. 2016-04-13 Bernd Schmidt diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 7cc40c6d0d4..9814d0d862b 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -622,6 +622,10 @@ struct cpp_callbacks C++-style comments it does not include the terminating newline. */ void (*comment) (cpp_reader *, source_location, const unsigned char *, size_t); + + /* Callback for filename remapping in __FILE__ and __BASE_FILE__ macro + expansions. */ + const char *(*remap_filename) (const char*); }; #ifdef VMS diff --git a/libcpp/macro.c b/libcpp/macro.c index 0f9f0632edd..c5f3ffde722 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -450,6 +450,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node, if (!name) abort (); } + if (pfile->cb.remap_filename) + name = pfile->cb.remap_filename (name); len = strlen (name); buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); result = buf; -- 2.30.2