[clang] [clang][driver] Add \<executable\>/../include/c++/v1 to include path on Darwin (PR #70817)

Liviu Ionescu via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 12 14:38:07 PST 2023


https://github.com/ilg-ul updated https://github.com/llvm/llvm-project/pull/70817

>From 7fbc229ee7316d826517480ee7896c91dad941f3 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Tue, 31 Oct 2023 17:09:04 +0200
Subject: [PATCH 1/6] Add \<executable\>/../include/c++/v1 to include path

On macOS, when clang is invoked via a symlink, since the InstalledDir is
where the link is located, the C++ headers are not identified and the
default system headers are used.

This fix adds a second check using the folder where the executable is
located.
---
 clang/lib/Driver/ToolChains/Darwin.cpp | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index f28e08d81bf29b4..de55307385966cf 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2494,6 +2494,19 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
                    << "\"\n";
     }
 
+    // Check for the folder where the executable is located, if different.
+    if (getDriver().getInstalledDir() != getDriver().Dir) {
+      InstallBin = llvm::StringRef(getDriver().Dir.c_str());
+      llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
+      if (getVFS().exists(InstallBin)) {
+        addSystemInclude(DriverArgs, CC1Args, InstallBin);
+        return;
+      } else if (DriverArgs.hasArg(options::OPT_v)) {
+        llvm::errs() << "ignoring nonexistent directory \"" << InstallBin
+                     << "\"\n";
+      }
+    }
+
     // Otherwise, check for (2)
     llvm::SmallString<128> SysrootUsr = Sysroot;
     llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");

>From b2f287c6a53697ac9bdce3eaa1ce55d345d734b1 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Fri, 10 Nov 2023 12:05:50 +0200
Subject: [PATCH 2/6] Darwin.cpp: update comments

---
 clang/lib/Driver/ToolChains/Darwin.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index de55307385966cf..e6bcf0227d7dc65 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2470,14 +2470,19 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
 
   switch (GetCXXStdlibType(DriverArgs)) {
   case ToolChain::CST_Libcxx: {
-    // On Darwin, libc++ can be installed in one of the following two places:
+    // On Darwin, libc++ can be installed in one of the following places:
     // 1. Alongside the compiler in         <install>/include/c++/v1
-    // 2. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
+    // 2. Alongside the compiler in         <clang-executable-location>/../include/c++/v1
+    // 3. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
     //
-    // The precendence of paths is as listed above, i.e. we take the first path
-    // that exists. Also note that we never include libc++ twice -- we take the
-    // first path that exists and don't send the other paths to CC1 (otherwise
+    // The precedence of paths is as listed above, i.e. we take the first path
+    // that exists. Note that we never include libc++ twice -- we take the first
+    // path that exists and don't send the other paths to CC1 (otherwise
     // include_next could break).
+    //
+    // Also note that in most cases, (1) and (2) are exactly the same path.
+    // Those two paths will differ only when the `clang` program being run
+    // is actually a symlink to the real executable.
 
     // Check for (1)
     // Get from '<install>/bin' to '<install>/include/c++/v1'.
@@ -2494,7 +2499,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
                    << "\"\n";
     }
 
-    // Check for the folder where the executable is located, if different.
+    // (2) Check for the folder where the executable is located, if different.
     if (getDriver().getInstalledDir() != getDriver().Dir) {
       InstallBin = llvm::StringRef(getDriver().Dir.c_str());
       llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");

>From 31b7482ffabda61c005a4cd90a07bcfec65c232e Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Fri, 10 Nov 2023 12:08:51 +0200
Subject: [PATCH 3/6] Darwin.cpp: construct StringRef from string

---
 clang/lib/Driver/ToolChains/Darwin.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index e6bcf0227d7dc65..d89b6d60f1852e4 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2501,7 +2501,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
 
     // (2) Check for the folder where the executable is located, if different.
     if (getDriver().getInstalledDir() != getDriver().Dir) {
-      InstallBin = llvm::StringRef(getDriver().Dir.c_str());
+      InstallBin = llvm::StringRef(getDriver().Dir);
       llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
       if (getVFS().exists(InstallBin)) {
         addSystemInclude(DriverArgs, CC1Args, InstallBin);

>From 6db76fecae69764ad6f66e2b30e3b093d9a91f40 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Fri, 10 Nov 2023 12:39:31 +0200
Subject: [PATCH 4/6] Darwin.cpp: reformat comments

---
 clang/lib/Driver/ToolChains/Darwin.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index d89b6d60f1852e4..1f61bb02c6ae226 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2471,8 +2471,8 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
   switch (GetCXXStdlibType(DriverArgs)) {
   case ToolChain::CST_Libcxx: {
     // On Darwin, libc++ can be installed in one of the following places:
-    // 1. Alongside the compiler in         <install>/include/c++/v1
-    // 2. Alongside the compiler in         <clang-executable-location>/../include/c++/v1
+    // 1. Alongside the compiler in <install>/include/c++/v1
+    // 2. Alongside the compiler in <clang-executable-folder>/../include/c++/v1
     // 3. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
     //
     // The precedence of paths is as listed above, i.e. we take the first path
@@ -2512,7 +2512,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
       }
     }
 
-    // Otherwise, check for (2)
+    // Otherwise, check for (3)
     llvm::SmallString<128> SysrootUsr = Sysroot;
     llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
     if (getVFS().exists(SysrootUsr)) {

>From 04b69133a1482c17e927306c1a77075d51d87315 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Sun, 12 Nov 2023 23:51:30 +0200
Subject: [PATCH 5/6] add test

---
 clang/test/Driver/darwin-header-search-libcxx.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index cc8ec9ceb89b3ab..e31c6ee182e65d0 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -172,3 +172,14 @@
 // RUN:               --check-prefix=CHECK-LIBCXX-STDLIB-UNSPECIFIED %s
 // CHECK-LIBCXX-STDLIB-UNSPECIFIED: "-cc1"
 // CHECK-LIBCXX-STDLIB-UNSPECIFIED: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
+
+// Make sure the target folder has no toolchain structure.
+// RUN: rm -rf %t/xpacks
+// RUN: mkdir -pv %t/xpacks/.bin
+// RUN: ln -s %clang++ %t/xpacks/.bin/clang++
+
+// RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
+// RUN:     --target=x86_64-apple-darwin \
+// RUN:   | FileCheck -DRESOURCE=%S/Inputs/resource_dir \
+// RUN:               --check-prefix=CHECK-SAME %s
+// CHECK-SAME: "-internal-isystem" "[[RESOURCE]]/bin/../include/c++/v1"

>From fc1108ed4d721f7070152cde131abccf46cf3b41 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 00:37:51 +0200
Subject: [PATCH 6/6] fix test

---
 clang/test/Driver/darwin-header-search-libcxx.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index e31c6ee182e65d0..8b63362a96ded99 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -176,10 +176,11 @@
 // Make sure the target folder has no toolchain structure.
 // RUN: rm -rf %t/xpacks
 // RUN: mkdir -pv %t/xpacks/.bin
-// RUN: ln -s %clang++ %t/xpacks/.bin/clang++
+// RUN: ln -s %clang_cpp %t/xpacks/.bin/clang++
 
 // RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
 // RUN:     --target=x86_64-apple-darwin \
 // RUN:   | FileCheck -DRESOURCE=%S/Inputs/resource_dir \
-// RUN:               --check-prefix=CHECK-SAME %s
-// CHECK-SAME: "-internal-isystem" "[[RESOURCE]]/bin/../include/c++/v1"
+// RUN:               --check-prefix=CHECK-LIBCXX-TOOLCHAIN-3 %s
+// CHECK-LIBCXX-TOOLCHAIN-3: "-internal-isystem" "{{.+}}/bin/../include/c++/v1"
+// CHECK-LIBCXX-TOOLCHAIN-3-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"



More information about the cfe-commits mailing list