blob: da16879132a535928314b1646e017bfa46625eb7 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001From ef7c2bda6b4c88f8007ed663b1108cd4651598c8 Mon Sep 17 00:00:00 2001
2From: Hongxu Jia <hongxu.jia@windriver.com>
3Date: Wed, 16 Mar 2016 02:27:43 -0400
4Subject: [PATCH] gcc/libcpp: support -ffile-prefix-map=<old>=<new>
5
6Similar -fdebug-prefix-map, add option -ffile-prefix-map to map one
7directory name (old) to another (new) in __FILE__, __BASE_FILE__ and
8__builtin_FILE ().
9
10https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70268
11
12Upstream-Status: Submitted [gcc-patches@gcc.gnu.org]
13Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
14---
15 gcc/c-family/c-opts.c | 6 ++++
16 gcc/c-family/c.opt | 4 +++
17 gcc/dwarf2out.c | 1 +
18 gcc/gimplify.c | 2 ++
19 libcpp/Makefile.in | 10 +++---
20 libcpp/file-map.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++
21 libcpp/include/file-map.h | 30 ++++++++++++++++
22 libcpp/macro.c | 2 ++
23 8 files changed, 142 insertions(+), 5 deletions(-)
24 create mode 100644 libcpp/file-map.c
25 create mode 100644 libcpp/include/file-map.h
26
27diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
28index 718a052..3f93c56 100644
29--- a/gcc/c-family/c-opts.c
30+++ b/gcc/c-family/c-opts.c
31@@ -46,6 +46,7 @@ along with GCC; see the file COPYING3. If not see
32 #include "opts.h"
33 #include "plugin.h" /* For PLUGIN_INCLUDE_FILE event. */
34 #include "mkdeps.h"
35+#include "file-map.h"
36 #include "c-target.h"
37 #include "tm.h" /* For BYTES_BIG_ENDIAN,
38 DOLLARS_IN_IDENTIFIERS,
39@@ -510,6 +511,11 @@ c_common_handle_option (size_t scode, const char *arg, int value,
40 cpp_opts->narrow_charset = arg;
41 break;
42
43+ case OPT_ffile_prefix_map_:
44+ if (add_file_prefix_map (arg) < 0)
45+ error ("invalid argument %qs to -ffile-prefix-map", arg);
46+ break;
47+
48 case OPT_fwide_exec_charset_:
49 cpp_opts->wide_charset = arg;
50 break;
51diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
52index 453ec8e..30ad053 100644
53--- a/gcc/c-family/c.opt
54+++ b/gcc/c-family/c.opt
55@@ -1117,6 +1117,10 @@ fexec-charset=
56 C ObjC C++ ObjC++ Joined RejectNegative
57 -fexec-charset=<cset> Convert all strings and character constants to character set <cset>
58
59+ffile-prefix-map=
60+C ObjC C++ ObjC++ Joined RejectNegative
61+-ffile-prefix-map=<old=new> Map one directory name to another in __FILE__, __BASE_FILE__ and __builtin_FILE ()
62+
63 fextended-identifiers
64 C ObjC C++ ObjC++
65 Permit universal character names (\\u and \\U) in identifiers
66diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
67index 526f114..438a475 100644
68--- a/gcc/dwarf2out.c
69+++ b/gcc/dwarf2out.c
70@@ -19671,6 +19671,7 @@ gen_producer_string (void)
71 case OPT_fltrans_output_list_:
72 case OPT_fresolution_:
73 case OPT_fdebug_prefix_map_:
74+ case OPT_ffile_prefix_map_:
75 /* Ignore these. */
76 continue;
77 default:
78diff --git a/gcc/gimplify.c b/gcc/gimplify.c
79index c85f83a..1ffe1e1 100644
80--- a/gcc/gimplify.c
81+++ b/gcc/gimplify.c
82@@ -87,6 +87,7 @@ along with GCC; see the file COPYING3. If not see
83 #include "gimple-low.h"
84 #include "cilk.h"
85 #include "gomp-constants.h"
86+#include "file-map.h"
87
88 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
89 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
90@@ -2370,6 +2371,7 @@ gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
91 case BUILT_IN_FILE:
92 {
93 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
94+ locfile = remap_file_filename (locfile);
95 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
96 return GS_OK;
97 }
98diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in
99index ad35563..c210ff9 100644
100--- a/libcpp/Makefile.in
101+++ b/libcpp/Makefile.in
102@@ -84,12 +84,12 @@ DEPMODE = $(CXXDEPMODE)
103
104
105 libcpp_a_OBJS = charset.o directives.o directives-only.o errors.o \
106- expr.o files.o identifiers.o init.o lex.o line-map.o macro.o \
107- mkdeps.o pch.o symtab.o traditional.o
108+ expr.o file-map.o files.o identifiers.o init.o lex.o line-map.o \
109+ macro.o mkdeps.o pch.o symtab.o traditional.o
110
111 libcpp_a_SOURCES = charset.c directives.c directives-only.c errors.c \
112- expr.c files.c identifiers.c init.c lex.c line-map.c macro.c \
113- mkdeps.c pch.c symtab.c traditional.c
114+ expr.c file-map.c files.c identifiers.c init.c lex.c line-map.c \
115+ macro.c mkdeps.c pch.c symtab.c traditional.c
116
117 all: libcpp.a $(USED_CATALOGS)
118
119@@ -263,7 +263,7 @@ po/$(PACKAGE).pot: $(libcpp_a_SOURCES)
120
121 TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h ucnid.h \
122 include/line-map.h include/symtab.h include/cpp-id-data.h \
123- include/cpplib.h include/mkdeps.h system.h
124+ include/cpplib.h include/mkdeps.h system.h include/file-map.h
125
126 TAGS: $(TAGS_SOURCES)
127 cd $(srcdir) && etags $(TAGS_SOURCES)
128diff --git a/libcpp/file-map.c b/libcpp/file-map.c
129new file mode 100644
130index 0000000..04e851b
131--- /dev/null
132+++ b/libcpp/file-map.c
133@@ -0,0 +1,92 @@
134+/* Map one directory name to another in __FILE__, __BASE_FILE__
135+ and __builtin_FILE ().
136+ Copyright (C) 2001-2016 Free Software Foundation, Inc.
137+
138+This program is free software; you can redistribute it and/or modify it
139+under the terms of the GNU General Public License as published by the
140+Free Software Foundation; either version 3, or (at your option) any
141+later version.
142+
143+This program is distributed in the hope that it will be useful,
144+but WITHOUT ANY WARRANTY; without even the implied warranty of
145+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
146+GNU General Public License for more details.
147+
148+You should have received a copy of the GNU General Public License
149+along with this program; see the file COPYING3. If not see
150+<http://www.gnu.org/licenses/>.
151+
152+ In other words, you are welcome to use, share and improve this program.
153+ You are forbidden to forbid anyone else to use, share and improve
154+ what you give them. Help stamp out software-hoarding! */
155+
156+#include "config.h"
157+#include "system.h"
158+#include "file-map.h"
159+
160+/* Structure recording the mapping from source file and directory
161+ names at compile time to __FILE__ */
162+typedef struct file_prefix_map
163+{
164+ const char *old_prefix;
165+ const char *new_prefix;
166+ size_t old_len;
167+ size_t new_len;
168+ struct file_prefix_map *next;
169+} file_prefix_map;
170+
171+/* Linked list of such structures. */
172+static file_prefix_map *file_prefix_maps;
173+
174+/* Record prefix mapping of __FILE__. ARG is the argument to
175+ -ffile-prefix-map and must be of the form OLD=NEW. */
176+int
177+add_file_prefix_map (const char *arg)
178+{
179+ file_prefix_map *map;
180+ const char *p;
181+
182+ p = strchr (arg, '=');
183+ if (!p)
184+ {
185+ fprintf(stderr, "invalid argument %qs to -ffile-prefix-map", arg);
186+ return -1;
187+ }
188+ map = XNEW (file_prefix_map);
189+ map->old_prefix = xstrndup (arg, p - arg);
190+ map->old_len = p - arg;
191+ p++;
192+ map->new_prefix = xstrdup (p);
193+ map->new_len = strlen (p);
194+ map->next = file_prefix_maps;
195+ file_prefix_maps = map;
196+
197+ return 0;
198+}
199+
200+/* Perform user-specified mapping of __FILE__ prefixes. Return
201+ the new name corresponding to filename. */
202+
203+const char *
204+remap_file_filename (const char *filename)
205+{
206+ file_prefix_map *map;
207+ char *s;
208+ const char *name;
209+ size_t name_len;
210+
211+ for (map = file_prefix_maps; map; map = map->next)
212+ if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
213+ break;
214+ if (!map)
215+ return filename;
216+ name = filename + map->old_len;
217+ name_len = strlen (name) + 1;
218+ s = (char *) alloca (name_len + map->new_len);
219+ memcpy (s, map->new_prefix, map->new_len);
220+ memcpy (s + map->new_len, name, name_len);
221+
222+ return xstrdup (s);
223+}
224+
225+
226diff --git a/libcpp/include/file-map.h b/libcpp/include/file-map.h
227new file mode 100644
228index 0000000..e6f8cbf
229--- /dev/null
230+++ b/libcpp/include/file-map.h
231@@ -0,0 +1,30 @@
232+/* Map one directory name to another in __FILE__, __BASE_FILE__
233+ and __builtin_FILE ().
234+ Copyright (C) 2001-2016 Free Software Foundation, Inc.
235+
236+This program is free software; you can redistribute it and/or modify it
237+under the terms of the GNU General Public License as published by the
238+Free Software Foundation; either version 3, or (at your option) any
239+later version.
240+
241+This program is distributed in the hope that it will be useful,
242+but WITHOUT ANY WARRANTY; without even the implied warranty of
243+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
244+GNU General Public License for more details.
245+
246+You should have received a copy of the GNU General Public License
247+along with this program; see the file COPYING3. If not see
248+<http://www.gnu.org/licenses/>.
249+
250+ In other words, you are welcome to use, share and improve this program.
251+ You are forbidden to forbid anyone else to use, share and improve
252+ what you give them. Help stamp out software-hoarding! */
253+
254+#ifndef LIBCPP_FILE_MAP_H
255+#define LIBCPP_FILE_MAP_H
256+
257+const char * remap_file_filename (const char *filename);
258+
259+int add_file_prefix_map (const char *arg);
260+
261+#endif /* !LIBCPP_FILE_MAP_H */
262diff --git a/libcpp/macro.c b/libcpp/macro.c
263index 1e0a0b5..c3d330c 100644
264--- a/libcpp/macro.c
265+++ b/libcpp/macro.c
266@@ -26,6 +26,7 @@ along with this program; see the file COPYING3. If not see
267 #include "system.h"
268 #include "cpplib.h"
269 #include "internal.h"
270+#include "file-map.h"
271
272 typedef struct macro_arg macro_arg;
273 /* This structure represents the tokens of a macro argument. These
274@@ -297,6 +298,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
275 if (!name)
276 abort ();
277 }
278+ name = remap_file_filename (name);
279 len = strlen (name);
280 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
281 result = buf;
282--
2831.9.1
284