[Lldb-commits] [PATCH] D101131: [lldb-vscode] Follow up of D99989 - store some strings more safely

walter erquinigo via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 27 16:06:45 PDT 2021


wallace updated this revision to Diff 341017.
wallace added a comment.

now checking if the strings gotten from the API are null or not


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101131/new/

https://reviews.llvm.org/D101131

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


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<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
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 : "<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();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101131.341017.patch
Type: text/x-patch
Size: 3442 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210427/14ab1114/attachment-0001.bin>


More information about the lldb-commits mailing list