-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
OutputFormatHelper.h
254 lines (206 loc) · 7.45 KB
/
OutputFormatHelper.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#ifndef OUTPUT_FORMAT_HELPER_H
#define OUTPUT_FORMAT_HELPER_H
//-----------------------------------------------------------------------------
#include <string_view>
#include <utility>
using namespace std::literals;
#include "InsightsOnce.h"
#include "InsightsStrCat.h"
#include "InsightsStrongTypes.h"
//-----------------------------------------------------------------------------
namespace clang::insights {
//-----------------------------------------------------------------------------
/// \brief The C++ Insights formatter.
///
/// Most of the code is handed to \ref OutputFormatHelper for easy code formatting.
class OutputFormatHelper
{
public:
OutputFormatHelper() = default;
explicit OutputFormatHelper(const unsigned indent)
: mDefaultIndent{indent}
{
}
operator std::string_view() const& { return {mOutput}; }
auto size() const { return mOutput.size(); }
/// \brief Returns the current position in the output buffer.
size_t CurrentPos() const { return mOutput.length(); }
/// \brief Insert a string before the position \c atPos
void InsertAt(const size_t atPos, std::string_view data) { mOutput.insert(atPos, data); }
STRONG_BOOL(SkipIndenting);
auto GetIndent() const { return mDefaultIndent; }
/// \brief Set the indent level of this class to \c indent.
void SetIndent(const unsigned indent, const SkipIndenting skipIndenting = SkipIndenting::No)
{
mDefaultIndent = indent;
if(SkipIndenting::No == skipIndenting) {
Indent(mDefaultIndent);
}
}
/// \brief Set the indent level of this class to that of \c rhs.
void SetIndent(const OutputFormatHelper& rhs, const SkipIndenting skipIndenting = SkipIndenting::No)
{
if(&rhs != this) {
mDefaultIndent = rhs.mDefaultIndent;
if(SkipIndenting::No == skipIndenting) {
Indent(mDefaultIndent);
}
}
}
/// \brief Check whether the buffer is empty.
///
/// This also treats a string of just whitespaces as empty.
bool empty() const { return mOutput.empty() or (std::string::npos == mOutput.find_first_not_of(' ', 0)); }
/// \brief Returns a reference to the underlying string buffer.
std::string& GetString() { return mOutput; }
/// \brief Append a single character
///
/// Append a single character to the buffer
void Append(const char c) { mOutput += c; }
void Append(const std::string_view& arg) { mOutput += arg; }
/// \brief Append a variable number of data
///
/// The \c StrCat function which is used ensures, that a \c StringRef or a char are converted appropriately.
void Append(const auto&... args) { details::StrCat(mOutput, args...); }
/// \brief Same as \ref Append but adds a newline after the last argument.
///
/// Append a single character to the buffer
void AppendNewLine(const char c)
{
mOutput += c;
NewLine();
}
void AppendNewLine(const std::string_view& arg)
{
mOutput += arg;
NewLine();
}
/// \brief Same as \ref Append but adds a newline after the last argument.
void AppendNewLine(const auto&... args)
{
if constexpr(0 < sizeof...(args)) {
details::StrCat(mOutput, args...);
}
NewLine();
}
void AppendComment(const std::string_view& arg)
{
Append("/* "sv);
Append(arg);
Append(" */"sv);
}
void AppendCommentNewLine(const std::string_view& arg)
{
AppendComment(arg);
NewLine();
}
void AppendCommentNewLine(const auto&... args)
{
if constexpr(0 < sizeof...(args)) {
AppendComment(StrCat(args...));
}
NewLine();
}
STRONG_BOOL(NameOnly);
STRONG_BOOL(GenMissingParamName);
/// \brief Append a \c ParamVarDecl array.
///
/// The parameter name is always added as well.
void AppendParameterList(const ArrayRef<ParmVarDecl*> parameters,
const NameOnly nameOnly = NameOnly::No,
const GenMissingParamName genMissingParamName = GenMissingParamName::No);
/// \brief Increase the current indention by \c SCOPE_INDENT
void IncreaseIndent() { mDefaultIndent += SCOPE_INDENT; }
/// \brief Decrease the current indention by \c SCOPE_INDENT
void DecreaseIndent()
{
if(mDefaultIndent >= SCOPE_INDENT) {
mDefaultIndent -= SCOPE_INDENT;
}
}
/// \brief Open a scope by inserting a '{' followed by an indented newline.
void OpenScope()
{
Append('{');
IncreaseIndent();
NewLine();
}
STRONG_BOOL(NoNewLineBefore);
/// \brief Close a scope by inserting a '}'
///
/// With the parameter \c newLineBefore a newline after the brace can be inserted.
void CloseScope(const NoNewLineBefore newLineBefore = NoNewLineBefore::No);
/// \brief Similiar to \ref CloseScope only this time a ';' is inserted after the brace.
void CloseScopeWithSemi(const NoNewLineBefore newLineBefore = NoNewLineBefore::No)
{
CloseScope(newLineBefore);
Append(';');
}
/// \brief Append a comma if needed.
void AppendComma(OnceFalse& needsComma)
{
if(needsComma) {
Append(", "sv);
}
}
/// \brief Append a semicolon and a newline.
void AppendSemiNewLine() { AppendNewLine(';'); }
/// \brief Append a semicolon and a newline.
template<typename... Args>
void AppendSemiNewLine(const Args&... args)
{
if constexpr(0 < sizeof...(args)) {
details::StrCat(mOutput, args...);
}
AppendNewLine(';');
}
void AppendSemiNewLine(const std::string_view& arg)
{
mOutput += arg;
AppendNewLine(';');
}
/// \brief Append a argument list to the buffer.
///
/// This function takes care of the delimiting ',' between the parameters. The lambda \c lambda is called to each
/// argument after the comma was inserted.
/// Usage:
/// \code
/// ForEachArg(parameters, [&](const auto& p) {
/// // do something with p
/// });
/// \endcode
inline void ForEachArg(const auto& arguments, /*XXX: invocable*/ auto&& lambda)
{
for(OnceFalse needsComma{}; const auto& arg : arguments) {
if constexpr(std::is_same_v<const TemplateArgument&, decltype(arg)>) {
if((TemplateArgument::Pack == arg.getKind()) and (0 == arg.pack_size())) {
break;
}
}
AppendComma(needsComma);
lambda(arg);
}
}
void InsertIfDefTemplateGuard() { AppendNewLine("#ifdef INSIGHTS_USE_TEMPLATE"sv); }
void InsertEndIfTemplateGuard() { AppendNewLine("#endif"sv); }
private:
static constexpr unsigned SCOPE_INDENT{2};
unsigned mDefaultIndent{};
std::string mOutput{};
void Indent(unsigned count);
void NewLine()
{
mOutput += '\n';
Indent(mDefaultIndent);
}
void RemoveIndent();
};
//-----------------------------------------------------------------------------
} // namespace clang::insights
#endif /* OUTPUT_FORMAT_HELPER_H */