[Lldb-commits] [lldb] 1141ba6 - [lldb-vscode] Follow up of D99989 - store some strings more safely

Walter Erquinigo via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 29 18:40:24 PDT 2021


Author: Walter Erquinigo
Date: 2021-04-29T18:37:44-07:00
New Revision: 1141ba677e09156daec8ef31d3dcdae7f59f60c5

URL: https://github.com/llvm/llvm-project/commit/1141ba677e09156daec8ef31d3dcdae7f59f60c5
DIFF: https://github.com/llvm/llvm-project/commit/1141ba677e09156daec8ef31d3dcdae7f59f60c5.diff

LOG: [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.

Added: 
    

Modified: 
    lldb/tools/lldb-vscode/JSONUtils.cpp
    lldb/tools/lldb-vscode/JSONUtils.h
    lldb/tools/lldb-vscode/lldb-vscode.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/tools/lldb-vscode/JSONUtils.cpp b/lldb/tools/lldb-vscode/JSONUtils.cpp
index a64e9778074a0..0e6227f3b15d4 100644
--- a/lldb/tools/lldb-vscode/JSONUtils.cpp
+++ b/lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -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();
 }

diff  --git a/lldb/tools/lldb-vscode/JSONUtils.h b/lldb/tools/lldb-vscode/JSONUtils.h
index fa640e7c5c91c..21d0b3232d5a8 100644
--- a/lldb/tools/lldb-vscode/JSONUtils.h
+++ b/lldb/tools/lldb-vscode/JSONUtils.h
@@ -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.
 ///

diff  --git a/lldb/tools/lldb-vscode/lldb-vscode.cpp b/lldb/tools/lldb-vscode/lldb-vscode.cpp
index 4ceca3806f4f4..823a8eb73bf05 100644
--- a/lldb/tools/lldb-vscode/lldb-vscode.cpp
+++ b/lldb/tools/lldb-vscode/lldb-vscode.cpp
@@ -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


        


More information about the lldb-commits mailing list