Skip to content

Commit cf5d8de

Browse files
[lldb-vscode] Show a fake child with the raw value of synthetic types (#65552)
Currently, if the user wants to inspect the raw version of a synthetic variable, they have to go to the debug console and type `frame var <variable>`, which is not a great experience. Taking inspiration from CodeLLDB, this adds a `[raw]` child to every synthetic variable so that this kind of inspection can be done visually. Some examples: <img width="500" alt="Screenshot 2023-09-06 at 7 56 25 PM" src="https://github.com/llvm/llvm-project/assets/1613874/7fefb7c5-0da7-49c7-968b-78ac88348fea"> <img width="479" alt="Screenshot 2023-09-06 at 6 58 25 PM" src="https://github.com/llvm/llvm-project/assets/1613874/6e650567-16e1-462f-9bf5-4a3a605cf6fc">
1 parent 9d10636 commit cf5d8de

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

lldb/test/API/tools/lldb-vscode/variables/TestVSCode_variables.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,20 @@ def test_indexedVariables(self):
517517
}
518518
self.verify_variables(verify_locals, locals)
519519

520+
# We also verify that we produce a "[raw]" fake child with the real
521+
# SBValue for the synthetic type.
522+
verify_children = {
523+
"[0]": {"equals": {"type": "int", "value": "0"}},
524+
"[1]": {"equals": {"type": "int", "value": "0"}},
525+
"[2]": {"equals": {"type": "int", "value": "0"}},
526+
"[3]": {"equals": {"type": "int", "value": "0"}},
527+
"[4]": {"equals": {"type": "int", "value": "0"}},
528+
"[raw]": {"contains": {"type": ["vector"]}},
529+
}
530+
children = self.vscode.request_variables(locals[2]["variablesReference"])["body"]["variables"]
531+
self.verify_variables(verify_children, children)
532+
533+
520534
@skipIfWindows
521535
@skipIfRemote
522536
def test_registers(self):

lldb/tools/lldb-vscode/JSONUtils.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,10 +1103,13 @@ std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
11031103
// }
11041104
llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
11051105
int64_t varID, bool format_hex,
1106-
bool is_name_duplicated) {
1106+
bool is_name_duplicated,
1107+
std::optional<std::string> custom_name) {
11071108
llvm::json::Object object;
1108-
EmplaceSafeString(object, "name",
1109-
CreateUniqueVariableNameForDisplay(v, is_name_duplicated));
1109+
EmplaceSafeString(
1110+
object, "name",
1111+
custom_name ? *custom_name
1112+
: CreateUniqueVariableNameForDisplay(v, is_name_duplicated));
11101113

11111114
if (format_hex)
11121115
v.SetFormat(lldb::eFormatHex);
@@ -1131,15 +1134,19 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
11311134
const bool is_synthetic = v.IsSynthetic();
11321135
if (is_array || is_synthetic) {
11331136
const auto num_children = v.GetNumChildren();
1137+
// We create a "[raw]" fake child for each synthetic type, so we have to
1138+
// account for it when returning indexed variables. We don't need to do this
1139+
// for non-indexed ones.
1140+
int actual_num_children = num_children + (is_synthetic ? 1 : 0);
11341141
if (is_array) {
1135-
object.try_emplace("indexedVariables", num_children);
1142+
object.try_emplace("indexedVariables", actual_num_children);
11361143
} else if (num_children > 0) {
11371144
// If a type has a synthetic child provider, then the SBType of "v" won't
11381145
// tell us anything about what might be displayed. So we can check if the
11391146
// first child's name is "[0]" and then we can say it is indexed.
11401147
const char *first_child_name = v.GetChildAtIndex(0).GetName();
11411148
if (first_child_name && strcmp(first_child_name, "[0]") == 0)
1142-
object.try_emplace("indexedVariables", num_children);
1149+
object.try_emplace("indexedVariables", actual_num_children);
11431150
}
11441151
}
11451152
EmplaceSafeString(object, "type", type_cstr ? type_cstr : NO_TYPENAME);

lldb/tools/lldb-vscode/JSONUtils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,17 @@ std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
440440
/// As VSCode doesn't render two of more variables with the same name, we
441441
/// apply a suffix to distinguish duplicated variables.
442442
///
443+
/// \param[in] custom_name
444+
/// A provided custom name that is used instead of the SBValue's when
445+
/// creating the JSON representation.
446+
///
443447
/// \return
444448
/// A "Variable" JSON object with that follows the formal JSON
445449
/// definition outlined by Microsoft.
446450
llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
447451
int64_t varID, bool format_hex,
448-
bool is_name_duplicated = false);
452+
bool is_name_duplicated = false,
453+
std::optional<std::string> custom_name = {});
449454

450455
llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit unit);
451456

lldb/tools/lldb-vscode/lldb-vscode.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,7 +3251,7 @@ void request_variables(const llvm::json::Object &request) {
32513251
break;
32523252

32533253
int64_t var_ref = 0;
3254-
if (variable.MightHaveChildren()) {
3254+
if (variable.MightHaveChildren() || variable.IsSynthetic()) {
32553255
var_ref = g_vsc.variables.InsertExpandableVariable(
32563256
variable, /*is_permanent=*/false);
32573257
}
@@ -3264,23 +3264,36 @@ void request_variables(const llvm::json::Object &request) {
32643264
// children.
32653265
lldb::SBValue variable = g_vsc.variables.GetVariable(variablesReference);
32663266
if (variable.IsValid()) {
3267-
const auto num_children = variable.GetNumChildren();
3268-
const int64_t end_idx = start + ((count == 0) ? num_children : count);
3269-
for (auto i = start; i < end_idx; ++i) {
3270-
lldb::SBValue child = variable.GetChildAtIndex(i);
3267+
auto addChild = [&](lldb::SBValue child,
3268+
std::optional<std::string> custom_name = {}) {
32713269
if (!child.IsValid())
3272-
break;
3270+
return;
32733271
if (child.MightHaveChildren()) {
32743272
auto is_permanent =
32753273
g_vsc.variables.IsPermanentVariableReference(variablesReference);
32763274
auto childVariablesReferences =
32773275
g_vsc.variables.InsertExpandableVariable(child, is_permanent);
3278-
variables.emplace_back(CreateVariable(child, childVariablesReferences,
3279-
childVariablesReferences, hex));
3276+
variables.emplace_back(CreateVariable(
3277+
child, childVariablesReferences, childVariablesReferences, hex,
3278+
/*is_name_duplicated=*/false, custom_name));
32803279
} else {
3281-
variables.emplace_back(CreateVariable(child, 0, INT64_MAX, hex));
3280+
variables.emplace_back(CreateVariable(child, 0, INT64_MAX, hex,
3281+
/*is_name_duplicated=*/false,
3282+
custom_name));
32823283
}
3283-
}
3284+
};
3285+
const int64_t num_children = variable.GetNumChildren();
3286+
int64_t end_idx = start + ((count == 0) ? num_children : count);
3287+
int64_t i = start;
3288+
for (; i < end_idx && i < num_children; ++i)
3289+
addChild(variable.GetChildAtIndex(i));
3290+
3291+
// If we haven't filled the count quota from the request, we insert a new
3292+
// "[raw]" child that can be used to inspect the raw version of a
3293+
// synthetic member. That eliminates the need for the user to go to the
3294+
// debug console and type `frame var <variable> to get these values.
3295+
if (variable.IsSynthetic() && i == num_children)
3296+
addChild(variable.GetNonSyntheticValue(), "[raw]");
32843297
}
32853298
}
32863299
llvm::json::Object body;

0 commit comments

Comments
 (0)