OutputFormatHelper.cpp
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#include <algorithm>
9
10#include "CodeGenerator.h"
11#include "InsightsHelpers.h"
13#include "OutputFormatHelper.h"
14//-----------------------------------------------------------------------------
15
16namespace clang::insights {
17
18void OutputFormatHelper::Indent(unsigned count)
19{
20 mOutput.insert(mOutput.size(), count, ' ');
21}
22//-----------------------------------------------------------------------------
23
24void OutputFormatHelper::AppendParameterList(const ArrayRef<ParmVarDecl*> parameters,
25 const NameOnly nameOnly,
26 const GenMissingParamName genMissingParamName)
27{
28 int count{};
29
30 ForEachArg(parameters, [&](const auto& p) {
31 auto name{GetName(*p)};
32
33 // A special case for CXXInheritedCtor. A user can omit the parameters name, but wihtout a name the call to the
34 // base constructor may look like calling the default constructor. In such a case we create a name.
35 if((GenMissingParamName::Yes == genMissingParamName) && (0 == name.length())) {
36 name = BuildInternalVarName(StrCat("param", count));
37 ++count;
38 }
39
40 // Get the attributes and insert them, if there are any
41 CodeGeneratorVariant codeGenerator{*this};
42 codeGenerator->InsertAttributes(p->attrs());
43
44 if(const auto type{GetType(p->getType())}; NameOnly::No == nameOnly) {
45
46 Append(GetTypeNameAsParameter(type, name));
47 } else {
48 Append(name);
49
50 if(isa<PackExpansionType>(type)) {
52 }
53 }
54 });
55}
56//-----------------------------------------------------------------------------
57
58void OutputFormatHelper::CloseScope(const NoNewLineBefore newLineBefore)
59{
60 if(NoNewLineBefore::No == newLineBefore) {
61 NewLine();
62 }
63
64 RemoveIndent();
65
66 Append('}');
67
69}
70//-----------------------------------------------------------------------------
71
72void OutputFormatHelper::RemoveIndent()
73{
74 /* After a newline we are already indented by one level to much. Try to decrease it. */
75 if(0 != mDefaultIndent) {
76 // go the string backwards and find the first non-whitespace character
77 const auto res = std::find_if(
78 std::rbegin(mOutput), std::rbegin(mOutput) + SCOPE_INDENT, [](const char& c) { return ' ' != c; });
79
80 // check if the string did end with at least one whitespace
81 if(const auto& end = std::rbegin(mOutput); res != end) {
82 // remove the whitespaces at the end of the string
83 mOutput.resize(mOutput.size() - std::distance(end, res));
84 }
85 }
86}
87//-----------------------------------------------------------------------------
88
89} // namespace clang::insights
constexpr std::string_view kwElipsis
A special container which creates either a CodeGenerator or a CfrontCodeGenerator depending on the co...
void DecreaseIndent()
Decrease the current indention by SCOPE_INDENT.
void ForEachArg(const auto &arguments, auto &&lambda)
Append a argument list to the buffer.
void AppendParameterList(const ArrayRef< ParmVarDecl * > parameters, const NameOnly nameOnly=NameOnly::No, const GenMissingParamName genMissingParamName=GenMissingParamName::No)
Append a ParamVarDecl array.
void Append(const char c)
Append a single character.
void CloseScope(const NoNewLineBefore newLineBefore=NoNewLineBefore::No)
Close a scope by inserting a '}'.
QualType GetType(QualType t)
In Cfront mode we transform references to pointers.
std::string GetName(const NamedDecl &nd, const QualifiedName qualifiedName)
std::string BuildInternalVarName(const std::string_view &varName)
std::string GetTypeNameAsParameter(const QualType &t, std::string_view varName, const Unqualified unqualified)
std::string StrCat(const auto &... args)