forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplicitCastHandler.cpp
60 lines (49 loc) · 2.44 KB
/
ImplicitCastHandler.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
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#include "ImplicitCastHandler.h"
#include "CodeGenerator.h"
#include "DPrint.h"
#include "InsightsMatchers.h"
#include "OutputFormatHelper.h"
//-----------------------------------------------------------------------------
using namespace clang;
using namespace clang::ast_matchers;
//-----------------------------------------------------------------------------
namespace clang::insights {
ImplicitCastHandler::ImplicitCastHandler(Rewriter& rewrite, MatchFinder& matcher)
: InsightsBase(rewrite)
{
static const auto implicitCastMatch =
anyOf(isExpansionInSystemHeader(),
isMacroOrInvalidLocation(),
isTemplate,
hasAncestor(functionDecl()),
hasAncestor(userDefinedLiteral()),
hasAncestor(implicitCastExpr(hasMatchingCast())), /* will be catch by the walk down */
/* we do not like to introduce casts in parameter decls. This happens when
we have a template parameter with a base class. CharLiteralTest.cpp */
hasAncestor(parmVarDecl()));
matcher.addMatcher(implicitCastExpr(unless(implicitCastMatch), hasMatchingCast()).bind("implicitCast"), this);
}
//-----------------------------------------------------------------------------
void ImplicitCastHandler::run(const MatchFinder::MatchResult& result)
{
const auto* implCastExpr = result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCast");
// we get the same ImplictCast matched twice. Unknown why. This give a little possible background:
// https://lists.llvm.org/pipermail/cfe-dev/2017-June/054205.html
// FIXME for now keep track of what we already have seen and bail out of something appears twice
SKIP_IF_ALREADY_SEEN(implCastExpr);
OutputFormatHelper outputFormatHelper{};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(implCastExpr);
if(!outputFormatHelper.GetString().empty()) {
DPrint("repllacement: %s\n", outputFormatHelper.GetString());
mRewrite.ReplaceText(implCastExpr->getSourceRange(), outputFormatHelper.GetString());
}
}
//-----------------------------------------------------------------------------
} // namespace clang::insights