1 Forward ported from attachment to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47047
3 --- a/gcc/c-family/c-opts.c
4 +++ b/gcc/c-family/c-opts.c
5 @@ -574,6 +574,10 @@ c_common_handle_option (size_t scode, co
6 add_path (xstrdup (arg), SYSTEM, 0, true);
10 + add_cpp_remap_path (arg);
14 add_prefixed_path (arg, SYSTEM);
16 --- a/gcc/c-family/c.opt
17 +++ b/gcc/c-family/c.opt
18 @@ -1632,6 +1632,10 @@ iquote
19 C ObjC C++ ObjC++ Joined Separate MissingArgError(missing path after %qs)
20 -iquote <dir> Add <dir> to the end of the quote include path.
23 +C ObjC C++ ObjC++ Joined Separate
24 +-iremap <src:dst> Convert <src> to <dst> if it occurs as prefix in __FILE__.
27 C ObjC C++ ObjC++ Joined Separate
28 -iwithprefix <dir> Add <dir> to the end of the system include path.
29 --- a/gcc/doc/cpp.texi
30 +++ b/gcc/doc/cpp.texi
31 @@ -4444,6 +4444,7 @@ without notice.
33 cpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}]
34 [@option{-I}@var{dir}@dots{}] [@option{-iquote}@var{dir}@dots{}]
35 + [@option{-iremap}@var{src}:@var{dst}]
36 [@option{-W}@var{warn}@dots{}]
37 [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}]
38 [@option{-MP}] [@option{-MQ} @var{target}@dots{}]
39 --- a/gcc/doc/cppopts.texi
40 +++ b/gcc/doc/cppopts.texi
41 @@ -532,6 +532,12 @@ Search @var{dir} only for header files r
42 If @var{dir} begins with @code{=}, then the @code{=} will be replaced
43 by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}.
45 +@item -iremap @var{src}:@var{dst}
47 +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
48 +This option can be specified more than once. Processing stops at the first
51 @item -fdirectives-only
52 @opindex fdirectives-only
53 When preprocessing, handle directives, but do not expand macros.
54 --- a/gcc/doc/invoke.texi
55 +++ b/gcc/doc/invoke.texi
56 @@ -476,8 +476,8 @@ Objective-C and Objective-C++ Dialects}.
57 @item Directory Options
58 @xref{Directory Options,,Options for Directory Search}.
59 @gccoptlist{-B@var{prefix} -I@var{dir} -iplugindir=@var{dir} @gol
60 --iquote@var{dir} -L@var{dir} -no-canonical-prefixes -I- @gol
61 ---sysroot=@var{dir} --no-sysroot-suffix}
62 +-iquote@var{dir} -iremap@var{src}:@var{dst} -L@var{dir} -no-canonical-prefixes @gol
63 +-I- --sysroot=@var{dir} --no-sysroot-suffix}
65 @item Code Generation Options
66 @xref{Code Gen Options,,Options for Code Generation Conventions}.
67 @@ -10861,6 +10861,12 @@ be searched for header files only for th
68 "@var{file}"}; they are not searched for @code{#include <@var{file}>},
69 otherwise just like @option{-I}.
71 +@item -iremap @var{src}:@var{dst}
73 +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
74 +This option can be specified more than once. Processing stops at the first
79 Add directory @var{dir} to the list of directories to be searched
80 --- a/libcpp/include/cpplib.h
81 +++ b/libcpp/include/cpplib.h
82 @@ -760,6 +760,9 @@ extern void cpp_set_lang (cpp_reader *,
83 /* Set the include paths. */
84 extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
86 +/* Provide src:dst pair for __FILE__ remapping. */
87 +extern void add_cpp_remap_path (const char *);
89 /* Call these to get pointers to the options, callback, and deps
90 structures for a given reader. These pointers are good until you
91 call cpp_finish on that reader. You can either edit the callbacks
94 @@ -227,6 +227,64 @@ static const char * const monthnames[] =
95 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
98 +static size_t remap_pairs;
99 +static char **remap_src;
100 +static char **remap_dst;
103 +add_cpp_remap_path (const char *arg)
105 + const char *arg_dst;
108 + arg_dst = strchr(arg, ':');
109 + if (arg_dst == NULL)
111 + fprintf(stderr, "Invalid argument for -iremap\n");
115 + len = arg_dst - arg;
118 + remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
119 + remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
121 + remap_src[remap_pairs] = (char *) xmalloc(len + 1);
122 + memcpy(remap_src[remap_pairs], arg, len);
123 + remap_src[remap_pairs][len] = '\0';
124 + remap_dst[remap_pairs] = xstrdup(arg_dst);
129 +cpp_remap_file (const char *arg, char **tmp_name)
134 + for (i = 0; i < remap_pairs; ++i)
136 + len = strlen (remap_src[i]);
137 + if (strncmp (remap_src[i], arg, len))
139 + if (arg[len] == '\0')
140 + return xstrdup (remap_dst[i]);
141 + if (arg[len] != '/')
144 + len = strlen (remap_dst[i]);
145 + result = (char *) xmalloc (len + strlen (arg) + 1);
146 + memcpy(result, remap_dst[i], len);
147 + strcpy(result + len, arg);
148 + *tmp_name = result;
156 /* Helper function for builtin_macro. Returns the text generated by
159 @@ -290,6 +348,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
163 + char *tmp_name = NULL;
166 if (node->value.builtin == BT_FILE)
167 @@ -301,6 +360,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
171 + name = cpp_remap_file (name, &tmp_name);
173 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
175 @@ -308,6 +368,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
176 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);