Skip to content

Commit e675022

Browse files
Merge pull request #8498 from adrian-prantl/124540552
Support Objective-C enums without SwiftASTContext
2 parents 5e8ac4b + 2d19597 commit e675022

File tree

27 files changed

+102
-18
lines changed

27 files changed

+102
-18
lines changed

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
1717
#include "lldb/Core/ValueObject.h"
1818
#include "lldb/Symbol/CompilerType.h"
19+
#include "lldb/Utility/LLDBLog.h"
20+
#include "lldb/Utility/Log.h"
1921
#include "lldb/Utility/StreamString.h"
2022

2123
#include "clang/AST/Decl.h"
@@ -87,17 +89,29 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
8789
// standalone and provided with a callback to read the APINote
8890
// information.
8991
auto ts = m_type.GetTypeSystem();
92+
TypeSystemSwift *tss = nullptr;
9093
std::shared_ptr<SwiftASTContext> swift_ast_ctx;
9194
if (auto trts =
9295
ts.dyn_cast_or_null<TypeSystemSwiftTypeRef>()) {
93-
m_type = trts->ReconstructType(m_type, exe_ctx);
94-
swift_ast_ctx = m_type.GetTypeSystem().dyn_cast_or_null<SwiftASTContext>();
95-
if (!swift_ast_ctx)
96-
return;
97-
decl_ts = GetAsEnumDecl(m_type);
98-
enum_decl = decl_ts.first;
99-
if (!enum_decl)
100-
return;
96+
tss = trts.get();
97+
CompilerType ast_type = trts->ReconstructType(m_type, exe_ctx);
98+
swift_ast_ctx =
99+
ast_type.GetTypeSystem().dyn_cast_or_null<SwiftASTContext>();
100+
if (swift_ast_ctx) {
101+
auto ast_decl_ts = GetAsEnumDecl(ast_type);
102+
if (ast_decl_ts.first) {
103+
tss = swift_ast_ctx.get();
104+
enum_decl = ast_decl_ts.first;
105+
m_type = ast_type;
106+
} else {
107+
LLDB_LOG(GetLog(LLDBLog::DataFormatters),
108+
"Cannot get Clang type for {0}",
109+
m_type.GetMangledTypeName());
110+
}
111+
} else {
112+
LLDB_LOG(GetLog(LLDBLog::DataFormatters), "Cannot reconstruct {0}",
113+
m_type.GetMangledTypeName());
114+
}
101115
}
102116

103117
auto iter = enum_decl->enumerator_begin(), end = enum_decl->enumerator_end();
@@ -112,8 +126,7 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
112126
case_init_val = case_init_val.zext(64);
113127
if (case_init_val.getBitWidth() > 64)
114128
continue;
115-
ConstString case_name(
116-
swift_ast_ctx->GetSwiftName(case_decl, *decl_ts.second));
129+
ConstString case_name(tss->GetSwiftName(case_decl, *decl_ts.second));
117130
m_cases->push_back({case_init_val, case_name});
118131
}
119132
}

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,32 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
688688
if (!ts)
689689
return llvm::make_error<llvm::StringError>("no Swift typesystem",
690690
llvm::inconvertibleErrorCode());
691+
if (!type)
692+
return llvm::make_error<llvm::StringError>("invalid type",
693+
llvm::inconvertibleErrorCode());
691694

692695
// Deal with the LLDB-only SILPackType variant.
693696
if (auto pack_type = ts->IsSILPackType(type))
694697
if (pack_type->expanded)
695698
return pack_type->count;
696699

700+
// Deal with Clang types.
701+
{
702+
CompilerType clang_type =
703+
LookupAnonymousClangType(type.GetMangledTypeName().AsCString());
704+
if (!clang_type)
705+
ts->IsImportedType(type.GetOpaqueQualType(), &clang_type);
706+
if (clang_type) {
707+
bool is_signed;
708+
if (clang_type.IsEnumerationType(is_signed))
709+
return 1;
710+
ExecutionContext exe_ctx;
711+
if (exe_scope)
712+
exe_scope->CalculateExecutionContext(exe_ctx);
713+
return clang_type.GetNumChildren(true, exe_scope ? &exe_ctx : nullptr);
714+
}
715+
}
716+
697717
// Try the static type metadata.
698718
const swift::reflection::TypeRef *tr = nullptr;
699719
auto *ti = GetSwiftRuntimeTypeInfo(type, exe_scope, &tr);
@@ -710,14 +730,6 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
710730
//
711731
// However, some imported Clang types (specifically enums) will also produce
712732
// `BuiltinTypeInfo` instances. These types are not to be handled here.
713-
swift::Demangle::Context dem;
714-
NodePointer root = SwiftLanguageRuntime::DemangleSymbolAsNode(
715-
type.GetMangledTypeName().GetStringRef(), dem);
716-
using Kind = Node::Kind;
717-
auto *builtin_type = swift_demangle::nodeAtPath(
718-
root, {Kind::TypeMangling, Kind::Type, Kind::BuiltinTypeName});
719-
if (builtin_type)
720-
return 0;
721733
LLDB_LOG(GetLog(LLDBLog::Types),
722734
"{0}: unrecognized builtin type info or this is a Clang type "
723735
"without DWARF debug info",
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_OBJC_INTEROP := 1
3+
SWIFTFLAGS_EXTRAS = -Xcc -I$(SRCDIR)
4+
include Makefile.rules
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 TestSwiftNSIntegerNSEnum(lldbtest.TestBase):
8+
def do_test(self, use_summary):
9+
def check(var, output):
10+
if use_summary:
11+
lldbutil.check_variable(self, var, False, summary='.'+output)
12+
else:
13+
lldbutil.check_variable(self, var, False, value=output)
14+
15+
self.build()
16+
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
17+
self, "break here", lldb.SBFileSpec("main.swift")
18+
)
19+
frame = thread.frames[0]
20+
self.assertTrue(frame)
21+
check(frame.FindVariable('e1'), 'eCase1')
22+
check(frame.FindVariable('e2'), 'eCase2')
23+
24+
@skipUnlessDarwin
25+
@swiftTest
26+
def test_reflection(self):
27+
self.expect("setting set symbols.swift-enable-ast-context false")
28+
self.do_test(use_summary=True)
29+
30+
# Don't run a clangimporter test without ClangImporter.
31+
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
32+
@skipUnlessDarwin
33+
@swiftTest
34+
def test_swift_ast(self):
35+
self.expect("setting set symbols.swift-enable-ast-context true")
36+
self.do_test(use_summary=False)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <Foundation/Foundation.h>
2+
3+
typedef NS_ENUM(NSInteger, ObjCEnum) {
4+
eCase1, eCase2
5+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Foundation
2+
import Enum
3+
4+
func main(_ e1 : ObjCEnum, _ e2 : ObjCEnum) {
5+
print("break here")
6+
}
7+
8+
let e1 : ObjCEnum = .eCase1
9+
let e2 : ObjCEnum = .eCase2
10+
main(e1, e2)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Enum {
2+
header "header.h"
3+
export *
4+
}

0 commit comments

Comments
 (0)