@@ -41,32 +41,81 @@ namespace llvm {
4141//
4242#define LDBG (...) _GET_LDBG_MACRO(__VA_ARGS__)(__VA_ARGS__)
4343
44- #define DEBUGLOG_WITH_STREAM_AND_TYPE (STREAM, TYPE ) \
45- for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c; \
46- _c = false ) \
47- ::llvm::impl::LogWithNewline (TYPE, __FILE__, __LINE__, (STREAM))
44+ // Helper macros to choose the correct macro based on the number of arguments.
45+ #define LDBG_FUNC_CHOOSER (_f1, _f2, ...) _f2
46+ #define LDBG_FUNC_RECOMPOSER (argsWithParentheses ) \
47+ LDBG_FUNC_CHOOSER argsWithParentheses
48+ #define LDBG_CHOOSE_FROM_ARG_COUNT (...) \
49+ LDBG_FUNC_RECOMPOSER ((__VA_ARGS__, LDBG_LOG_LEVEL, ))
50+ #define LDBG_NO_ARG_EXPANDER () , LDBG_LOG_LEVEL_1
51+ #define _GET_LDBG_MACRO (...) \
52+ LDBG_CHOOSE_FROM_ARG_COUNT (LDBG_NO_ARG_EXPANDER __VA_ARGS__ ())
53+
54+ // Dispatch macros to support the `level` argument or none (default to 1)
55+ #define LDBG_LOG_LEVEL (LEVEL ) \
56+ DEBUGLOG_WITH_STREAM_AND_TYPE (llvm::dbgs(), LEVEL, DEBUG_TYPE)
57+ #define LDBG_LOG_LEVEL_1 () LDBG_LOG_LEVEL(1 )
58+
59+ #define DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE (STREAM, LEVEL, TYPE, FILE, \
60+ LINE) \
61+ for (bool _c = \
62+ (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
63+ _c; _c = false ) \
64+ for (::llvm::impl::RAIINewLineStream NewLineStream{(STREAM)}; _c; \
65+ _c = false ) \
66+ ::llvm::impl::raw_ldbg_ostream{ \
67+ ::llvm::impl::computePrefix (TYPE, FILE, LINE, LEVEL), NewLineStream} \
68+ .asLvalue ()
69+
70+ #define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, FILE ) \
71+ DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE (STREAM, LEVEL, TYPE, FILE, __LINE__)
72+ // When __SHORT_FILE__ is not defined, the File is the full path,
73+ // otherwise __SHORT_FILE__ is defined in CMake to provide the file name
74+ // without the path prefix.
75+ #if defined(__SHORT_FILE__)
76+ #define DEBUGLOG_WITH_STREAM_AND_TYPE (STREAM, LEVEL, TYPE ) \
77+ DEBUGLOG_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, __SHORT_FILE__)
78+ #else
79+ #define DEBUGLOG_WITH_STREAM_AND_TYPE (STREAM, LEVEL, TYPE ) \
80+ DEBUGLOG_WITH_STREAM_TYPE_AND_FILE (STREAM, LEVEL, TYPE, \
81+ ::llvm::impl::getShortFileName (__FILE__))
82+ #endif
4883
4984namespace impl {
50- class LogWithNewline {
51- public:
52- LogWithNewline (const char *debug_type, const char *file, int line,
53- raw_ostream &os)
54- : os(os) {
55- if (debug_type)
56- os << " [" << debug_type << " ] " ;
57- os << file << " :" << line << " " ;
85+
86+ // / A raw_ostream that tracks `\n` and print the prefix after each
87+ // / newline.
88+ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
89+ std::string Prefix;
90+ raw_ostream &Os;
91+ bool HasPendingNewline;
92+
93+ // / Split the line on newlines and insert the prefix before each
94+ // / newline. Forward everything to the underlying stream.
95+ void write_impl (const char *Ptr, size_t Size) final {
96+ auto Str = StringRef (Ptr, Size);
97+ // Handle the initial prefix.
98+ if (!Str.empty ())
99+ writeWithPrefix (StringRef ());
100+
101+ auto Eol = Str.find (' \n ' );
102+ while (Eol != StringRef::npos) {
103+ StringRef Line = Str.take_front (Eol + 1 );
104+ if (!Line.empty ())
105+ writeWithPrefix (Line);
106+ HasPendingNewline = true ;
107+ Str = Str.drop_front (Eol + 1 );
108+ Eol = Str.find (' \n ' );
109+ }
110+ if (!Str.empty ())
111+ writeWithPrefix (Str);
58112 }
59- ~LogWithNewline () { os << ' \n ' ; }
60- template <typename T> raw_ostream &operator <<(const T &t) && {
61- return os << t;
113+ void emitPrefix () { Os.write (Prefix.c_str (), Prefix.size ()); }
114+ void writeWithPrefix (StringRef Str) {
115+ flushEol ();
116+ Os.write (Str.data (), Str.size ());
62117 }
63118
64- // Prevent copying, as this class manages newline responsibility and is
65- // intended for use as a temporary.
66- LogWithNewline (const LogWithNewline &) = delete ;
67- LogWithNewline &operator =(const LogWithNewline &) = delete ;
68- LogWithNewline &operator =(LogWithNewline &&) = delete ;
69-
70119public:
71120 explicit raw_ldbg_ostream (std::string Prefix, raw_ostream &Os,
72121 bool HasPendingNewline = true )
0 commit comments