Skip to content

Commit 475ac3e

Browse files
[swift/release/6.2][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 llvm#75638 rdar://149969643
1 parent 262cfab commit 475ac3e

File tree

20 files changed

+273
-74
lines changed

20 files changed

+273
-74
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8221,6 +8221,11 @@ def objc_isystem : Separate<["-"], "objc-isystem">,
82218221
def objcxx_isystem : Separate<["-"], "objcxx-isystem">,
82228222
MetaVarName<"<directory>">,
82238223
HelpText<"Add directory to the ObjC++ SYSTEM include search path">;
8224+
def internal_iframework : Separate<["-"], "internal-iframework">,
8225+
MetaVarName<"<directory>">,
8226+
HelpText<"Add directory to the internal system framework search path; these "
8227+
"are assumed to not be user-provided and are used to model system "
8228+
"and standard frameworks' paths.">;
82248229
def internal_isystem : Separate<["-"], "internal-isystem">,
82258230
MetaVarName<"<directory>">,
82268231
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
@@ -227,6 +227,9 @@ class ToolChain {
227227

228228
/// \name Utilities for implementing subclasses.
229229
///@{
230+
static void addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
231+
llvm::opt::ArgStringList &CC1Args,
232+
const Twine &Path);
230233
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
231234
llvm::opt::ArgStringList &CC1Args,
232235
const Twine &Path);
@@ -237,6 +240,9 @@ class ToolChain {
237240
addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
238241
llvm::opt::ArgStringList &CC1Args,
239242
const Twine &Path);
243+
static void addSystemFrameworkIncludes(const llvm::opt::ArgList &DriverArgs,
244+
llvm::opt::ArgStringList &CC1Args,
245+
ArrayRef<StringRef> Paths);
240246
static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
241247
llvm::opt::ArgStringList &CC1Args,
242248
ArrayRef<StringRef> Paths);

clang/lib/Driver/Job.cpp

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

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

clang/lib/Driver/ToolChain.cpp

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

1181+
/// Utility function to add a system framework directory to CC1 arguments.
1182+
void ToolChain::addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
1183+
llvm::opt::ArgStringList &CC1Args,
1184+
const Twine &Path) {
1185+
CC1Args.push_back("-internal-iframework");
1186+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1187+
}
1188+
11811189
/// Utility function to add a system include directory to CC1 arguments.
1182-
/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
1183-
ArgStringList &CC1Args,
1184-
const Twine &Path) {
1190+
void ToolChain::addSystemInclude(const ArgList &DriverArgs,
1191+
ArgStringList &CC1Args, const Twine &Path) {
11851192
CC1Args.push_back("-internal-isystem");
11861193
CC1Args.push_back(DriverArgs.MakeArgString(Path));
11871194
}
@@ -1194,9 +1201,9 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
11941201
/// "C" semantics. These semantics are *ignored* by and large today, but its
11951202
/// important to preserve the preprocessor changes resulting from the
11961203
/// classification.
1197-
/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
1198-
ArgStringList &CC1Args,
1199-
const Twine &Path) {
1204+
void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
1205+
ArgStringList &CC1Args,
1206+
const Twine &Path) {
12001207
CC1Args.push_back("-internal-externc-isystem");
12011208
CC1Args.push_back(DriverArgs.MakeArgString(Path));
12021209
}
@@ -1208,19 +1215,28 @@ void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
12081215
addExternCSystemInclude(DriverArgs, CC1Args, Path);
12091216
}
12101217

1218+
/// Utility function to add a list of system framework directories to CC1.
1219+
void ToolChain::addSystemFrameworkIncludes(const ArgList &DriverArgs,
1220+
ArgStringList &CC1Args,
1221+
ArrayRef<StringRef> Paths) {
1222+
for (const auto &Path : Paths) {
1223+
CC1Args.push_back("-internal-iframework");
1224+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1225+
}
1226+
}
1227+
12111228
/// Utility function to add a list of system include directories to CC1.
1212-
/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
1213-
ArgStringList &CC1Args,
1214-
ArrayRef<StringRef> Paths) {
1229+
void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
1230+
ArgStringList &CC1Args,
1231+
ArrayRef<StringRef> Paths) {
12151232
for (const auto &Path : Paths) {
12161233
CC1Args.push_back("-internal-isystem");
12171234
CC1Args.push_back(DriverArgs.MakeArgString(Path));
12181235
}
12191236
}
12201237

1221-
/*static*/ std::string ToolChain::concat(StringRef Path, const Twine &A,
1222-
const Twine &B, const Twine &C,
1223-
const Twine &D) {
1238+
std::string ToolChain::concat(StringRef Path, const Twine &A, const Twine &B,
1239+
const Twine &C, const Twine &D) {
12241240
SmallString<128> Result(Path);
12251241
llvm::sys::path::append(Result, llvm::sys::path::Style::posix, A, B, C, D);
12261242
return std::string(Result);

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,26 @@ void AppleMachO::AddClangSystemIncludeArgs(
26692669
}
26702670
}
26712671

2672+
void DarwinClang::AddClangSystemIncludeArgs(
2673+
const llvm::opt::ArgList &DriverArgs,
2674+
llvm::opt::ArgStringList &CC1Args) const {
2675+
AppleMachO::AddClangSystemIncludeArgs(DriverArgs, CC1Args);
2676+
2677+
if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc))
2678+
return;
2679+
2680+
llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs);
2681+
2682+
// Add <sysroot>/System/Library/Frameworks
2683+
// Add <sysroot>/System/Library/SubFrameworks
2684+
// Add <sysroot>/Library/Frameworks
2685+
SmallString<128> P1(Sysroot), P2(Sysroot), P3(Sysroot);
2686+
llvm::sys::path::append(P1, "System", "Library", "Frameworks");
2687+
llvm::sys::path::append(P2, "System", "Library", "SubFrameworks");
2688+
llvm::sys::path::append(P3, "Library", "Frameworks");
2689+
addSystemFrameworkIncludes(DriverArgs, CC1Args, {P1, P2, P3});
2690+
}
2691+
26722692
bool DarwinClang::AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
26732693
llvm::opt::ArgStringList &CC1Args,
26742694
llvm::SmallString<128> Base,

clang/lib/Driver/ToolChains/Darwin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
642642
/// @name Apple ToolChain Implementation
643643
/// {
644644

645+
void
646+
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
647+
llvm::opt::ArgStringList &CC1Args) const override;
648+
645649
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override;
646650

647651
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
@@ -3505,6 +3505,8 @@ static void GenerateHeaderSearchArgs(const HeaderSearchOptions &Opts,
35053505
: OPT_internal_externc_isystem;
35063506
GenerateArg(Consumer, Opt, It->Path);
35073507
}
3508+
for (; It < End && Matches(*It, {frontend::System}, true, true); ++It)
3509+
GenerateArg(Consumer, OPT_internal_iframework, It->Path);
35083510

35093511
assert(It == End && "Unhandled HeaderSearchOption::Entry.");
35103512

@@ -3637,6 +3639,8 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
36373639
Group = frontend::ExternCSystem;
36383640
Opts.AddPath(A->getValue(), Group, false, true);
36393641
}
3642+
for (const auto *A : Args.filtered(OPT_internal_iframework))
3643+
Opts.AddPath(A->getValue(), frontend::System, true, true);
36403644

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

clang/lib/Lex/InitHeaderSearch.cpp

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

326+
if (triple.isOSDarwin())
327+
return false;
328+
326329
return true; // Everything else uses AddDefaultIncludePaths().
327330
}
328331

@@ -337,22 +340,6 @@ void InitHeaderSearch::AddDefaultIncludePaths(
337340
if (!ShouldAddDefaultIncludePaths(triple))
338341
return;
339342

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

clang/test/CAS/depscan-prefix-map.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// RUN: -isysroot %S/Inputs/SDK \
1010
// RUN: -resource-dir %S/Inputs/toolchain_dir/usr/lib/clang/1000 \
1111
// RUN: -internal-isystem %S/Inputs/toolchain_dir/usr/lib/clang/1000/include \
12+
// RUN: -internal-iframework %S/Inputs/SDK/Library/Frameworks \
1213
// RUN: -working-directory %t.d \
1314
// RUN: -fcas-path %t.d/cas \
1415
// RUN: -fdepscan-prefix-map=%S=/^source \
@@ -23,6 +24,7 @@
2324
// RUN: -isysroot %S/Inputs/SDK \
2425
// RUN: -resource-dir %S/Inputs/toolchain_dir/lib/clang/1000 \
2526
// RUN: -internal-isystem %S/Inputs/toolchain_dir/lib/clang/1000/include \
27+
// RUN: -internal-iframework %S/Inputs/SDK/Library/Frameworks \
2628
// RUN: -working-directory %t.d \
2729
// RUN: -fcas-path %t.d/cas \
2830
// RUN: -fdepscan-prefix-map=%S=/^source \
@@ -40,6 +42,7 @@
4042
// RUN: -isysroot %S/Inputs/SDK \
4143
// RUN: -resource-dir %S/Inputs/toolchain_dir/usr/lib/clang/1000 \
4244
// RUN: -internal-isystem %S/Inputs/toolchain_dir/usr/lib/clang/1000/include \
45+
// RUN: -internal-iframework %S/Inputs/SDK/Library/Frameworks \
4346
// RUN: -working-directory %t.d \
4447
// RUN: -fcas-path %t.d/cas \
4548
// RUN: -fdepscan-prefix-map=%S=/^source \

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
@@ -16,6 +16,7 @@ add_clang_unittest(FrontendTests
1616
PCHPreambleTest.cpp
1717
ReparseWorkingDirTest.cpp
1818
OutputStreamTest.cpp
19+
SearchPathTest.cpp
1920
TextDiagnosticTest.cpp
2021
UtilsTest.cpp
2122
)

0 commit comments

Comments
 (0)