Repository navigation
Expand file tree
/
Copy pathStaticHandler.cpp
More file actions
67 lines (54 loc) · 2.82 KB
/
Copy pathStaticHandler.cpp
File metadata and controls
67 lines (54 loc) · 2.82 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
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#include "StaticHandler.h"
#include "CodeGenerator.h"
#include "InsightsHelpers.h"
#include "InsightsMatchers.h"
#include "InsightsStaticStrings.h"
#include "OutputFormatHelper.h"
//-----------------------------------------------------------------------------
using namespace clang;
using namespace clang::ast_matchers;
//-----------------------------------------------------------------------------
namespace clang::insights {
StaticHandler::StaticHandler(Rewriter& rewrite, MatchFinder& matcher)
: InsightsBase(rewrite)
{
static const auto localStatic = varDecl(allOf(isStaticLocal(),
// if the type is trivial destructible we are fine
unless(hasType(cxxRecordDecl(hasTrivialDestructor()))),
unless(isExpansionInSystemHeader()),
unless(isMacroOrInvalidLocation()),
unless(isTemplate),
hasDescendant(cxxConstructExpr())))
.bind("varDecl");
matcher.addMatcher(
functionDecl(hasDescendant(localStatic), unless(hasDescendant(returnStmt()))).bind("functionDecl"), this);
matcher.addMatcher(functionDecl(hasDescendant(localStatic), forEachDescendant(returnStmt().bind("returnStmt")))
.bind("functionDecl"),
this);
}
//-----------------------------------------------------------------------------
void StaticHandler::run(const MatchFinder::MatchResult& result)
{
const auto* vd = result.Nodes.getNodeAs<VarDecl>("varDecl");
auto& sm = GetSM(result);
auto columnNr = sm.getSpellingColumnNumber(vd->getLocStart()) - 1;
OutputFormatHelper outputFormatHelper{columnNr};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(vd);
mRewrite.ReplaceText(vd->getSourceRange(), outputFormatHelper.GetString());
if(const auto* returnStmt = result.Nodes.getNodeAs<ReturnStmt>("returnStmt")) {
auto returnStmtColumnNr = sm.getSpellingColumnNumber(returnStmt->getLocStart()) - 1;
OutputFormatHelper ofm{returnStmtColumnNr};
CodeGenerator codeGenerator{ofm};
codeGenerator.InsertArg(returnStmt);
mRewrite.ReplaceText(returnStmt->getSourceRange(), ofm.GetString());
}
}
//-----------------------------------------------------------------------------
} // namespace clang::insights