gcc: add a patch to 5.x that supports translation of __FILE__ paths
[openwrt/staging/yousong.git] / toolchain / gcc / patches / 5.2.0 / 950-cpp_file_path_translation.patch
1 Forward ported from attachment to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47047
2
3 --- a/gcc/c-family/c-opts.c
4 +++ b/gcc/c-family/c-opts.c
5 @@ -581,6 +581,10 @@ c_common_handle_option (size_t scode, co
6 add_path (xstrdup (arg), SYSTEM, 0, true);
7 break;
8
9 + case OPT_iremap:
10 + add_cpp_remap_path (arg);
11 + break;
12 +
13 case OPT_iwithprefix:
14 add_prefixed_path (arg, SYSTEM);
15 break;
16 --- a/gcc/c-family/c.opt
17 +++ b/gcc/c-family/c.opt
18 @@ -1528,6 +1528,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
21
22 +iremap
23 +C ObjC C++ ObjC++ Joined Separate
24 +-iremap <src:dst> Convert <src> to <dst> if it occurs as prefix in __FILE__.
25 +
26 iwithprefix
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 @@ -4441,6 +4441,7 @@ without notice.
32 @c man begin SYNOPSIS
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}.
44
45 +@item -iremap @var{src}:@var{dst}
46 +@opindex iremap
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
49 +match.
50 +
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 @@ -494,8 +494,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} -specs=@var{file} -I- @gol
61 ---sysroot=@var{dir} --no-sysroot-suffix}
62 +-iquote@var{dir} -iremap@var{src}:@var{dst} -L@var{dir} -specs=@var{file} @gol
63 +-I- --sysroot=@var{dir} --no-sysroot-suffix}
64
65 @item Machine Dependent Options
66 @xref{Submodel Options,,Hardware Models and Configurations}.
67 @@ -11463,6 +11463,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}.
70
71 +@item -iremap @var{src}:@var{dst}
72 +@opindex iremap
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
75 +match.
76 +
77 @item -L@var{dir}
78 @opindex L
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 @@ -751,6 +751,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);
85
86 +/* Provide src:dst pair for __FILE__ remapping. */
87 +extern void add_cpp_remap_path (const char *);
88 +
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
92 --- a/libcpp/macro.c
93 +++ b/libcpp/macro.c
94 @@ -224,6 +224,64 @@ static const char * const monthnames[] =
95 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
96 };
97
98 +static size_t remap_pairs;
99 +static char **remap_src;
100 +static char **remap_dst;
101 +
102 +void
103 +add_cpp_remap_path (const char *arg)
104 +{
105 + const char *arg_dst;
106 + size_t len;
107 +
108 + arg_dst = strchr(arg, ':');
109 + if (arg_dst == NULL)
110 + {
111 + fprintf(stderr, "Invalid argument for -iremap\n");
112 + exit(1);
113 + }
114 +
115 + len = arg_dst - arg;
116 + ++arg_dst;
117 +
118 + remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
119 + remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
120 +
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);
125 + ++remap_pairs;
126 +}
127 +
128 +static const char *
129 +cpp_remap_file (const char *arg, char **tmp_name)
130 +{
131 + char *result;
132 + size_t i, len;
133 +
134 + for (i = 0; i < remap_pairs; ++i)
135 + {
136 + len = strlen (remap_src[i]);
137 + if (strncmp (remap_src[i], arg, len))
138 + continue;
139 + if (arg[len] == '\0')
140 + return xstrdup (remap_dst[i]);
141 + if (arg[len] != '/')
142 + continue;
143 + 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;
149 +
150 + return result;
151 + }
152 +
153 + return arg;
154 +}
155 +
156 /* Helper function for builtin_macro. Returns the text generated by
157 a builtin macro. */
158 const uchar *
159 @@ -286,6 +344,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
160 {
161 unsigned int len;
162 const char *name;
163 + char *tmp_name = NULL;
164 uchar *buf;
165
166 if (node->value.builtin == BT_FILE)
167 @@ -297,6 +356,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
168 if (!name)
169 abort ();
170 }
171 + name = cpp_remap_file (name, &tmp_name);
172 len = strlen (name);
173 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
174 result = buf;
175 @@ -304,6 +364,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
176 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
177 *buf++ = '"';
178 *buf = '\0';
179 + free (tmp_name);
180 }
181 break;
182