950-cpp_file_path_translation.patch 5.1 KB

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