Skip to content

Commit 6f59b11

Browse files
committed
[lldb] Store the return ValueObject in the CommandReturnObject
There are a lot of lldb commands whose result is really a ValueObject that we then print with the ValueObjectPrinter. Now that we have the ability to access the SBCommandReturnObject through a callback (#125006), we can store the resultant ValueObject in the return object, allowing an IDE to access the SBValue and do its own rich formatting. rdar://143965453
1 parent f71b83b commit 6f59b11

File tree

8 files changed

+41
-0
lines changed

8 files changed

+41
-0
lines changed

lldb/include/lldb/API/SBCommandReturnObject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class LLDB_API SBCommandReturnObject {
136136

137137
void SetError(const char *error_cstr);
138138

139+
lldb::SBValue GetReturnValue(lldb::DynamicValueType use_dynamic);
140+
139141
protected:
140142
friend class SBCommandInterpreter;
141143
friend class SBOptions;

lldb/include/lldb/API/SBValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ class LLDB_API SBValue {
442442

443443
protected:
444444
friend class SBBlock;
445+
friend class SBCommandReturnObject;
445446
friend class SBFrame;
446447
friend class SBModule;
447448
friend class SBTarget;

lldb/include/lldb/Interpreter/CommandReturnObject.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ class CommandReturnObject {
165165
return m_diagnostic_indent;
166166
}
167167

168+
lldb::ValueObjectSP GetValueObjectSP() const { return m_value_object_sp; }
169+
170+
void SetValueObjectSP(lldb::ValueObjectSP value_object_sp) {
171+
m_value_object_sp = value_object_sp;
172+
}
173+
168174
lldb::ReturnStatus GetStatus() const;
169175

170176
void SetStatus(lldb::ReturnStatus status);
@@ -197,6 +203,9 @@ class CommandReturnObject {
197203

198204
lldb::ReturnStatus m_status = lldb::eReturnStatusStarted;
199205

206+
/// An optional return ValueObjectSP.
207+
lldb::ValueObjectSP m_value_object_sp;
208+
200209
bool m_did_change_process_state = false;
201210
bool m_suppress_immediate_output = false;
202211

lldb/source/API/SBCommandReturnObject.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/API/SBFile.h"
1313
#include "lldb/API/SBStream.h"
1414
#include "lldb/API/SBStructuredData.h"
15+
#include "lldb/API/SBValue.h"
1516
#include "lldb/Core/StructuredDataImpl.h"
1617
#include "lldb/Interpreter/CommandReturnObject.h"
1718
#include "lldb/Utility/ConstString.h"
@@ -356,3 +357,12 @@ void SBCommandReturnObject::SetError(const char *error_cstr) {
356357
if (error_cstr)
357358
ref().AppendError(error_cstr);
358359
}
360+
361+
SBValue
362+
SBCommandReturnObject::GetReturnValue(lldb::DynamicValueType use_dynamic) {
363+
LLDB_INSTRUMENT_VA(this, use_dynamic);
364+
365+
SBValue sb_value;
366+
sb_value.SetSP(ref().GetValueObjectSP(), use_dynamic);
367+
return sb_value;
368+
}

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
205205
ExpressionResults expr_result = target.EvaluateExpression(
206206
expr, exe_scope, valobj_sp, eval_options, &fixed_expression);
207207

208+
if (valobj_sp)
209+
result.SetValueObjectSP(valobj_sp);
210+
208211
// Record the position of the expression in the command.
209212
std::optional<uint16_t> indent;
210213
if (fixed_expression.empty()) {

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
434434
}
435435

436436
if (result_valobj_sp) {
437+
result.SetValueObjectSP(result_valobj_sp);
438+
437439
Format format = m_format_options.GetFormat();
438440

439441
if (result_valobj_sp->GetError().Success()) {

lldb/source/Commands/CommandObjectFrame.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class CommandObjectFrameDiagnose : public CommandObjectParsed {
152152
return;
153153
}
154154

155+
result.SetValueObjectSP(valobj_sp);
155156
DumpValueObjectOptions::DeclPrintingHelper helper =
156157
[&valobj_sp](ConstString type, ConstString var,
157158
const DumpValueObjectOptions &opts,

lldb/test/API/api/command-return-object/TestSBCommandReturnObject.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ def test_get_command(self):
3333
ci.HandleCommand("help help", res)
3434
self.assertTrue(res.Succeeded())
3535
self.assertEqual(res.GetCommand(), "help help")
36+
37+
value = res.GetReturnValue(lldb.eNoDynamicValues)
38+
self.assertFalse(value)
39+
40+
def test_get_value(self):
41+
res = lldb.SBCommandReturnObject()
42+
ci = self.dbg.GetCommandInterpreter()
43+
ci.HandleCommand("p 1 + 1", res)
44+
self.assertTrue(res.Succeeded())
45+
46+
value = res.GetReturnValue(lldb.eNoDynamicValues)
47+
self.assertTrue(value)
48+
self.assertEqual(value.GetValue(), "2")

0 commit comments

Comments
 (0)