Skip to content

Improve the data formatter UX if an array is uninitialized. #9026

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

Merged
merged 1 commit into from
Jul 30, 2024
Merged
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
13 changes: 8 additions & 5 deletions lldb/source/Plugins/Language/Swift/SwiftArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ SwiftArrayNativeBufferHandler::SwiftArrayNativeBufferHandler(
}

bool SwiftArrayNativeBufferHandler::IsValid() {
return m_metadata_ptr != LLDB_INVALID_ADDRESS &&
return m_metadata_ptr != LLDB_INVALID_ADDRESS && m_metadata_ptr &&
m_first_elem_ptr != LLDB_INVALID_ADDRESS && m_capacity >= m_size &&
m_elem_type.IsValid();
}
Expand Down Expand Up @@ -437,10 +437,7 @@ SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &static_valobj) {
handler.reset(new SwiftArrayBridgedBufferHandler(
process_sp, masked_storage_location));
}

if (handler && handler->IsValid())
return handler;
return nullptr;
return handler;
} else {
CompilerType elem_type(
valobj.GetCompilerType().GetArrayElementType(exe_scope));
Expand All @@ -457,6 +454,12 @@ bool lldb_private::formatters::swift::Array_SummaryProvider(
if (!handler)
return false;

if (!handler->IsValid()) {
// FIXME: This should be an out-of-band llvm::Error return value.
stream << "<uninitialized>";
return true;
}

auto count = handler->GetCount();

stream.Printf("%zu value%s", count, (count == 1 ? "" : "s"));
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/lang/swift/array_uninitialized/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SWIFT_SOURCES := main.swift
SWIFTFLAGS_EXTRAS := -parse-as-library
include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import lldb
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbtest as lldbtest
import lldbsuite.test.lldbutil as lldbutil


class TestSwiftArrayUninitialized(lldbtest.TestBase):
@swiftTest
def test(self):
"""Test unitialized global arrays"""
self.build()
lldbutil.run_to_source_breakpoint(
self, 'break here', lldb.SBFileSpec('main.swift'))
self.expect("target variable -- array_unused", substrs=['<uninitialized>'])
self.expect("target variable -- array_used_empty", substrs=['0 values'])
8 changes: 8 additions & 0 deletions lldb/test/API/lang/swift/array_uninitialized/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var array_unused : [Int] = [1,2,3]
var array_used_empty : [Int] = []
@main struct Main {
static func main() {
print(array_used_empty)
print("break here")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
var tb = Test() as Array + []

print("second stop") //% self.expect('frame variable -d run -- t', substrs=['t = 0x', 'NSArray = {', 'NSObject = {'])
//% self.expect('frame variable -d run -- ta', substrs=['ta = {', '_buffer = {', '_storage =', 'rawValue = 0x'])
//% self.expect('frame variable -d run -- ta', substrs=['ta = <uninitialized>', '_buffer = {', '_storage =', 'rawValue = 0x'])
Copy link
Author

Choose a reason for hiding this comment

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

Note that this test was basically showing that the formatter didn't work here before either.

//% self.expect('frame variable -d run -- tb', substrs=['tb = 1 value {', '"abc"'])
//% self.expect('po t', substrs=['0 : abc'])
//% self.expect('po ta', substrs=['0 : abc'])
Expand Down