forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DPrint.h
138 lines (114 loc) · 4.18 KB
/
DPrint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#ifndef INSIGHTS_DPRINT_H
#define INSIGHTS_DPRINT_H
#include "InsightsStrCat.h"
//-----------------------------------------------------------------------------
namespace clang::insights {
namespace details {
static inline const char* Normalize(const std::string& arg)
{
static constexpr const char emptyString[]{""};
if(arg.empty()) {
return emptyString;
}
return arg.c_str();
}
//-----------------------------------------------------------------------------
static inline uint64_t Normalize(const llvm::APInt& arg)
{
return arg.getZExtValue();
}
//-----------------------------------------------------------------------------
static inline const char* Normalize(const llvm::APSInt& arg)
{
return Normalize(ToString(arg));
}
//-----------------------------------------------------------------------------
static inline const char* Normalize(const StringRef& arg)
{
return Normalize(arg.str());
}
//-----------------------------------------------------------------------------
template<class T>
static inline const T& Normalize(const T& arg)
{
return arg;
}
//-----------------------------------------------------------------------------
template<typename... Args>
inline void FPrintf(const char* fmt, Args&&... args)
{
if constexpr(0 < (sizeof...(args))) {
fprintf(stderr, fmt, Normalize(std::forward<Args>(args))...);
} else {
fprintf(stderr, "%s", fmt);
}
}
//-----------------------------------------------------------------------------
} // namespace details
/// \brief Debug print which is disabled in release-mode.
///
/// It takes a variable number of parameters which are normalized if they are a \ref std::string or a \ref StringRef.
template<typename... Args>
inline void DPrint([[maybe_unused]] const char* fmt, [[maybe_unused]] Args&&... args)
{
#ifdef INSIGHTS_DEBUG
details::FPrintf(fmt, std::forward<Args>(args)...);
#endif /* INSIGHTS_DEBUG */
}
//-----------------------------------------------------------------------------
/// \brief Log an error.
template<typename... Args>
inline void Error(const char* fmt, Args&&... args)
{
details::FPrintf(fmt, std::forward<Args>(args)...);
}
//-----------------------------------------------------------------------------
template<typename T>
inline void Dump([[maybe_unused]] const T* stmt)
{
#ifdef INSIGHTS_DEBUG
if(stmt) {
stmt->dump();
}
#endif /* INSIGHTS_DEBUG */
}
//-----------------------------------------------------------------------------
/// \brief Log an error.
///
/// In debug-mode this dumps the \ref Decl which caused the error and the error message.
template<typename... Args>
inline void Error(const Decl* stmt, const char* fmt, Args&&... args)
{
if(stmt) {
Dump(stmt);
}
Error(fmt, std::forward<Args>(args)...);
}
//-----------------------------------------------------------------------------
/// \brief Log an error.
///
/// In debug-mode this dumps the \ref Stmt which caused the error and the error message.
template<typename... Args>
inline void Error(const Stmt* stmt, const char* fmt, Args&&... args)
{
if(stmt) {
Dump(stmt);
}
Error(fmt, std::forward<Args>(args)...);
}
//-----------------------------------------------------------------------------
/// \brief Helper function to generate TODO comments for an unsupported \ref Stmt.
void ToDo(const class Stmt* stmt, class OutputFormatHelper& outputFormatHelper, const char* file, const int line);
/// \brief Helper function to generate TODO comments for an unsupported \ref Decl.
void ToDo(const class Decl* stmt, class OutputFormatHelper& outputFormatHelper, const char* file, const int line);
//-----------------------------------------------------------------------------
/// \brief Convenience marco to get file-name and line-number for better analysis.
#define TODO(stmt, outputFormatHelper) ToDo(stmt, outputFormatHelper, __FILE__, __LINE__)
} // namespace clang::insights
#endif /* INSIGHTS_DPRINT_H */