In many setups, especially when CI and/or some meta-build system like Yocto or buildroot, is involved, gcc ends up being invoked using absolute path names, which are often long and uninteresting.
That amounts to a lot of noise both when trying to decipher the warning or error, when the warning text is copy-pasted to a commit fixing the issue, or posted to a mailing list. In the latter case, the path might also reveal details that should not be public; as a made-up example, seeing the string /home/ravi/customers/acme-corp/yocto/tmp/work/bird-spa/ would make Road Runner wary of any "free plumage treatment" sign. Removing the prefixes manually is tedious and error-prone. So similar to the other f*-prefix-map options, provide an option allowing one to strip/remap certain prefixes when emitting diagnostics. [Of course, one still has to be very careful whenever there might be confidential information in the messages, but having the prefix likely to contain customer or project names removed automatically is helpful.] --- This is mostly just a POC, to ask if something like this could be implemented. It obviously lacks documentation and tests. I've tested this very lightly, and it works as expected for the simple cases I've tried. But I don't know if I managed to find all the locations that would need to call remap_diag_filename(). To make linking work, I had to do a bit of juggling, moving file-prefix-map.o to OBJS-libcommon in order to make that function available to the diagnostic*.cc files, and then also move the definition of flag_canon_prefix_map to file-prefix-map.cc. It builds for me, but maybe it's broken in some way I don't know about. gcc/Makefile.in | 2 +- gcc/common.opt | 4 ++++ gcc/diagnostic-format-text.cc | 3 +++ gcc/diagnostic.cc | 2 ++ gcc/file-prefix-map.cc | 20 ++++++++++++++++++++ gcc/file-prefix-map.h | 2 ++ gcc/lto-opts.cc | 1 + gcc/opts-global.cc | 4 ++++ gcc/opts.cc | 5 ++--- 9 files changed, 39 insertions(+), 4 deletions(-) diff --git a/gcc/Makefile.in b/gcc/Makefile.in index ebfcd8a8a0d..5d3cd24a440 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1472,7 +1472,6 @@ OBJS = \ expr.o \ ext-dce.o \ fibonacci_heap.o \ - file-prefix-map.o \ final.o \ fixed-value.o \ fold-const.o \ @@ -1857,6 +1856,7 @@ OBJS-libcommon = diagnostic-spec.o diagnostic.o diagnostic-color.o \ diagnostic-path.o \ diagnostic-show-locus.o \ edit-context.o \ + file-prefix-map.o \ pretty-print.o intl.o \ json.o json-parsing.o \ sbitmap.o \ diff --git a/gcc/common.opt b/gcc/common.opt index b9e74cd3ac4..46e043878b4 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1336,6 +1336,10 @@ fdebug-prefix-map= Common Joined RejectNegative Var(common_deferred_options) Defer -fdebug-prefix-map=<old>=<new> Map one directory name to another in debug information. +fdiag-prefix-map= +Common Joined RejectNegative Var(common_deferred_options) Defer +-fdiag-prefix-map=<old>=<new> Map one directory name to another in diagnostics messages. + ffile-prefix-map= Common Joined RejectNegative Var(common_deferred_options) Defer -ffile-prefix-map=<old>=<new> Map one directory name to another in compilation result. diff --git a/gcc/diagnostic-format-text.cc b/gcc/diagnostic-format-text.cc index 9273973baaf..ae7b3c445a6 100644 --- a/gcc/diagnostic-format-text.cc +++ b/gcc/diagnostic-format-text.cc @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic-buffer.h" #include "text-art/theme.h" #include "make-unique.h" +#include "file-prefix-map.h" /* Disable warnings about quoting issues in the pp_xxx calls below that (intentionally) don't follow GCC diagnostic conventions. */ @@ -337,6 +338,8 @@ diagnostic_text_output_format::file_name_as_prefix (const char *f) const const char *locus_cs = colorize_start (pp_show_color (pp), "locus"); const char *locus_ce = colorize_stop (pp_show_color (pp)); + + f = remap_diag_filename (f); return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce); } diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index 07c76b6c652..1bc4319a9eb 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -50,6 +50,7 @@ along with GCC; see the file COPYING3. If not see #include "logical-location.h" #include "diagnostic-buffer.h" #include "make-unique.h" +#include "file-prefix-map.h" #ifdef HAVE_TERMIOS_H # include <termios.h> @@ -761,6 +762,7 @@ diagnostic_column_policy::get_location_text (const expanded_location &s, line = s.line; if (show_column) col = converted_column (s); + file = remap_diag_filename (file); } const char *line_col = maybe_line_and_column (line, col); diff --git a/gcc/file-prefix-map.cc b/gcc/file-prefix-map.cc index 3a77b195ae3..7ae3e7f95d5 100644 --- a/gcc/file-prefix-map.cc +++ b/gcc/file-prefix-map.cc @@ -21,6 +21,9 @@ #include "diagnostic.h" #include "file-prefix-map.h" +/* Set by -fcanon-prefix-map. */ +bool flag_canon_prefix_map; + /* 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). */ @@ -127,6 +130,7 @@ remap_filename (file_prefix_map *maps, const char *filename) static file_prefix_map *macro_prefix_maps; /* -fmacro-prefix-map */ static file_prefix_map *debug_prefix_maps; /* -fdebug-prefix-map */ static file_prefix_map *profile_prefix_maps; /* -fprofile-prefix-map */ +static file_prefix_map *diag_prefix_maps; /* -fdiag-prefix-map */ /* Record a file prefix mapping for -fmacro-prefix-map. */ void @@ -149,6 +153,7 @@ 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"); add_prefix_map (profile_prefix_maps, arg, "-ffile-prefix-map"); + add_prefix_map (diag_prefix_maps, arg, "-ffile-prefix-map"); } /* Record a file prefix mapping for -fprofile-prefix-map. */ @@ -158,6 +163,13 @@ add_profile_prefix_map (const char *arg) add_prefix_map (profile_prefix_maps, arg, "-fprofile-prefix-map"); } +/* Record a file prefix mapping for -fdiag-prefix-map. */ +void +add_diag_prefix_map (const char *arg) +{ + add_prefix_map (diag_prefix_maps, arg, "-fdiag-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 * @@ -181,3 +193,11 @@ remap_profile_filename (const char *filename) { return remap_filename (profile_prefix_maps, filename); } + +/* Remap using -fdiag-prefix-map. Return the GC-allocated new name + corresponding to FILENAME or FILENAME if no remapping was performed. */ +const char * +remap_diag_filename (const char *filename) +{ + return remap_filename (diag_prefix_maps, filename); +} diff --git a/gcc/file-prefix-map.h b/gcc/file-prefix-map.h index 8a6210a8a52..4a379881aeb 100644 --- a/gcc/file-prefix-map.h +++ b/gcc/file-prefix-map.h @@ -22,10 +22,12 @@ void add_macro_prefix_map (const char *); void add_debug_prefix_map (const char *); void add_file_prefix_map (const char *); void add_profile_prefix_map (const char *); +void add_diag_prefix_map (const char *); extern bool flag_canon_prefix_map; const char *remap_macro_filename (const char *); const char *remap_debug_filename (const char *); const char *remap_profile_filename (const char *); +const char *remap_diag_filename (const char *); #endif /* !GCC_FILE_PREFIX_MAP_H */ diff --git a/gcc/lto-opts.cc b/gcc/lto-opts.cc index dee1caafc55..4c32bcdbc81 100644 --- a/gcc/lto-opts.cc +++ b/gcc/lto-opts.cc @@ -150,6 +150,7 @@ lto_write_options (void) case OPT_ffile_prefix_map_: case OPT_fmacro_prefix_map_: case OPT_fprofile_prefix_map_: + case OPT_fdiag_prefix_map_: case OPT_fcanon_prefix_map: case OPT_fwhole_program: case OPT_fltrans_output_list_: diff --git a/gcc/opts-global.cc b/gcc/opts-global.cc index b9b42d3b233..da34988f01c 100644 --- a/gcc/opts-global.cc +++ b/gcc/opts-global.cc @@ -402,6 +402,10 @@ handle_common_deferred_options (void) add_profile_prefix_map (opt->arg); break; + case OPT_fdiag_prefix_map_: + add_diag_prefix_map (opt->arg); + break; + case OPT_fcanon_prefix_map: flag_canon_prefix_map = opt->value; break; diff --git a/gcc/opts.cc b/gcc/opts.cc index 80c7a971582..ee8a9648168 100644 --- a/gcc/opts.cc +++ b/gcc/opts.cc @@ -40,9 +40,6 @@ along with GCC; see the file COPYING3. If not see /* In this file all option sets are explicit. */ #undef OPTION_SET_P -/* Set by -fcanon-prefix-map. */ -bool flag_canon_prefix_map; - /* Set by finish_options when flag_stack_protector was set only because of -fhardened. Yuck. */ bool flag_stack_protector_set_by_fhardened_p; @@ -2900,6 +2897,7 @@ common_handle_option (struct gcc_options *opts, case OPT_fdebug_prefix_map_: case OPT_ffile_prefix_map_: case OPT_fprofile_prefix_map_: + case OPT_fdiag_prefix_map_: /* Deferred. */ break; @@ -3861,6 +3859,7 @@ gen_command_line_string (cl_decoded_option *options, case OPT_fmacro_prefix_map_: case OPT_ffile_prefix_map_: case OPT_fprofile_prefix_map_: + case OPT_fdiag_prefix_map_: case OPT_fcanon_prefix_map: case OPT_fcompare_debug: case OPT_fchecking: -- 2.49.0