blob: 6e731190113f339f67ec2915457a73780f169d53 [file] [log] [blame]
Alexander Kornienkof1a65522017-08-08 14:53:52 +00001//===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===//
Alexander Kornienko40d307d2016-01-29 15:21:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alexander Kornienkof1a65522017-08-08 14:53:52 +000010#include "ImplicitConversionInLoopCheck.h"
Alexander Kornienko40d307d2016-01-29 15:21:32 +000011
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/Decl.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/Lex/Lexer.h"
17
Etienne Bergeron456177b2016-05-02 18:00:29 +000018using namespace clang::ast_matchers;
19
Alexander Kornienko40d307d2016-01-29 15:21:32 +000020namespace clang {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000021namespace tidy {
22namespace performance {
23
Alexander Kornienko40d307d2016-01-29 15:21:32 +000024// Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp.
25// The subtelty is that in some cases (user defined conversions), we can
26// get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this
27// case we skip the first cast expr.
Alexander Kornienkof1a65522017-08-08 14:53:52 +000028static bool IsNonTrivialImplicitCast(const Stmt *ST) {
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000029 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000030 return (ICE->getCastKind() != CK_NoOp) ||
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000031 IsNonTrivialImplicitCast(ICE->getSubExpr());
Alexander Kornienko40d307d2016-01-29 15:21:32 +000032 }
33 return false;
34}
Alexander Kornienko40d307d2016-01-29 15:21:32 +000035
Alexander Kornienkof1a65522017-08-08 14:53:52 +000036void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000037 // We look for const ref loop variables that (optionally inside an
Alexander Kornienkof1a65522017-08-08 14:53:52 +000038 // ExprWithCleanup) materialize a temporary, and contain a implicit
39 // conversion. The check on the implicit conversion is done in check() because
40 // we can't access implicit conversion subnode via matchers: has() skips casts
41 // and materialize! We also bind on the call to operator* to get the proper
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000042 // type in the diagnostic message. We use both cxxOperatorCallExpr for user
43 // defined operator and unaryOperator when the iterator is a pointer, like
44 // for arrays or std::array.
Alexander Kornienkof1a65522017-08-08 14:53:52 +000045 //
46 // Note that when the implicit conversion is done through a user defined
47 // conversion operator, the node is a CXXMemberCallExpr, not a
48 // CXXOperatorCallExpr, so it should not get caught by the
49 // cxxOperatorCallExpr() matcher.
Alexander Kornienko40d307d2016-01-29 15:21:32 +000050 Finder->addMatcher(
51 cxxForRangeStmt(hasLoopVariable(
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000052 varDecl(
53 hasType(qualType(references(qualType(isConstQualified())))),
54 hasInitializer(
55 expr(anyOf(hasDescendant(
56 cxxOperatorCallExpr().bind("operator-call")),
57 hasDescendant(unaryOperator(hasOperatorName("*"))
58 .bind("operator-call"))))
59 .bind("init")))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000060 .bind("faulty-var"))),
61 this);
62}
63
Alexander Kornienkof1a65522017-08-08 14:53:52 +000064void ImplicitConversionInLoopCheck::check(
65 const MatchFinder::MatchResult &Result) {
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000066 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
67 const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
68 const auto *OperatorCall =
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000069 Result.Nodes.getNodeAs<Expr>("operator-call");
Alexander Kornienko40d307d2016-01-29 15:21:32 +000070
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000071 if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000072 Init = Cleanup->getSubExpr();
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000073
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000074 const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000075 if (!Materialized)
Alexander Kornienko40d307d2016-01-29 15:21:32 +000076 return;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000077
78 // We ignore NoOp casts. Those are generated if the * operator on the
79 // iterator returns a value instead of a reference, and the loop variable
80 // is a reference. This situation is fine (it probably produces the same
81 // code at the end).
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000082 if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000083 ReportAndFix(Result.Context, VD, OperatorCall);
Alexander Kornienko40d307d2016-01-29 15:21:32 +000084}
85
Alexander Kornienkof1a65522017-08-08 14:53:52 +000086void ImplicitConversionInLoopCheck::ReportAndFix(
Alexander Kornienko40d307d2016-01-29 15:21:32 +000087 const ASTContext *Context, const VarDecl *VD,
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000088 const Expr *OperatorCall) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000089 // We only match on const ref, so we should print a const ref version of the
90 // type.
91 QualType ConstType = OperatorCall->getType().withConst();
92 QualType ConstRefType = Context->getLValueReferenceType(ConstType);
93 const char Message[] =
Benjamin Kramera62e2232016-04-07 14:55:25 +000094 "the type of the loop variable %0 is different from the one returned "
Alexander Kornienkof1a65522017-08-08 14:53:52 +000095 "by the iterator and generates an implicit conversion; you can either "
96 "change the type to the matching one (%1 but 'const auto&' is always a "
Alexander Kornienko40d307d2016-01-29 15:21:32 +000097 "valid option) or remove the reference to make it explicit that you are "
98 "creating a new value";
Stephen Kelly43465bf2018-08-09 22:42:26 +000099 diag(VD->getBeginLoc(), Message) << VD << ConstRefType;
Alexander Kornienko40d307d2016-01-29 15:21:32 +0000100}
101
102} // namespace performance
103} // namespace tidy
104} // namespace clang