blob: e3b90d3b4279b579551cb18395f91f8e1558af7a [file] [log] [blame]
Alexander Kornienkof1a65522017-08-08 14:53:52 +00001//===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===//
Alexander Kornienko40d307d2016-01-29 15:21:32 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexander Kornienko40d307d2016-01-29 15:21:32 +00006//
7//===----------------------------------------------------------------------===//
8
Alexander Kornienkof1a65522017-08-08 14:53:52 +00009#include "ImplicitConversionInLoopCheck.h"
Alexander Kornienko40d307d2016-01-29 15:21:32 +000010
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/ASTMatchers/ASTMatchers.h"
15#include "clang/Lex/Lexer.h"
16
Etienne Bergeron456177b2016-05-02 18:00:29 +000017using namespace clang::ast_matchers;
18
Alexander Kornienko40d307d2016-01-29 15:21:32 +000019namespace clang {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000020namespace tidy {
21namespace performance {
22
Alexander Kornienko40d307d2016-01-29 15:21:32 +000023// Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp.
24// The subtelty is that in some cases (user defined conversions), we can
25// get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this
26// case we skip the first cast expr.
Alexander Kornienkof1a65522017-08-08 14:53:52 +000027static bool IsNonTrivialImplicitCast(const Stmt *ST) {
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000028 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000029 return (ICE->getCastKind() != CK_NoOp) ||
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000030 IsNonTrivialImplicitCast(ICE->getSubExpr());
Alexander Kornienko40d307d2016-01-29 15:21:32 +000031 }
32 return false;
33}
Alexander Kornienko40d307d2016-01-29 15:21:32 +000034
Alexander Kornienkof1a65522017-08-08 14:53:52 +000035void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000036 // We look for const ref loop variables that (optionally inside an
Alexander Kornienkof1a65522017-08-08 14:53:52 +000037 // ExprWithCleanup) materialize a temporary, and contain a implicit
38 // conversion. The check on the implicit conversion is done in check() because
39 // we can't access implicit conversion subnode via matchers: has() skips casts
40 // and materialize! We also bind on the call to operator* to get the proper
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000041 // type in the diagnostic message. We use both cxxOperatorCallExpr for user
42 // defined operator and unaryOperator when the iterator is a pointer, like
43 // for arrays or std::array.
Alexander Kornienkof1a65522017-08-08 14:53:52 +000044 //
45 // Note that when the implicit conversion is done through a user defined
46 // conversion operator, the node is a CXXMemberCallExpr, not a
47 // CXXOperatorCallExpr, so it should not get caught by the
48 // cxxOperatorCallExpr() matcher.
Alexander Kornienko40d307d2016-01-29 15:21:32 +000049 Finder->addMatcher(
50 cxxForRangeStmt(hasLoopVariable(
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000051 varDecl(
52 hasType(qualType(references(qualType(isConstQualified())))),
53 hasInitializer(
54 expr(anyOf(hasDescendant(
55 cxxOperatorCallExpr().bind("operator-call")),
56 hasDescendant(unaryOperator(hasOperatorName("*"))
57 .bind("operator-call"))))
58 .bind("init")))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000059 .bind("faulty-var"))),
60 this);
61}
62
Alexander Kornienkof1a65522017-08-08 14:53:52 +000063void ImplicitConversionInLoopCheck::check(
64 const MatchFinder::MatchResult &Result) {
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000065 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
66 const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
67 const auto *OperatorCall =
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000068 Result.Nodes.getNodeAs<Expr>("operator-call");
Alexander Kornienko40d307d2016-01-29 15:21:32 +000069
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000070 if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000071 Init = Cleanup->getSubExpr();
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000072
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000073 const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000074 if (!Materialized)
Alexander Kornienko40d307d2016-01-29 15:21:32 +000075 return;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000076
77 // We ignore NoOp casts. Those are generated if the * operator on the
78 // iterator returns a value instead of a reference, and the loop variable
79 // is a reference. This situation is fine (it probably produces the same
80 // code at the end).
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000081 if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000082 ReportAndFix(Result.Context, VD, OperatorCall);
Alexander Kornienko40d307d2016-01-29 15:21:32 +000083}
84
Alexander Kornienkof1a65522017-08-08 14:53:52 +000085void ImplicitConversionInLoopCheck::ReportAndFix(
Alexander Kornienko40d307d2016-01-29 15:21:32 +000086 const ASTContext *Context, const VarDecl *VD,
Alexander Kornienko5f8ede42018-06-11 12:46:48 +000087 const Expr *OperatorCall) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000088 // We only match on const ref, so we should print a const ref version of the
89 // type.
90 QualType ConstType = OperatorCall->getType().withConst();
91 QualType ConstRefType = Context->getLValueReferenceType(ConstType);
92 const char Message[] =
Benjamin Kramera62e2232016-04-07 14:55:25 +000093 "the type of the loop variable %0 is different from the one returned "
Alexander Kornienkof1a65522017-08-08 14:53:52 +000094 "by the iterator and generates an implicit conversion; you can either "
95 "change the type to the matching one (%1 but 'const auto&' is always a "
Alexander Kornienko40d307d2016-01-29 15:21:32 +000096 "valid option) or remove the reference to make it explicit that you are "
97 "creating a new value";
Stephen Kelly43465bf2018-08-09 22:42:26 +000098 diag(VD->getBeginLoc(), Message) << VD << ConstRefType;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000099}
100
101} // namespace performance
102} // namespace tidy
103} // namespace clang