Skip to content

Commit ce4b4ee

Browse files
committed
[lldb] Allow SwiftASTContext fallbacks for types from expressions
Generally, it would be feasible to rely on DWARF and reflectio metadata for JIT-compiled images, but in practice, not everything that an expression returns may be anchored by a variable, which makes having debug info for expression-defined types hit and miss. If an expression type is involved, LLDB will have done the costly SwiftASTContext initialization already, so there isn't much saved by avoiding the fallback in this case.
1 parent 22f7040 commit ce4b4ee

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftDemangle.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ NodeAtPath(swift::Demangle::NodePointer root,
6060
return ChildAtPath(root, kind_path.drop_front());
6161
}
6262

63+
/// Find the first child node of root that satisfies cond.
64+
inline swift::Demangle::NodePointer
65+
FindIf(swift::Demangle::NodePointer root,
66+
std::function<bool(swift::Demangle::NodePointer)> cond) {
67+
if (!root)
68+
return nullptr;
69+
70+
auto *node = root;
71+
if (cond(node))
72+
return node;
73+
for (auto *child : *node) {
74+
assert(child && "swift::Demangle::Node has null child");
75+
if (auto *found = FindIf(child, cond))
76+
return found;
77+
}
78+
79+
return nullptr;
80+
}
81+
6382
/// \return the child of the TypeMangling node.
6483
static swift::Demangle::NodePointer
6584
GetTypeMangling(swift::Demangle::NodePointer n) {

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,29 +1653,6 @@ swift::Demangle::NodePointer TypeSystemSwiftTypeRef::GetDemangleTreeForPrinting(
16531653
return GetNodeForPrintingImpl(dem, node, flavor, resolve_objc_module);
16541654
}
16551655

1656-
/// Determine wether this demangle tree contains a node of kind \c kind and with
1657-
/// text \c text (if provided).
1658-
static bool Contains(swift::Demangle::NodePointer node,
1659-
swift::Demangle::Node::Kind kind,
1660-
llvm::StringRef text = "") {
1661-
if (!node)
1662-
return false;
1663-
1664-
if (node->getKind() == kind) {
1665-
if (text.empty())
1666-
return true;
1667-
if (!node->hasText())
1668-
return false;
1669-
return node->getText() == text;
1670-
}
1671-
1672-
for (swift::Demangle::NodePointer child : *node)
1673-
if (Contains(child, kind, text))
1674-
return true;
1675-
1676-
return false;
1677-
}
1678-
16791656
static bool ProtocolCompositionContainsSingleObjcProtocol(
16801657
swift::Demangle::NodePointer node) {
16811658
// Kind=ProtocolList
@@ -1691,12 +1668,18 @@ static bool ProtocolCompositionContainsSingleObjcProtocol(
16911668
type_list->getNumChildren() != 1)
16921669
return false;
16931670
NodePointer type = type_list->getFirstChild();
1694-
return Contains(type, Node::Kind::Module, swift::MANGLING_MODULE_OBJC);
1671+
return swift_demangle::FindIf(type, [](NodePointer node) {
1672+
return node->getKind() == Node::Kind::Module && node->hasText() &&
1673+
node->getText() == swift::MANGLING_MODULE_OBJC;
1674+
});
16951675
}
16961676

16971677
/// Determine wether this demangle tree contains a generic type parameter.
16981678
static bool ContainsGenericTypeParameter(swift::Demangle::NodePointer node) {
1699-
return Contains(node, swift::Demangle::Node::Kind::DependentGenericParamType);
1679+
return swift_demangle::FindIf(node, [](NodePointer node) {
1680+
return node->getKind() ==
1681+
swift::Demangle::Node::Kind::DependentGenericParamType;
1682+
});
17001683
}
17011684

17021685
/// Collect TypeInfo flags from a demangle tree. For most attributes
@@ -1929,7 +1912,9 @@ uint32_t TypeSystemSwiftTypeRef::CollectTypeInfo(
19291912
if (swift_flags != eTypeIsSwift) {
19301913
if (ContainsGenericTypeParameter(node))
19311914
swift_flags |= eTypeHasUnboundGeneric;
1932-
if (Contains(node, Node::Kind::DynamicSelf))
1915+
if (swift_demangle::FindIf(node, [](NodePointer node) {
1916+
return node->getKind() == swift::Demangle::Node::Kind::DynamicSelf;
1917+
}))
19331918
swift_flags |= eTypeHasDynamicSelf;
19341919
return swift_flags;
19351920
}
@@ -2817,6 +2802,8 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
28172802

28182803
bool TypeSystemSwiftTypeRef::UseSwiftASTContextFallback(
28192804
const char *func_name, lldb::opaque_compiler_type_t type) {
2805+
if (IsExpressionEvaluatorDefined(type))
2806+
return true;
28202807
if (!ModuleList::GetGlobalModuleListProperties().GetSwiftTypeSystemFallback())
28212808
return false;
28222809

@@ -2827,8 +2814,11 @@ bool TypeSystemSwiftTypeRef::UseSwiftASTContextFallback(
28272814
return true;
28282815
}
28292816

2830-
bool TypeSystemSwiftTypeRef::DiagnoseSwiftASTContextFallback(
2817+
void TypeSystemSwiftTypeRef::DiagnoseSwiftASTContextFallback(
28312818
const char *func_name, lldb::opaque_compiler_type_t type) {
2819+
if (IsExpressionEvaluatorDefined(type))
2820+
return;
2821+
28322822
const char *type_name = AsMangledName(type);
28332823

28342824
std::optional<lldb::user_id_t> debugger_id;
@@ -2842,7 +2832,6 @@ bool TypeSystemSwiftTypeRef::DiagnoseSwiftASTContextFallback(
28422832
Debugger::ReportWarning(msg, debugger_id, &m_fallback_warning);
28432833

28442834
LLDB_LOGF(GetLog(LLDBLog::Types), "%s", msg.c_str());
2845-
return true;
28462835
}
28472836

28482837
CompilerType
@@ -2900,6 +2889,20 @@ TypeSystemSwiftTypeRef::DemangleCanonicalOutermostType(
29002889
return canonical;
29012890
}
29022891

2892+
bool TypeSystemSwiftTypeRef::IsExpressionEvaluatorDefined(
2893+
lldb::opaque_compiler_type_t type) {
2894+
using namespace swift::Demangle;
2895+
const auto *mangled_name = AsMangledName(type);
2896+
Demangler dem;
2897+
NodePointer node = GetDemangledType(dem, mangled_name);
2898+
return swift_demangle::FindIf(node, [](NodePointer node) -> NodePointer {
2899+
if (node->getKind() == Node::Kind::Module &&
2900+
node->getText().starts_with("__lldb_expr"))
2901+
return node;
2902+
return nullptr;
2903+
});
2904+
}
2905+
29032906
CompilerType TypeSystemSwiftTypeRef::CreateGenericTypeParamType(
29042907
unsigned int depth, unsigned int index,
29052908
swift::Mangle::ManglingFlavor flavor) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
446446
bool UseSwiftASTContextFallback(const char *func_name,
447447
lldb::opaque_compiler_type_t type);
448448
/// Print a warning that a fallback was necessary.
449-
bool DiagnoseSwiftASTContextFallback(const char *func_name,
449+
void DiagnoseSwiftASTContextFallback(const char *func_name,
450450
lldb::opaque_compiler_type_t type);
451451

452452
/// Helper that creates an AST type from \p type.
@@ -544,6 +544,11 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
544544
swift::Demangle::NodePointer
545545
GetClangTypeTypeNode(swift::Demangle::Demangler &dem,
546546
CompilerType clang_type);
547+
548+
/// Determine if this type contains a type from a module that looks
549+
/// like it was JIT-compiled by LLDB.
550+
bool IsExpressionEvaluatorDefined(lldb::opaque_compiler_type_t type);
551+
547552
#ifndef NDEBUG
548553
/// Check whether the type being dealt with is tricky to validate due to
549554
/// discrepancies between TypeSystemSwiftTypeRef and SwiftASTContext.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
SWIFT_SOURCES := main.swift
22
include Makefile.rules
33

4-
cleanup:
5-
rm -f Makefile *.d
6-

0 commit comments

Comments
 (0)