Skip to content

Commit 7dab1d0

Browse files
Merge pull request #10629 from adrian-prantl/145326497
Add a data formatter for Swift.InlineArray<>
2 parents fc3baa8 + cfb0b96 commit 7dab1d0

File tree

8 files changed

+118
-18
lines changed

8 files changed

+118
-18
lines changed

lldb/source/Plugins/Language/Swift/SwiftArray.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,29 @@ bool SwiftArraySliceBufferHandler::IsValid() {
247247
return m_first_elem_ptr != LLDB_INVALID_ADDRESS && m_elem_type.IsValid();
248248
}
249249

250+
size_t SwiftArrayInlineBufferHandler::GetCount() { return m_size; }
251+
252+
size_t SwiftArrayInlineBufferHandler::GetCapacity() { return GetCount(); }
253+
254+
lldb_private::CompilerType SwiftArrayInlineBufferHandler::GetElementType() {
255+
return m_elem_type;
256+
}
257+
258+
ValueObjectSP SwiftArrayInlineBufferHandler::GetElementAtIndex(size_t idx) {
259+
if (idx >= m_size)
260+
return {};
261+
return m_valobj_sp->GetChildAtIndex(idx);
262+
}
263+
264+
SwiftArrayInlineBufferHandler::SwiftArrayInlineBufferHandler(
265+
ValueObjectSP valobj, lldb::addr_t native_ptr, CompilerType elem_type)
266+
: m_valobj_sp(valobj),
267+
m_size(
268+
llvm::expectedToOptional(m_valobj_sp->GetNumChildren()).value_or(0)) {
269+
}
270+
271+
bool SwiftArrayInlineBufferHandler::IsValid() { return m_valobj_sp.get(); }
272+
250273
size_t SwiftSyntheticFrontEndBufferHandler::GetCount() {
251274
return m_frontend->CalculateNumChildrenIgnoringErrors();
252275
}
@@ -395,6 +418,28 @@ SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &static_valobj) {
395418
if (handler && handler->IsValid())
396419
return handler;
397420
return nullptr;
421+
} else if (valobj_typename.starts_with("Swift.InlineArray<")) {
422+
CompilerType elem_type(
423+
valobj.GetCompilerType().GetArrayElementType(exe_scope));
424+
if (!elem_type)
425+
return nullptr;
426+
427+
static ConstString g__storage("_storage");
428+
auto nonsynth_sp = valobj.GetNonSyntheticValue();
429+
if (!nonsynth_sp)
430+
return nullptr;
431+
ValueObjectSP storage_sp(nonsynth_sp->GetChildMemberWithName(g__storage));
432+
433+
lldb::addr_t storage_location =
434+
storage_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
435+
if (auto process_sp = valobj.GetProcessSP())
436+
if (auto *swift_runtime = SwiftLanguageRuntime::Get(process_sp))
437+
storage_location =
438+
swift_runtime->MaskMaybeBridgedPointer(storage_location);
439+
std::unique_ptr<SwiftArrayBufferHandler> handler;
440+
handler.reset(new SwiftArrayInlineBufferHandler(
441+
storage_sp, storage_location, elem_type));
442+
return handler;
398443
} else {
399444
// Swift.Array
400445
static ConstString g_buffer("_buffer");

lldb/source/Plugins/Language/Swift/SwiftArray.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ class SwiftArraySliceBufferHandler : public SwiftArrayBufferHandler {
139139
uint64_t m_start_index;
140140
};
141141

142+
/// Handles Swift.InlineArray<>
143+
class SwiftArrayInlineBufferHandler : public SwiftArrayBufferHandler {
144+
public:
145+
size_t GetCount() override;
146+
size_t GetCapacity() override;
147+
lldb_private::CompilerType GetElementType() override;
148+
lldb::ValueObjectSP GetElementAtIndex(size_t) override;
149+
bool IsValid() override;
150+
151+
protected:
152+
SwiftArrayInlineBufferHandler(lldb::ValueObjectSP, lldb::addr_t,
153+
CompilerType);
154+
friend class SwiftArrayBufferHandler;
155+
156+
private:
157+
CompilerType m_elem_type;
158+
lldb::ValueObjectSP m_valobj_sp;
159+
unsigned m_size;
160+
};
161+
142162
class SwiftSyntheticFrontEndBufferHandler : public SwiftArrayBufferHandler {
143163
public:
144164
size_t GetCount() override;

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,8 @@ static void LoadSwiftFormatters(lldb::TypeCategoryImplSP swift_category_sp) {
309309
AddCXXSummary(swift_category_sp,
310310
lldb_private::formatters::swift::Array_SummaryProvider,
311311
"Swift.Array summary provider",
312-
ConstString("^Swift.Array<.+>$"), summary_flags, true);
313-
AddCXXSummary(
314-
swift_category_sp, lldb_private::formatters::swift::Array_SummaryProvider,
315-
"Swift.ContiguousArray summary provider",
316-
ConstString("^Swift.ContiguousArray<.+>$"), summary_flags, true);
312+
ConstString("^Swift.(Contiguous|Inline)?Array<.+>$"),
313+
summary_flags, true);
317314
AddCXXSummary(swift_category_sp,
318315
lldb_private::formatters::swift::Array_SummaryProvider,
319316
"Swift.ArraySlice summary provider",
@@ -357,13 +354,8 @@ static void LoadSwiftFormatters(lldb::TypeCategoryImplSP swift_category_sp) {
357354
AddCXXSynthetic(
358355
swift_category_sp,
359356
lldb_private::formatters::swift::ArraySyntheticFrontEndCreator,
360-
"Swift.Array synthetic children", ConstString("^Swift.Array<.+>$"),
361-
synth_flags, true);
362-
AddCXXSynthetic(
363-
swift_category_sp,
364-
lldb_private::formatters::swift::ArraySyntheticFrontEndCreator,
365-
"Swift.ContiguousArray synthetic children",
366-
ConstString("^Swift.ContiguousArray<.+>$"), synth_flags, true);
357+
"Swift.Array synthetic children",
358+
ConstString("^Swift.(Contiguous|Inline)?Array<.+>$"), synth_flags, true);
367359
AddCXXSynthetic(
368360
swift_category_sp,
369361
lldb_private::formatters::swift::ArraySyntheticFrontEndCreator,

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,13 +2958,21 @@ bool TypeSystemSwiftTypeRef::IsArrayType(opaque_compiler_type_t type,
29582958
!node->getChild(1)->hasText() ||
29592959
(node->getChild(1)->getText() != "Array" &&
29602960
node->getChild(1)->getText() != "ContiguousArray" &&
2961+
node->getChild(1)->getText() != "InlineArray" &&
29612962
node->getChild(1)->getText() != "ArraySlice"))
29622963
return false;
29632964

2964-
if (elem_node->getNumChildren() != 1 ||
2965-
elem_node->getKind() != Node::Kind::TypeList)
2965+
if (elem_node->getKind() != Node::Kind::TypeList)
29662966
return false;
2967-
elem_node = elem_node->getFirstChild();
2967+
if (node->getChild(1)->getText() == "InlineArray") {
2968+
if (elem_node->getNumChildren() != 2)
2969+
return false;
2970+
elem_node = elem_node->getChild(1);
2971+
} else {
2972+
if (elem_node->getNumChildren() != 1)
2973+
return false;
2974+
elem_node = elem_node->getFirstChild();
2975+
}
29682976
if (element_type)
29692977
// FIXME: This expensive canonicalization is only there for
29702978
// SwiftASTContext compatibility.

lldb/test/API/lang/swift/embedded/frame_variable/TestSwiftEmbeddedFrameVariable.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ def implementation(self):
212212
lldbutil.check_variable(self, innerFuncField, False, value='8479')
213213

214214
array = frame.FindVariable("array")
215-
arrayStorage = array.GetChildMemberWithName("_storage")
216-
lldbutil.check_variable(self, arrayStorage, False, num_children=4)
215+
lldbutil.check_variable(self, array, False, num_children=4)
217216
for i in range(4):
218-
lldbutil.check_variable(self, arrayStorage.GetChildAtIndex(i),
217+
lldbutil.check_variable(self, array.GetChildAtIndex(i),
219218
False, value=str(i+1))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFT_EMBEDDED_MODE := 1
3+
4+
include Makefile.rules
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
import lldbsuite.test.lldbtest as lldbtest
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
7+
class TestSwiftInlineArray(lldbtest.TestBase):
8+
9+
NO_DEBUG_INFO_TESTCASE = True
10+
11+
@swiftTest
12+
def test(self):
13+
"""Test the inline array synthetic child provider and summary"""
14+
self.build()
15+
lldbutil.run_to_source_breakpoint(
16+
self, 'break here', lldb.SBFileSpec('main.swift'))
17+
18+
var_a = self.frame().FindVariable("a")
19+
self.assertEqual(var_a.GetSummary(), "4 values")
20+
synth_a = var_a.GetSyntheticValue()
21+
self.assertEqual(synth_a.GetNumChildren(), 4)
22+
self.assertEqual(synth_a.GetChildAtIndex(0).GetValue(), '4')
23+
self.assertEqual(synth_a.GetChildAtIndex(1).GetValue(), '3')
24+
self.assertEqual(synth_a.GetChildAtIndex(2).GetValue(), '2')
25+
self.assertEqual(synth_a.GetChildAtIndex(3).GetValue(), '1')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func main() {
2+
let a : InlineArray<4, Int> = [4,3,2,1]
3+
print("break here \(a)")
4+
}
5+
6+
main()
7+

0 commit comments

Comments
 (0)