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