forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalVariableHandler.cpp
97 lines (78 loc) · 4.05 KB
/
GlobalVariableHandler.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
90
91
92
93
94
95
96
97
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#include <algorithm>
#include "CodeGenerator.h"
#include "GlobalVariableHandler.h"
#include "InsightsHelpers.h"
#include "InsightsMatchers.h"
#include "OutputFormatHelper.h"
//-----------------------------------------------------------------------------
using namespace clang;
using namespace clang::ast_matchers;
//-----------------------------------------------------------------------------
namespace clang::ast_matchers {
const internal::VariadicDynCastAllOfMatcher<Decl, VarTemplateSpecializationDecl> varTemplateSpecDecl; // NOLINT
} // namespace clang::ast_matchers
namespace clang::insights {
constexpr auto idVar{"varDevl"sv};
//-----------------------------------------------------------------------------
GlobalVariableHandler::GlobalVariableHandler(Rewriter& rewrite, MatchFinder& matcher)
: InsightsBase(rewrite)
{
matcher.addMatcher(
varDecl(hasThisTUParent,
unless(anyOf(
varTemplateSpecDecl(),
// A DecompositionDecl in global scope is different in the AST than one in a function for example.
// Try to find out whether this VarDecl is the result of a DecompositionDecl, if so bail out.
hasInitializer(ignoringImpCasts(
callExpr(hasAnyArgument(ignoringParenImpCasts(declRefExpr(to(decompositionDecl()))))))))))
.bind(idVar),
this);
}
//-----------------------------------------------------------------------------
void GlobalVariableHandler::run(const MatchFinder::MatchResult& result)
{
if(const auto* matchedDecl = result.Nodes.getNodeAs<VarDecl>(idVar)) {
OutputFormatHelper outputFormatHelper{};
// Take a preceding attribute into account
const auto sr = [&] {
auto tmpSr = GetSourceRangeAfterSemi(matchedDecl->getSourceRange(), result, RequireSemi::Yes);
const auto& attrs = matchedDecl->attrs();
SourceLocation start = tmpSr.getBegin();
std::for_each(
attrs.begin(), attrs.end(), [&](const auto* attr) { start = std::min(attr->getLocation(), start); });
return SourceRange{start, tmpSr.getEnd()};
}();
// Check whether we already have rewritten this location. If so, insert the text after the location. This is the
// case for:
// - an anonymous struct declared with TU as root
// - a out-of-line static member variable of a class template
if(not IsMacroLocation(matchedDecl->getSourceRange()) and (mRewrite.buffer_begin() != mRewrite.buffer_end()) &&
(isTemplateInstantiation(matchedDecl->getTemplateSpecializationKind()) or
IsAnonymousStructOrUnion(matchedDecl->getType()->getAsCXXRecordDecl()))) {
if(auto&& text = mRewrite.getRewrittenText(sr); not text.empty()) {
const char* data = result.SourceManager->getCharacterData(matchedDecl->getSourceRange().getBegin());
StringRef sref{data, static_cast<size_t>(std::distance(text.begin(), text.end()))};
if(not std::equal(text.begin(), text.end(), sref.begin(), sref.end())) {
outputFormatHelper.Append(std::move(text));
}
}
}
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(matchedDecl);
if(IsMacroLocation(sr)) {
// Special case for AnonymousStructInMacroTest.cpp (#290) where a macro gets expanded
mRewrite.ReplaceText(GetSM(result).getImmediateExpansionRange(matchedDecl->getSourceRange().getBegin()),
outputFormatHelper);
} else {
mRewrite.ReplaceText(sr, outputFormatHelper);
}
}
}
//-----------------------------------------------------------------------------
} // namespace clang::insights