Skip to content

[lldb] Gardening in StreamAsynchronousIO (NFC) #127717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025

Conversation

JDevlieghere
Copy link
Member

@JDevlieghere JDevlieghere commented Feb 18, 2025

A handful of minor improvements to StreamAsynchronousIO:

  • Document the class.
  • Use a named enum value to distinguishing between stdout and stderr.
  • Add missing period to comment.
  • Clear the string instead of assigning to it.
  • Eliminate color argument.

A handful of minor improvements to StreamAsynchronousIO:

 - Document the class.
 - Use a named enum value to distinguishing between stdout and stderr.
 - Add missing period to comment.
 - Clear the string instead of assigning to it.
@JDevlieghere JDevlieghere requested a review from labath February 18, 2025 23:16
@llvmbot llvmbot added the lldb label Feb 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

A handful of minor improvements to StreamAsynchronousIO:

  • Document the class.
  • Use a named enum value to distinguishing between stdout and stderr.
  • Add missing period to comment.
  • Clear the string instead of assigning to it.

Full diff: https://github.com/llvm/llvm-project/pull/127717.diff

3 Files Affected:

  • (modified) lldb/include/lldb/Core/StreamAsynchronousIO.h (+10-2)
  • (modified) lldb/source/Core/Debugger.cpp (+4-2)
  • (modified) lldb/source/Core/StreamAsynchronousIO.cpp (+6-6)
diff --git a/lldb/include/lldb/Core/StreamAsynchronousIO.h b/lldb/include/lldb/Core/StreamAsynchronousIO.h
index b7adbc42096ce..7ae65757e2d73 100644
--- a/lldb/include/lldb/Core/StreamAsynchronousIO.h
+++ b/lldb/include/lldb/Core/StreamAsynchronousIO.h
@@ -18,9 +18,17 @@
 namespace lldb_private {
 class Debugger;
 
+/// A stream meant for asynchronously printing output. Output is buffered until
+/// the stream is flushed or destroyed. Printing is handled by the currently
+/// active IOHandler, or the debugger's output or error stream if there is none.
 class StreamAsynchronousIO : public Stream {
 public:
-  StreamAsynchronousIO(Debugger &debugger, bool for_stdout, bool colors);
+  enum ForSTDOUT : bool {
+    STDOUT = true,
+    STDERR = false,
+  };
+
+  StreamAsynchronousIO(Debugger &debugger, ForSTDOUT for_stdout);
 
   ~StreamAsynchronousIO() override;
 
@@ -32,7 +40,7 @@ class StreamAsynchronousIO : public Stream {
 private:
   Debugger &m_debugger;
   std::string m_data;
-  bool m_for_stdout;
+  ForSTDOUT m_for_stdout;
 };
 
 } // namespace lldb_private
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 18569e155b517..b8a0436e14968 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -1321,11 +1321,13 @@ bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) {
 }
 
 StreamSP Debugger::GetAsyncOutputStream() {
-  return std::make_shared<StreamAsynchronousIO>(*this, true, GetUseColor());
+  return std::make_shared<StreamAsynchronousIO>(*this,
+                                                StreamAsynchronousIO::STDOUT);
 }
 
 StreamSP Debugger::GetAsyncErrorStream() {
-  return std::make_shared<StreamAsynchronousIO>(*this, false, GetUseColor());
+  return std::make_shared<StreamAsynchronousIO>(*this,
+                                                StreamAsynchronousIO::STDERR);
 }
 
 void Debugger::RequestInterrupt() {
diff --git a/lldb/source/Core/StreamAsynchronousIO.cpp b/lldb/source/Core/StreamAsynchronousIO.cpp
index c2c64b61ab726..dbd56a69675b4 100644
--- a/lldb/source/Core/StreamAsynchronousIO.cpp
+++ b/lldb/source/Core/StreamAsynchronousIO.cpp
@@ -14,20 +14,20 @@
 using namespace lldb;
 using namespace lldb_private;
 
-StreamAsynchronousIO::StreamAsynchronousIO(Debugger &debugger, bool for_stdout,
-                                           bool colors)
-    : Stream(0, 4, eByteOrderBig, colors), m_debugger(debugger), m_data(),
-      m_for_stdout(for_stdout) {}
+StreamAsynchronousIO::StreamAsynchronousIO(
+    Debugger &debugger, StreamAsynchronousIO::ForSTDOUT for_stdout)
+    : Stream(0, 4, eByteOrderBig, debugger.GetUseColor()), m_debugger(debugger),
+      m_data(), m_for_stdout(for_stdout) {}
 
 StreamAsynchronousIO::~StreamAsynchronousIO() {
-  // Flush when we destroy to make sure we display the data
+  // Flush when we destroy to make sure we display the data.
   Flush();
 }
 
 void StreamAsynchronousIO::Flush() {
   if (!m_data.empty()) {
     m_debugger.PrintAsync(m_data.data(), m_data.size(), m_for_stdout);
-    m_data = std::string();
+    m_data.clear();
   }
 }
 

@JDevlieghere JDevlieghere merged commit 70e693c into llvm:main Feb 19, 2025
9 checks passed
@JDevlieghere JDevlieghere deleted the StreamAsynchronousIO-gardening branch February 19, 2025 16:34
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Mar 27, 2025
A handful of minor improvements to StreamAsynchronousIO:

 - Document the class.
 - Use a named enum value to distinguishing between stdout and stderr.
 - Add missing period to comment.
 - Clear the string instead of assigning to it.
 - Eliminate color argument.

(cherry picked from commit 70e693c)
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Mar 28, 2025
A handful of minor improvements to StreamAsynchronousIO:

 - Document the class.
 - Use a named enum value to distinguishing between stdout and stderr.
 - Add missing period to comment.
 - Clear the string instead of assigning to it.
 - Eliminate color argument.

(cherry picked from commit 70e693c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants