Skip to content

Commit 4b73f06

Browse files
committed
Reland "[lldb][ClangExpressionParser] Set BuiltinHeadersInSystemModules depending on SDK version (llvm#102309)"
Depends on llvm#102488 This reverts commit cf56e26. The original change was reverted because it was causing linker failures in the unit-tests: ``` Undefined symbols for architecture arm64: "lldb_private::PlatformDarwin::GetSDKPathFromDebugInfo(lldb_private::Module&)", referenced from: lldb_private::ClangExpressionParser::ClangExpressionParser(lldb_private::ExecutionContextScope*, lldb_private::Expression&, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>) in liblldbPluginExpressionParserClang.a[11](ClangExpressionParser.cpp.o) ld: symbol(s) not found for architecture arm64 c++: error: linker command failed with exit code 1 (use -v to see invocation) ``` The relanded version differs only in the fact that we now use the generic `Platform` abstraction to get to `GetSDKPathFromDebugInfo`.
1 parent d2f0b68 commit 4b73f06

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

lldb/include/lldb/Utility/XcodeSDK.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ class XcodeSDK {
8585
/// Whether LLDB feels confident importing Clang modules from this SDK.
8686
static bool SDKSupportsModules(Type type, llvm::VersionTuple version);
8787
static bool SDKSupportsModules(Type desired_type, const FileSpec &sdk_path);
88+
89+
/// Returns true if the SDK for the specified triple supports
90+
/// builtin modules in system headers.
91+
///
92+
/// NOTE: should be kept in sync with sdkSupportsBuiltinModules in
93+
/// Toolchains/Darwin.cpp
94+
///
95+
/// FIXME: this function will be removed once LLDB's ClangExpressionParser
96+
/// constructs the compiler instance through the driver/toolchain. See \ref
97+
/// SetupImportStdModuleLangOpts
98+
///
99+
static bool SDKSupportsBuiltinModules(const llvm::Triple &target_triple,
100+
llvm::VersionTuple sdk_version);
101+
88102
/// Return the canonical SDK name, such as "macosx" for the macOS SDK.
89103
static std::string GetCanonicalName(Info info);
90104
/// Return the best-matching SDK type for a specific triple.

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "clang/AST/ExternalASTSource.h"
1212
#include "clang/AST/PrettyPrinter.h"
1313
#include "clang/Basic/Builtins.h"
14+
#include "clang/Basic/DarwinSDKInfo.h"
1415
#include "clang/Basic/DiagnosticIDs.h"
1516
#include "clang/Basic/SourceLocation.h"
1617
#include "clang/Basic/TargetInfo.h"
@@ -39,6 +40,7 @@
3940
#include "llvm/ExecutionEngine/ExecutionEngine.h"
4041
#include "llvm/Support/CrashRecoveryContext.h"
4142
#include "llvm/Support/Debug.h"
43+
#include "llvm/Support/Error.h"
4244
#include "llvm/Support/FileSystem.h"
4345
#include "llvm/Support/TargetSelect.h"
4446

@@ -91,6 +93,8 @@
9193
#include "lldb/Utility/StringList.h"
9294

9395
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
96+
#include "Plugins/Platform/MacOSX/PlatformDarwin.h"
97+
#include "lldb/Utility/XcodeSDK.h"
9498

9599
#include <cctype>
96100
#include <memory>
@@ -279,6 +283,53 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
279283
std::string m_output;
280284
};
281285

286+
/// Returns true if the SDK for the specified triple supports
287+
/// builtin modules in system headers. This is used to decide
288+
/// whether to pass -fbuiltin-headers-in-system-modules to
289+
/// the compiler instance when compiling the `std` module.
290+
static llvm::Expected<bool>
291+
sdkSupportsBuiltinModules(lldb_private::Target &target) {
292+
#ifndef __APPLE__
293+
return false;
294+
#else
295+
auto arch_spec = target.GetArchitecture();
296+
auto const &triple = arch_spec.GetTriple();
297+
auto module_sp = target.GetExecutableModule();
298+
if (!module_sp)
299+
return llvm::createStringError("Executable module not found.");
300+
301+
// Get SDK path that the target was compiled against.
302+
auto platform_sp = target.GetPlatform();
303+
if (!platform_sp)
304+
return llvm::createStringError("No Platform plugin found on target.");
305+
306+
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(*module_sp);
307+
if (!sdk_or_err)
308+
return sdk_or_err.takeError();
309+
310+
// Use the SDK path from debug-info to find a local matching SDK directory.
311+
auto sdk_path_or_err =
312+
HostInfo::GetSDKRoot(HostInfo::SDKOptions{std::move(sdk_or_err->first)});
313+
if (!sdk_path_or_err)
314+
return sdk_path_or_err.takeError();
315+
316+
auto VFS = FileSystem::Instance().GetVirtualFileSystem();
317+
if (!VFS)
318+
return llvm::createStringError("No virtual filesystem available.");
319+
320+
// Extract SDK version from the /path/to/some.sdk/SDKSettings.json
321+
auto parsed_or_err = clang::parseDarwinSDKInfo(*VFS, *sdk_path_or_err);
322+
if (!parsed_or_err)
323+
return parsed_or_err.takeError();
324+
325+
auto maybe_sdk = *parsed_or_err;
326+
if (!maybe_sdk)
327+
return llvm::createStringError("Couldn't find Darwin SDK info.");
328+
329+
return XcodeSDK::SDKSupportsBuiltinModules(triple, maybe_sdk->getVersion());
330+
#endif
331+
}
332+
282333
static void SetupModuleHeaderPaths(CompilerInstance *compiler,
283334
std::vector<std::string> include_directories,
284335
lldb::TargetSP target_sp) {
@@ -561,7 +612,9 @@ static void SetupLangOpts(CompilerInstance &compiler,
561612
lang_opts.NoBuiltin = true;
562613
}
563614

564-
static void SetupImportStdModuleLangOpts(CompilerInstance &compiler) {
615+
static void SetupImportStdModuleLangOpts(CompilerInstance &compiler,
616+
lldb_private::Target &target) {
617+
Log *log = GetLog(LLDBLog::Expressions);
565618
LangOptions &lang_opts = compiler.getLangOpts();
566619
lang_opts.Modules = true;
567620
// We want to implicitly build modules.
@@ -578,7 +631,14 @@ static void SetupImportStdModuleLangOpts(CompilerInstance &compiler) {
578631
lang_opts.GNUMode = true;
579632
lang_opts.GNUKeywords = true;
580633
lang_opts.CPlusPlus11 = true;
581-
lang_opts.BuiltinHeadersInSystemModules = true;
634+
635+
auto supported_or_err = sdkSupportsBuiltinModules(target);
636+
if (supported_or_err)
637+
lang_opts.BuiltinHeadersInSystemModules = !*supported_or_err;
638+
else
639+
LLDB_LOG_ERROR(log, supported_or_err.takeError(),
640+
"Failed to determine BuiltinHeadersInSystemModules when "
641+
"setting up import-std-module: {0}");
582642

583643
// The Darwin libc expects this macro to be set.
584644
lang_opts.GNUCVersion = 40201;
@@ -659,7 +719,7 @@ ClangExpressionParser::ClangExpressionParser(
659719
if (auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr);
660720
clang_expr && clang_expr->DidImportCxxModules()) {
661721
LLDB_LOG(log, "Adding lang options for importing C++ modules");
662-
SetupImportStdModuleLangOpts(*m_compiler);
722+
SetupImportStdModuleLangOpts(*m_compiler, *target_sp);
663723
SetupModuleHeaderPaths(m_compiler.get(), m_include_directories, target_sp);
664724
}
665725

lldb/source/Utility/XcodeSDK.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,27 @@ bool XcodeSDK::SupportsSwift() const {
259259
}
260260
}
261261

262+
bool XcodeSDK::SDKSupportsBuiltinModules(const llvm::Triple &target_triple,
263+
llvm::VersionTuple sdk_version) {
264+
using namespace llvm;
265+
266+
switch (target_triple.getOS()) {
267+
case Triple::OSType::MacOSX:
268+
return sdk_version >= VersionTuple(15U);
269+
case Triple::OSType::IOS:
270+
return sdk_version >= VersionTuple(18U);
271+
case Triple::OSType::TvOS:
272+
return sdk_version >= VersionTuple(18U);
273+
case Triple::OSType::WatchOS:
274+
return sdk_version >= VersionTuple(11U);
275+
case Triple::OSType::XROS:
276+
return sdk_version >= VersionTuple(2U);
277+
default:
278+
// New SDKs support builtin modules from the start.
279+
return true;
280+
}
281+
}
282+
262283
bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type desired_type,
263284
const FileSpec &sdk_path) {
264285
ConstString last_path_component = sdk_path.GetFilename();

0 commit comments

Comments
 (0)