Skip to content

Commit f24aa69

Browse files
author
Dmitry Polukhin
committed
[clang] Match -isysroot behaviour with system compiler on Darwin
See discussion in https://reviews.llvm.org/D89001 I updated test to match actual behavior of /Applications/Xcode_14.3.1_14E300b.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang after that modified upstream to match the test. Differential Revision: https://reviews.llvm.org/D157283
1 parent 7fd9f0f commit f24aa69

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,24 +2465,34 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
24652465
// Also check whether this is used for setting library search paths.
24662466
ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs, CC1Args);
24672467

2468-
if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
2469-
options::OPT_nostdincxx))
2468+
if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdincxx))
24702469
return;
24712470

24722471
llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs);
24732472

24742473
switch (GetCXXStdlibType(DriverArgs)) {
24752474
case ToolChain::CST_Libcxx: {
24762475
// On Darwin, libc++ can be installed in one of the following two places:
2477-
// 1. Alongside the compiler in <install>/include/c++/v1
2478-
// 2. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
2476+
// 1. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
2477+
// 2. Alongside the compiler in <install>/include/c++/v1
24792478
//
24802479
// The precendence of paths is as listed above, i.e. we take the first path
24812480
// that exists. Also note that we never include libc++ twice -- we take the
24822481
// first path that exists and don't send the other paths to CC1 (otherwise
24832482
// include_next could break).
24842483

24852484
// Check for (1)
2485+
llvm::SmallString<128> SysrootUsr = Sysroot;
2486+
llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
2487+
if (getVFS().exists(SysrootUsr)) {
2488+
addSystemInclude(DriverArgs, CC1Args, SysrootUsr);
2489+
return;
2490+
} else if (DriverArgs.hasArg(options::OPT_v)) {
2491+
llvm::errs() << "ignoring nonexistent directory \"" << SysrootUsr
2492+
<< "\"\n";
2493+
}
2494+
2495+
// Otherwise, check for (2)
24862496
// Get from '<install>/bin' to '<install>/include/c++/v1'.
24872497
// Note that InstallBin can be relative, so we use '..' instead of
24882498
// parent_path.
@@ -2497,17 +2507,6 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
24972507
<< "\"\n";
24982508
}
24992509

2500-
// Otherwise, check for (2)
2501-
llvm::SmallString<128> SysrootUsr = Sysroot;
2502-
llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
2503-
if (getVFS().exists(SysrootUsr)) {
2504-
addSystemInclude(DriverArgs, CC1Args, SysrootUsr);
2505-
return;
2506-
} else if (DriverArgs.hasArg(options::OPT_v)) {
2507-
llvm::errs() << "ignoring nonexistent directory \"" << SysrootUsr
2508-
<< "\"\n";
2509-
}
2510-
25112510
// Otherwise, don't add any path.
25122511
break;
25132512
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
// CHECK-LIBCXX-SYSROOT-1-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
5454

5555
// Check with both headers in the sysroot and headers alongside the installation
56-
// (the headers in the toolchain should be preferred over the <sysroot> headers).
56+
// (the <sysroot> headers should be preferred over the headers in the toolchain).
5757
// Ensure that both -isysroot and --sysroot work, and that isysroot has precedence
5858
// over --sysroot.
5959
//
@@ -89,10 +89,10 @@
8989
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
9090
//
9191
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-cc1"
92-
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
93-
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
92+
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
93+
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
9494

95-
// Make sure that using -nostdinc, -nostdinc++ or -nostdlib will drop both the toolchain
95+
// Make sure that using -nostdinc++ or -nostdlib will drop both the toolchain
9696
// C++ include path and the sysroot one.
9797
//
9898
// RUN: %clang -### %s -fsyntax-only 2>&1 \
@@ -104,9 +104,9 @@
104104
// RUN: -nostdinc \
105105
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
106106
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
107-
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDLIBINC %s
107+
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC %s
108108
// CHECK-LIBCXX-NOSTDINC: "-cc1"
109-
// CHECK-LIBCXX-NOSTDINC-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
109+
// CHECK-LIBCXX-NOSTDINC: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
110110
// CHECK-LIBCXX-NOSTDINC-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
111111
//
112112
// RUN: %clang -### %s -fsyntax-only 2>&1 \
@@ -157,8 +157,8 @@
157157
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
158158
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
159159
// RUN: --check-prefix=CHECK-LIBCXX-MISSING-BOTH %s
160-
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
161160
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[SYSROOT]]/usr/include/c++/v1"
161+
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
162162

163163
// Make sure that on Darwin, we use libc++ header search paths by default even when
164164
// -stdlib= isn't passed.

0 commit comments

Comments
 (0)