Skip to content

Commit ba40d11

Browse files
[clang][Darwin] Remove legacy framework search path logic in the frontend
Move the Darwin framework search path logic from InitHeaderSearch::AddDefaultIncludePaths to DarwinClang::AddClangSystemIncludeArgs. Add a new -internal-iframework cc1 argument to support the tool chain adding these paths. Now that the tool chain is adding search paths via cc1 flag, they're only added if they exist, so the Preprocessor/cuda-macos-includes.cu test is no longer relevant. Change Driver/driverkit-path.c and Driver/darwin-subframeworks.c to do -### style testing similar to the darwin-header-search and darwin-embedded-search-paths tests. Rename darwin-subframeworks.c to darwin-framework-search-paths.c and have it test all framework search paths, not just SubFrameworks. Add a unit test to validate that the myriad of search path flags result in the expected search path list. Fixes #75638
1 parent eebb50a commit ba40d11

File tree

18 files changed

+270
-74
lines changed

18 files changed

+270
-74
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8451,6 +8451,11 @@ def objc_isystem : Separate<["-"], "objc-isystem">,
84518451
def objcxx_isystem : Separate<["-"], "objcxx-isystem">,
84528452
MetaVarName<"<directory>">,
84538453
HelpText<"Add directory to the ObjC++ SYSTEM include search path">;
8454+
def internal_iframework : Separate<["-"], "internal-iframework">,
8455+
MetaVarName<"<directory>">,
8456+
HelpText<"Add directory to the internal system framework search path; these "
8457+
"are assumed to not be user-provided and are used to model system "
8458+
"and standard frameworks' paths.">;
84548459
def internal_isystem : Separate<["-"], "internal-isystem">,
84558460
MetaVarName<"<directory>">,
84568461
HelpText<"Add directory to the internal system include search path; these "

clang/include/clang/Driver/ToolChain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class ToolChain {
226226

227227
/// \name Utilities for implementing subclasses.
228228
///@{
229+
static void addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
230+
llvm::opt::ArgStringList &CC1Args,
231+
const Twine &Path);
229232
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
230233
llvm::opt::ArgStringList &CC1Args,
231234
const Twine &Path);
@@ -236,6 +239,9 @@ class ToolChain {
236239
addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
237240
llvm::opt::ArgStringList &CC1Args,
238241
const Twine &Path);
242+
static void addSystemFrameworkIncludes(const llvm::opt::ArgList &DriverArgs,
243+
llvm::opt::ArgStringList &CC1Args,
244+
ArrayRef<StringRef> Paths);
239245
static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
240246
llvm::opt::ArgStringList &CC1Args,
241247
ArrayRef<StringRef> Paths);

clang/lib/Driver/Job.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
6767
return true;
6868

6969
// Some include flags shouldn't be skipped if we have a crash VFS
70-
IsInclude = llvm::StringSwitch<bool>(Flag)
71-
.Cases("-include", "-header-include-file", true)
72-
.Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
73-
.Cases("-internal-externc-isystem", "-iprefix", true)
74-
.Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
75-
.Cases("-isysroot", "-I", "-F", "-resource-dir", true)
76-
.Cases("-iframework", "-include-pch", true)
77-
.Default(false);
70+
IsInclude =
71+
llvm::StringSwitch<bool>(Flag)
72+
.Cases("-include", "-header-include-file", true)
73+
.Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
74+
.Cases("-internal-externc-isystem", "-iprefix", true)
75+
.Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
76+
.Cases("-isysroot", "-I", "-F", "-resource-dir", true)
77+
.Cases("-internal-iframework", "-iframework", "-include-pch", true)
78+
.Default(false);
7879
if (IsInclude)
7980
return !HaveCrashVFS;
8081

clang/lib/Driver/ToolChain.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,10 +1366,17 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
13661366
return *cxxStdlibType;
13671367
}
13681368

1369+
/// Utility function to add a system framework directory to CC1 arguments.
1370+
void ToolChain::addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
1371+
llvm::opt::ArgStringList &CC1Args,
1372+
const Twine &Path) {
1373+
CC1Args.push_back("-internal-iframework");
1374+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1375+
}
1376+
13691377
/// Utility function to add a system include directory to CC1 arguments.
1370-
/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
1371-
ArgStringList &CC1Args,
1372-
const Twine &Path) {
1378+
void ToolChain::addSystemInclude(const ArgList &DriverArgs,
1379+
ArgStringList &CC1Args, const Twine &Path) {
13731380
CC1Args.push_back("-internal-isystem");
13741381
CC1Args.push_back(DriverArgs.MakeArgString(Path));
13751382
}
@@ -1382,9 +1389,9 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
13821389
/// "C" semantics. These semantics are *ignored* by and large today, but its
13831390
/// important to preserve the preprocessor changes resulting from the
13841391
/// classification.
1385-
/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
1386-
ArgStringList &CC1Args,
1387-
const Twine &Path) {
1392+
void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
1393+
ArgStringList &CC1Args,
1394+
const Twine &Path) {
13881395
CC1Args.push_back("-internal-externc-isystem");
13891396
CC1Args.push_back(DriverArgs.MakeArgString(Path));
13901397
}
@@ -1396,19 +1403,28 @@ void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
13961403
addExternCSystemInclude(DriverArgs, CC1Args, Path);
13971404
}
13981405

1406+
/// Utility function to add a list of system framework directories to CC1.
1407+
void ToolChain::addSystemFrameworkIncludes(const ArgList &DriverArgs,
1408+
ArgStringList &CC1Args,
1409+
ArrayRef<StringRef> Paths) {
1410+
for (const auto &Path : Paths) {
1411+
CC1Args.push_back("-internal-iframework");
1412+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1413+
}
1414+
}
1415+
13991416
/// Utility function to add a list of system include directories to CC1.
1400-
/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
1401-
ArgStringList &CC1Args,
1402-
ArrayRef<StringRef> Paths) {
1417+
void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
1418+
ArgStringList &CC1Args,
1419+
ArrayRef<StringRef> Paths) {
14031420
for (const auto &Path : Paths) {
14041421
CC1Args.push_back("-internal-isystem");
14051422
CC1Args.push_back(DriverArgs.MakeArgString(Path));
14061423
}
14071424
}
14081425

1409-
/*static*/ std::string ToolChain::concat(StringRef Path, const Twine &A,
1410-
const Twine &B, const Twine &C,
1411-
const Twine &D) {
1426+
std::string ToolChain::concat(StringRef Path, const Twine &A, const Twine &B,
1427+
const Twine &C, const Twine &D) {
14121428
SmallString<128> Result(Path);
14131429
llvm::sys::path::append(Result, llvm::sys::path::Style::posix, A, B, C, D);
14141430
return std::string(Result);

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,27 @@ void AppleMachO::AddClangSystemIncludeArgs(
25772577
}
25782578
}
25792579

2580+
void DarwinClang::AddClangSystemIncludeArgs(
2581+
const llvm::opt::ArgList &DriverArgs,
2582+
llvm::opt::ArgStringList &CC1Args) const {
2583+
AppleMachO::AddClangSystemIncludeArgs(DriverArgs, CC1Args);
2584+
2585+
if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2586+
DriverArgs.hasArg(options::OPT_nostdlibinc))
2587+
return;
2588+
2589+
llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs);
2590+
2591+
// Add <sysroot>/System/Library/Frameworks
2592+
// Add <sysroot>/System/Library/SubFrameworks
2593+
// Add <sysroot>/Library/Frameworks
2594+
SmallString<128> P1(Sysroot), P2(Sysroot), P3(Sysroot);
2595+
llvm::sys::path::append(P1, "System", "Library", "Frameworks");
2596+
llvm::sys::path::append(P2, "System", "Library", "SubFrameworks");
2597+
llvm::sys::path::append(P3, "Library", "Frameworks");
2598+
addSystemFrameworkIncludes(DriverArgs, CC1Args, {P1, P2, P3});
2599+
}
2600+
25802601
bool DarwinClang::AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
25812602
llvm::opt::ArgStringList &CC1Args,
25822603
llvm::SmallString<128> Base,

clang/lib/Driver/ToolChains/Darwin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
647647
/// @name Apple ToolChain Implementation
648648
/// {
649649

650+
void
651+
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
652+
llvm::opt::ArgStringList &CC1Args) const override;
653+
650654
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override;
651655

652656
void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,8 @@ static void GenerateHeaderSearchArgs(const HeaderSearchOptions &Opts,
33733373
: OPT_internal_externc_isystem;
33743374
GenerateArg(Consumer, Opt, It->Path);
33753375
}
3376+
for (; It < End && Matches(*It, {frontend::System}, true, true); ++It)
3377+
GenerateArg(Consumer, OPT_internal_iframework, It->Path);
33763378

33773379
assert(It == End && "Unhandled HeaderSearchOption::Entry.");
33783380

@@ -3505,6 +3507,8 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
35053507
Group = frontend::ExternCSystem;
35063508
Opts.AddPath(A->getValue(), Group, false, true);
35073509
}
3510+
for (const auto *A : Args.filtered(OPT_internal_iframework))
3511+
Opts.AddPath(A->getValue(), frontend::System, true, true);
35083512

35093513
// Add the path prefixes which are implicitly treated as being system headers.
35103514
for (const auto *A :

clang/lib/Lex/InitHeaderSearch.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
321321
break;
322322
}
323323

324+
if (triple.isOSDarwin())
325+
return false;
326+
324327
return true; // Everything else uses AddDefaultIncludePaths().
325328
}
326329

@@ -335,22 +338,6 @@ void InitHeaderSearch::AddDefaultIncludePaths(
335338
if (!ShouldAddDefaultIncludePaths(triple))
336339
return;
337340

338-
// NOTE: some additional header search logic is handled in the driver for
339-
// Darwin.
340-
if (triple.isOSDarwin()) {
341-
if (HSOpts.UseStandardSystemIncludes) {
342-
// Add the default framework include paths on Darwin.
343-
if (triple.isDriverKit()) {
344-
AddPath("/System/DriverKit/System/Library/Frameworks", System, true);
345-
} else {
346-
AddPath("/System/Library/Frameworks", System, true);
347-
AddPath("/System/Library/SubFrameworks", System, true);
348-
AddPath("/Library/Frameworks", System, true);
349-
}
350-
}
351-
return;
352-
}
353-
354341
if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
355342
HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) {
356343
if (HSOpts.UseLibcxx) {

clang/test/Driver/Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/Library/Frameworks/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/System/Library/Frameworks/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/System/Library/SubFrameworks/.keep

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// UNSUPPORTED: system-windows
2+
// Windows is unsupported because we use the Unix path separator `/` in the test.
3+
4+
// RUN: %clang %s -target arm64-apple-macosx15.1 -isysroot %S/Inputs/MacOSX15.1.sdk -c %s -### 2>&1 \
5+
// RUN: | FileCheck -DSDKROOT=%S/Inputs/MacOSX15.1.sdk %s
6+
//
7+
// CHECK: "-cc1"
8+
// CHECK: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
9+
// CHECK-SAME: "-internal-iframework" "[[SDKROOT]]/System/Library/Frameworks"
10+
// CHECK-SAME: "-internal-iframework" "[[SDKROOT]]/System/Library/SubFrameworks"
11+
// CHECK-SAME: "-internal-iframework" "[[SDKROOT]]/Library/Frameworks"
12+
13+
// Verify that -nostdlibinc and -nostdinc removes the default search paths.
14+
//
15+
// RUN: %clang %s -target arm64-apple-macosx15.1 -isysroot %S/Inputs/MacOSX15.1.sdk -nostdinc -c %s -### 2>&1 \
16+
// RUN: | FileCheck --check-prefix=CHECK-NOSTD -DSDKROOT=%S/Inputs/MacOSX15.1.sdk %s
17+
//
18+
// RUN: %clang %s -target arm64-apple-macosx15.1 -isysroot %S/Inputs/MacOSX15.1.sdk -nostdlibinc -c %s -### 2>&1 \
19+
// RUN: | FileCheck --check-prefix=CHECK-NOSTD -DSDKROOT=%S/Inputs/MacOSX15.1.sdk %s
20+
//
21+
// CHECK-NOSTD: "-cc1"
22+
// CHECK-NOSTD: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
23+
// CHECK-NOSTD-NOT: "-internal-iframework"

clang/test/Driver/darwin-subframeworks.c

Lines changed: 0 additions & 18 deletions
This file was deleted.

clang/test/Driver/driverkit-path.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ int main() { return 0; }
2121
// LD64-NEW: "-isysroot" "[[PATH:[^"]*]]Inputs/DriverKit19.0.sdk"
2222
// LD64-NEW-NOT: "-L[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/usr/lib"
2323
// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/Frameworks"
24+
// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks"
2425

2526

26-
// RUN: %clang %s -target x86_64-apple-driverkit19.0 -isysroot %S/Inputs/DriverKit19.0.sdk -E -v -x c++ 2>&1 | FileCheck %s --check-prefix=INC
27+
// RUN: %clang %s -target x86_64-apple-driverkit19.0 -isysroot %S/Inputs/DriverKit19.0.sdk -x c++ -### 2>&1 \
28+
// RUN: | FileCheck %s -DSDKROOT=%S/Inputs/DriverKit19.0.sdk --check-prefix=INC
2729
//
28-
// INC: -isysroot [[PATH:[^ ]*/Inputs/DriverKit19.0.sdk]]
29-
// INC-LABEL: #include <...> search starts here:
30-
// INC: [[PATH]]/System/DriverKit/usr/local/include
31-
// INC: /lib{{(64)?}}/clang/{{[^/ ]+}}/include
32-
// INC: [[PATH]]/System/DriverKit/usr/include
33-
// INC: [[PATH]]/System/DriverKit/System/Library/Frameworks (framework directory)
30+
// INC: "-isysroot" "[[SDKROOT]]"
31+
// INC: "-internal-isystem" "[[SDKROOT]]/System/DriverKit/usr/local/include"
32+
// INC: "-internal-isystem" "{{.+}}/lib{{(64)?}}/clang/{{[^/ ]+}}/include"
33+
// INC: "-internal-externc-isystem" "[[SDKROOT]]/System/DriverKit/usr/include"
34+
// INC: "-internal-iframework" "[[SDKROOT]]/System/DriverKit/System/Library/Frameworks"
35+
// INC: "-internal-iframework" "[[SDKROOT]]/System/DriverKit/System/Library/SubFrameworks"

clang/test/Preprocessor/cuda-macos-includes.cu

Lines changed: 0 additions & 13 deletions
This file was deleted.

clang/unittests/Frontend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_clang_unittest(FrontendTests
1010
PCHPreambleTest.cpp
1111
ReparseWorkingDirTest.cpp
1212
OutputStreamTest.cpp
13+
SearchPathTest.cpp
1314
TextDiagnosticTest.cpp
1415
UtilsTest.cpp
1516
CLANG_LIBS

0 commit comments

Comments
 (0)