forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordDeclHandler.cpp
More file actions
81 lines (62 loc) · 2.99 KB
/
RecordDeclHandler.cpp
File metadata and controls
81 lines (62 loc) · 2.99 KB
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
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#include "RecordDeclHandler.h"
#include "ClangCompat.h"
#include "CodeGenerator.h"
#include "InsightsHelpers.h"
#include "InsightsMatchers.h"
#include "OutputFormatHelper.h"
//-----------------------------------------------------------------------------
using namespace clang;
using namespace clang::ast_matchers;
//-----------------------------------------------------------------------------
namespace clang::insights {
constexpr auto idRecordDecl{"rd"sv};
constexpr auto idDecl{"decl"sv};
//-----------------------------------------------------------------------------
RecordDeclHandler::RecordDeclHandler(Rewriter& rewrite, MatchFinder& matcher)
: InsightsBase(rewrite)
{
matcher.addMatcher(
cxxRecordDecl(hasDefinition(), hasThisTUParent, unless(anyOf(isLambda(), isTemplate))).bind(idRecordDecl),
this);
matcher.addMatcher(namespaceDecl(hasThisTUParent).bind(idDecl), this);
matcher.addMatcher(enumDecl(hasThisTUParent).bind(idDecl), this);
matcher.addMatcher(typeAliasDecl(hasThisTUParent).bind(idDecl), this);
matcher.addMatcher(staticAssertDecl(hasThisTUParent).bind(idDecl), this);
}
//-----------------------------------------------------------------------------
void RecordDeclHandler::run(const MatchFinder::MatchResult& result)
{
if(const auto* cxxRecordDecl = result.Nodes.getNodeAs<CXXRecordDecl>(idRecordDecl)) {
OutputFormatHelper outputFormatHelper{};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(cxxRecordDecl);
if(auto sourceRange = cxxRecordDecl->getSourceRange(); not IsMacroLocation(sourceRange)) {
if(IsAnonymousStructOrUnion(cxxRecordDecl)) {
sourceRange.setEnd(sourceRange.getEnd().getLocWithOffset(2)); // 2 is just what worked
}
mRewrite.ReplaceText(GetSourceRangeAfterSemi(sourceRange, result, RequireSemi::Yes), outputFormatHelper);
} else {
// We're just interested in the start location, -1 work(s|ed)
const auto startLoc =
GetSourceRangeAfterSemi(sourceRange, result, RequireSemi::No).getBegin().getLocWithOffset(-1);
InsertIndentedText(startLoc, outputFormatHelper);
}
// Catch all for:
// - NamespaceDecl
// - EnumDecl
// - TypeAliasDecl
} else if(const auto* decl = result.Nodes.getNodeAs<Decl>(idDecl)) {
OutputFormatHelper outputFormatHelper{};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(decl);
mRewrite.ReplaceText(GetSourceRangeAfterSemi(decl->getSourceRange(), result), outputFormatHelper);
}
}
//-----------------------------------------------------------------------------
} // namespace clang::insights