[lldb-vscode] Follow up of D99989 - store some strings more safely

As a follow up of https://reviews.llvm.org/D99989#inline-953343, I'm now
storing std::string instead of char *. I know it might never break as char *,
but if it does, chasing that bug might be dauting.
Besides, I'm also checking of the strings gotten through the SB API are
null or not.
This commit is contained in:
Walter Erquinigo 2021-04-27 16:02:38 -07:00
parent 1cf3d68f97
commit 1141ba677e
3 changed files with 18 additions and 12 deletions

View File

@ -914,21 +914,24 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
return llvm::json::Value(std::move(event));
}
const char *GetNonNullVariableName(lldb::SBValue v) {
const char *name = v.GetName();
return name ? name : "<null>";
}
std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
bool is_name_duplicated) {
lldb::SBStream name_builder;
const char *name = v.GetName();
name_builder.Print(name ? name : "<null>");
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();
}

View File

@ -399,6 +399,10 @@ llvm::json::Value CreateThread(lldb::SBThread &thread);
/// 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.
///

View File

@ -2931,24 +2931,23 @@ void request_variables(const llvm::json::Object &request) {
const int64_t end_idx = start_idx + ((count == 0) ? num_children : count);
// We first find out which variable names are duplicated
llvm::DenseMap<const char *, int> variable_name_counts;
std::map<std::string, int> 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