| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 1 | //===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===// | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 |  | 
| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 9 | #include "ImplicitConversionInLoopCheck.h" | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 10 |  | 
|  | 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 Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 17 | using namespace clang::ast_matchers; | 
|  | 18 |  | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 19 | namespace clang { | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 20 | namespace tidy { | 
|  | 21 | namespace performance { | 
|  | 22 |  | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 23 | // 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 Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 27 | static bool IsNonTrivialImplicitCast(const Stmt *ST) { | 
| Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 28 | if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) { | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 29 | return (ICE->getCastKind() != CK_NoOp) || | 
| Alexander Kornienko | 5f8ede4 | 2018-06-11 12:46:48 +0000 | [diff] [blame] | 30 | IsNonTrivialImplicitCast(ICE->getSubExpr()); | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 31 | } | 
|  | 32 | return false; | 
|  | 33 | } | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 34 |  | 
| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 35 | void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) { | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 36 | // We look for const ref loop variables that (optionally inside an | 
| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 37 | // 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 Kornienko | 5f8ede4 | 2018-06-11 12:46:48 +0000 | [diff] [blame] | 41 | // 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 Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 44 | // | 
|  | 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 Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 49 | Finder->addMatcher( | 
|  | 50 | cxxForRangeStmt(hasLoopVariable( | 
| Alexander Kornienko | 5f8ede4 | 2018-06-11 12:46:48 +0000 | [diff] [blame] | 51 | 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 Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 59 | .bind("faulty-var"))), | 
|  | 60 | this); | 
|  | 61 | } | 
|  | 62 |  | 
| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 63 | void ImplicitConversionInLoopCheck::check( | 
|  | 64 | const MatchFinder::MatchResult &Result) { | 
| Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 65 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var"); | 
|  | 66 | const auto *Init = Result.Nodes.getNodeAs<Expr>("init"); | 
|  | 67 | const auto *OperatorCall = | 
| Alexander Kornienko | 5f8ede4 | 2018-06-11 12:46:48 +0000 | [diff] [blame] | 68 | Result.Nodes.getNodeAs<Expr>("operator-call"); | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 69 |  | 
| Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 70 | if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init)) | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 71 | Init = Cleanup->getSubExpr(); | 
| Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 72 |  | 
| Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 73 | const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init); | 
| Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 74 | if (!Materialized) | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 75 | return; | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 76 |  | 
|  | 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). | 
| Nico Weber | c9276fb | 2019-11-17 02:09:25 -0500 | [diff] [blame^] | 81 | if (IsNonTrivialImplicitCast(Materialized->getTemporary())) | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 82 | ReportAndFix(Result.Context, VD, OperatorCall); | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 85 | void ImplicitConversionInLoopCheck::ReportAndFix( | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 86 | const ASTContext *Context, const VarDecl *VD, | 
| Alexander Kornienko | 5f8ede4 | 2018-06-11 12:46:48 +0000 | [diff] [blame] | 87 | const Expr *OperatorCall) { | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 88 | // 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 Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 93 | "the type of the loop variable %0 is different from the one returned " | 
| Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 94 | "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 Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 96 | "valid option) or remove the reference to make it explicit that you are " | 
|  | 97 | "creating a new value"; | 
| Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 98 | diag(VD->getBeginLoc(), Message) << VD << ConstRefType; | 
| Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
|  | 101 | } // namespace performance | 
|  | 102 | } // namespace tidy | 
|  | 103 | } // namespace clang |