Skip to content

Commit 76e8303

Browse files
author
git apple-llvm automerger
committed
Merge commit 'c7c1565a3e2b' from swift/release/6.2 into stable/20240723
2 parents e41ef37 + c7c1565 commit 76e8303

File tree

3 files changed

+158
-4
lines changed

3 files changed

+158
-4
lines changed

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,48 @@ SwiftLanguageRuntime::GetGenericSignature(llvm::StringRef function_name,
14991499
return signature;
15001500
}
15011501

1502+
/// Returns true if a function called `symbol_name` exists in `module`.
1503+
static bool SymbolExists(StringRef symbol_name, Module &module) {
1504+
SymbolContextList sc_list;
1505+
Module::LookupInfo lookup_info(ConstString(symbol_name),
1506+
lldb::FunctionNameType::eFunctionNameTypeFull,
1507+
lldb::eLanguageTypeSwift);
1508+
module.FindFunctions(lookup_info, CompilerDeclContext(),
1509+
ModuleFunctionSearchOptions(), sc_list);
1510+
return !sc_list.IsEmpty();
1511+
}
1512+
1513+
/// There are two possible variants of constructors: allocating
1514+
/// (Kind::Allocator) and initializing (Kind::Constructor). When mangling a
1515+
/// closure, the "context" is arbitrarily chosen to be the Kind::Constructor
1516+
/// variant. This function checks for the existence of the Kind::Constructor,
1517+
/// given by the symbol `ctor_kind_mangled_name` in the module. If it exists,
1518+
/// `ctor_kind_mangled_name` is returned. Otherwise, creates and returns the
1519+
/// symbol name for the `Kind::Allocating` variant.
1520+
static std::string
1521+
HandleCtorAndAllocatorVariants(StringRef ctor_kind_mangled_name, Module &module,
1522+
Node &top_level_node, Node &ctor_node) {
1523+
if (SymbolExists(ctor_kind_mangled_name, module))
1524+
return ctor_kind_mangled_name.str();
1525+
1526+
using Kind = Node::Kind;
1527+
NodeFactory factory;
1528+
1529+
// Create a kind::Allocator node with all the children of the
1530+
// kind::Constructor node.
1531+
Node *allocating_ctor = factory.createNode(Kind::Allocator);
1532+
if (!allocating_ctor)
1533+
return "";
1534+
for (auto *child : ctor_node)
1535+
allocating_ctor->addChild(child, factory);
1536+
swift_demangle::ReplaceChildWith(top_level_node, ctor_node, *allocating_ctor);
1537+
1538+
if (auto mangled = swift::Demangle::mangleNode(&top_level_node);
1539+
mangled.isSuccess())
1540+
return std::move(mangled.result());
1541+
return "";
1542+
}
1543+
15021544
std::string SwiftLanguageRuntime::GetParentNameIfClosure(Function &func) {
15031545
using Kind = Node::Kind;
15041546
swift::Demangle::Context ctx;
@@ -1522,9 +1564,14 @@ std::string SwiftLanguageRuntime::GetParentNameIfClosure(Function &func) {
15221564
return "";
15231565
swift_demangle::ReplaceChildWith(*node, *closure_node, *parent_func_node);
15241566

1525-
if (ManglingErrorOr<std::string> mangled = swift::Demangle::mangleNode(node);
1526-
mangled.isSuccess())
1527-
return mangled.result();
1528-
return "";
1567+
ManglingErrorOr<std::string> mangled = swift::Demangle::mangleNode(node);
1568+
if (!mangled.isSuccess())
1569+
return "";
1570+
1571+
if (parent_func_node->getKind() == Kind::Constructor)
1572+
if (Module *module = func.CalculateSymbolContextModule().get())
1573+
return HandleCtorAndAllocatorVariants(mangled.result(), *module, *node,
1574+
*parent_func_node);
1575+
return mangled.result();
15291576
}
15301577
} // namespace lldb_private

lldb/test/API/lang/swift/closures_var_not_captured/TestSwiftClosureVarNotCaptured.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,61 @@ def test_ctor_class_closure(self):
134134
"static MY_CLASS.static_func(input_static:)",
135135
)
136136
check_no_enhanced_diagnostic(self, thread.frames[0], "dont_find_me_static")
137+
138+
@swiftTest
139+
def test_ctor_struct_closure(self):
140+
self.build()
141+
(target, process, thread) = self.get_to_bkpt("break_ctor_struct")
142+
check_not_captured_error(
143+
self, thread.frames[0], "input", "MY_STRUCT.init(input:)"
144+
)
145+
check_not_captured_error(
146+
self, thread.frames[0], "find_me", "MY_STRUCT.init(input:)"
147+
)
148+
check_no_enhanced_diagnostic(self, thread.frames[0], "dont_find_me")
149+
150+
lldbutil.continue_to_source_breakpoint(
151+
self, process, "break_static_member_struct", lldb.SBFileSpec("main.swift")
152+
)
153+
check_not_captured_error(
154+
self,
155+
thread.frames[0],
156+
"input_static",
157+
"static MY_STRUCT.static_func(input_static:)",
158+
)
159+
check_not_captured_error(
160+
self,
161+
thread.frames[0],
162+
"find_me_static",
163+
"static MY_STRUCT.static_func(input_static:)",
164+
)
165+
check_no_enhanced_diagnostic(self, thread.frames[0], "dont_find_me_static")
166+
167+
@swiftTest
168+
def test_ctor_enum_closure(self):
169+
self.build()
170+
(target, process, thread) = self.get_to_bkpt("break_ctor_enum")
171+
check_not_captured_error(
172+
self, thread.frames[0], "input", "MY_ENUM.init(input:)"
173+
)
174+
check_not_captured_error(
175+
self, thread.frames[0], "find_me", "MY_ENUM.init(input:)"
176+
)
177+
check_no_enhanced_diagnostic(self, thread.frames[0], "dont_find_me")
178+
179+
lldbutil.continue_to_source_breakpoint(
180+
self, process, "break_static_member_enum", lldb.SBFileSpec("main.swift")
181+
)
182+
check_not_captured_error(
183+
self,
184+
thread.frames[0],
185+
"input_static",
186+
"static MY_ENUM.static_func(input_static:)",
187+
)
188+
check_not_captured_error(
189+
self,
190+
thread.frames[0],
191+
"find_me_static",
192+
"static MY_ENUM.static_func(input_static:)",
193+
)
194+
check_no_enhanced_diagnostic(self, thread.frames[0], "dont_find_me_static")

lldb/test/API/lang/swift/closures_var_not_captured/main.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,57 @@ class MY_CLASS {
105105
}
106106
}
107107

108+
struct MY_STRUCT {
109+
init(input: [Int]) {
110+
let find_me = "hello"
111+
let _ = input.map {
112+
print("break_ctor_struct")
113+
return $0
114+
}
115+
let dont_find_me = "hello"
116+
}
117+
118+
static func static_func(input_static: [Int]) {
119+
let find_me_static = "hello"
120+
let _ = input_static.map {
121+
print("break_static_member_struct")
122+
return $0
123+
}
124+
let dont_find_me_static = "hello"
125+
}
126+
}
127+
128+
enum MY_ENUM {
129+
case case1(Double)
130+
case case2(Double)
131+
132+
init(input: [Int]) {
133+
let find_me = "hello"
134+
let _ = input.map {
135+
print("break_ctor_enum")
136+
return $0
137+
}
138+
139+
let dont_find_me = "hello"
140+
self = .case1(42.0)
141+
}
142+
143+
static func static_func(input_static: [Int]) {
144+
let find_me_static = "hello"
145+
let _ = input_static.map {
146+
print("break_static_member_enum")
147+
return $0
148+
}
149+
let dont_find_me_static = "hello"
150+
}
151+
}
152+
108153
func_1(arg: 42)
109154
func_2(arg: 42)
110155
await func_3(arg: 42)
111156
let _ = MY_CLASS(input: [1, 2])
112157
MY_CLASS.static_func(input_static: [42])
158+
let _ = MY_STRUCT(input: [1, 2])
159+
MY_STRUCT.static_func(input_static: [42])
160+
let _ = MY_ENUM(input: [1,2])
161+
MY_ENUM.static_func(input_static: [42])

0 commit comments

Comments
 (0)