1 //===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===//
2 //
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
10 #include "ImplicitConversionInLoopCheck.h"
11
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
18 using namespace clang::ast_matchers;
19
20 namespace clang {
21 namespace tidy {
22 namespace performance {
23
24 // 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.
IsNonTrivialImplicitCast(const Stmt * ST)28 static bool IsNonTrivialImplicitCast(const Stmt *ST) {
29 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
30 return (ICE->getCastKind() != CK_NoOp) ||
31 IsNonTrivialImplicitCast(ICE->getSubExpr());
32 }
33 return false;
34 }
35
registerMatchers(MatchFinder * Finder)36 void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {
37 // We look for const ref loop variables that (optionally inside an
38 // 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
42 // 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.
45 //
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.
50 Finder->addMatcher(
51 cxxForRangeStmt(hasLoopVariable(
52 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")))
60 .bind("faulty-var"))),
61 this);
62 }
63
check(const MatchFinder::MatchResult & Result)64 void ImplicitConversionInLoopCheck::check(
65 const MatchFinder::MatchResult &Result) {
66 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
67 const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
68 const auto *OperatorCall =
69 Result.Nodes.getNodeAs<Expr>("operator-call");
70
71 if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
72 Init = Cleanup->getSubExpr();
73
74 const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
75 if (!Materialized)
76 return;
77
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).
82 if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
83 ReportAndFix(Result.Context, VD, OperatorCall);
84 }
85
ReportAndFix(const ASTContext * Context,const VarDecl * VD,const Expr * OperatorCall)86 void ImplicitConversionInLoopCheck::ReportAndFix(
87 const ASTContext *Context, const VarDecl *VD,
88 const Expr *OperatorCall) {
89 // 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[] =
94 "the type of the loop variable %0 is different from the one returned "
95 "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 "
97 "valid option) or remove the reference to make it explicit that you are "
98 "creating a new value";
99 diag(VD->getBeginLoc(), Message) << VD << ConstRefType;
100 }
101
102 } // namespace performance
103 } // namespace tidy
104 } // namespace clang
105