Skip to content

Commit 31fd2df

Browse files
Merge pull request #9704 from adrian-prantl/141013288
[lldb] Read the precise SDK for the current CU first.
2 parents f51dca2 + 4a44cf7 commit 31fd2df

File tree

6 files changed

+114
-9
lines changed

6 files changed

+114
-9
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/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ static std::string GetSDKPathFromDebugInfo(std::string m_description,
21482148
return {};
21492149
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(module);
21502150
if (!sdk_or_err) {
2151-
Debugger::ReportError("Error while parsing SDK path from debug-info: " +
2151+
Debugger::ReportError("Error while parsing SDK path from debug info: " +
21522152
toString(sdk_or_err.takeError()));
21532153
return {};
21542154
}
@@ -2825,9 +2825,29 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28252825
FileSpec target_sdk_spec = target_sp ? target_sp->GetSDKPath() : FileSpec();
28262826
if (target_sdk_spec && FileSystem::Instance().Exists(target_sdk_spec)) {
28272827
swift_ast_sp->SetPlatformSDKPath(target_sdk_spec.GetPath());
2828+
LOG_PRINTF(GetLog(LLDBLog::Types), "Using target SDK override: %s",
2829+
target_sdk_spec.GetPath().c_str());
28282830
handled_sdk_path = true;
28292831
}
28302832

2833+
// Get the precise SDK from the symbol context.
2834+
if (cu)
2835+
if (auto platform_sp = Platform::GetHostPlatform()) {
2836+
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(*cu);
2837+
if (!sdk_or_err)
2838+
Debugger::ReportError("Error while parsing SDK path from debug info: " +
2839+
toString(sdk_or_err.takeError()));
2840+
else {
2841+
std::string sdk_path = GetSDKPath(m_description, *sdk_or_err);
2842+
if (!sdk_path.empty()) {
2843+
swift_ast_sp->SetPlatformSDKPath(sdk_path);
2844+
handled_sdk_path = true;
2845+
LOG_PRINTF(GetLog(LLDBLog::Types), "Using precise SDK: %s",
2846+
sdk_path.c_str());
2847+
}
2848+
}
2849+
}
2850+
28312851
if (!handled_sdk_path) {
28322852
for (size_t mi = 0; mi != num_images; ++mi) {
28332853
ModuleSP module_sp = modules.GetModuleAtIndex(mi);
@@ -2839,8 +2859,8 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28392859
if (sdk_path.empty())
28402860
continue;
28412861

2842-
handled_sdk_path = true;
28432862
swift_ast_sp->SetPlatformSDKPath(sdk_path);
2863+
handled_sdk_path = true;
28442864
break;
28452865
}
28462866
}

lldb/test/API/lang/swift/xcode_sdk/TestSwiftXcodeSDK.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ class TestSwiftXcodeSDK(lldbtest.TestBase):
1010
mydir = lldbtest.TestBase.compute_mydir(__file__)
1111
NO_DEBUG_INFO_TESTCASE = True
1212

13-
def check_log(self, log, expected_path):
13+
def check_log(self, log, expected_path, expect_precise):
1414
import io
1515
logfile = io.open(log, "r", encoding='utf-8')
16-
in_expr_log = 0
1716
found = 0
17+
precise = False
1818
for line in logfile:
19-
if (line.startswith(' SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration()') and
20-
"SDK path" in line and expected_path in line):
19+
if not line.startswith(' SwiftASTContextForExpressions(module: "a", cu: "main.swift")'):
20+
continue
21+
if "Using precise SDK" in line:
22+
precise = True
23+
continue
24+
if "Override target.sdk-path" in line:
25+
precise = False
26+
continue
27+
if not '::LogConfiguration()' in line:
28+
continue
29+
if "SDK path" in line and expected_path in line:
2130
found += 1
2231
self.assertEqual(found, 1)
32+
self.assertEqual(precise, expect_precise)
2333

2434
@swiftTest
2535
@skipUnlessDarwin
@@ -33,7 +43,7 @@ def test_decode(self):
3343
lldbutil.run_to_name_breakpoint(self, 'main')
3444

3545
self.expect("expression 1")
36-
self.check_log(log, "MacOSX")
46+
self.check_log(log, "MacOSX", True)
3747

3848
@swiftTest
3949
@skipUnlessDarwin
@@ -52,7 +62,7 @@ def test_decode_sim(self):
5262
lldbutil.run_to_name_breakpoint(self, 'main')
5363

5464
self.expect("expression 1")
55-
self.check_log(log, "iPhoneSimulator")
65+
self.check_log(log, "iPhoneSimulator", True)
5666

5767
@swiftTest
5868
@skipUnlessDarwin
@@ -72,4 +82,4 @@ def test_override(self):
7282
lldbutil.run_to_name_breakpoint(self, 'main')
7383

7484
self.expect("expression 1")
75-
self.check_log(log, ios_sdk)
85+
self.check_log(log, ios_sdk, False)

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)