-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerlLiteralFunctionCheck.cpp
More file actions
73 lines (63 loc) · 2.52 KB
/
Copy pathPerlLiteralFunctionCheck.cpp
File metadata and controls
73 lines (63 loc) · 2.52 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
#include "PerlCheck.h"
#include "PerlLiteralFunctionCheck.h"
#include <cassert>
using namespace clang;
using namespace clang::tidy;
using namespace clang::ast_matchers;
using namespace perl_check;
PerlLiteralFunctionCheck::PerlLiteralFunctionCheck(
StringRef Name, ClangTidyContext* Context,
StringRef PvnMacro_, StringRef PvsMacro_,
int LiteralArgNum_, int LengthArgNum_,
llvm::SmallVector<int> &&KeepArgs_) :
ClangTidyCheck(Name, Context),
UseMultiplicity(Options.getLocalOrGlobal("PerlCheckMultiplicity", 0)),
PvnMacro(PvnMacro_),
PvsMacro(PvsMacro_),
LiteralArgNum(LiteralArgNum_),
LengthArgNum(LengthArgNum_),
KeepArgs(KeepArgs_)
{
assert(KeepArgs.size() > 0);
}
void PerlLiteralFunctionCheck::registerMatchers(MatchFinder* Finder)
{
Finder->addMatcher(
callExpr(isExpandedFromMacro(PvnMacro),
unless(isExpandedFromMacro(PvsMacro)),
hasArgument(LiteralArgNum+UseMultiplicity,
traverse(TK_IgnoreUnlessSpelledInSource,
stringLiteral().bind("literal"))
),
hasArgument(LengthArgNum+UseMultiplicity,
expr().bind("size"))
).bind("call"), this);
}
void PerlLiteralFunctionCheck::check(const MatchFinder::MatchResult& Result)
{
const auto *matchedCall = Result.Nodes.getNodeAs<CallExpr>("call");
const auto *strLit = Result.Nodes.getNodeAs<StringLiteral>("literal");
const auto sizeArg = Result.Nodes.getNodeAs<Expr>("size");
Expr::EvalResult sizeIntResult;
if (!sizeArg->EvaluateAsInt(sizeIntResult, *Result.Context)
|| !sizeIntResult.Val.isInt())
return;
const auto sizeInt = sizeIntResult.Val.getInt();
if (sizeInt > strLit->getLength())
diag(matchedCall->getExprLoc(), "length too long for literal");
if (sizeInt != strLit->getLength())
return;
const LangOptions &Opts = getLangOpts();
auto argString = getArgText(matchedCall, Result, Opts, UseMultiplicity);
std::string repl;
llvm::raw_string_ostream srepl{repl};
srepl.reserveExtraSpace(80); // typically enough
srepl << PvsMacro << '(' << argString(KeepArgs[0]);
for (auto i = std::next(KeepArgs.begin()); i != KeepArgs.end(); ++i) {
srepl << ", " << argString(*i);
}
srepl << ')';
auto hint = FixItHint::CreateReplacement(matchedCall->getSourceRange(), repl);
diag(matchedCall->getExprLoc(), "%0() with literal better written as %1()")
<< PvnMacro << PvsMacro << hint;
}