Skip to content

Commit de43ec3

Browse files
authored
[lldb] Add summary formatter for TaskPriority (#10205)
Add a summary formatter for `TaskPriority` to print values with the names of the constants. See https://developer.apple.com/documentation/swift/taskpriority Some values have more than one name, ex `high` and `userInitiated` are the same value. The [definition of `TaskPriority`](https://github.com/swiftlang/swift/blob/28f96411c9a1fe9c8768ac48c24a6974973876ff/stdlib/public/Concurrency/Task.swift#L308-L331) defines some some constants in terms of others. This summary formatter uses the seemingly "primary" names. As a result, if a user expects to see `userInitiated`, they will see `high` instead. Similarly, they'll see `low` instead of `utility`, and `medium` instead of `default`.
1 parent 0a0c9a6 commit de43ec3

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,38 @@ bool lldb_private::formatters::swift::TypePreservingNSNumber_SummaryProvider(
16051605
return false;
16061606
}
16071607

1608+
bool lldb_private::formatters::swift::TaskPriority_SummaryProvider(
1609+
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1610+
uint64_t raw_value = UINT8_MAX;
1611+
if (auto child_sp = valobj.GetChildMemberWithName("rawValue"))
1612+
if (auto synthetic_sp = child_sp->GetSyntheticValue())
1613+
raw_value = synthetic_sp->GetValueAsUnsigned(UINT8_MAX);
1614+
if (raw_value >= UINT8_MAX)
1615+
return false;
1616+
1617+
switch (raw_value) {
1618+
case 0x19:
1619+
// Also .userInitiated
1620+
stream.PutCString(".high");
1621+
break;
1622+
case 0x15:
1623+
// Also .default (deprecated)
1624+
stream.PutCString(".medium");
1625+
break;
1626+
case 0x11:
1627+
// Also .utilitiy
1628+
stream.PutCString(".low");
1629+
break;
1630+
case 0x09:
1631+
stream.PutCString(".background");
1632+
break;
1633+
default:
1634+
stream.Format("{0}", raw_value);
1635+
break;
1636+
}
1637+
return true;
1638+
}
1639+
16081640
namespace {
16091641

16101642
/// Enumerate the kinds of SIMD elements.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ bool ObjC_Selector_SummaryProvider(ValueObject &valobj, Stream &stream,
114114
bool TypePreservingNSNumber_SummaryProvider(ValueObject &valobj, Stream &stream,
115115
const TypeSummaryOptions &options);
116116

117+
bool TaskPriority_SummaryProvider(ValueObject &valobj, Stream &stream,
118+
const TypeSummaryOptions &options);
119+
117120
SyntheticChildrenFrontEnd *EnumSyntheticFrontEndCreator(CXXSyntheticChildren *,
118121
lldb::ValueObjectSP);
119122

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ static void LoadSwiftFormatters(lldb::TypeCategoryImplSP swift_category_sp) {
482482
AddCXXSummary(swift_category_sp, staticstring_summary_provider,
483483
"Swift.StaticString summary provider",
484484
ConstString("Swift.StaticString"), summary_flags);
485+
AddCXXSummary(swift_category_sp,
486+
lldb_private::formatters::swift::TaskPriority_SummaryProvider,
487+
"Swift TaskPriority summary provider", "Swift.TaskPriority",
488+
summary_flags);
485489
summary_flags.SetSkipPointers(false);
486490
// this is an ObjC dynamic type - as such it comes in pointer form
487491
// NSContiguousString* - do not skip pointers here
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFTFLAGS_EXTRAS := -parse-as-library
3+
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+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class TestCase(TestBase):
8+
9+
@swiftTest
10+
def test(self):
11+
"""Test summary formatter for TaskPriority."""
12+
self.build()
13+
lldbutil.run_to_source_breakpoint(
14+
self, "break here", lldb.SBFileSpec("main.swift")
15+
)
16+
self.expect(
17+
"frame var",
18+
substrs=[
19+
"high = .high",
20+
"medium = .medium",
21+
"low = .low",
22+
"background = .background",
23+
"custom = 15",
24+
],
25+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@main struct Entry {
2+
static func main() {
3+
let high = TaskPriority.high
4+
let medium = TaskPriority.medium
5+
let low = TaskPriority.low
6+
let background = TaskPriority.background
7+
let custom = TaskPriority(rawValue: 15)
8+
print("break here")
9+
}
10+
}

0 commit comments

Comments
 (0)