Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 1 | //===--- ImplicitCastInLoopCheck.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 "ImplicitCastInLoopCheck.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 | |
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 | |
| 24 | namespace { |
| 25 | // Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp. |
| 26 | // The subtelty is that in some cases (user defined conversions), we can |
| 27 | // get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this |
| 28 | // case we skip the first cast expr. |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 29 | bool IsNonTrivialImplicitCast(const Stmt *ST) { |
| 30 | if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) { |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 31 | return (ICE->getCastKind() != CK_NoOp) || |
| 32 | IsNonTrivialImplicitCast(ICE->getSubExpr()); |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | } // namespace |
| 37 | |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 38 | void ImplicitCastInLoopCheck::registerMatchers(MatchFinder *Finder) { |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 39 | // We look for const ref loop variables that (optionally inside an |
| 40 | // ExprWithCleanup) materialize a temporary, and contain a implicit cast. The |
| 41 | // check on the implicit cast is done in check() because we can't access |
| 42 | // implicit cast subnode via matchers: has() skips casts and materialize! |
| 43 | // We also bind on the call to operator* to get the proper type in the |
| 44 | // diagnostic message. |
| 45 | // Note that when the implicit cast is done through a user defined cast |
| 46 | // operator, the node is a CXXMemberCallExpr, not a CXXOperatorCallExpr, so |
| 47 | // it should not get caught by the cxxOperatorCallExpr() matcher. |
| 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 | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 58 | void ImplicitCastInLoopCheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 59 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var"); |
| 60 | const auto *Init = Result.Nodes.getNodeAs<Expr>("init"); |
| 61 | const auto *OperatorCall = |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 62 | Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call"); |
| 63 | |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 64 | if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init)) |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 65 | Init = Cleanup->getSubExpr(); |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 66 | |
Alexander Kornienko | bfee5f7 | 2016-01-29 15:22:20 +0000 | [diff] [blame] | 67 | const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init); |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 68 | if (!Materialized) |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 69 | return; |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 70 | |
| 71 | // We ignore NoOp casts. Those are generated if the * operator on the |
| 72 | // iterator returns a value instead of a reference, and the loop variable |
| 73 | // is a reference. This situation is fine (it probably produces the same |
| 74 | // code at the end). |
Alexander Kornienko | 42e8cf4 | 2016-01-29 15:21:43 +0000 | [diff] [blame] | 75 | if (IsNonTrivialImplicitCast(Materialized->getTemporary())) |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 76 | ReportAndFix(Result.Context, VD, OperatorCall); |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void ImplicitCastInLoopCheck::ReportAndFix( |
| 80 | const ASTContext *Context, const VarDecl *VD, |
| 81 | const CXXOperatorCallExpr *OperatorCall) { |
| 82 | // We only match on const ref, so we should print a const ref version of the |
| 83 | // type. |
| 84 | QualType ConstType = OperatorCall->getType().withConst(); |
| 85 | QualType ConstRefType = Context->getLValueReferenceType(ConstType); |
| 86 | const char Message[] = |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 87 | "the type of the loop variable %0 is different from the one returned " |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 88 | "by the iterator and generates an implicit cast; you can either " |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 89 | "change the type to the correct one (%1 but 'const auto&' is always a " |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 90 | "valid option) or remove the reference to make it explicit that you are " |
| 91 | "creating a new value"; |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 92 | diag(VD->getLocStart(), Message) << VD << ConstRefType; |
Alexander Kornienko | 40d307d | 2016-01-29 15:21:32 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } // namespace performance |
| 96 | } // namespace tidy |
| 97 | } // namespace clang |