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