950-cpp_file_path_translation.patch 5.4 KB

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