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