On Mon, Aug 15, 2022 at 10:28 AM Richard Purdie via Gcc <gcc@gcc.gnu.org> wrote: > > On Mon, 2022-08-15 at 12:13 +0100, Richard Purdie via Gcc wrote: > > Hi, > > > > I'm wondering if we'd be able to improve path handling in the -f*- > > prefix-map compiler options to cover relative paths? > > > > Currently it works well for absolute paths but if a file uses a > > relative path or a path with a symlink in, or a non-absolute path, it > > will miss those cases. For relative paths in particular it is > > problematic as you can't easily construct a compiler commandline that > > would cover all relative path options. > > > > At first glance this is relatively straight forward, for example: > > > > Index: gcc-12.1.0/gcc/file-prefix-map.cc > > =================================================================== > > --- gcc-12.1.0.orig/gcc/file-prefix-map.cc > > +++ gcc-12.1.0/gcc/file-prefix-map.cc > > @@ -70,19 +70,25 @@ remap_filename (file_prefix_map *maps, c > > file_prefix_map *map; > > char *s; > > const char *name; > > + char *realname; > > size_t name_len; > > > > + realname = lrealpath (filename); > > + > > for (map = maps; map; map = map->next) > > - if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0) > > + if (filename_ncmp (realname, map->old_prefix, map->old_len) == 0) > > break; > > - if (!map) > > + if (!map) { > > + free (realname); > > return filename; > > - name = filename + map->old_len; > > + } > > + name = realname + map->old_len; > > name_len = strlen (name) + 1; > > > > s = (char *) ggc_alloc_atomic (name_len + map->new_len); > > memcpy (s, map->new_prefix, map->new_len); > > memcpy (s + map->new_len, name, name_len); > > + free (realname); > > return s; > > } > > > > which address a realpath() call into the prefix mapping code. I did > > experiment with this and found it breaks compiling ruby and xen-tools > > which both have code which does: > > > > #include __FILE__ > > > > It may be possible to make the remapping conditional of not being > > directly in a #include statement but I didn't find the gcc code > > responsible for that as yet. I also noticed some valgrind tests fails > > after it, I've not looked into why that would be yet. > > > > I wanted to ask if there would be any interest in adding support for > > something like this? I suspect the include/__FILE__ issue is probably a > > latent bug anyway. If anyone has any pointers to the code I could > > improve my patch with I'm also happy to have them! > > To answer my own question, something like: > > +Index: gcc-12.1.0/libcpp/macro.cc > +=================================================================== > +--- gcc-12.1.0.orig/libcpp/macro.cc > ++++ gcc-12.1.0/libcpp/macro.cc > +@@ -563,7 +563,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi > + if (!name) > + abort (); > + } > +- if (pfile->cb.remap_filename) > ++ if (pfile->cb.remap_filename && !pfile->state.in_directive) > + name = pfile->cb.remap_filename (name); > + len = strlen (name); > + buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); > > seems to do roughly what I was wondering about. > > I'd be interested to understand whether some patch along the lines I've > mentioned here would stand a chance of being accepted or not.
Thanks for recognizing this issue and proposing a solution. It's probably more effective to submit this as an actual patch to gcc-patches and cc David Malcolm, libcpp maintainer, than to ask hypotheticals on the GCC mailing list. Thanks, David