kernel: update 4.4 to 4.4.88
[oweals/openwrt.git] / toolchain / gcc / patches / 7.1.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 @@ -588,6 +588,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 @@ -1825,6 +1825,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 @@ -4272,6 +4272,7 @@ Refer to the GCC manual for full documen
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{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}]
37      [@option{-MP}] [@option{-MQ} @var{target}@dots{}]
38      [@option{-MT} @var{target}@dots{}]
39 --- a/gcc/doc/cppopts.texi
40 +++ b/gcc/doc/cppopts.texi
41 @@ -220,6 +220,12 @@ extensions @samp{.i}, @samp{.ii} or @sam
42  extensions that GCC uses for preprocessed files created by
43  @option{-save-temps}.
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 @@ -11861,6 +11861,12 @@ by @option{-fplugin=@var{name}} instead
57  @option{-fplugin=@var{path}/@var{name}.so}.  This option is not meant
58  to be used by the user, but only passed by the driver.
59  
60 +@item -iremap @var{src}:@var{dst}
61 +@opindex iremap
62 +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
63 +This option can be specified more than once.  Processing stops at the first
64 +match.
65 +
66  @item -L@var{dir}
67  @opindex L
68  Add directory @var{dir} to the list of directories to be searched
69 --- a/libcpp/include/cpplib.h
70 +++ b/libcpp/include/cpplib.h
71 @@ -820,6 +820,9 @@ extern void cpp_set_lang (cpp_reader *,
72  /* Set the include paths.  */
73  extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
74  
75 +/* Provide src:dst pair for __FILE__ remapping.  */
76 +extern void add_cpp_remap_path (const char *);
77 +
78  /* Call these to get pointers to the options, callback, and deps
79     structures for a given reader.  These pointers are good until you
80     call cpp_finish on that reader.  You can either edit the callbacks
81 --- a/libcpp/macro.c
82 +++ b/libcpp/macro.c
83 @@ -227,6 +227,64 @@ static const char * const monthnames[] =
84    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
85  };
86  
87 +static size_t remap_pairs;
88 +static char **remap_src;
89 +static char **remap_dst;
90 +
91 +void
92 +add_cpp_remap_path (const char *arg)
93 +{
94 +  const char *arg_dst;
95 +  size_t len;
96 +
97 +  arg_dst = strchr(arg, ':');
98 +  if (arg_dst == NULL)
99 +    {
100 +      fprintf(stderr, "Invalid argument for -iremap\n");
101 +      exit(1);
102 +    }
103 +
104 +  len = arg_dst - arg;
105 +  ++arg_dst;
106 +
107 +  remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
108 +  remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
109 +
110 +  remap_src[remap_pairs] = (char *) xmalloc(len + 1);
111 +  memcpy(remap_src[remap_pairs], arg, len);
112 +  remap_src[remap_pairs][len] = '\0';
113 +  remap_dst[remap_pairs] = xstrdup(arg_dst);
114 +  ++remap_pairs;
115 +}
116 +
117 +static const char *
118 +cpp_remap_file (const char *arg, char **tmp_name)
119 +{
120 +  char *result;
121 +  size_t i, len;
122 +
123 +  for (i = 0; i < remap_pairs; ++i)
124 +    {
125 +      len = strlen (remap_src[i]);
126 +      if (strncmp (remap_src[i], arg, len))
127 +       continue;
128 +      if (arg[len] == '\0')
129 +       return xstrdup (remap_dst[i]);
130 +      if (arg[len] != '/')
131 +       continue;
132 +      arg += len;
133 +      len = strlen (remap_dst[i]);
134 +      result = (char *) xmalloc (len + strlen (arg) + 1);
135 +      memcpy(result, remap_dst[i], len);
136 +      strcpy(result + len, arg);
137 +      *tmp_name = result;
138 +
139 +      return result;
140 +    }
141 +
142 +   return arg;
143 +}
144 +
145  /* Helper function for builtin_macro.  Returns the text generated by
146     a builtin macro. */
147  const uchar *
148 @@ -290,6 +348,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
149        {
150         unsigned int len;
151         const char *name;
152 +       char *tmp_name = NULL;
153         uchar *buf;
154         
155         if (node->value.builtin == BT_FILE)
156 @@ -301,6 +360,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
157             if (!name)
158               abort ();
159           }
160 +       name = cpp_remap_file (name, &tmp_name);
161         len = strlen (name);
162         buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
163         result = buf;
164 @@ -308,6 +368,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
165         buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
166         *buf++ = '"';
167         *buf = '\0';
168 +       free (tmp_name);
169        }
170        break;
171