[lldb] Assert that CommandResultObject error messages are not empty
authorDavid Spickett <david.spickett@linaro.org>
Fri, 18 Jun 2021 12:17:53 +0000 (12:17 +0000)
committerDavid Spickett <david.spickett@linaro.org>
Mon, 21 Jun 2021 09:44:47 +0000 (09:44 +0000)
The intention is now that AppendError/SetError/AppendRawError only
be called with some message to show. This enforces that.

For SetError with a Status and a fallback string first assert
that the Status is a failure Status. Then it calls SetError(StringRef)
which checks the message is valid. (which could be the fallback
or the Status')

Reviewed By: dblaikie

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

lldb/source/Interpreter/CommandReturnObject.cpp

index 5edd9a3..d0d0ced 100644 (file)
@@ -99,24 +99,18 @@ void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
 
 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
   SetStatus(eReturnStatusFailed);
-  if (in_string.empty())
-    return;
+  assert(!in_string.empty() && "Expected a non-empty error message");
   error(GetErrorStream()) << in_string.rtrim() << '\n';
 }
 
 void CommandReturnObject::SetError(const Status &error,
                                    const char *fallback_error_cstr) {
-  const char *error_cstr = error.AsCString();
-  if (error_cstr == nullptr)
-    error_cstr = fallback_error_cstr;
-  SetError(error_cstr);
+  assert(error.Fail() && "Expected a failed Status");
+  SetError(error.AsCString(fallback_error_cstr));
 }
 
 void CommandReturnObject::SetError(llvm::StringRef error_str) {
   SetStatus(eReturnStatusFailed);
-  if (error_str.empty())
-    return;
-
   AppendError(error_str);
 }
 
@@ -125,8 +119,7 @@ void CommandReturnObject::SetError(llvm::StringRef error_str) {
 
 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
   SetStatus(eReturnStatusFailed);
-  if (in_string.empty())
-    return;
+  assert(!in_string.empty() && "Expected a non-empty error message");
   GetErrorStream() << in_string;
 }