tammela created this revision.
tammela requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This patch changes the GetStringValue method on the SBStructuredData
class to return a `const char *`.

This change allows the use of the method on the Lua interpreter.
The auto-generated Lua binding can't handle `char *` arguments
properly. That's because it forces the method call with a `char *` like
Lua object.

This means that to retrieve the string value an operation would need to write 
to an
internal Lua string, instead of using the Lua C API.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94937

Files:
  lldb/bindings/interface/SBStructuredData.i
  lldb/examples/python/in_call_stack.py
  lldb/include/lldb/API/SBStructuredData.h
  lldb/include/lldb/Core/StructuredDataImpl.h
  lldb/packages/Python/lldbsuite/test/decorators.py
  lldb/source/API/SBStructuredData.cpp
  lldb/test/API/commands/platform/basic/TestPlatformPython.py
  lldb/test/API/commands/target/stop-hooks/stop_hook.py
  lldb/test/API/functionalities/breakpoint/breakpoint_command/bktptcmd.py
  lldb/test/API/functionalities/breakpoint/scripted_bkpt/resolver.py
  
lldb/test/API/functionalities/breakpoint/serialize/TestBreakpointSerialization.py
  lldb/test/API/functionalities/step_scripted/Steps.py
  lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
  lldb/test/Shell/ScriptInterpreter/Lua/breakpoint_function_callback.test

Index: lldb/test/Shell/ScriptInterpreter/Lua/breakpoint_function_callback.test
===================================================================
--- lldb/test/Shell/ScriptInterpreter/Lua/breakpoint_function_callback.test
+++ lldb/test/Shell/ScriptInterpreter/Lua/breakpoint_function_callback.test
@@ -5,6 +5,7 @@
 script
 function abc(a, b, c, ...)
 print(c)
+if c then print(c:GetValueForKey("foo"):GetStringValue()) end
 end
 quit
 breakpoint command add -s lua -F abc
Index: lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
===================================================================
--- lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -100,7 +100,7 @@
             self.fail("Wrong type returned: " + str(string_struct.GetType()))
 
         # Check API returning 'string' value
-        output = string_struct.GetStringValue(25)
+        output = string_struct.GetStringValue()
         if not "STRING" in output:
             self.fail("wrong output: " + output)
 
@@ -131,7 +131,7 @@
 
         # Calling wrong API on a SBStructuredData
         # (e.g. getting a string value from an integer type structure)
-        output = int_struct.GetStringValue(25)
+        output = int_struct.GetStringValue()
         if output:
             self.fail(
                 "Valid string " +
@@ -192,7 +192,7 @@
             self.fail("A valid object should have been returned")
         if not string_struct.GetType() == lldb.eStructuredDataTypeString:
             self.fail("Wrong type returned: " + str(string_struct.GetType()))
-        output = string_struct.GetStringValue(5)
+        output = string_struct.GetStringValue()
         if not output == "23":
             self.fail("wrong output: " + str(output))
 
@@ -201,6 +201,6 @@
             self.fail("A valid object should have been returned")
         if not string_struct.GetType() == lldb.eStructuredDataTypeString:
             self.fail("Wrong type returned: " + str(string_struct.GetType()))
-        output = string_struct.GetStringValue(5)
+        output = string_struct.GetStringValue()
         if not output == "arr":
             self.fail("wrong output: " + str(output))
Index: lldb/test/API/functionalities/step_scripted/Steps.py
===================================================================
--- lldb/test/API/functionalities/step_scripted/Steps.py
+++ lldb/test/API/functionalities/step_scripted/Steps.py
@@ -45,7 +45,7 @@
         
         if not func_entry.IsValid():
             print("Did not get a valid entry for variable_name")
-        func_name = func_entry.GetStringValue(100)
+        func_name = func_entry.GetStringValue()
 
         self.value = self.frame.FindVariable(func_name)
         if self.value.GetError().Fail():
@@ -88,7 +88,7 @@
     
     def __init__(self, thread_plan, args_data, dict):
         self.thread_plan = thread_plan
-        self.key = args_data.GetValueForKey("token").GetStringValue(1000)
+        self.key = args_data.GetValueForKey("token").GetStringValue()
         
     def should_stop(self, event):
         self.thread_plan.SetPlanComplete(True)
Index: lldb/test/API/functionalities/breakpoint/serialize/TestBreakpointSerialization.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/serialize/TestBreakpointSerialization.py
+++ lldb/test/API/functionalities/breakpoint/serialize/TestBreakpointSerialization.py
@@ -407,7 +407,7 @@
 
         for idx in range(0, orig_keys.GetSize()):
             key = orig_keys.GetStringAtIndex(idx)
-            copy_value = copy_extra_args.GetValueForKey(key).GetStringValue(100)
+            copy_value = copy_extra_args.GetValueForKey(key).GetStringValue()
 
             if key == "first_arg":
                 self.assertEqual(copy_value, "first_value")
Index: lldb/test/API/functionalities/breakpoint/scripted_bkpt/resolver.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/scripted_bkpt/resolver.py
+++ lldb/test/API/functionalities/breakpoint/scripted_bkpt/resolver.py
@@ -15,7 +15,7 @@
       sym_name = "not_a_real_function_name"
       sym_item = self.extra_args.GetValueForKey("symbol")
       if sym_item.IsValid():
-          sym_name = sym_item.GetStringValue(1000)
+          sym_name = sym_item.GetStringValue()
       else:
           print("Didn't have a value for key 'symbol'")
 
Index: lldb/test/API/functionalities/breakpoint/breakpoint_command/bktptcmd.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_command/bktptcmd.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_command/bktptcmd.py
@@ -9,12 +9,12 @@
 
 def another_function(frame, bp_loc, extra_args, dict):
     se_value = extra_args.GetValueForKey("side_effect")
-    se_string = se_value.GetStringValue(100)
+    se_string = se_value.GetStringValue()
     side_effect.fancy = se_string
 
 def a_third_function(frame, bp_loc, extra_args, dict):
     se_value = extra_args.GetValueForKey("side_effect")
-    se_string = se_value.GetStringValue(100)
+    se_string = se_value.GetStringValue()
     side_effect.fancier = se_string
 
 def empty_extra_args(frame, bp_loc, extra_args, dict):
Index: lldb/test/API/commands/target/stop-hooks/stop_hook.py
===================================================================
--- lldb/test/API/commands/target/stop-hooks/stop_hook.py
+++ lldb/test/API/commands/target/stop-hooks/stop_hook.py
@@ -17,7 +17,7 @@
         increment = 1
         value = self.extra_args.GetValueForKey("increment")
         if value:
-            incr_as_str = value.GetStringValue(100)
+            incr_as_str = value.GetStringValue()
             increment = int(incr_as_str)
         else:
             stream.Print("Could not find increment in extra_args\n")
Index: lldb/test/API/commands/platform/basic/TestPlatformPython.py
===================================================================
--- lldb/test/API/commands/platform/basic/TestPlatformPython.py
+++ lldb/test/API/commands/platform/basic/TestPlatformPython.py
@@ -31,7 +31,7 @@
         if platform_idx < 1:
             self.fail('No platforms other than host are available')
         platform_data = self.dbg.GetAvailablePlatformInfoAtIndex(platform_idx)
-        platform_name = platform_data.GetValueForKey('name').GetStringValue(100)
+        platform_name = platform_data.GetValueForKey('name').GetStringValue()
         self.assertNotEqual(platform_name, 'host')
         self.dbg.SetCurrentPlatform(platform_name)
         selected_platform = self.dbg.GetSelectedPlatform()
Index: lldb/source/API/SBStructuredData.cpp
===================================================================
--- lldb/source/API/SBStructuredData.cpp
+++ lldb/source/API/SBStructuredData.cpp
@@ -195,11 +195,11 @@
   return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
 }
 
-size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
-  LLDB_RECORD_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue,
-                                    (char *, size_t), dst, "", dst_len);
+const char *SBStructuredData::GetStringValue(const char *fail_value) const {
+  LLDB_RECORD_METHOD_CONST(const char *, SBStructuredData, GetStringValue,
+                           (const char *), fail_value);
 
-  return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
+  return (m_impl_up ? m_impl_up->GetStringValue(fail_value) : fail_value);
 }
 
 namespace lldb_private {
@@ -236,7 +236,8 @@
                              (uint64_t));
   LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double));
   LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool));
-  LLDB_REGISTER_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue);
+  LLDB_REGISTER_METHOD_CONST(const char *, SBStructuredData, GetStringValue,
+                             (const char *));
 }
 
 } // namespace repro
Index: lldb/packages/Python/lldbsuite/test/decorators.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/decorators.py
+++ lldb/packages/Python/lldbsuite/test/decorators.py
@@ -852,7 +852,7 @@
     targets = config.GetValueForKey("targets").GetValueForKey("value")
     found = False
     for i in range(targets.GetSize()):
-        if targets.GetItemAtIndex(i).GetStringValue(99) == target:
+        if targets.GetItemAtIndex(i).GetStringValue() == target:
             found = True
             break
 
Index: lldb/include/lldb/Core/StructuredDataImpl.h
===================================================================
--- lldb/include/lldb/Core/StructuredDataImpl.h
+++ lldb/include/lldb/Core/StructuredDataImpl.h
@@ -137,19 +137,9 @@
     return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
   }
 
-  size_t GetStringValue(char *dst, size_t dst_len) const {
-    if (!m_data_sp)
-      return 0;
-
-    llvm::StringRef result = m_data_sp->GetStringValue();
-    if (result.empty())
-      return 0;
-
-    if (!dst || !dst_len) {
-      char s[1];
-      return (::snprintf(s, 1, "%s", result.data()));
-    }
-    return (::snprintf(dst, dst_len, "%s", result.data()));
+  const char *GetStringValue(const char *fail_value = nullptr) const {
+    return (m_data_sp ? m_data_sp->GetStringValue(fail_value).data()
+                      : fail_value);
   }
 
 private:
Index: lldb/include/lldb/API/SBStructuredData.h
===================================================================
--- lldb/include/lldb/API/SBStructuredData.h
+++ lldb/include/lldb/API/SBStructuredData.h
@@ -70,22 +70,8 @@
   /// Return the boolean value if this data structure is a boolean type.
   bool GetBooleanValue(bool fail_value = false) const;
 
-  /// Provides the string value if this data structure is a string type.
-  ///
-  /// \param[out] dst
-  ///     pointer where the string value will be written. In case it is null,
-  ///     nothing will be written at \a dst.
-  ///
-  /// \param[in] dst_len
-  ///     max number of characters that can be written at \a dst. In case it is
-  ///     zero, nothing will be written at \a dst. If this length is not enough
-  ///     to write the complete string value, (\a dst_len - 1) bytes of the
-  ///     string value will be written at \a dst followed by a null character.
-  ///
-  /// \return
-  ///     Returns the byte size needed to completely write the string value at
-  ///     \a dst in all cases.
-  size_t GetStringValue(char *dst, size_t dst_len) const;
+  /// Return the string value if this data structure is a string type.
+  const char *GetStringValue(const char *fail_value = nullptr) const;
 
 protected:
   friend class SBTraceOptions;
Index: lldb/examples/python/in_call_stack.py
===================================================================
--- lldb/examples/python/in_call_stack.py
+++ lldb/examples/python/in_call_stack.py
@@ -9,7 +9,7 @@
 
 def in_call_stack(frame, bp_loc, arg_dict, _):
   """Only break if the given name is in the current call stack."""
-  name = arg_dict.GetValueForKey('name').GetStringValue(1000)
+  name = arg_dict.GetValueForKey('name').GetStringValue()
   thread = frame.GetThread()
   found = False
   for frame in thread.frames:
Index: lldb/bindings/interface/SBStructuredData.i
===================================================================
--- lldb/bindings/interface/SBStructuredData.i
+++ lldb/bindings/interface/SBStructuredData.i
@@ -49,7 +49,7 @@
 
         bool GetBooleanValue(bool fail_value = false) const;
 
-        size_t GetStringValue(char *dst, size_t dst_len) const;
+        const char *GetStringValue(const char *fail_value = nullptr) const;
 
         lldb::SBError
         GetAsJSON(lldb::SBStream &stream) const;
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to