[llvm] Implement a custom stream for LDBG macro to handle newlines (PR #150750)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 26 15:12:59 PDT 2025


================
@@ -30,37 +30,95 @@ namespace llvm {
 #define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE)                            \
   for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c;  \
        _c = false)                                                             \
-  ::llvm::impl::LogWithNewline(TYPE, __SHORT_FILE__, __LINE__, (STREAM))
+  ::llvm::impl::LogWithNewline(TYPE, __SHORT_FILE__, __LINE__, (STREAM))       \
+      .stream()
 #else
 #define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE)                            \
   for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c;  \
        _c = false)                                                             \
-  ::llvm::impl::LogWithNewline(TYPE, __FILE__, __LINE__, (STREAM))
+  ::llvm::impl::LogWithNewline(TYPE, __FILE__, __LINE__, (STREAM)).stream()
 #endif
 
 namespace impl {
-class LogWithNewline {
+
+/// A raw_ostream that tracks `\n` and print the prefix.
+class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
+  std::string Prefix;
+  raw_ostream &Os;
+  bool HasPendingNewline = true;
+
+  /// Split the line on newlines and insert the prefix before each newline.
+  /// Forward everything to the underlying stream.
+  void write_impl(const char *Ptr, size_t Size) final {
+    auto Str = StringRef(Ptr, Size);
+    if (!Str.empty()) {
+      if (HasPendingNewline) {
+        emitPrefix();
+        HasPendingNewline = false;
+      }
+    }
+    auto Eol = Str.find('\n');
+    while (Eol != StringRef::npos) {
+      StringRef Line = Str.take_front(Eol + 1);
+      if (!Line.empty()) {
+        if (HasPendingNewline) {
+          emitPrefix();
+          HasPendingNewline = false;
+        }
+        Os.write(Line.data(), Line.size());
+      }
+      HasPendingNewline = true;
+      Str = Str.drop_front(Eol + 1);
+      Eol = Str.find('\n');
+    }
+    if (!Str.empty()) {
+      if (HasPendingNewline) {
+        emitPrefix();
+        HasPendingNewline = false;
+      }
----------------
joker-eph wrote:

What about if the input is "\nFoo"? 

The first prefix would be emitted Line 55-58, then inside the loop we would set `HasPendingNewline = true;` and then emit a new prefix line 76.

https://github.com/llvm/llvm-project/pull/150750


More information about the llvm-commits mailing list