Skip to content

Commit c6d7adb

Browse files
Merge pull request #2007 from adrian-prantl/dynamic-main
Use the use_dynamic option to determine whether to dynamically resolve
2 parents 23b3574 + 46fd0ae commit c6d7adb

File tree

13 files changed

+69
-47
lines changed

13 files changed

+69
-47
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,10 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
447447
};
448448
}; // END Anonymous namespace
449449

450-
static void
451-
AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
452-
SwiftASTContextForExpressions &swift_ast_context,
453-
SwiftASTManipulator &manipulator) {
450+
static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
451+
SwiftASTContextForExpressions &swift_ast_context,
452+
SwiftASTManipulator &manipulator,
453+
lldb::DynamicValueType use_dynamic) {
454454
// First emit the typealias for "$__lldb_context".
455455
if (!block)
456456
return;
@@ -480,7 +480,7 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
480480
if (stack_frame_sp) {
481481
lldb::ValueObjectSP valobj_sp =
482482
stack_frame_sp->GetValueObjectForFrameVariable(self_var_sp,
483-
lldb::eDynamicCanRunTarget);
483+
use_dynamic);
484484

485485
if (valobj_sp)
486486
self_type = valobj_sp->GetCompilerType();
@@ -578,17 +578,18 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
578578

579579
/// Create a \c VariableInfo record for \c variable if there isn't
580580
/// already shadowing inner declaration in \c processed_variables.
581-
static void AddVariableInfo(
581+
static llvm::Optional<llvm::Error> AddVariableInfo(
582582
lldb::VariableSP variable_sp, lldb::StackFrameSP &stack_frame_sp,
583583
SwiftASTContextForExpressions &ast_context,
584584
SwiftLanguageRuntime *language_runtime,
585585
llvm::SmallDenseSet<const char *, 8> &processed_variables,
586-
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables) {
586+
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables,
587+
lldb::DynamicValueType use_dynamic) {
587588
StringRef name = variable_sp->GetUnqualifiedName().GetStringRef();
588589
const char *name_cstr = name.data();
589590
assert(StringRef(name_cstr) == name && "missing null terminator");
590591
if (name.empty())
591-
return;
592+
return {};
592593

593594
// To support "guard let self = self" the function argument "self"
594595
// is processed (as the special self argument) even if it is
@@ -599,35 +600,35 @@ static void AddVariableInfo(
599600
overridden_name = "$__lldb_injected_self";
600601

601602
if (processed_variables.count(overridden_name))
602-
return;
603+
return {};
603604

604605
CompilerType var_type;
605606
if (stack_frame_sp) {
606607
lldb::ValueObjectSP valobj_sp =
607608
stack_frame_sp->GetValueObjectForFrameVariable(variable_sp,
608-
lldb::eDynamicCanRunTarget);
609+
use_dynamic);
609610

610611
if (!valobj_sp || valobj_sp->GetError().Fail()) {
611612
// Ignore the variable if we couldn't find its corresponding
612613
// value object. TODO if the expression tries to use an
613614
// ignored variable, produce a sensible error.
614-
return;
615+
return {};
615616
}
616617
var_type = valobj_sp->GetCompilerType();
617618
}
618619

619620
if (!var_type.IsValid())
620-
return;
621+
return {};
621622

622623
if (!var_type.GetTypeSystem()->SupportsLanguage(lldb::eLanguageTypeSwift))
623-
return;
624+
return {};
624625

625626
Status error;
626627
CompilerType target_type = ast_context.ImportType(var_type, error);
627628

628629
// If the import failed, give up.
629630
if (!target_type.IsValid())
630-
return;
631+
return {};
631632

632633
// If we couldn't fully realize the type, then we aren't going
633634
// to get very far making a local out of it, so discard it here.
@@ -638,7 +639,16 @@ static void AddVariableInfo(
638639
log->Printf("Discarding local %s because we couldn't fully realize it, "
639640
"our best attempt was: %s.",
640641
name_cstr, target_type.GetDisplayTypeName().AsCString("<unknown>"));
641-
return;
642+
// Not realizing self is a fatal error for an expression and the
643+
// Swift compiler error alone is not particularly useful.
644+
if (is_self)
645+
return make_error<StringError>(
646+
inconvertibleErrorCode(),
647+
llvm::Twine("Couldn't realize type of self.") +
648+
(use_dynamic
649+
? ""
650+
: " Try evaluating the expression with -d run-target"));
651+
return {};
642652
}
643653

644654
if (log && is_self)
@@ -680,15 +690,17 @@ static void AddVariableInfo(
680690

681691
local_variables.push_back(variable_info);
682692
processed_variables.insert(overridden_name);
693+
return {};
683694
}
684695

685696
/// Create a \c VariableInfo record for each visible variable.
686-
static void RegisterAllVariables(
697+
static llvm::Optional<llvm::Error> RegisterAllVariables(
687698
SymbolContext &sc, lldb::StackFrameSP &stack_frame_sp,
688699
SwiftASTContextForExpressions &ast_context,
689-
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables) {
700+
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables,
701+
lldb::DynamicValueType use_dynamic) {
690702
if (!sc.block && !sc.function)
691-
return;
703+
return {};
692704

693705
Block *block = sc.block;
694706
Block *top_block = block->GetContainingInlinedBlock();
@@ -743,9 +755,11 @@ static void RegisterAllVariables(
743755
}
744756

745757
for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi)
746-
AddVariableInfo({variables.GetVariableAtIndex(vi)}, stack_frame_sp,
747-
ast_context, language_runtime, processed_names,
748-
local_variables);
758+
if (auto error = AddVariableInfo(
759+
{variables.GetVariableAtIndex(vi)}, stack_frame_sp, ast_context,
760+
language_runtime, processed_names, local_variables, use_dynamic))
761+
return error;
762+
return {};
749763
}
750764

751765
static void ResolveSpecialNames(
@@ -1331,11 +1345,12 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
13311345

13321346
if (local_context_is_swift) {
13331347
AddRequiredAliases(sc.block, stack_frame_sp, *swift_ast_context,
1334-
*code_manipulator);
1348+
*code_manipulator, options.GetUseDynamic());
13351349

13361350
// Register all local variables so that lookups to them resolve.
1337-
RegisterAllVariables(sc, stack_frame_sp, *swift_ast_context,
1338-
local_variables);
1351+
if (auto error = RegisterAllVariables(sc, stack_frame_sp, *swift_ast_context,
1352+
local_variables, options.GetUseDynamic()))
1353+
return std::move(*error);
13391354
}
13401355

13411356
// Register all magic variables.

lldb/test/API/lang/swift/availability/TestAvailability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ class C4<U> {
140140

141141
for breakpoint in breakpoints:
142142
threads = lldbutil.continue_to_breakpoint(process, breakpoint)
143-
self.expect("p f()", "can call")
143+
self.expect("expr -d no-run-target -- f()", "can call")
144144

lldb/test/API/lang/swift/clangimporter/remoteast_import/TestSwiftRemoteASTImport.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class TestSwiftRemoteASTImport(TestBase):
2424
def setUp(self):
2525
TestBase.setUp(self)
2626

27-
@expectedFailureAll #FIXME: This regressed silently due to 2c911bceb06ed376801251bdfd992905a66f276c
2827
# Don't run ClangImporter tests if Clangimporter is disabled.
2928
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
3029
@skipUnlessDarwin

lldb/test/API/lang/swift/expression/class_constrained_protocol/TestClassConstrainedProtocol.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,42 @@ class TestClassConstrainedProtocol(TestBase):
1818
mydir = TestBase.compute_mydir(__file__)
1919

2020
@swiftTest
21-
def test_extension_weak_self (self):
21+
def test_extension_weak_self(self):
2222
"""Test that we can reconstruct weak self captured in a class constrained protocol."""
2323
self.build()
24-
self.do_self_test("Break here for weak self")
24+
self.do_self_test("Break here for weak self", needs_dynamic=True)
2525

2626
@swiftTest
2727
def test_extension_self (self):
2828
"""Test that we can reconstruct self in method of a class constrained protocol."""
2929
self.build()
30-
self.do_self_test("Break here in class protocol")
30+
self.do_self_test("Break here in class protocol", needs_dynamic=True)
3131

3232
@swiftTest
33-
def test_method_weak_self (self):
33+
def test_method_weak_self(self):
3434
"""Test that we can reconstruct weak self capture in method of a class conforming to a class constrained protocol."""
3535
self.build()
36-
self.do_self_test("Break here for method weak self")
36+
self.do_self_test("Break here for method weak self", needs_dynamic=False)
3737

3838
@swiftTest
39-
def test_method_self (self):
39+
def test_method_self(self):
4040
"""Test that we can reconstruct self in method of a class conforming to a class constrained protocol."""
4141
self.build()
42-
self.do_self_test("Break here in method")
42+
self.do_self_test("Break here in method", needs_dynamic=False)
4343

4444
def setUp(self):
4545
# Call super's setUp().
4646
TestBase.setUp(self)
4747

48-
def check_self(self, bkpt_pattern):
48+
def check_self(self, bkpt_pattern, needs_dynamic):
4949
opts = lldb.SBExpressionOptions()
50+
if needs_dynamic:
51+
opts.SetFetchDynamicValue(lldb.eNoDynamicValues)
52+
result = self.frame().EvaluateExpression("self", opts)
53+
error = result.GetError()
54+
self.assertTrue("self" in error.GetCString())
55+
self.assertTrue("run-target" in error.GetCString())
56+
opts.SetFetchDynamicValue(lldb.eDynamicCanRunTarget)
5057
result = self.frame().EvaluateExpression("self", opts)
5158
error = result.GetError()
5259
self.assertTrue(error.Success(),
@@ -58,7 +65,7 @@ def check_self(self, bkpt_pattern):
5865
self.assertTrue(f_ivar.GetValueAsSigned() == 12345,
5966
"Wrong value for f: %d"%(f_ivar.GetValueAsSigned()))
6067

61-
def do_self_test(self, bkpt_pattern):
68+
def do_self_test(self, bkpt_pattern, needs_dynamic):
6269
lldbutil.run_to_source_breakpoint(
6370
self, bkpt_pattern, lldb.SBFileSpec('main.swift'))
64-
self.check_self(bkpt_pattern)
71+
self.check_self(bkpt_pattern, needs_dynamic)

lldb/test/API/lang/swift/expression/generic/TestSwiftGenericExpressions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def test_ivars_in_generic_expressions(self):
4242
self.do_ivar_test()
4343

4444
def check_expression(self, expression, expected_result, use_summary=True):
45-
value = self.frame().EvaluateExpression(expression)
45+
opts = lldb.SBExpressionOptions()
46+
opts.SetFetchDynamicValue(lldb.eDynamicCanRunTarget)
47+
value = self.frame().EvaluateExpression(expression, opts)
4648
self.assertTrue(value.IsValid(), expression + "returned a valid value")
4749

4850
self.assertTrue(value.GetError().Success(), "expression failed")

lldb/test/API/lang/swift/expression/generic_protocol_extension/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class C<T> : P {
2020

2121
extension P {
2222
func f() -> Int {
23-
return v //% self.expect("p self", DATA_TYPES_DISPLAYED_CORRECTLY, substrs = ["a.C"])
23+
return v //% self.expect("expr -d run-target -- self", DATA_TYPES_DISPLAYED_CORRECTLY, substrs = ["a.C"])
2424
}
2525
}
2626

lldb/test/API/lang/swift/expression/mutating_struct_extension/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension Array
1414
{
1515
mutating func doGenericStuff()
1616
{
17-
print("generic stuff") //% self.expect("expr 2+3", DATA_TYPES_DISPLAYED_CORRECTLY, substrs = ["5"])
17+
print("generic stuff") //% self.expect("expr -d run-target -- 2+3", DATA_TYPES_DISPLAYED_CORRECTLY, substrs = ["5"])
1818
}
1919
}
2020

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// -----------------------------------------------------------------------------
1212
extension Array {
1313
func test() {
14-
print("PATPAT") //% self.expect("p self", substrs=['[0] = 1', '[1] = 2', '[2] = 3'])
14+
print("PATPAT") //% self.expect("expr -d run-target -- self", substrs=['[0] = 1', '[1] = 2', '[2] = 3'])
1515
return //% self.expect("frame variable -d run -- self", substrs=['[0] = 1', '[1] = 2', '[2] = 3'])
1616
}
1717
}

lldb/test/API/lang/swift/variables/generic_struct_debug_info/generic_apply/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func apply<Type>(_ T : Type, fn: (Type) -> Type) -> Type { return fn(T) }
1414
public func f<Type>(_ value : Type)
1515
{
1616
apply(value) { arg in
17-
return arg //% self.expect('po arg', substrs=['3735928559'])
17+
return arg //% self.expect('expr -o -d run -- arg', substrs=['3735928559'])
1818
//% self.expect('expr -d run -- arg', substrs=['Int', '3735928559'])
1919
//% self.expect('fr var -d run -- arg', substrs=['Int', '3735928559'])
2020
}

lldb/test/API/lang/swift/variables/generic_struct_debug_info/generic_array/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct Q<T> {
1313
let x: T
1414
}
1515
public func foo<T>(_ arg: [Q<T>]) {
16-
print(arg) //% self.expect('po arg', substrs=['x : 3735928559'])
16+
print(arg) //% self.expect('expr -o -d run -- arg', substrs=['x : 3735928559'])
1717
//% self.expect('expr -d run -- arg', substrs=['x = 3735928559'])
1818
//% self.expect('frame var -d run -- arg', substrs=['x = 3735928559'])
1919
}

lldb/test/API/lang/swift/variables/generic_struct_debug_info/generic_flatmap/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ struct FlatMapper<Type>
3434
]
3535

3636
let _ = tuples.flatMap { tuple in
37-
return tuple //% self.expect('po tuple', substrs=['originalIndex : 0', 'filteredIndex : 0', 'name : "Coffee"', 'ID : "1"'])
37+
return tuple //% self.expect('expr -o -d run -- tuple', substrs=['originalIndex : 0', 'filteredIndex : 0', 'name : "Coffee"', 'ID : "1"'])
3838
//% self.expect('expr -d run -- tuple', substrs=['originalIndex = 0', 'filteredIndex = 0', 'name = "Coffee"', 'ID = "1"'])
3939
//% self.expect('frame var -d run -- tuple', substrs=['originalIndex = 0', 'filteredIndex = 0', 'name = "Coffee"', 'ID = "1"'])
4040
}
4141

4242
let _ = values.flatMap { value in
43-
return value //% self.expect('po value', substrs=['name : "Coffee"', 'ID : "1"'])
43+
return value //% self.expect('expr -o -d run -- value', substrs=['name : "Coffee"', 'ID : "1"'])
4444
//% self.expect('expr -d run -- value', substrs=['name = "Coffee"', 'ID = "1"'])
4545
//% self.expect('frame var -d run -- value', substrs=['name = "Coffee"', 'ID = "1"'])
4646
}

lldb/test/API/lang/swift/variables/protocol/TestSwiftProtocolTypes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class TestSwiftProtocolTypes(TestBase):
2323

2424
mydir = TestBase.compute_mydir(__file__)
2525

26-
@expectedFailureAll #FIXME: This regressed silently due to 2c911bceb06ed376801251bdfd992905a66f276c
2726
@swiftTest
2827
def test_swift_protocol_types(self):
2928
"""Test support for protocol types"""
@@ -102,7 +101,7 @@ def do_test(self):
102101
'x = 1.25', 'y = 2.5'])
103102

104103
self.expect("expression --raw-output --show-types -- loc3dCB",
105-
substrs=['PointUtils & AnyObject) $R',
104+
substrs=['PointUtils & Swift.AnyObject) $R',
106105
'(Builtin.RawPointer) instance = 0x',
107106
'(Builtin.RawPointer) witness_table_PointUtils = 0x'])
108107

lldb/test/API/lang/swift/variables/uninitialized/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct A<T> {
2323
let c = cs[0]
2424

2525
let k1 = b(t:c)
26-
let k2 = b(t:c) //% self.expect("expression -- c", "Unreadable variable is ignored", substrs = ["= 3"])
26+
let k2 = b(t:c) //% self.expect("expr -d run -- c", "Unreadable variable is ignored", substrs = ["= 3"])
2727
let k3 = b(t:c)
2828

2929
if let maybeT = process(i : adict.count, k1:k1, k2:k2, k3:k3) {

0 commit comments

Comments
 (0)