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 | // |
| 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 Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame^] | 10 | #include "ImplicitConversionInLoopCheck.h" |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 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 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 18 | using namespace clang::ast_matchers; |
| 19 | |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 20 | namespace clang { |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 21 | namespace tidy { |
| 22 | namespace performance { |
| 23 | |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 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. |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame^] | 28 | static bool IsNonTrivialImplicitCast(const Stmt *ST) { |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 29 | if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) { |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 30 | return (ICE->getCastKind() != CK_NoOp) || |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 31 | IsNonTrivialImplicitCast(ICE->getSubExpr()); |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 32 | } |
| 33 | return false; |
| 34 | } |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 35 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame^] | 36 | void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) { |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 37 | // We look for const ref loop variables that (optionally inside an |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame^] | 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. |
| 43 | // |
| 44 | // Note that when the implicit conversion is done through a user defined |
| 45 | // conversion operator, the node is a CXXMemberCallExpr, not a |
| 46 | // CXXOperatorCallExpr, so it should not get caught by the |
| 47 | // cxxOperatorCallExpr() matcher. |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 48 | Finder->addMatcher( |
| 49 | cxxForRangeStmt(hasLoopVariable( |
| 50 | varDecl(hasType(qualType(references(qualType(isConstQualified())))), |
| 51 | hasInitializer(expr(hasDescendant(cxxOperatorCallExpr().bind( |
| 52 | "operator-call"))) |
| 53 | .bind("init"))) |
| 54 | .bind("faulty-var"))), |
| 55 | this); |
| 56 | } |
| 57 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame^] | 58 | void ImplicitConversionInLoopCheck::check( |
| 59 | const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 60 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var"); |
| 61 | const auto *Init = Result.Nodes.getNodeAs<Expr>("init"); |
| 62 | const auto *OperatorCall = |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 63 | Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call"); |
| 64 | |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 65 | if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init)) |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 66 | Init = Cleanup->getSubExpr(); |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 67 | |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 68 | const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init); |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 69 | if (!Materialized) |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 70 | return; |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 71 | |
| 72 | // We ignore NoOp casts. Those are generated if the * operator on the |
| 73 | // iterator returns a value instead of a reference, and the loop variable |
| 74 | // is a reference. This situation is fine (it probably produces the same |
| 75 | // code at the end). |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 76 | if (IsNonTrivialImplicitCast(Materialized->getTemporary())) |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 77 | ReportAndFix(Result.Context, VD, OperatorCall); |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame^] | 80 | void ImplicitConversionInLoopCheck::ReportAndFix( |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 81 | const ASTContext *Context, const VarDecl *VD, |
| 82 | const CXXOperatorCallExpr *OperatorCall) { |
| 83 | // We only match on const ref, so we should print a const ref version of the |
| 84 | // type. |
| 85 | QualType ConstType = OperatorCall->getType().withConst(); |
| 86 | QualType ConstRefType = Context->getLValueReferenceType(ConstType); |
| 87 | const char Message[] = |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 88 | "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^] | 89 | "by the iterator and generates an implicit conversion; you can either " |
| 90 | "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] | 91 | "valid option) or remove the reference to make it explicit that you are " |
| 92 | "creating a new value"; |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 93 | diag(VD->getLocStart(), Message) << VD << ConstRefType; |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace performance |
| 97 | } // namespace tidy |
| 98 | } // namespace clang |