Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / toolchain / gcc / patches / 7.4.0 / 950-cpp_file_path_translation.patch
1 commit 331735a357a73c7b8adc205241ac3cc6543d985e
2 Author: Felix Fietkau <nbd@openwrt.org>
3 Date:   Tue Nov 17 12:38:22 2015 +0000
4
5     gcc: add a patch to 5.x that supports translation of __FILE__ paths
6     
7     Signed-off-by: Felix Fietkau <nbd@openwrt.org>
8     
9     SVN-Revision: 47490
10
11 Forward ported from attachment to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47047
12
13 --- a/gcc/c-family/c-opts.c
14 +++ b/gcc/c-family/c-opts.c
15 @@ -588,6 +588,10 @@ c_common_handle_option (size_t scode, co
16        add_path (xstrdup (arg), SYSTEM, 0, true);
17        break;
18  
19 +    case OPT_iremap:
20 +      add_cpp_remap_path (arg);
21 +      break;
22 +
23      case OPT_iwithprefix:
24        add_prefixed_path (arg, SYSTEM);
25        break;
26 --- a/gcc/c-family/c.opt
27 +++ b/gcc/c-family/c.opt
28 @@ -1825,6 +1825,10 @@ iquote
29  C ObjC C++ ObjC++ Joined Separate MissingArgError(missing path after %qs)
30  -iquote <dir>  Add <dir> to the end of the quote include path.
31  
32 +iremap
33 +C ObjC C++ ObjC++ Joined Separate
34 +-iremap <src:dst>  Convert <src> to <dst> if it occurs as prefix in __FILE__.
35 +
36  iwithprefix
37  C ObjC C++ ObjC++ Joined Separate
38  -iwithprefix <dir>     Add <dir> to the end of the system include path.
39 --- a/gcc/doc/cpp.texi
40 +++ b/gcc/doc/cpp.texi
41 @@ -4272,6 +4272,7 @@ Refer to the GCC manual for full documen
42  @c man begin SYNOPSIS
43  cpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}]
44      [@option{-I}@var{dir}@dots{}] [@option{-iquote}@var{dir}@dots{}]
45 +    [@option{-iremap}@var{src}:@var{dst}]
46      [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}]
47      [@option{-MP}] [@option{-MQ} @var{target}@dots{}]
48      [@option{-MT} @var{target}@dots{}]
49 --- a/gcc/doc/cppopts.texi
50 +++ b/gcc/doc/cppopts.texi
51 @@ -220,6 +220,12 @@ extensions @samp{.i}, @samp{.ii} or @sam
52  extensions that GCC uses for preprocessed files created by
53  @option{-save-temps}.
54  
55 +@item -iremap @var{src}:@var{dst}
56 +@opindex iremap
57 +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
58 +This option can be specified more than once.  Processing stops at the first
59 +match.
60 +
61  @item -fdirectives-only
62  @opindex fdirectives-only
63  When preprocessing, handle directives, but do not expand macros.
64 --- a/gcc/doc/invoke.texi
65 +++ b/gcc/doc/invoke.texi
66 @@ -11871,6 +11871,12 @@ by @option{-fplugin=@var{name}} instead
67  @option{-fplugin=@var{path}/@var{name}.so}.  This option is not meant
68  to be used by the user, but only passed by the driver.
69  
70 +@item -iremap @var{src}:@var{dst}
71 +@opindex iremap
72 +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
73 +This option can be specified more than once.  Processing stops at the first
74 +match.
75 +
76  @item -L@var{dir}
77  @opindex L
78  Add directory @var{dir} to the list of directories to be searched
79 --- a/libcpp/include/cpplib.h
80 +++ b/libcpp/include/cpplib.h
81 @@ -820,6 +820,9 @@ extern void cpp_set_lang (cpp_reader *,
82  /* Set the include paths.  */
83  extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
84  
85 +/* Provide src:dst pair for __FILE__ remapping.  */
86 +extern void add_cpp_remap_path (const char *);
87 +
88  /* Call these to get pointers to the options, callback, and deps
89     structures for a given reader.  These pointers are good until you
90     call cpp_finish on that reader.  You can either edit the callbacks
91 --- a/libcpp/macro.c
92 +++ b/libcpp/macro.c
93 @@ -227,6 +227,64 @@ static const char * const monthnames[] =
94    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
95  };
96  
97 +static size_t remap_pairs;
98 +static char **remap_src;
99 +static char **remap_dst;
100 +
101 +void
102 +add_cpp_remap_path (const char *arg)
103 +{
104 +  const char *arg_dst;
105 +  size_t len;
106 +
107 +  arg_dst = strchr(arg, ':');
108 +  if (arg_dst == NULL)
109 +    {
110 +      fprintf(stderr, "Invalid argument for -iremap\n");
111 +      exit(1);
112 +    }
113 +
114 +  len = arg_dst - arg;
115 +  ++arg_dst;
116 +
117 +  remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
118 +  remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
119 +
120 +  remap_src[remap_pairs] = (char *) xmalloc(len + 1);
121 +  memcpy(remap_src[remap_pairs], arg, len);
122 +  remap_src[remap_pairs][len] = '\0';
123 +  remap_dst[remap_pairs] = xstrdup(arg_dst);
124 +  ++remap_pairs;
125 +}
126 +
127 +static const char *
128 +cpp_remap_file (const char *arg, char **tmp_name)
129 +{
130 +  char *result;
131 +  size_t i, len;
132 +
133 +  for (i = 0; i < remap_pairs; ++i)
134 +    {
135 +      len = strlen (remap_src[i]);
136 +      if (strncmp (remap_src[i], arg, len))
137 +       continue;
138 +      if (arg[len] == '\0')
139 +       return xstrdup (remap_dst[i]);
140 +      if (arg[len] != '/')
141 +       continue;
142 +      arg += len;
143 +      len = strlen (remap_dst[i]);
144 +      result = (char *) xmalloc (len + strlen (arg) + 1);
145 +      memcpy(result, remap_dst[i], len);
146 +      strcpy(result + len, arg);
147 +      *tmp_name = result;
148 +
149 +      return result;
150 +    }
151 +
152 +   return arg;
153 +}
154 +
155  /* Helper function for builtin_macro.  Returns the text generated by
156     a builtin macro. */
157  const uchar *
158 @@ -290,6 +348,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
159        {
160         unsigned int len;
161         const char *name;
162 +       char *tmp_name = NULL;
163         uchar *buf;
164         
165         if (node->value.builtin == BT_FILE)
166 @@ -301,6 +360,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
167             if (!name)
168               abort ();
169           }
170 +       name = cpp_remap_file (name, &tmp_name);
171         len = strlen (name);
172         buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
173         result = buf;
174 @@ -308,6 +368,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
175         buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
176         *buf++ = '"';
177         *buf = '\0';
178 +       free (tmp_name);
179        }
180        break;
181