2017-08-08 22:53:52 +08:00
|
|
|
//===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===//
|
2016-01-29 23:21:32 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
#include "ImplicitConversionInLoopCheck.h"
|
2016-01-29 23:21:32 +08:00
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
using namespace clang::ast_matchers;
|
2016-01-29 23:21:32 +08:00
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace clang {
|
2016-01-29 23:21:32 +08:00
|
|
|
namespace tidy {
|
|
|
|
namespace performance {
|
|
|
|
|
|
|
|
// Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp.
|
|
|
|
// The subtelty is that in some cases (user defined conversions), we can
|
|
|
|
// get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this
|
|
|
|
// case we skip the first cast expr.
|
2017-08-08 22:53:52 +08:00
|
|
|
static bool IsNonTrivialImplicitCast(const Stmt *ST) {
|
2016-01-29 23:22:20 +08:00
|
|
|
if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
|
2016-01-29 23:21:32 +08:00
|
|
|
return (ICE->getCastKind() != CK_NoOp) ||
|
2018-06-11 20:46:48 +08:00
|
|
|
IsNonTrivialImplicitCast(ICE->getSubExpr());
|
2016-01-29 23:21:32 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {
|
2016-01-29 23:21:32 +08:00
|
|
|
// We look for const ref loop variables that (optionally inside an
|
2017-08-08 22:53:52 +08:00
|
|
|
// ExprWithCleanup) materialize a temporary, and contain a implicit
|
|
|
|
// conversion. The check on the implicit conversion is done in check() because
|
|
|
|
// we can't access implicit conversion subnode via matchers: has() skips casts
|
|
|
|
// and materialize! We also bind on the call to operator* to get the proper
|
2018-06-11 20:46:48 +08:00
|
|
|
// type in the diagnostic message. We use both cxxOperatorCallExpr for user
|
|
|
|
// defined operator and unaryOperator when the iterator is a pointer, like
|
|
|
|
// for arrays or std::array.
|
2017-08-08 22:53:52 +08:00
|
|
|
//
|
|
|
|
// Note that when the implicit conversion is done through a user defined
|
|
|
|
// conversion operator, the node is a CXXMemberCallExpr, not a
|
|
|
|
// CXXOperatorCallExpr, so it should not get caught by the
|
|
|
|
// cxxOperatorCallExpr() matcher.
|
2016-01-29 23:21:32 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
cxxForRangeStmt(hasLoopVariable(
|
2018-06-11 20:46:48 +08:00
|
|
|
varDecl(
|
|
|
|
hasType(qualType(references(qualType(isConstQualified())))),
|
|
|
|
hasInitializer(
|
|
|
|
expr(anyOf(hasDescendant(
|
|
|
|
cxxOperatorCallExpr().bind("operator-call")),
|
|
|
|
hasDescendant(unaryOperator(hasOperatorName("*"))
|
|
|
|
.bind("operator-call"))))
|
|
|
|
.bind("init")))
|
2016-01-29 23:21:32 +08:00
|
|
|
.bind("faulty-var"))),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitConversionInLoopCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2016-01-29 23:22:20 +08:00
|
|
|
const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
|
|
|
|
const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
|
|
|
|
const auto *OperatorCall =
|
2018-06-11 20:46:48 +08:00
|
|
|
Result.Nodes.getNodeAs<Expr>("operator-call");
|
2016-01-29 23:21:32 +08:00
|
|
|
|
2016-01-29 23:22:20 +08:00
|
|
|
if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
|
2016-01-29 23:21:32 +08:00
|
|
|
Init = Cleanup->getSubExpr();
|
2016-01-29 23:21:43 +08:00
|
|
|
|
2016-01-29 23:22:20 +08:00
|
|
|
const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
|
2016-01-29 23:21:43 +08:00
|
|
|
if (!Materialized)
|
2016-01-29 23:21:32 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We ignore NoOp casts. Those are generated if the * operator on the
|
|
|
|
// iterator returns a value instead of a reference, and the loop variable
|
|
|
|
// is a reference. This situation is fine (it probably produces the same
|
|
|
|
// code at the end).
|
2016-01-29 23:21:43 +08:00
|
|
|
if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
|
2016-01-29 23:21:32 +08:00
|
|
|
ReportAndFix(Result.Context, VD, OperatorCall);
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitConversionInLoopCheck::ReportAndFix(
|
2016-01-29 23:21:32 +08:00
|
|
|
const ASTContext *Context, const VarDecl *VD,
|
2018-06-11 20:46:48 +08:00
|
|
|
const Expr *OperatorCall) {
|
2016-01-29 23:21:32 +08:00
|
|
|
// We only match on const ref, so we should print a const ref version of the
|
|
|
|
// type.
|
|
|
|
QualType ConstType = OperatorCall->getType().withConst();
|
|
|
|
QualType ConstRefType = Context->getLValueReferenceType(ConstType);
|
|
|
|
const char Message[] =
|
2016-04-07 22:55:25 +08:00
|
|
|
"the type of the loop variable %0 is different from the one returned "
|
2017-08-08 22:53:52 +08:00
|
|
|
"by the iterator and generates an implicit conversion; you can either "
|
|
|
|
"change the type to the matching one (%1 but 'const auto&' is always a "
|
2016-01-29 23:21:32 +08:00
|
|
|
"valid option) or remove the reference to make it explicit that you are "
|
|
|
|
"creating a new value";
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(VD->getBeginLoc(), Message) << VD << ConstRefType;
|
2016-01-29 23:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace performance
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|