]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: #include rewrite and installed compiler [PR123879]
authorJason Merrill <jason@redhat.com>
Tue, 21 Apr 2026 20:23:37 +0000 (16:23 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 21 Apr 2026 21:14:25 +0000 (17:14 -0400)
In an installed compiler, the pathname in the expanded_location might be
canonicalized while the pathname in the cpp_dir is not, e.g. eloc.file is
".../include/c++/16.0.1/stdbit.h" while dir->name is
".../lib/gcc/x86_64-pc-linux-gnu/16.0.1/../../../../include/c++/16.0.1".  So
let's use lrealpath to compare canonical paths for both.  And filename_ncmp,
while we're at it.

PR c++/123879

gcc/cp/ChangeLog:

* module.cc (maybe_translate_include): Use lrealpath in check
whether we're including something in the same directory.

gcc/cp/module.cc

index e5547f7d029fd5a1e23a5b6527c20096f50ea01f..a7245aae46a1201281b32bbbfa3869958d77f20b 100644 (file)
@@ -23131,13 +23131,22 @@ maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
       /* Redirect importable <name> to <bits/stdc++.h>.  */
       /* ??? Generalize to use a .json.  */
       expanded_location eloc = expand_location (loc);
+      auto indir = [](const char *f, const char *d)
+      {
+       if (!filename_ncmp (f, d, strlen (d))) return true;
+       /* Also check canonical paths (c++/123879).  */
+       auto cf = lrealpath (f); auto cd = lrealpath (d);
+       bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
+       free (cf); free (cd);
+       return r;
+      };
       if (angle && is_importable_header (fname)
          /* Exclude <version> which often goes with import std.  */
          && strcmp (fname, "version") != 0
          /* Don't redirect #includes between headers under the same include
             path directory (i.e. between library headers); if the import
             brings in the current file we then get redefinition errors.  */
-         && !strstr (eloc.file, _cpp_get_file_dir (file)->name)
+         && !indir (eloc.file, _cpp_get_file_dir (file)->name)
          /* ??? These are needed when running a toolchain from the build
             directory, because libsupc++ headers aren't linked into
             libstdc++-v3/include with the other headers.  */