13#ifndef LLVM_SUPPORT_DEBUGLOG_H
14#define LLVM_SUPPORT_DEBUGLOG_H
45#define LDBG(...) _GET_LDBG_MACRO(__VA_ARGS__)(__VA_ARGS__)
48#define LDBG_FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3
49#define LDBG_FUNC_RECOMPOSER(argsWithParentheses) \
50 LDBG_FUNC_CHOOSER argsWithParentheses
51#define LDBG_CHOOSE_FROM_ARG_COUNT(...) \
52 LDBG_FUNC_RECOMPOSER( \
53 (__VA_ARGS__, LDBG_LOG_LEVEL_WITH_TYPE, LDBG_LOG_LEVEL, ))
54#define LDBG_NO_ARG_EXPANDER() , , LDBG_LOG_LEVEL_1
55#define _GET_LDBG_MACRO(...) \
56 LDBG_CHOOSE_FROM_ARG_COUNT(LDBG_NO_ARG_EXPANDER __VA_ARGS__())
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)
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))
73#if defined(__FILE_NAME__)
74#define __LLVM_FILE_NAME__ __FILE_NAME__
76#define __LLVM_FILE_NAME__ ::llvm::impl::getShortFileName(__FILE__)
79#define DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, \
82 (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
84 for (::llvm::impl::raw_ldbg_ostream LdbgOS{ \
85 ::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM)}; \
87 ::llvm::impl::RAIINewLineStream{LdbgOS}.asLvalue()
89#define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, FILE) \
90 DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, __LINE__)
91#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, LEVEL, TYPE) \
92 DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, __LLVM_FILE_NAME__)
101 bool ShouldPrefixNextString;
105 void write_impl(
const char *
Ptr,
size_t Size)
final {
107 auto Eol = Str.find(
'\n');
110 while (Eol != StringRef::npos) {
112 StringRef Line = Str.take_front(Eol + 1);
114 writeWithPrefix(Line);
116 ShouldPrefixNextString =
true;
117 Str = Str.drop_front(Eol + 1);
118 Eol = Str.find(
'\n');
121 writeWithPrefix(Str);
123 void emitPrefix() { Os.
write(Prefix.c_str(), Prefix.size()); }
125 if (ShouldPrefixNextString) {
127 ShouldPrefixNextString =
false;
129 Os.
write(Str.data(), Str.size());
134 bool ShouldPrefixNextString =
true)
135 : Prefix(
std::
move(Prefix)), Os(Os),
136 ShouldPrefixNextString(ShouldPrefixNextString) {
164 const char *filename = path;
165 for (
const char *p = path; *p !=
'\0'; ++p) {
166 if (*p ==
'/' || *p ==
'\\')
176computePrefix(
const char *DebugType,
const char *File,
int Line,
int Level) {
180 OsPrefix <<
"[" << DebugType <<
":" << Level <<
"] ";
181 OsPrefix << File <<
":" << Line <<
" ";
182 return OsPrefix.
str();
186computePrefix(
int Level,
const char *File,
int Line,
const char *DebugType) {
195 for (bool _c = false; _c; _c = false) \
#define LLVM_ATTRIBUTE_UNUSED
place backedge safepoints impl
StringRef - Represent a constant reference to a string, i.e.
A raw_ostream that prints a newline on destruction, useful for LDBG()
void write_impl(const char *Ptr, size_t Size) final
The is the piece of the class that is implemented by subclasses.
RAIINewLineStream & asLvalue()
uint64_t current_pos() const final
Return the current position within the stream, not counting the bytes currently in the buffer.
RAIINewLineStream(raw_ostream &Os)
A raw_ostream that tracks \n and print the prefix after each newline.
~raw_ldbg_ostream() final
raw_ldbg_ostream(std::string Prefix, raw_ostream &Os, bool ShouldPrefixNextString=true)
raw_ldbg_ostream & asLvalue()
Some of the << operators expect an lvalue, so we trick the type system.
uint64_t current_pos() const final
Forward the current_pos method to the underlying stream.
This class implements an extremely fast bulk output stream that can only output to a stream.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
void SetUnbuffered()
Set the stream to be unbuffered.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
static LLVM_ATTRIBUTE_UNUSED constexpr const char * getShortFileName(const char *path)
Remove the path prefix from the file name.
static LLVM_ATTRIBUTE_UNUSED std::string computePrefix(const char *DebugType, const char *File, int Line, int Level)
Compute the prefix for the debug log in the form of: "[DebugType] File:Line " Where the File is the f...
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.