Skip to content

Commit 57ab61a

Browse files
committed
[lldb] Add a per-CU API to read the SDK.
This is needed by the Swift plugin. (cherry picked from commit 60ab35f) Conflicts: lldb/include/lldb/Target/Platform.h
1 parent 119bd56 commit 57ab61a

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

lldb/include/lldb/Target/Platform.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,32 @@ class Platform : public PluginInterface {
473473
LLVM_PRETTY_FUNCTION, GetName()));
474474
}
475475

476+
/// Search CU for the SDK path the CUs was compiled against.
477+
///
478+
/// \param[in] unit The CU
479+
///
480+
/// \returns If successful, returns a parsed XcodeSDK object.
481+
virtual llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) {
482+
return llvm::createStringError(
483+
llvm::formatv("{0} not implemented for '{1}' platform.",
484+
LLVM_PRETTY_FUNCTION, GetName()));
485+
}
486+
487+
/// Returns the full path of the most appropriate SDK for the
488+
/// specified compile unit. This function gets this path by parsing
489+
/// debug-info (see \ref `GetSDKPathFromDebugInfo`).
490+
///
491+
/// \param[in] unit The CU to scan.
492+
///
493+
/// \returns If successful, returns the full path to an
494+
/// Xcode SDK.
495+
virtual llvm::Expected<std::string>
496+
ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
497+
return llvm::createStringError(
498+
llvm::formatv("{0} not implemented for '{1}' platform.",
499+
LLVM_PRETTY_FUNCTION, GetName()));
500+
}
501+
476502
const std::string &GetRemoteURL() const { return m_remote_url; }
477503

478504
bool IsHost() const {

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "lldb/Interpreter/OptionValueProperties.h"
3030
#include "lldb/Interpreter/OptionValueString.h"
3131
#include "lldb/Interpreter/Options.h"
32+
#include "lldb/Symbol/CompileUnit.h"
3233
#include "lldb/Symbol/ObjectFile.h"
3334
#include "lldb/Symbol/SymbolFile.h"
3435
#include "lldb/Symbol/SymbolVendor.h"
@@ -1429,3 +1430,39 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) {
14291430

14301431
return path_or_err->str();
14311432
}
1433+
1434+
llvm::Expected<XcodeSDK>
1435+
PlatformDarwin::GetSDKPathFromDebugInfo(CompileUnit &unit) {
1436+
ModuleSP module_sp = unit.CalculateSymbolContextModule();
1437+
if (!module_sp)
1438+
return llvm::createStringError("compile unit has no module");
1439+
SymbolFile *sym_file = module_sp->GetSymbolFile();
1440+
if (!sym_file)
1441+
return llvm::createStringError(
1442+
llvm::formatv("No symbol file available for module '{0}'",
1443+
module_sp->GetFileSpec().GetFilename()));
1444+
1445+
return sym_file->ParseXcodeSDK(unit);
1446+
}
1447+
1448+
llvm::Expected<std::string>
1449+
PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
1450+
auto sdk_or_err = GetSDKPathFromDebugInfo(unit);
1451+
if (!sdk_or_err)
1452+
return llvm::createStringError(
1453+
llvm::inconvertibleErrorCode(),
1454+
llvm::formatv("Failed to parse SDK path from debug-info: {0}",
1455+
llvm::toString(sdk_or_err.takeError())));
1456+
1457+
auto sdk = std::move(*sdk_or_err);
1458+
1459+
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
1460+
if (!path_or_err)
1461+
return llvm::createStringError(
1462+
llvm::inconvertibleErrorCode(),
1463+
llvm::formatv("Error while searching for SDK (XcodeSDK '{0}'): {1}",
1464+
sdk.GetString(),
1465+
llvm::toString(path_or_err.takeError())));
1466+
1467+
return path_or_err->str();
1468+
}

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ class PlatformDarwin : public PlatformPOSIX {
130130
llvm::Expected<std::string>
131131
ResolveSDKPathFromDebugInfo(Module &module) override;
132132

133+
llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) override;
134+
135+
llvm::Expected<std::string>
136+
ResolveSDKPathFromDebugInfo(CompileUnit &unit) override;
137+
133138
protected:
134139
static const char *GetCompatibleArch(ArchSpec::Core core, size_t idx);
135140

lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ TEST_P(SDKPathParsingMultiparamTests, TestSDKPathFromDebugInfo) {
268268
EXPECT_EQ(found_mismatch, expect_mismatch);
269269
EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk);
270270
EXPECT_NE(sdk.GetString().find(expect_sdk_path_pattern), std::string::npos);
271+
272+
{
273+
auto sdk_or_err =
274+
platform_sp->GetSDKPathFromDebugInfo(*dwarf_cu->GetLLDBCompUnit());
275+
ASSERT_TRUE(static_cast<bool>(sdk_or_err));
276+
EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk);
277+
}
271278
}
272279

273280
SDKPathParsingTestData sdkPathParsingTestCases[] = {

0 commit comments

Comments
 (0)