19
19
namespace llvm {
20
20
#ifndef NDEBUG
21
21
22
- // LDBG() is a macro that can be used as a raw_ostream for debugging.
23
- // It will stream the output to the dbgs() stream, with a prefix of the
24
- // debug type and the file and line number. A trailing newline is added to the
25
- // output automatically. If the streamed content contains a newline, the prefix
26
- // is added to each beginning of a new line. Nothing is printed if the debug
27
- // output is not enabled or the debug type does not match.
28
- //
29
- // E.g.,
30
- // LDBG() << "Bitset contains: " << Bitset;
31
- // is somehow equivalent to
32
- // LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] " << __FILE__ << ":" <<
33
- // __LINE__ << " "
34
- // << "Bitset contains: " << Bitset << "\n");
35
- //
22
+ // / LDBG() is a macro that can be used as a raw_ostream for debugging.
23
+ // / It will stream the output to the dbgs() stream, with a prefix of the
24
+ // / debug type and the file and line number. A trailing newline is added to the
25
+ // / output automatically. If the streamed content contains a newline, the prefix
26
+ // / is added to each beginning of a new line. Nothing is printed if the debug
27
+ // / output is not enabled or the debug type does not match.
28
+ // /
29
+ // / E.g.,
30
+ // / LDBG() << "Bitset contains: " << Bitset;
31
+ // / is somehow equivalent to
32
+ // / LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] " << __FILE__ << ":" <<
33
+ // / __LINE__ << " "
34
+ // / << "Bitset contains: " << Bitset << "\n");
35
+ // /
36
36
// An optional `level` argument can be provided to control the verbosity of the
37
- // output. The default level is 1, and is in increasing level of verbosity.
38
- //
39
- // The `level` argument can be a literal integer, or a macro that evaluates to
40
- // an integer.
41
- //
42
- // An optional `type` argument can be provided to control the debug type. The
43
- // default type is DEBUG_TYPE. The `type` argument can be a literal string, or a
44
- // macro that evaluates to a string.
37
+ // / output. The default level is 1, and is in increasing level of verbosity.
38
+ // /
39
+ // / The `level` argument can be a literal integer, or a macro that evaluates to
40
+ // / an integer.
41
+ // /
42
+ // / An optional `type` argument can be provided to control the debug type. The
43
+ // / default type is DEBUG_TYPE. The `type` argument can be a literal string, or
44
+ // / a macro that evaluates to a string.
45
45
#define LDBG (...) _GET_LDBG_MACRO(__VA_ARGS__)(__VA_ARGS__)
46
46
47
+ // / LDBG_OS() is a macro that can be used as a raw_ostream for debugging.
48
+ // / It will stream the output to the dbgs() stream, with a prefix of the
49
+ // / debug type and the file and line number. A trailing newline is added to the
50
+ // / output automatically. If the streamed content contains a newline, the prefix
51
+ // / is added to each beginning of a new line. Nothing is printed if the debug
52
+ // / output is not enabled or the debug type does not match.
53
+ // / Note: this macro isn't guarded by a debug flag so it will always print in
54
+ // / assert builds.
55
+ #define LDBG_OS (...) _GET_LDBG_OS_MACRO(__VA_ARGS__)(__VA_ARGS__)
56
+
47
57
// Helper macros to choose the correct macro based on the number of arguments.
48
58
#define LDBG_FUNC_CHOOSER (_f1, _f2, _f3, ...) _f3
49
59
#define LDBG_FUNC_RECOMPOSER (argsWithParentheses ) \
@@ -55,17 +65,6 @@ namespace llvm {
55
65
#define _GET_LDBG_MACRO (...) \
56
66
LDBG_CHOOSE_FROM_ARG_COUNT (LDBG_NO_ARG_EXPANDER __VA_ARGS__ ())
57
67
58
- // Dispatch macros to support the `level` argument or none (default to 1)
59
- #define LDBG_LOG_LEVEL (LEVEL ) \
60
- DEBUGLOG_WITH_STREAM_AND_TYPE (llvm::dbgs(), LEVEL, DEBUG_TYPE)
61
- #define LDBG_LOG_LEVEL_1 () LDBG_LOG_LEVEL(1 )
62
- // This macro is a helper when LDBG() is called with 2 arguments.
63
- // In this case we want to allow the order of the arguments to be swapped.
64
- // We rely on the fact that the `level` argument is an integer, and the `type`
65
- // is a string and dispatch to a C++ API that is overloaded.
66
- #define LDBG_LOG_LEVEL_WITH_TYPE (LEVEL_OR_TYPE, TYPE_OR_LEVEL ) \
67
- DEBUGLOG_WITH_STREAM_AND_TYPE (llvm::dbgs(), (LEVEL_OR_TYPE), (TYPE_OR_LEVEL))
68
-
69
68
// We want the filename without the full path. We are using the __FILE__ macro
70
69
// and a constexpr function to strip the path prefix. We can avoid the frontend
71
70
// repeated evaluation of __FILE__ by using the __FILE_NAME__ when defined
@@ -82,15 +81,63 @@ namespace llvm {
82
81
(::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
83
82
_c; _c = false ) \
84
83
for (::llvm::impl::raw_ldbg_ostream LdbgOS{ \
85
- ::llvm::impl::computePrefix (TYPE, FILE, LINE, LEVEL), (STREAM)}; \
84
+ ::llvm::impl::computePrefix (TYPE, FILE, LINE, LEVEL), (STREAM), \
85
+ /* ShouldPrefixNextString=*/ true, \
86
+ /* ShouldEmitNewLineOnDestruction=*/ true}; \
86
87
_c; _c = false ) \
87
- ::llvm::impl::RAIINewLineStream{ LdbgOS}. asLvalue ()
88
+ LdbgOS
88
89
89
90
#define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, FILE ) \
90
91
DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE (STREAM, LEVEL, TYPE, FILE, __LINE__)
91
92
#define DEBUGLOG_WITH_STREAM_AND_TYPE (STREAM, LEVEL, TYPE ) \
92
93
DEBUGLOG_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, __LLVM_FILE_NAME__)
93
94
95
+ // Dispatch macros to support the `level` argument or none (default to 1)
96
+ #define LDBG_LOG_LEVEL (LEVEL ) \
97
+ DEBUGLOG_WITH_STREAM_AND_TYPE (llvm::dbgs (), LEVEL, DEBUG_TYPE)
98
+ #define LDBG_LOG_LEVEL_1 () LDBG_LOG_LEVEL(1 )
99
+ // This macro is a helper when LDBG() is called with 2 arguments.
100
+ // In this case we want to allow the order of the arguments to be swapped.
101
+ // We rely on the fact that the `level` argument is an integer, and the `type`
102
+ // is a string and dispatch to a C++ API that is overloaded.
103
+ #define LDBG_LOG_LEVEL_WITH_TYPE (LEVEL_OR_TYPE, TYPE_OR_LEVEL ) \
104
+ DEBUGLOG_WITH_STREAM_AND_TYPE (llvm::dbgs (), (LEVEL_OR_TYPE), (TYPE_OR_LEVEL))
105
+
106
+ // Helper macros to choose the correct macro based on the number of arguments.
107
+ // This is the same logic as for LDBG(), but for LDBG_OS().
108
+ #define LDBG_OS_FUNC_CHOOSER (_f1, _f2, _f3, ...) _f3
109
+ #define LDBG_OS_FUNC_RECOMPOSER (argsWithParentheses ) \
110
+ LDBG_OS_FUNC_CHOOSER argsWithParentheses
111
+ #define LDBG_OS_CHOOSE_FROM_ARG_COUNT (...) \
112
+ LDBG_OS_FUNC_RECOMPOSER ( \
113
+ (__VA_ARGS__, LDBG_OS_LOG_LEVEL_WITH_TYPE, LDBG_OS_LOG_LEVEL, ))
114
+ #define LDBG_OS_NO_ARG_EXPANDER () , , LDBG_OS_LOG_LEVEL_1
115
+ #define _GET_LDBG_OS_MACRO (...) \
116
+ LDBG_OS_CHOOSE_FROM_ARG_COUNT (LDBG_OS_NO_ARG_EXPANDER __VA_ARGS__ ())
117
+
118
+ #define LDBG_OS_LOG_LEVEL (LEVEL ) \
119
+ LDBG_OS_WITH_STREAM_AND_TYPE (llvm::dbgs (), LEVEL, DEBUG_TYPE)
120
+ #define LDBG_OS_LOG_LEVEL_1 () LDBG_OS_LOG_LEVEL(1 )
121
+ // This macro is a helper when LDBG_OS() is called with 2 arguments.
122
+ // In this case we want to allow the order of the arguments to be swapped.
123
+ // We rely on the fact that the `level` argument is an integer, and the `type`
124
+ // is a string and dispatch to a C++ API that is overloaded.
125
+ #define LDBG_OS_LOG_LEVEL_WITH_TYPE (LEVEL_OR_TYPE, TYPE_OR_LEVEL ) \
126
+ LDBG_OS_WITH_STREAM_AND_TYPE (llvm::dbgs (), LEVEL_OR_TYPE, TYPE_OR_LEVEL)
127
+
128
+ #define LDBG_OS_WITH_STREAM_TYPE_FILE_AND_LINE (STREAM, LEVEL, TYPE, FILE, \
129
+ LINE) \
130
+ ::llvm::impl::raw_ldbg_ostream { \
131
+ ::llvm::impl::computePrefix (TYPE, FILE, LINE, LEVEL), (STREAM), \
132
+ /* ShouldPrefixNextString=*/ true, \
133
+ /* ShouldEmitNewLineOnDestruction=*/ true \
134
+ }
135
+
136
+ #define LDBG_OS_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, FILE ) \
137
+ LDBG_OS_WITH_STREAM_TYPE_FILE_AND_LINE (STREAM, LEVEL, TYPE, FILE, __LINE__)
138
+ #define LDBG_OS_WITH_STREAM_AND_TYPE (STREAM, LEVEL, TYPE ) \
139
+ LDBG_OS_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, __LLVM_FILE_NAME__)
140
+
94
141
namespace impl {
95
142
96
143
// / A raw_ostream that tracks `\n` and print the prefix after each
@@ -99,6 +146,7 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
99
146
std::string Prefix;
100
147
raw_ostream &Os;
101
148
bool ShouldPrefixNextString;
149
+ bool ShouldEmitNewLineOnDestruction;
102
150
103
151
// / Split the line on newlines and insert the prefix before each
104
152
// / newline. Forward everything to the underlying stream.
@@ -131,12 +179,17 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
131
179
132
180
public:
133
181
explicit raw_ldbg_ostream (std::string Prefix, raw_ostream &Os,
134
- bool ShouldPrefixNextString = true )
182
+ bool ShouldPrefixNextString = true ,
183
+ bool ShouldEmitNewLineOnDestruction = false )
135
184
: Prefix(std::move(Prefix)), Os(Os),
136
- ShouldPrefixNextString(ShouldPrefixNextString) {
185
+ ShouldPrefixNextString(ShouldPrefixNextString),
186
+ ShouldEmitNewLineOnDestruction(ShouldEmitNewLineOnDestruction) {
137
187
SetUnbuffered ();
138
188
}
139
- ~raw_ldbg_ostream () final {}
189
+ ~raw_ldbg_ostream () final {
190
+ if (ShouldEmitNewLineOnDestruction)
191
+ Os << ' \n ' ;
192
+ }
140
193
141
194
// / Forward the current_pos method to the underlying stream.
142
195
uint64_t current_pos () const final { return Os.tell (); }
@@ -194,6 +247,7 @@ computePrefix(int Level, const char *File, int Line, const char *DebugType) {
194
247
#define LDBG (...) \
195
248
for (bool _c = false ; _c; _c = false ) \
196
249
::llvm::nulls ()
250
+ #define LDBG_OS (...)
197
251
#endif
198
252
} // end namespace llvm
199
253
0 commit comments