[lldb] Prevent double new lines behind errors/warning/messages from LLDB commands
authorRaphael Isemann <teemperor@gmail.com>
Wed, 24 Feb 2021 13:08:49 +0000 (14:08 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Wed, 24 Feb 2021 13:42:01 +0000 (14:42 +0100)
The current API for printing errors/warnings/messages from LLDB commands
sometimes adds newlines behind the messages for the caller. However, this
happens unconditionally so when the caller already specified a trailing newline
in the error message (or is trying to print a generated error message that ends
in a newline), LLDB ends up printing both the automatically added newline and
the one that was in the error message string. This leads to all the randomly
appearing new lines in error such as:

```
(lldb) command a
error: 'command alias' requires at least two arguments
(lldb) apropos a b
error: 'apropos' must be called with exactly one argument.

(lldb) why is there an empty line behind the second error?
```

This code adds a check that only appends the new line if the passed message
doesn't already contain a trailing new line.

Also removes the AppendRawWarning which had only one caller and doesn't serve
any purpose now.

Reviewed By: #lldb, mib

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

lldb/include/lldb/Interpreter/CommandReturnObject.h
lldb/source/Commands/CommandObjectMemory.cpp
lldb/source/Interpreter/CommandReturnObject.cpp
lldb/test/Shell/Commands/command-disassemble.s

index a7c2eea..06b6485 100644 (file)
@@ -95,8 +95,6 @@ public:
   void AppendMessageWithFormat(const char *format, ...)
       __attribute__((format(printf, 2, 3)));
 
-  void AppendRawWarning(llvm::StringRef in_string);
-
   void AppendWarning(llvm::StringRef in_string);
 
   void AppendWarningWithFormat(const char *format, ...)
index 7d5c642..2aac492 100644 (file)
@@ -356,8 +356,8 @@ protected:
       result.AppendErrorWithFormat("%s takes a start address expression with "
                                    "an optional end address expression.\n",
                                    m_cmd_name.c_str());
-      result.AppendRawWarning("Expressions should be quoted if they contain "
-                              "spaces or other special characters.\n");
+      result.AppendWarning("Expressions should be quoted if they contain "
+                           "spaces or other special characters.");
       result.SetStatus(eReturnStatusFailed);
       return false;
     }
index 6f3732e..441a8e0 100644 (file)
@@ -90,28 +90,19 @@ void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
   if (in_string.empty())
     return;
-  GetOutputStream() << in_string << "\n";
+  GetOutputStream() << in_string.rtrim() << '\n';
 }
 
 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
   if (in_string.empty())
     return;
-  warning(GetErrorStream()) << in_string << '\n';
-}
-
-// Similar to AppendWarning, but do not prepend 'warning: ' to message, and
-// don't append "\n" to the end of it.
-
-void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
-  if (in_string.empty())
-    return;
-  GetErrorStream() << in_string;
+  warning(GetErrorStream()) << in_string.rtrim() << '\n';
 }
 
 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
   if (in_string.empty())
     return;
-  error(GetErrorStream()) << in_string << '\n';
+  error(GetErrorStream()) << in_string.rtrim() << '\n';
 }
 
 void CommandReturnObject::SetError(const Status &error,
index e43c4e3..e59edd0 100644 (file)
@@ -6,16 +6,12 @@
 
 # CHECK:      (lldb) disassemble
 # CHECK-NEXT: error: Cannot disassemble around the current function without a selected frame.
-# CHECK-EMPTY:
 # CHECK-NEXT: (lldb) disassemble --line
 # CHECK-NEXT: error: Cannot disassemble around the current line without a selected frame.
-# CHECK-EMPTY:
 # CHECK-NEXT: (lldb) disassemble --frame
 # CHECK-NEXT: error: Cannot disassemble around the current function without a selected frame.
-# CHECK-EMPTY:
 # CHECK-NEXT: (lldb) disassemble --pc
 # CHECK-NEXT: error: Cannot disassemble around the current PC without a selected frame.
-# CHECK-EMPTY:
 # CHECK-NEXT: (lldb) disassemble --start-address 0x0
 # CHECK-NEXT: command-disassemble.s.tmp`foo:
 # CHECK-NEXT: command-disassemble.s.tmp[0x0] <+0>:   int    $0x10