Repository navigation
Expand file tree
/
Copy pathRecordDeclHandler.cpp
More file actions
92 lines (73 loc) · 3.92 KB
/
Copy pathRecordDeclHandler.cpp
File metadata and controls
92 lines (73 loc) · 3.92 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
82
83
84
85
86
87
88
89
90
91
92
/******************************************************************************
*
* 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 {
RecordDeclHandler::RecordDeclHandler(Rewriter& rewrite, MatchFinder& matcher)
: InsightsBase(rewrite)
{
matcher.addMatcher(cxxRecordDecl(hasDefinition(),
unless(anyOf(isLambda(),
hasAncestor(namespaceDecl()),
hasAncestor(functionDecl()),
hasAncestor(cxxRecordDecl()),
isTemplate,
isExpansionInSystemHeader())))
.bind("cxxRecordDecl"),
this);
matcher.addMatcher(namespaceDecl(hasParent(translationUnitDecl()),
unless(anyOf(isExpansionInSystemHeader(), isMacroOrInvalidLocation())))
.bind("namespaceDecl"),
this);
matcher.addMatcher(enumDecl(hasParent(translationUnitDecl()),
unless(anyOf(isExpansionInSystemHeader(), isMacroOrInvalidLocation())))
.bind("enumDecl"),
this);
}
//-----------------------------------------------------------------------------
void RecordDeclHandler::run(const MatchFinder::MatchResult& result)
{
if(const auto* cxxRecordDecl = result.Nodes.getNodeAs<CXXRecordDecl>("cxxRecordDecl")) {
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.GetString());
} else {
// We're just interested in the start location, -1 work(s|ed)
const auto startLoc =
GetSourceRangeAfterSemi(sourceRange, result, RequireSemi::No).getBegin().getLocWithOffset(-1);
mRewrite.InsertText(startLoc, outputFormatHelper.GetString());
}
} else if(const auto* namespaceDecl = result.Nodes.getNodeAs<NamespaceDecl>("namespaceDecl")) {
OutputFormatHelper outputFormatHelper{};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(namespaceDecl);
mRewrite.ReplaceText(GetSourceRangeAfterSemi(namespaceDecl->getSourceRange(), result, RequireSemi::No),
outputFormatHelper.GetString());
} else if(const auto* enumDecl = result.Nodes.getNodeAs<EnumDecl>("enumDecl")) {
OutputFormatHelper outputFormatHelper{};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(enumDecl);
mRewrite.ReplaceText(GetSourceRangeAfterSemi(enumDecl->getSourceRange(), result, RequireSemi::No),
outputFormatHelper.GetString());
}
}
//-----------------------------------------------------------------------------
} // namespace clang::insights