Skip to content

[lldb] SwiftLanguageRuntime: Support Clang typedef-to-enums. #10392

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 2 commits into from
Apr 1, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,15 @@ void LogUnimplementedTypeKind(const char *function, CompilerType type) {
#endif
}

CompilerType GetCanonicalClangType(CompilerType type) {

Choose a reason for hiding this comment

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

Why is this called CanonicalClangType? There doesn't seem to be anything specific about clang in this function.

Choose a reason for hiding this comment

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

I guess this would only be a canonical type if it's a clang type, since untypedefing wouldn't necessarily result in a canonical Swift type, right? Maybe add an assert or early return if this isn't a clang type?

Copy link
Author

Choose a reason for hiding this comment

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

You're right, the function itself is generic, but I wanted to differentiate it from TypeSystemSwiftTypeRef::GetCanonicalNode, which also resolves arbitrarily deep nested Swift type aliases.
I like the idea of asserting that it's a clang type.

for (unsigned i = 0; i < 32; ++i) {

Choose a reason for hiding this comment

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

Why 32? Is this just an arbitrary limit? If so I'd raise it to 1024 or something like that...

Copy link
Author

Choose a reason for hiding this comment

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

It's an arbitrary limit to guard against cycles.
It's for chains of typedefs, so practically I wouldn't expect more than two to ever appear, but I'm fine with dialing up the limit a bit.

if (!type.IsTypedefType())
return type;
type = type.GetTypedefedType();
}
return type;
}

} // namespace

llvm::Expected<uint32_t>
Expand All @@ -721,6 +730,7 @@ SwiftLanguageRuntime::GetNumChildren(CompilerType type,
if (!clang_type)
ts.IsImportedType(type.GetOpaqueQualType(), &clang_type);
if (clang_type) {
clang_type = GetCanonicalClangType(clang_type);
bool is_signed;
if (clang_type.IsEnumerationType(is_signed))
return 1;
Expand Down Expand Up @@ -1150,11 +1160,12 @@ SwiftLanguageRuntime::GetIndexOfChildMemberWithName(
}
}
case TypeInfoKind::Builtin: {
// Clang enums have an artificial rawValue property.
CompilerType clang_type;
if (ts.IsImportedType(type.GetOpaqueQualType(), &clang_type)) {
clang_type = GetCanonicalClangType(clang_type);
bool is_signed;
if (clang_type.IsEnumerationType(is_signed) && name == "rawValue") {
if (clang_type.IsEnumerationType(is_signed)) {
// Clang enums have an artificial rawValue property.
child_indexes.push_back(0);
return {SwiftLanguageRuntime::eFound, {1}};
}
Expand Down
25 changes: 17 additions & 8 deletions lldb/test/API/lang/swift/enum_objc/TestSwiftEnumObjC.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import lldbsuite.test.lldbinline as lldbinline
import lldb
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbtest as lldbtest
import lldbsuite.test.lldbutil as lldbutil

lldbinline.MakeInlineTest(
__file__,
globals(),
decorators=[
expectedFailureAll(oslist=["linux"], bugnumber="rdar://83444822"),
swiftTest
])
class TestSwiftDWARFImporterC(lldbtest.TestBase):

@swiftTest
@expectedFailureAll(oslist=["linux"], bugnumber="rdar://83444822")
def test(self):
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, 'break here', lldb.SBFileSpec('main.swift'))
self.expect('frame var -d run-target -- ascending', substrs=['OrderedAscending'])
self.expect('frame var -d run-target -- descending', substrs=['OrderedDescending'])
self.expect('frame var -d run-target -- same', substrs=['OrderedSame'])
self.expect('expr -d run-target -- ascending', substrs=['OrderedAscending'])
self.expect('expr -d run-target -- descending', substrs=['OrderedDescending'])
self.expect('expr -d run-target -- same', substrs=['OrderedSame'])
19 changes: 6 additions & 13 deletions lldb/test/API/lang/swift/enum_objc/main.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import Enum

func test()
{
let ascending = getReturn(-1)
let descending = getReturn(1)
let same = getReturn(0)
return //%self.expect('frame var -d run-target -- ascending', substrs=['OrderedAscending'])
//%self.expect('frame var -d run-target -- descending', substrs=['OrderedDescending'])
//%self.expect('frame var -d run-target -- same', substrs=['OrderedSame'])
//%self.expect('expr -d run-target -- ascending', substrs=['OrderedAscending'])
//%self.expect('expr -d run-target -- descending', substrs=['OrderedDescending'])
//%self.expect('expr -d run-target -- same', substrs=['OrderedSame'])
func test() {
let ascending = getReturn(-1)
let descending = getReturn(1)
let same = getReturn(0)
print("break here")
}

_ = test()
print("this is needed to load the stdlib")
test()