Skip to content

Return a SwiftArrayEmptyBufferHandler if loc points to an EmptyArray Symbol. #10920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "Plugins/TypeSystem/Swift/SwiftDemangle.h"
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
#include "lldb/Core/Address.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
Expand All @@ -23,6 +25,7 @@
// FIXME: we should not need this
#include "Plugins/Language/ObjC/Cocoa.h"
#include "lldb/lldb-enumerations.h"
#include "swift/Demangling/Demangler.h"

using namespace lldb;
using namespace lldb_private;
Expand Down Expand Up @@ -460,6 +463,41 @@ SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &static_valobj) {
lldb::addr_t storage_location =
buffer_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);

lldb_private::Address addr = Address();
addr.SetLoadAddress(storage_location, exe_ctx.GetTargetPtr());

// If the storage_location points to a swiftEmptyArrayStorage symbol, return
// a SwiftArrayEmptyBufferHandler.
if (auto *symbol = addr.CalculateSymbolContextSymbol()) {
auto mangledName = symbol->GetMangled().GetMangledName().GetStringRef();
using namespace ::swift::Demangle;
Demangler dem;
NodePointer node = dem.demangleSymbol(mangledName);
if (node && swift_demangle::NodeAtPath(node, Node::Kind::Global)) {
auto *class_node = swift_demangle::ChildAtPath(
node,
{Node::Kind::TypeMetadata, Node::Kind::Type, Node::Kind::Class});
if (class_node && class_node->getNumChildren() == 2) {
auto *module_node = class_node->getFirstChild();
auto *ident_node = class_node->getLastChild();
if (module_node->getKind() == Node::Kind::Module &&
module_node->hasText() &&
ident_node->getKind() == Node::Kind::Identifier &&
ident_node->hasText()) {
auto module_name = module_node->getText();
auto class_name = ident_node->getText();
if (module_name == ::swift::STDLIB_NAME &&
class_name == "__EmptyArrayStorage") {
CompilerType elem_type(
valobj.GetCompilerType().GetArrayElementType(exe_scope));
return std::unique_ptr<SwiftArrayBufferHandler>(
new SwiftArrayEmptyBufferHandler(elem_type));
}
}
}
}
}

if (storage_location != LLDB_INVALID_ADDRESS) {
ProcessSP process_sp(valobj.GetProcessSP());
if (!process_sp)
Expand Down
2 changes: 2 additions & 0 deletions lldb/test/API/lang/swift/swift_empty_arr/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SWIFT_SOURCES := main.swift
include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lldb
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbtest as lldbtest
import lldbsuite.test.lldbutil as lldbutil


class TestSwiftGlobalEmptyArray(lldbtest.TestBase):
@swiftTest
def test(self):
"""Test that printing a global swift array of type SwiftEmptyArrayStorage uses the correct data formatter"""

self.build()
filespec = lldb.SBFileSpec("main.swift")
lldbutil.run_to_source_breakpoint(
self, "break here", filespec

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self, "break here", filespec
self, "break here", lldb.SBFileSpec("main.swift")

)
x = self.frame().FindVariable("x")
self.assertEqual(x.GetSummary(), "0 values")
9 changes: 9 additions & 0 deletions lldb/test/API/lang/swift/swift_empty_arr/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
protocol P {}

func go() {
let x: [any P] = []
print("break here")
print(x)
}

go()