Index: lldb/tools/lldb-vscode/JSONUtils.h =================================================================== --- lldb/tools/lldb-vscode/JSONUtils.h +++ lldb/tools/lldb-vscode/JSONUtils.h @@ -399,6 +399,10 @@ /// definition outlined by Microsoft. llvm::json::Value CreateThreadStopped(lldb::SBThread &thread, uint32_t stop_id); +/// \return +/// The variable name of \a value or a default placeholder. +const char *GetNonNullVariableName(lldb::SBValue value); + /// VSCode can't display two variables with the same name, so we need to /// distinguish them by using a suffix. /// Index: lldb/tools/lldb-vscode/JSONUtils.cpp =================================================================== --- lldb/tools/lldb-vscode/JSONUtils.cpp +++ lldb/tools/lldb-vscode/JSONUtils.cpp @@ -914,21 +914,24 @@ return llvm::json::Value(std::move(event)); } +const char *GetNonNullVariableName(lldb::SBValue v) { + const char *name = v.GetName(); + return name ? name : ""; +} + std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v, bool is_name_duplicated) { lldb::SBStream name_builder; - const char *name = v.GetName(); - name_builder.Print(name ? name : ""); + name_builder.Print(GetNonNullVariableName(v)); if (is_name_duplicated) { - name_builder.Print(" @ "); lldb::SBDeclaration declaration = v.GetDeclaration(); - std::string file_name(declaration.GetFileSpec().GetFilename()); + const char *file_name = declaration.GetFileSpec().GetFilename(); const uint32_t line = declaration.GetLine(); - if (!file_name.empty() && line > 0) - name_builder.Printf("%s:%u", file_name.c_str(), line); - else - name_builder.Print(v.GetLocation()); + if (file_name != nullptr && line > 0) + name_builder.Printf(" @ %s:%u", file_name, line); + else if (const char *location = v.GetLocation()) + name_builder.Printf(" @ %s", location); } return name_builder.GetData(); } Index: lldb/tools/lldb-vscode/lldb-vscode.cpp =================================================================== --- lldb/tools/lldb-vscode/lldb-vscode.cpp +++ lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -2931,24 +2931,23 @@ const int64_t end_idx = start_idx + ((count == 0) ? num_children : count); // We first find out which variable names are duplicated - llvm::DenseMap variable_name_counts; + std::map variable_name_counts; for (auto i = start_idx; i < end_idx; ++i) { lldb::SBValue variable = g_vsc.variables.GetValueAtIndex(i); if (!variable.IsValid()) break; - variable_name_counts[variable.GetName()]++; + variable_name_counts[GetNonNullVariableName(variable)]++; } // Now we construct the result with unique display variable names for (auto i = start_idx; i < end_idx; ++i) { lldb::SBValue variable = g_vsc.variables.GetValueAtIndex(i); - const char *name = variable.GetName(); if (!variable.IsValid()) break; variables.emplace_back(CreateVariable(variable, VARIDX_TO_VARREF(i), i, hex, - variable_name_counts[name] > 1)); + variable_name_counts[GetNonNullVariableName(variable)] > 1)); } } else { // We are expanding a variable that has children, so we will return its