[Lldb-commits] [lldb] r368806 - [API] Have SBCommandReturnObject::GetOutput/Error return "" instead of nullptr

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 14 01:11:20 PDT 2019


Author: labath
Date: Wed Aug 14 01:11:20 2019
New Revision: 368806

URL: http://llvm.org/viewvc/llvm-project?rev=368806&view=rev
Log:
[API] Have SBCommandReturnObject::GetOutput/Error return "" instead of nullptr

Summary:
It seems this was an unintended side-effect of D26698. AFAICT, these
functions did return an empty string before that patch, and the patch
contained code which attempted to ensure that, but those efforts were
negated by ConstString::AsCString, which by default returns a nullptr
even for empty strings.

This patch:
- fixes the GetOutput/Error methods to really return empty strings
- adds and explicit test for that
- removes a workaround in lldbtest.py, which was masking this problem
  from our other tests

Reviewers: jingham, clayborg

Subscribers: zturner, lldb-commits

Differential Revision: https://reviews.llvm.org/D65739

Modified:
    lldb/trunk/packages/Python/lldbsuite/test/lldbtest.py
    lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestCommandInterpreterAPI.py
    lldb/trunk/source/API/SBCommandReturnObject.cpp

Modified: lldb/trunk/packages/Python/lldbsuite/test/lldbtest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lldbtest.py?rev=368806&r1=368805&r2=368806&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lldbtest.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lldbtest.py Wed Aug 14 01:11:20 2019
@@ -2318,8 +2318,6 @@ FileCheck output:
             with recording(self, trace) as sbuf:
                 print("looking at:", output, file=sbuf)
 
-        if output is None:
-            output = ""
         # The heading says either "Expecting" or "Not expecting".
         heading = "Expecting" if matching else "Not expecting"
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestCommandInterpreterAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestCommandInterpreterAPI.py?rev=368806&r1=368805&r2=368806&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestCommandInterpreterAPI.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestCommandInterpreterAPI.py Wed Aug 14 01:11:20 2019
@@ -12,6 +12,7 @@ from lldbsuite.test import lldbutil
 class CommandInterpreterAPICase(TestBase):
 
     mydir = TestBase.compute_mydir(__file__)
+    NO_DEBUG_INFO_TESTCASE = True
 
     def setUp(self):
         # Call super's setUp().
@@ -72,3 +73,19 @@ class CommandInterpreterAPICase(TestBase
 
         if self.TraceOn():
             lldbutil.print_stacktraces(process)
+
+    @add_test_categories(['pyapi'])
+    def test_command_output(self):
+        """Test command output handling."""
+        ci = self.dbg.GetCommandInterpreter()
+        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
+
+        # Test that a command which produces no output returns "" instead of
+        # None.
+        res = lldb.SBCommandReturnObject()
+        ci.HandleCommand("settings set use-color false", res)
+        self.assertTrue(res.Succeeded())
+        self.assertIsNotNone(res.GetOutput())
+        self.assertEquals(res.GetOutput(), "")
+        self.assertIsNotNone(res.GetError())
+        self.assertEquals(res.GetError(), "")

Modified: lldb/trunk/source/API/SBCommandReturnObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBCommandReturnObject.cpp?rev=368806&r1=368805&r2=368806&view=diff
==============================================================================
--- lldb/trunk/source/API/SBCommandReturnObject.cpp (original)
+++ lldb/trunk/source/API/SBCommandReturnObject.cpp Wed Aug 14 01:11:20 2019
@@ -72,10 +72,8 @@ const char *SBCommandReturnObject::GetOu
   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommandReturnObject, GetOutput);
 
   if (m_opaque_up) {
-    llvm::StringRef output = m_opaque_up->GetOutputData();
-    ConstString result(output.empty() ? llvm::StringRef("") : output);
-
-    return result.AsCString();
+    ConstString output(m_opaque_up->GetOutputData());
+    return output.AsCString(/*value_if_empty*/ "");
   }
 
   return nullptr;
@@ -85,9 +83,8 @@ const char *SBCommandReturnObject::GetEr
   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommandReturnObject, GetError);
 
   if (m_opaque_up) {
-    llvm::StringRef output = m_opaque_up->GetErrorData();
-    ConstString result(output.empty() ? llvm::StringRef("") : output);
-    return result.AsCString();
+    ConstString output(m_opaque_up->GetErrorData());
+    return output.AsCString(/*value_if_empty*/ "");
   }
 
   return nullptr;




More information about the lldb-commits mailing list