Skip to content

Commit 680c20d

Browse files
[lldb] Document the behaviour of IsValid for SBError (#170862)
This reverts commit d20d84f. Fixes #169788, but in a different way. In which I changed an SBError use so that when the function succeeded, IsValid on the SBError would be true. This seemed to make sense but SBError acts differently to other SB classes in this respect. For something like SBMemoryRegionInfo, if IsValid() is false, you can't do anything with it. However for SBError, IsValid() true only means there's some underlying error object in there. If the SBError represents a success, there's no need to put anything in there. You can see this intent from a lot of its methods, many have handling for IsValid() false. This is not a bug but a misunderstanding of what IsValid means. Yes it does function the way I expected for most classes, but it does not for SBError and though that's not intuitive, it is consistent with how we describe IsValid in the documentation. So instead of changing that method's use of SBError I'm documenting this initially counterintuitive behaviour in the SBError header and on the website (https://lldb.llvm.org/resources/sbapi.html).
1 parent 951795a commit 680c20d

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

lldb/docs/resources/sbapi.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ prepared to handle their opaque implementation pointer being empty, and doing
4646
something reasonable. We also always have an "IsValid" method on all the SB
4747
classes to report whether the object is empty or not.
4848

49+
.. note::
50+
The implication of an object being "empty" can vary by class.
51+
52+
For most classes, the lack of anything backing the class means that it
53+
would not be valid to interact with it by calling any other methods
54+
on it.
55+
56+
One exception to this is ``SBError``, which can provide valid
57+
information even when empty. This is because it does not need an
58+
underlying object to be able to represent a success state.
59+
4960
Another piece of the SB API infrastructure is the Python (or other script
5061
interpreter) customization. SWIG allows you to add property access, iterators
5162
and documentation to classes. We place the property accessors and iterators in

lldb/include/lldb/API/SBError.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ class LLDB_API SBError {
7272

7373
explicit operator bool() const;
7474

75+
/// \brief Returns \c true if this object contains an underlying \c Status
76+
/// object.
77+
///
78+
/// That object may represent a success or a failure. When \c IsValid returns
79+
/// \c false, it may be the case that the \c SBError represents a success but
80+
/// does not contain a \c Status representing that success.
81+
///
82+
/// It is safe to call \c Success or \c Fail in the case where \c IsValid
83+
/// returns \c false.
7584
bool IsValid() const;
7685

7786
bool GetDescription(lldb::SBStream &description);

lldb/source/API/SBDebugger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void SBDebugger::Initialize() {
179179
lldb::SBError SBDebugger::InitializeWithErrorHandling() {
180180
LLDB_INSTRUMENT();
181181

182-
SBError error((Status()));
182+
SBError error;
183183
if (auto e = g_debugger_lifetime->Initialize(
184184
std::make_unique<SystemInitializerFull>())) {
185185
error.SetError(Status::FromError(std::move(e)));

lldb/unittests/DAP/TestBase.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ void DAPTestBase::TearDown() {
7373

7474
void DAPTestBase::SetUpTestSuite() {
7575
lldb::SBError error = SBDebugger::InitializeWithErrorHandling();
76-
EXPECT_TRUE(error.IsValid());
7776
EXPECT_TRUE(error.Success());
7877
}
7978
void DAPTestBase::TeatUpTestSuite() { SBDebugger::Terminate(); }

0 commit comments

Comments
 (0)