-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
OutputFormatHelper.cpp
89 lines (71 loc) · 3.05 KB
/
OutputFormatHelper.cpp
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
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#include <algorithm>
#include "CodeGenerator.h"
#include "InsightsHelpers.h"
#include "InsightsStaticStrings.h"
#include "OutputFormatHelper.h"
//-----------------------------------------------------------------------------
namespace clang::insights {
void OutputFormatHelper::Indent(unsigned count)
{
mOutput.insert(mOutput.size(), count, ' ');
}
//-----------------------------------------------------------------------------
void OutputFormatHelper::AppendParameterList(const ArrayRef<ParmVarDecl*> parameters,
const NameOnly nameOnly,
const GenMissingParamName genMissingParamName)
{
int count{};
ForEachArg(parameters, [&](const auto& p) {
auto name{GetName(*p)};
// A special case for CXXInheritedCtor. A user can omit the parameters name, but wihtout a name the call to the
// base constructor may look like calling the default constructor. In such a case we create a name.
if((GenMissingParamName::Yes == genMissingParamName) && (0 == name.length())) {
name = BuildInternalVarName(StrCat("param", count));
++count;
}
// Get the attributes and insert them, if there are any
CodeGeneratorVariant codeGenerator{*this};
codeGenerator->InsertAttributes(p->attrs());
if(const auto type{GetType(p->getType())}; NameOnly::No == nameOnly) {
Append(GetTypeNameAsParameter(type, name));
} else {
Append(name);
if(isa<PackExpansionType>(type)) {
Append(kwElipsis);
}
}
});
}
//-----------------------------------------------------------------------------
void OutputFormatHelper::CloseScope(const NoNewLineBefore newLineBefore)
{
if(NoNewLineBefore::No == newLineBefore) {
NewLine();
}
RemoveIndent();
Append('}');
DecreaseIndent();
}
//-----------------------------------------------------------------------------
void OutputFormatHelper::RemoveIndent()
{
/* After a newline we are already indented by one level to much. Try to decrease it. */
if(0 != mDefaultIndent) {
// go the string backwards and find the first non-whitespace character
const auto res = std::find_if(
std::rbegin(mOutput), std::rbegin(mOutput) + SCOPE_INDENT, [](const char& c) { return ' ' != c; });
// check if the string did end with at least one whitespace
if(const auto& end = std::rbegin(mOutput); res != end) {
// remove the whitespaces at the end of the string
mOutput.resize(mOutput.size() - std::distance(end, res));
}
}
}
//-----------------------------------------------------------------------------
} // namespace clang::insights