Skip to content

Commit b83a5be

Browse files
committed
[Swift] Implement error handling for dwim-print -O
1 parent 0e8fa5a commit b83a5be

File tree

8 files changed

+61
-61
lines changed

8 files changed

+61
-61
lines changed

lldb/source/Core/FormatEntity.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,17 +1346,13 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
13461346
bool is_swift_error_return = false;
13471347
ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject(
13481348
stop_info_sp, is_swift_error_return);
1349+
// END SWIFT
13491350
if (return_valobj_sp) {
13501351
DumpValueObjectOptions options;
13511352
if (return_valobj_sp->IsDynamic())
13521353
options.SetUseDynamicType(eDynamicCanRunTarget);
13531354
if (return_valobj_sp->DoesProvideSyntheticValue())
13541355
options.SetUseSyntheticValue(true);
1355-
return_valobj_sp->Dump(s, options);
1356-
return true;
1357-
}
1358-
// END SWIFT
1359-
if (return_valobj_sp) {
13601356
if (llvm::Error error = return_valobj_sp->Dump(s)) {
13611357
s << "error: " << toString(std::move(error));
13621358
return false;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ bool SwiftREPL::PrintOneVariable(Debugger &debugger, StreamFileSP &output_sp,
536536
fprintf(output_sp->GetFile().GetStream(), "%s", color);
537537
}
538538

539-
valobj_sp->Dump(*output_sp, options);
539+
if (llvm::Error error = valobj_sp->Dump(*output_sp, options))
540+
*output_sp << "error: " << toString(std::move(error));
540541

541542
if (colorize_out)
542543
fprintf(output_sp->GetFile().GetStream(), ANSI_ESCAPE1(ANSI_CTRL_NORMAL));

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,11 @@ bool lldb_private::formatters::swift::CountableClosedRange_SummaryProvider(
710710
bool lldb_private::formatters::swift::BuiltinObjC_SummaryProvider(
711711
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
712712
stream.Printf("0x%" PRIx64 " ", valobj.GetValueAsUnsigned(0));
713-
stream.Printf("%s", valobj.GetObjectDescription());
713+
llvm::Expected<std::string> desc = valobj.GetObjectDescription();
714+
if (desc)
715+
stream << toString(desc.takeError());
716+
else
717+
stream << *desc;
714718
return true;
715719
}
716720

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

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ class SwiftLanguageRuntimeStub {
336336
return {};
337337
}
338338

339-
bool GetObjectDescription(Stream &str, ValueObject &object) {
339+
llvm::Error GetObjectDescription(Stream &str, ValueObject &object) {
340340
STUB_LOG();
341-
return false;
341+
return llvm::createStringError("Swift runtime not initialized");
342342
}
343343

344344
void AddToLibraryNegativeCache(llvm::StringRef library_name) {}
@@ -1046,11 +1046,8 @@ SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Copy(ValueObject &object,
10461046
return expr_string;
10471047
}
10481048

1049-
bool
1050-
SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
1051-
std::string &expr_string,
1052-
Stream &result)
1053-
{
1049+
llvm::Error SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(
1050+
ValueObject &object, std::string &expr_string, Stream &result) {
10541051
Log *log(GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions));
10551052
ValueObjectSP result_sp;
10561053
EvaluateExpressionOptions eval_options;
@@ -1064,10 +1061,8 @@ SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
10641061
frame_sp
10651062
= m_process.GetThreadList().GetSelectedThread()
10661063
->GetSelectedFrame(DoNoSelectMostRelevantFrame);
1067-
if (!frame_sp) {
1068-
log->Printf("no execution context to run expression in");
1069-
return false;
1070-
}
1064+
if (!frame_sp)
1065+
return llvm::createStringError("no execution context to run expression in");
10711066
auto eval_result = m_process.GetTarget().EvaluateExpression(
10721067
expr_string,
10731068
frame_sp.get(),
@@ -1085,26 +1080,22 @@ SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
10851080
log->Printf(
10861081
"[RunObjectDescriptionExpr] expression generated no result");
10871082

1088-
result.Printf("expression produced no result");
1089-
return false;
1083+
return llvm::createStringError("expression produced no result");
10901084
}
10911085
if (result_sp->GetError().Fail()) {
10921086
if (log)
10931087
log->Printf(
10941088
"[RunObjectDescriptionExpr] expression generated error: %s",
10951089
result_sp->GetError().AsCString());
10961090

1097-
result.Printf("expression produced error: %s",
1098-
result_sp->GetError().AsCString());
1099-
return false;
1091+
return result_sp->GetError().ToError();
11001092
}
1101-
if (false == result_sp->GetCompilerType().IsValid()) {
1093+
if (!result_sp->GetCompilerType().IsValid()) {
11021094
if (log)
11031095
log->Printf("[RunObjectDescriptionExpr] expression generated "
11041096
"invalid type");
11051097

1106-
result.Printf("expression produced invalid result type");
1107-
return false;
1098+
return llvm::createStringError("expression produced invalid result type");
11081099
}
11091100

11101101
formatters::StringPrinter::ReadStringAndDumpToStreamOptions dump_options;
@@ -1120,15 +1111,13 @@ SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
11201111
if (log)
11211112
log->Printf("[RunObjectDescriptionExpr] expression completed "
11221113
"successfully");
1123-
return true;
1124-
} else {
1125-
if (log)
1126-
log->Printf("[RunObjectDescriptionExpr] expression generated "
1127-
"invalid string data");
1128-
1129-
result.Printf("expression produced unprintable string");
1130-
return false;
1114+
return llvm::Error::success();
11311115
}
1116+
if (log)
1117+
log->Printf("[RunObjectDescriptionExpr] expression generated "
1118+
"invalid string data");
1119+
1120+
return llvm::createStringError("expression produced unprintable string");
11321121
}
11331122

11341123
static bool IsVariable(ValueObject &object) {
@@ -1160,12 +1149,11 @@ static bool IsSwiftReferenceType(ValueObject &object) {
11601149
return false;
11611150
}
11621151

1163-
bool SwiftLanguageRuntimeImpl::GetObjectDescription(Stream &str,
1164-
ValueObject &object) {
1165-
if (object.IsUninitializedReference()) {
1166-
str.Printf("<uninitialized>");
1167-
return true;
1168-
}
1152+
llvm::Error
1153+
SwiftLanguageRuntimeImpl::GetObjectDescription(Stream &str,
1154+
ValueObject &object) {
1155+
if (object.IsUninitializedReference())
1156+
return llvm::createStringError("<uninitialized>");
11691157

11701158
std::string expr_string;
11711159

@@ -1185,26 +1173,27 @@ bool SwiftLanguageRuntimeImpl::GetObjectDescription(Stream &str,
11851173
}
11861174
if (!expr_string.empty()) {
11871175
StreamString probe_stream;
1188-
if (RunObjectDescriptionExpr(object, expr_string, probe_stream)) {
1189-
str.Printf("%s", probe_stream.GetData());
1190-
return true;
1191-
}
1176+
auto error = RunObjectDescriptionExpr(object, expr_string, probe_stream);
1177+
if (error)
1178+
return error;
1179+
str.Printf("%s", probe_stream.GetData());
1180+
return llvm::Error::success();
11921181
}
11931182
// In general, don't try to use the name of the ValueObject as it might end up
11941183
// referring to the wrong thing. Instead, copy the object data into the
11951184
// target and call object description on the copy.
11961185
lldb::addr_t copy_location = LLDB_INVALID_ADDRESS;
11971186
expr_string = GetObjectDescriptionExpr_Copy(object, copy_location);
11981187
if (copy_location == LLDB_INVALID_ADDRESS) {
1199-
str.Printf("Failed to allocate memory for copy object.");
1200-
return false;
1188+
return llvm::createStringError(
1189+
"Failed to allocate memory for copy object.");
12011190
}
12021191

1203-
auto cleanup =
1204-
llvm::make_scope_exit([&]() { m_process.DeallocateMemory(copy_location); });
1192+
auto cleanup = llvm::make_scope_exit(
1193+
[&]() { m_process.DeallocateMemory(copy_location); });
12051194

12061195
if (expr_string.empty())
1207-
return false;
1196+
return llvm::createStringError("no object description");
12081197
return RunObjectDescriptionExpr(object, expr_string, str);
12091198
}
12101199

@@ -2472,8 +2461,8 @@ SwiftLanguageRuntime::GetNumFields(CompilerType type,
24722461
FORWARD(GetNumFields, type, exe_ctx);
24732462
}
24742463

2475-
bool SwiftLanguageRuntime::GetObjectDescription(Stream &str,
2476-
ValueObject &object) {
2464+
llvm::Error SwiftLanguageRuntime::GetObjectDescription(Stream &str,
2465+
ValueObject &object) {
24772466
FORWARD(GetObjectDescription, str, object);
24782467
}
24792468

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ class SwiftLanguageRuntime : public LanguageRuntime {
109109
/// PluginInterface protocol.
110110
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
111111

112-
bool GetObjectDescription(Stream &str, Value &value,
113-
ExecutionContextScope *exe_scope) override {
112+
llvm::Error
113+
GetObjectDescription(Stream &str, Value &value,
114+
ExecutionContextScope *exe_scope) override {
114115
// This is only interesting to do with a ValueObject for Swift.
115-
return false;
116-
}
116+
return llvm::createStringError(
117+
"Swift values do not have an object description");
118+
}
117119

118120
lldb::LanguageType GetLanguageType() const override {
119121
return lldb::eLanguageTypeSwift;
@@ -237,7 +239,7 @@ class SwiftLanguageRuntime : public LanguageRuntime {
237239
bool catch_bp,
238240
bool throw_bp) override;
239241
bool CouldHaveDynamicValue(ValueObject &in_value) override;
240-
bool GetObjectDescription(Stream &str, ValueObject &object) override;
242+
llvm::Error GetObjectDescription(Stream &str, ValueObject &object) override;
241243
CompilerType GetConcreteType(ExecutionContextScope *exe_scope,
242244
ConstString abstract_type_name) override;
243245

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeImpl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class SwiftLanguageRuntimeImpl {
6868
void SymbolsDidLoad(const ModuleList &module_list) { ++m_generation; }
6969
void ModulesDidLoad(const ModuleList &module_list);
7070

71-
bool GetObjectDescription(Stream &str, ValueObject &object);
71+
llvm::Error GetObjectDescription(Stream &str, ValueObject &object);
7272

7373
SwiftExceptionPrecondition *GetExceptionPrecondition();
7474

@@ -329,8 +329,9 @@ class SwiftLanguageRuntimeImpl {
329329
std::string GetObjectDescriptionExpr_Ref(ValueObject &object);
330330
std::string GetObjectDescriptionExpr_Copy(ValueObject &object,
331331
lldb::addr_t &copy_location);
332-
bool RunObjectDescriptionExpr(ValueObject &object, std::string &expr_string,
333-
Stream &result);
332+
llvm::Error RunObjectDescriptionExpr(ValueObject &object,
333+
std::string &expr_string,
334+
Stream &result);
334335
/// We have to load swift dependent libraries by hand, but if they
335336
/// are missing, we shouldn't keep trying.
336337
llvm::StringSet<> m_library_negative_cache;

lldb/test/API/lang/swift/expression/error_reporting/TestSwiftExpressionErrorReporting.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ def check(value):
5252
self.assertFalse(line.startswith('warning:'))
5353

5454
check(value)
55+
56+
self.expect('dwim-print -O -- strct', error=True,
57+
substrs=['Missing type'])
58+
59+
process.Continue()
60+
self.expect('dwim-print -O -- number', error=True,
61+
substrs=['self', 'not', 'found'])

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class POClass {
1515

1616
func main() {
1717
var object: POClass
18-
object = POClass() //% self.assertTrue(self.frame().FindVariable('object').GetObjectDescription() == '<uninitialized>', 'po correctly detects uninitialized instances')
19-
print("yay I am done") //% self.assertFalse(self.frame().FindVariable('object').GetObjectDescription() == '<uninitialized>', 'po incorrectly detects uninitialized instances')
18+
object = POClass() //% self.assertTrue(self.frame().FindVariable('object').GetObjectDescription() == 'error: <uninitialized>', 'po correctly detects uninitialized instances')
19+
print("yay I am done") //% self.assertTrue('POClass:' in self.frame().FindVariable('object').GetObjectDescription())
2020
}
2121

2222
print("Some code here")

0 commit comments

Comments
 (0)