@@ -61,8 +61,10 @@ namespace llvm {
61
61
for (bool _c = \
62
62
(::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
63
63
_c; _c = false ) \
64
+ for (::llvm::impl::RAIINewLineStream NewLineStream{(STREAM)}; _c; \
65
+ _c = false ) \
64
66
::llvm::impl::raw_ldbg_ostream{ \
65
- ::llvm::impl::computePrefix (TYPE, FILE, LINE, LEVEL), (STREAM)} \
67
+ ::llvm::impl::computePrefix (TYPE, FILE, LINE, LEVEL), NewLineStream} \
66
68
.asLvalue ()
67
69
68
70
#define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, FILE ) \
@@ -81,14 +83,15 @@ namespace llvm {
81
83
82
84
namespace impl {
83
85
84
- // / A raw_ostream that tracks `\n` and print the prefix.
86
+ // / A raw_ostream that tracks `\n` and print the prefix after each
87
+ // / newline.
85
88
class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
86
89
std::string Prefix;
87
90
raw_ostream &Os;
88
- bool HasPendingNewline = true ;
91
+ bool HasPendingNewline;
89
92
90
- // / Split the line on newlines and insert the prefix before each newline.
91
- // / Forward everything to the underlying stream.
93
+ // / Split the line on newlines and insert the prefix before each
94
+ // / newline. Forward everything to the underlying stream.
92
95
void write_impl (const char *Ptr, size_t Size) final {
93
96
auto Str = StringRef (Ptr, Size);
94
97
// Handle the initial prefix.
@@ -109,22 +112,18 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
109
112
}
110
113
void emitPrefix () { Os.write (Prefix.c_str (), Prefix.size ()); }
111
114
void writeWithPrefix (StringRef Str) {
112
- if (HasPendingNewline) {
113
- emitPrefix ();
114
- HasPendingNewline = false ;
115
- }
115
+ flushEol ();
116
116
Os.write (Str.data (), Str.size ());
117
117
}
118
118
119
119
public:
120
- explicit raw_ldbg_ostream (std::string Prefix, raw_ostream &Os)
121
- : Prefix(std::move(Prefix)), Os(Os) {
120
+ explicit raw_ldbg_ostream (std::string Prefix, raw_ostream &Os,
121
+ bool HasPendingNewline = true )
122
+ : Prefix(std::move(Prefix)), Os(Os),
123
+ HasPendingNewline(HasPendingNewline) {
122
124
SetUnbuffered ();
123
125
}
124
- ~raw_ldbg_ostream () final {
125
- flushEol ();
126
- Os << ' \n ' ;
127
- }
126
+ ~raw_ldbg_ostream () final { flushEol (); }
128
127
void flushEol () {
129
128
if (HasPendingNewline) {
130
129
emitPrefix ();
@@ -135,10 +134,22 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
135
134
// / Forward the current_pos method to the underlying stream.
136
135
uint64_t current_pos () const final { return Os.tell (); }
137
136
138
- // / Some of the `<<` operators expect an lvalue, so we trick the type system.
137
+ // / Some of the `<<` operators expect an lvalue, so we trick the type
138
+ // / system.
139
139
raw_ldbg_ostream &asLvalue () { return *this ; }
140
140
};
141
141
142
+ // / A raw_ostream that prints a newline on destruction, useful for LDBG()
143
+ class RAIINewLineStream final : public raw_ostream {
144
+ raw_ostream &Os;
145
+
146
+ public:
147
+ RAIINewLineStream (raw_ostream &Os) : Os(Os) { SetUnbuffered (); }
148
+ ~RAIINewLineStream () { Os << ' \n ' ; }
149
+ void write_impl (const char *Ptr, size_t Size) final { Os.write (Ptr, Size); }
150
+ uint64_t current_pos () const final { return Os.tell (); }
151
+ };
152
+
142
153
// / Remove the path prefix from the file name.
143
154
static LLVM_ATTRIBUTE_UNUSED constexpr const char *
144
155
getShortFileName (const char *path) {
0 commit comments