Skip to content

Commit e6329a0

Browse files
committed
[clang] Don't look into <sysroot> for C++ headers if they are found alongside the toolchain
Currently, Clang looks for libc++ headers alongside the installation directory of Clang, and it also adds a search path for headers in the -isysroot. This is problematic if headers are found in both the toolchain and in the sysroot, since #include_next will end up finding the libc++ headers in the sysroot instead of the intended system headers. This patch changes the logic such that if the toolchain contains libc++ headers, no C++ header paths are added in the sysroot. However, if the toolchain does *not* contain libc++ headers, the sysroot is searched as usual. This should not be a breaking change, since any code that previously relied on some libc++ headers being found in the sysroot suffered from the #include_next issue described above, which renders any libc++ header basically useless. Differential Revision: https://reviews.llvm.org/D89001
1 parent 0d98305 commit e6329a0

File tree

4 files changed

+112
-36
lines changed

4 files changed

+112
-36
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,21 +2098,42 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
20982098

20992099
switch (GetCXXStdlibType(DriverArgs)) {
21002100
case ToolChain::CST_Libcxx: {
2101-
// On Darwin, libc++ is installed alongside the compiler in
2102-
// include/c++/v1, so get from '<install>/bin' to '<install>/include/c++/v1'.
2103-
{
2104-
llvm::SmallString<128> P = llvm::StringRef(getDriver().getInstalledDir());
2105-
// Note that P can be relative, so we have to '..' and not parent_path.
2106-
llvm::sys::path::append(P, "..", "include", "c++", "v1");
2107-
addSystemInclude(DriverArgs, CC1Args, P);
2101+
// On Darwin, libc++ can be installed in one of the following two places:
2102+
// 1. Alongside the compiler in <install>/include/c++/v1
2103+
// 2. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
2104+
//
2105+
// The precendence of paths is as listed above, i.e. we take the first path
2106+
// that exists. Also note that we never include libc++ twice -- we take the
2107+
// first path that exists and don't send the other paths to CC1 (otherwise
2108+
// include_next could break).
2109+
2110+
// Check for (1)
2111+
// Get from '<install>/bin' to '<install>/include/c++/v1'.
2112+
// Note that InstallBin can be relative, so we use '..' instead of
2113+
// parent_path.
2114+
llvm::SmallString<128> InstallBin =
2115+
llvm::StringRef(getDriver().getInstalledDir()); // <install>/bin
2116+
llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
2117+
if (getVFS().exists(InstallBin)) {
2118+
addSystemInclude(DriverArgs, CC1Args, InstallBin);
2119+
return;
2120+
} else if (DriverArgs.hasArg(options::OPT_v)) {
2121+
llvm::errs() << "ignoring nonexistent directory \"" << InstallBin
2122+
<< "\"\n";
21082123
}
2109-
// Also add <sysroot>/usr/include/c++/v1 unless -nostdinc is used,
2110-
// to match the legacy behavior in CC1.
2111-
if (!DriverArgs.hasArg(options::OPT_nostdinc)) {
2112-
llvm::SmallString<128> P = Sysroot;
2113-
llvm::sys::path::append(P, "usr", "include", "c++", "v1");
2114-
addSystemInclude(DriverArgs, CC1Args, P);
2124+
2125+
// Otherwise, check for (2)
2126+
llvm::SmallString<128> SysrootUsr = Sysroot;
2127+
llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
2128+
if (getVFS().exists(SysrootUsr)) {
2129+
addSystemInclude(DriverArgs, CC1Args, SysrootUsr);
2130+
return;
2131+
} else if (DriverArgs.hasArg(options::OPT_v)) {
2132+
llvm::errs() << "ignoring nonexistent directory \"" << SysrootUsr
2133+
<< "\"\n";
21152134
}
2135+
2136+
// Otherwise, don't add any path.
21162137
break;
21172138
}
21182139

clang/test/Driver/Inputs/basic_darwin_sdk_usr_cxx_v1/usr/include/c++/v1/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/basic_darwin_sdk_usr_cxx_v1/usr/lib/.keep

Whitespace-only changes.

clang/test/Driver/darwin-header-search-libcxx.cpp

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,57 @@
1313
// RUN: | FileCheck --check-prefix=CHECK-LIBCXX-NONE %s
1414
// CHECK-LIBCXX-NONE: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
1515

16-
// Check with only headers alongside the installation (those should be used,
17-
// but we should still add /usr/include/c++/v1 after to preserve legacy).
16+
// Check with only headers alongside the installation (those should be used).
1817
//
1918
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
2019
// RUN: -target x86_64-apple-darwin \
2120
// RUN: -stdlib=libc++ \
2221
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
2322
// RUN: --sysroot="" \
24-
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain --check-prefix=CHECK-LIBCXX-TOOLCHAIN-1 %s
23+
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
24+
// RUN: --check-prefix=CHECK-LIBCXX-TOOLCHAIN-1 %s
2525
// CHECK-LIBCXX-TOOLCHAIN-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
2626
// CHECK-LIBCXX-TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
27-
// CHECK-LIBCXX-TOOLCHAIN-1: "-internal-isystem" "/usr/include/c++/v1"
27+
// CHECK-LIBCXX-TOOLCHAIN-1-NOT: "-internal-isystem" "/usr/include/c++/v1"
2828
//
2929
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
3030
// RUN: -target x86_64-apple-darwin \
3131
// RUN: -stdlib=libc++ \
3232
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
3333
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
34-
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain --check-prefix=CHECK-LIBCXX-TOOLCHAIN-2 %s
34+
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
35+
// RUN: -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
36+
// RUN: --check-prefix=CHECK-LIBCXX-TOOLCHAIN-2 %s
3537
// CHECK-LIBCXX-TOOLCHAIN-2: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
3638
// CHECK-LIBCXX-TOOLCHAIN-2: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
39+
// CHECK-LIBCXX-TOOLCHAIN-2-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
40+
41+
// Check with only headers in the sysroot (those should be used).
42+
//
43+
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
44+
// RUN: -target x86_64-apple-darwin \
45+
// RUN: -stdlib=libc++ \
46+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
47+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
48+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
49+
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
50+
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT-1 %s
51+
// CHECK-LIBCXX-SYSROOT-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
52+
// CHECK-LIBCXX-SYSROOT-1: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
53+
// CHECK-LIBCXX-SYSROOT-1-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
3754

3855
// Check with both headers in the sysroot and headers alongside the installation
39-
// (the headers in <sysroot> should be added after the toolchain headers).
40-
// Ensure that both -isysroot and --sysroot work, and that isysroot has precedence.
56+
// (the headers in the toolchain should be preferred over the <sysroot> headers).
57+
// Ensure that both -isysroot and --sysroot work, and that isysroot has precedence
58+
// over --sysroot.
4159
//
4260
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
4361
// RUN: -target x86_64-apple-darwin \
4462
// RUN: -stdlib=libc++ \
4563
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
4664
// RUN: -resource-dir=%S/Inputs/resource_dir \
47-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
48-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
65+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
66+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
4967
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
5068
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
5169
//
@@ -54,8 +72,8 @@
5472
// RUN: -stdlib=libc++ \
5573
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
5674
// RUN: -resource-dir=%S/Inputs/resource_dir \
57-
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_usr \
58-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
75+
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
76+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
5977
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
6078
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
6179
//
@@ -64,32 +82,46 @@
6482
// RUN: -stdlib=libc++ \
6583
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
6684
// RUN: -resource-dir=%S/Inputs/resource_dir \
67-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
85+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
6886
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
69-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
87+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
7088
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
7189
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
7290
//
7391
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
7492
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
75-
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
93+
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
7694

77-
// Make sure that using -nostdinc will drop the sysroot C++ library include
78-
// path, but not the toolchain one.
95+
// Make sure that using -nostdinc does not drop any C++ library include path.
96+
// This behavior is strange, but it is compatible with the legacy CC1 behavior.
7997
//
8098
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
8199
// RUN: -target x86_64-apple-darwin16 \
82100
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
83101
// RUN: -resource-dir=%S/Inputs/resource_dir \
84-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
102+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
85103
// RUN: -stdlib=platform \
86104
// RUN: -nostdinc \
87-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
105+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
88106
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
89-
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC %s
90-
// CHECK-LIBCXX-NOSTDINC: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
91-
// CHECK-LIBCXX-NOSTDINC: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
92-
// CHECK-LIBCXX-NOSTDINC-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
107+
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC-1 %s
108+
// CHECK-LIBCXX-NOSTDINC-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
109+
// CHECK-LIBCXX-NOSTDINC-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
110+
// CHECK-LIBCXX-NOSTDINC-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
111+
//
112+
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
113+
// RUN: -target x86_64-apple-darwin16 \
114+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
115+
// RUN: -resource-dir=%S/Inputs/resource_dir \
116+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
117+
// RUN: -stdlib=platform \
118+
// RUN: -nostdinc \
119+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
120+
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
121+
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC-2 %s
122+
// CHECK-LIBCXX-NOSTDINC-2: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
123+
// CHECK-LIBCXX-NOSTDINC-2: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
124+
// CHECK-LIBCXX-NOSTDINC-2-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
93125

94126
// Make sure that using -nostdinc++ or -nostdlib will drop both the toolchain
95127
// C++ include path and the sysroot one.
@@ -98,7 +130,7 @@
98130
// RUN: -target x86_64-apple-darwin16 \
99131
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
100132
// RUN: -resource-dir=%S/Inputs/resource_dir \
101-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
133+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
102134
// RUN: -stdlib=platform \
103135
// RUN: -nostdinc++ \
104136
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
@@ -121,3 +153,26 @@
121153
// CHECK-LIBCXX-NOSTDLIBINC: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
122154
// CHECK-LIBCXX-NOSTDLIBINC-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
123155
// CHECK-LIBCXX-NOSTDLIBINC-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
156+
157+
// Make sure we explain that we considered a path but didn't add it when it
158+
// doesn't exist.
159+
//
160+
// RUN: %clang -no-canonical-prefixes %s -fsyntax-only -v 2>&1 \
161+
// RUN: -target x86_64-apple-darwin \
162+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
163+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk \
164+
// RUN: -stdlib=libc++ \
165+
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
166+
// RUN: --check-prefix=CHECK-LIBCXX-MISSING-TOOLCHAIN %s
167+
// CHECK-LIBCXX-MISSING-TOOLCHAIN: ignoring nonexistent directory "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
168+
//
169+
// RUN: %clang -no-canonical-prefixes %s -fsyntax-only -v 2>&1 \
170+
// RUN: -target x86_64-apple-darwin \
171+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
172+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
173+
// RUN: -stdlib=libc++ \
174+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
175+
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
176+
// RUN: --check-prefix=CHECK-LIBCXX-MISSING-BOTH %s
177+
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
178+
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[SYSROOT]]/usr/include/c++/v1"

0 commit comments

Comments
 (0)