2
0

950-cpp_file_path_translation.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. Forward ported from attachment to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47047
  2. --- a/gcc/c-family/c-opts.c
  3. +++ b/gcc/c-family/c-opts.c
  4. @@ -574,6 +574,10 @@ c_common_handle_option (size_t scode, co
  5. add_path (xstrdup (arg), SYSTEM, 0, true);
  6. break;
  7. + case OPT_iremap:
  8. + add_cpp_remap_path (arg);
  9. + break;
  10. +
  11. case OPT_iwithprefix:
  12. add_prefixed_path (arg, SYSTEM);
  13. break;
  14. --- a/gcc/c-family/c.opt
  15. +++ b/gcc/c-family/c.opt
  16. @@ -1632,6 +1632,10 @@ iquote
  17. C ObjC C++ ObjC++ Joined Separate MissingArgError(missing path after %qs)
  18. -iquote <dir> Add <dir> to the end of the quote include path.
  19. +iremap
  20. +C ObjC C++ ObjC++ Joined Separate
  21. +-iremap <src:dst> Convert <src> to <dst> if it occurs as prefix in __FILE__.
  22. +
  23. iwithprefix
  24. C ObjC C++ ObjC++ Joined Separate
  25. -iwithprefix <dir> Add <dir> to the end of the system include path.
  26. --- a/gcc/doc/cpp.texi
  27. +++ b/gcc/doc/cpp.texi
  28. @@ -4444,6 +4444,7 @@ without notice.
  29. @c man begin SYNOPSIS
  30. cpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}]
  31. [@option{-I}@var{dir}@dots{}] [@option{-iquote}@var{dir}@dots{}]
  32. + [@option{-iremap}@var{src}:@var{dst}]
  33. [@option{-W}@var{warn}@dots{}]
  34. [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}]
  35. [@option{-MP}] [@option{-MQ} @var{target}@dots{}]
  36. --- a/gcc/doc/cppopts.texi
  37. +++ b/gcc/doc/cppopts.texi
  38. @@ -532,6 +532,12 @@ Search @var{dir} only for header files r
  39. If @var{dir} begins with @code{=}, then the @code{=} will be replaced
  40. by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}.
  41. +@item -iremap @var{src}:@var{dst}
  42. +@opindex iremap
  43. +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
  44. +This option can be specified more than once. Processing stops at the first
  45. +match.
  46. +
  47. @item -fdirectives-only
  48. @opindex fdirectives-only
  49. When preprocessing, handle directives, but do not expand macros.
  50. --- a/gcc/doc/invoke.texi
  51. +++ b/gcc/doc/invoke.texi
  52. @@ -476,8 +476,8 @@ Objective-C and Objective-C++ Dialects}.
  53. @item Directory Options
  54. @xref{Directory Options,,Options for Directory Search}.
  55. @gccoptlist{-B@var{prefix} -I@var{dir} -iplugindir=@var{dir} @gol
  56. --iquote@var{dir} -L@var{dir} -no-canonical-prefixes -I- @gol
  57. ---sysroot=@var{dir} --no-sysroot-suffix}
  58. +-iquote@var{dir} -iremap@var{src}:@var{dst} -L@var{dir} -no-canonical-prefixes @gol
  59. +-I- --sysroot=@var{dir} --no-sysroot-suffix}
  60. @item Code Generation Options
  61. @xref{Code Gen Options,,Options for Code Generation Conventions}.
  62. @@ -10861,6 +10861,12 @@ be searched for header files only for th
  63. "@var{file}"}; they are not searched for @code{#include <@var{file}>},
  64. otherwise just like @option{-I}.
  65. +@item -iremap @var{src}:@var{dst}
  66. +@opindex iremap
  67. +Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
  68. +This option can be specified more than once. Processing stops at the first
  69. +match.
  70. +
  71. @item -L@var{dir}
  72. @opindex L
  73. Add directory @var{dir} to the list of directories to be searched
  74. --- a/libcpp/include/cpplib.h
  75. +++ b/libcpp/include/cpplib.h
  76. @@ -760,6 +760,9 @@ extern void cpp_set_lang (cpp_reader *,
  77. /* Set the include paths. */
  78. extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
  79. +/* Provide src:dst pair for __FILE__ remapping. */
  80. +extern void add_cpp_remap_path (const char *);
  81. +
  82. /* Call these to get pointers to the options, callback, and deps
  83. structures for a given reader. These pointers are good until you
  84. call cpp_finish on that reader. You can either edit the callbacks
  85. --- a/libcpp/macro.c
  86. +++ b/libcpp/macro.c
  87. @@ -227,6 +227,64 @@ static const char * const monthnames[] =
  88. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  89. };
  90. +static size_t remap_pairs;
  91. +static char **remap_src;
  92. +static char **remap_dst;
  93. +
  94. +void
  95. +add_cpp_remap_path (const char *arg)
  96. +{
  97. + const char *arg_dst;
  98. + size_t len;
  99. +
  100. + arg_dst = strchr(arg, ':');
  101. + if (arg_dst == NULL)
  102. + {
  103. + fprintf(stderr, "Invalid argument for -iremap\n");
  104. + exit(1);
  105. + }
  106. +
  107. + len = arg_dst - arg;
  108. + ++arg_dst;
  109. +
  110. + remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
  111. + remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
  112. +
  113. + remap_src[remap_pairs] = (char *) xmalloc(len + 1);
  114. + memcpy(remap_src[remap_pairs], arg, len);
  115. + remap_src[remap_pairs][len] = '\0';
  116. + remap_dst[remap_pairs] = xstrdup(arg_dst);
  117. + ++remap_pairs;
  118. +}
  119. +
  120. +static const char *
  121. +cpp_remap_file (const char *arg, char **tmp_name)
  122. +{
  123. + char *result;
  124. + size_t i, len;
  125. +
  126. + for (i = 0; i < remap_pairs; ++i)
  127. + {
  128. + len = strlen (remap_src[i]);
  129. + if (strncmp (remap_src[i], arg, len))
  130. + continue;
  131. + if (arg[len] == '\0')
  132. + return xstrdup (remap_dst[i]);
  133. + if (arg[len] != '/')
  134. + continue;
  135. + arg += len;
  136. + len = strlen (remap_dst[i]);
  137. + result = (char *) xmalloc (len + strlen (arg) + 1);
  138. + memcpy(result, remap_dst[i], len);
  139. + strcpy(result + len, arg);
  140. + *tmp_name = result;
  141. +
  142. + return result;
  143. + }
  144. +
  145. + return arg;
  146. +}
  147. +
  148. /* Helper function for builtin_macro. Returns the text generated by
  149. a builtin macro. */
  150. const uchar *
  151. @@ -290,6 +348,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
  152. {
  153. unsigned int len;
  154. const char *name;
  155. + char *tmp_name = NULL;
  156. uchar *buf;
  157. if (node->value.builtin == BT_FILE)
  158. @@ -301,6 +360,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
  159. if (!name)
  160. abort ();
  161. }
  162. + name = cpp_remap_file (name, &tmp_name);
  163. len = strlen (name);
  164. buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
  165. result = buf;
  166. @@ -308,6 +368,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
  167. buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
  168. *buf++ = '"';
  169. *buf = '\0';
  170. + free (tmp_name);
  171. }
  172. break;