prefix-map-realpath.patch 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Relative paths don't work with -fdebug-prefix-map and friends. This
  2. can lead to paths which the user wanted to be remapped being missed.
  3. Setting -fdebug-prefix-map to work with a relative path isn't practical
  4. either.
  5. Instead, call gcc's realpath function on the incomming path name before
  6. comparing it with the remapping. This means other issues like symlinks
  7. are also accounted for and leads to a more consistent remapping experience.
  8. Upstream-Status: Submitted [https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599885.html]
  9. [Also https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599884.html]
  10. Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
  11. Index: gcc-12.1.0/gcc/file-prefix-map.cc
  12. ===================================================================
  13. --- gcc-12.1.0.orig/gcc/file-prefix-map.cc
  14. +++ gcc-12.1.0/gcc/file-prefix-map.cc
  15. @@ -70,19 +70,28 @@ remap_filename (file_prefix_map *maps, c
  16. file_prefix_map *map;
  17. char *s;
  18. const char *name;
  19. + char *realname;
  20. size_t name_len;
  21. + if (lbasename (filename) == filename)
  22. + return filename;
  23. +
  24. + realname = lrealpath (filename);
  25. +
  26. for (map = maps; map; map = map->next)
  27. - if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
  28. + if (filename_ncmp (realname, map->old_prefix, map->old_len) == 0)
  29. break;
  30. - if (!map)
  31. + if (!map) {
  32. + free (realname);
  33. return filename;
  34. - name = filename + map->old_len;
  35. + }
  36. + name = realname + map->old_len;
  37. name_len = strlen (name) + 1;
  38. s = (char *) ggc_alloc_atomic (name_len + map->new_len);
  39. memcpy (s, map->new_prefix, map->new_len);
  40. memcpy (s + map->new_len, name, name_len);
  41. + free (realname);
  42. return s;
  43. }
  44. Index: gcc-12.1.0/libcpp/macro.cc
  45. ===================================================================
  46. --- gcc-12.1.0.orig/libcpp/macro.cc
  47. +++ gcc-12.1.0/libcpp/macro.cc
  48. @@ -563,7 +563,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
  49. if (!name)
  50. abort ();
  51. }
  52. - if (pfile->cb.remap_filename)
  53. + if (pfile->cb.remap_filename && !pfile->state.in_directive)
  54. name = pfile->cb.remap_filename (name);
  55. len = strlen (name);
  56. buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);