[lldb][Darwin] revert change to lang_opts.BuiltinHeadersInSystemModules (#145864)

Revert the changes made in the following PRs as they are causing bot
failures:

- https://github.com/llvm/llvm-project/pull/145744
- https://github.com/llvm/llvm-project/pull/144913
This commit is contained in:
Charles Zablit
2025-06-26 11:56:50 +01:00
committed by GitHub
parent d3fd7921d4
commit 9ae41f017d
7 changed files with 140 additions and 27 deletions

View File

@@ -319,6 +319,49 @@ private:
StringRef m_filename;
};
/// Returns true if the SDK for the specified triple supports
/// builtin modules in system headers. This is used to decide
/// whether to pass -fbuiltin-headers-in-system-modules to
/// the compiler instance when compiling the `std` module.
static llvm::Expected<bool>
sdkSupportsBuiltinModules(lldb_private::Target &target) {
auto arch_spec = target.GetArchitecture();
auto const &triple = arch_spec.GetTriple();
auto module_sp = target.GetExecutableModule();
if (!module_sp)
return llvm::createStringError("Executable module not found.");
// Get SDK path that the target was compiled against.
auto platform_sp = target.GetPlatform();
if (!platform_sp)
return llvm::createStringError("No Platform plugin found on target.");
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(*module_sp);
if (!sdk_or_err)
return sdk_or_err.takeError();
// Use the SDK path from debug-info to find a local matching SDK directory.
auto sdk_path_or_err =
HostInfo::GetSDKRoot(HostInfo::SDKOptions{std::move(sdk_or_err->first)});
if (!sdk_path_or_err)
return sdk_path_or_err.takeError();
auto VFS = FileSystem::Instance().GetVirtualFileSystem();
if (!VFS)
return llvm::createStringError("No virtual filesystem available.");
// Extract SDK version from the /path/to/some.sdk/SDKSettings.json
auto parsed_or_err = clang::parseDarwinSDKInfo(*VFS, *sdk_path_or_err);
if (!parsed_or_err)
return parsed_or_err.takeError();
auto maybe_sdk = *parsed_or_err;
if (!maybe_sdk)
return llvm::createStringError("Couldn't find Darwin SDK info.");
return XcodeSDK::SDKSupportsBuiltinModules(triple, maybe_sdk->getVersion());
}
static void SetupModuleHeaderPaths(CompilerInstance *compiler,
std::vector<std::string> include_directories,
lldb::TargetSP target_sp) {
@@ -662,6 +705,7 @@ static void SetupLangOpts(CompilerInstance &compiler,
static void SetupImportStdModuleLangOpts(CompilerInstance &compiler,
lldb_private::Target &target) {
Log *log = GetLog(LLDBLog::Expressions);
LangOptions &lang_opts = compiler.getLangOpts();
lang_opts.Modules = true;
// We want to implicitly build modules.
@@ -679,7 +723,12 @@ static void SetupImportStdModuleLangOpts(CompilerInstance &compiler,
lang_opts.GNUKeywords = true;
lang_opts.CPlusPlus11 = true;
lang_opts.BuiltinHeadersInSystemModules = false;
if (auto supported_or_err = sdkSupportsBuiltinModules(target))
lang_opts.BuiltinHeadersInSystemModules = !*supported_or_err;
else
LLDB_LOG_ERROR(log, supported_or_err.takeError(),
"Failed to determine BuiltinHeadersInSystemModules when "
"setting up import-std-module: {0}");
// The Darwin libc expects this macro to be set.
lang_opts.GNUCVersion = 40201;

View File

@@ -1130,33 +1130,13 @@ void PlatformDarwin::AddClangModuleCompilationOptionsForSDKType(
if (target) {
if (ModuleSP exe_module_sp = target->GetExecutableModule()) {
SymbolFile *sym_file = exe_module_sp->GetSymbolFile();
if (!sym_file)
return;
XcodeSDK merged_sdk;
for (unsigned i = 0; i < sym_file->GetNumCompileUnits(); ++i) {
if (auto cu_sp = sym_file->GetCompileUnitAtIndex(i)) {
auto cu_sdk = sym_file->ParseXcodeSDK(*cu_sp);
merged_sdk.Merge(cu_sdk);
}
}
// TODO: The result of this loop is almost equivalent to deriving the SDK
// from the target triple, which would be a lot cheaper.
if (FileSystem::Instance().Exists(merged_sdk.GetSysroot())) {
sysroot_spec = merged_sdk.GetSysroot();
auto path_or_err = ResolveSDKPathFromDebugInfo(*exe_module_sp);
if (path_or_err) {
sysroot_spec = FileSpec(*path_or_err);
} else {
auto path_or_err =
HostInfo::GetSDKRoot(HostInfo::SDKOptions{merged_sdk});
if (path_or_err) {
sysroot_spec = FileSpec(*path_or_err);
} else {
LLDB_LOG_ERROR(GetLog(LLDBLog::Types | LLDBLog::Host),
path_or_err.takeError(),
"Failed to resolve SDK path: {0}");
}
LLDB_LOG_ERROR(GetLog(LLDBLog::Types | LLDBLog::Host),
path_or_err.takeError(),
"Failed to resolve SDK path: {0}");
}
}
}
@@ -1404,6 +1384,31 @@ PlatformDarwin::GetSDKPathFromDebugInfo(Module &module) {
return std::pair{std::move(merged_sdk), found_mismatch};
}
llvm::Expected<std::string>
PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) {
auto sdk_or_err = GetSDKPathFromDebugInfo(module);
if (!sdk_or_err)
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
llvm::formatv("Failed to parse SDK path from debug-info: {0}",
llvm::toString(sdk_or_err.takeError())));
auto [sdk, _] = std::move(*sdk_or_err);
if (FileSystem::Instance().Exists(sdk.GetSysroot()))
return sdk.GetSysroot().GetPath();
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
if (!path_or_err)
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
llvm::formatv("Error while searching for SDK (XcodeSDK '{0}'): {1}",
sdk.GetString(),
llvm::toString(path_or_err.takeError())));
return path_or_err->str();
}
llvm::Expected<XcodeSDK>
PlatformDarwin::GetSDKPathFromDebugInfo(CompileUnit &unit) {
ModuleSP module_sp = unit.CalculateSymbolContextModule();

View File

@@ -120,6 +120,9 @@ public:
llvm::Expected<std::pair<XcodeSDK, bool>>
GetSDKPathFromDebugInfo(Module &module) override;
llvm::Expected<std::string>
ResolveSDKPathFromDebugInfo(Module &module) override;
llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) override;
llvm::Expected<std::string>

View File

@@ -266,6 +266,27 @@ bool XcodeSDK::SupportsSwift() const {
}
}
bool XcodeSDK::SDKSupportsBuiltinModules(const llvm::Triple &target_triple,
llvm::VersionTuple sdk_version) {
using namespace llvm;
switch (target_triple.getOS()) {
case Triple::OSType::MacOSX:
return sdk_version >= VersionTuple(15U);
case Triple::OSType::IOS:
return sdk_version >= VersionTuple(18U);
case Triple::OSType::TvOS:
return sdk_version >= VersionTuple(18U);
case Triple::OSType::WatchOS:
return sdk_version >= VersionTuple(11U);
case Triple::OSType::XROS:
return sdk_version >= VersionTuple(2U);
default:
// New SDKs support builtin modules from the start.
return true;
}
}
bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type desired_type,
const FileSpec &sdk_path) {
ConstString last_path_component = sdk_path.GetFilename();