102 lines · cpp
1//===----------------------------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "ImplicitConversionInLoopCheck.h"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 17using namespace clang::ast_matchers;18 19namespace clang::tidy::performance {20 21// Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp.22// The subtlety is that in some cases (user defined conversions), we can23// get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this24// case we skip the first cast expr.25static bool isNonTrivialImplicitCast(const Stmt *ST) {26 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {27 return (ICE->getCastKind() != CK_NoOp) ||28 isNonTrivialImplicitCast(ICE->getSubExpr());29 }30 return false;31}32 33void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {34 // We look for const ref loop variables that (optionally inside an35 // ExprWithCleanup) materialize a temporary, and contain a implicit36 // conversion. The check on the implicit conversion is done in check() because37 // we can't access implicit conversion subnode via matchers: has() skips casts38 // and materialize! We also bind on the call to operator* to get the proper39 // type in the diagnostic message. We use both cxxOperatorCallExpr for user40 // defined operator and unaryOperator when the iterator is a pointer, like41 // for arrays or std::array.42 //43 // Note that when the implicit conversion is done through a user defined44 // conversion operator, the node is a CXXMemberCallExpr, not a45 // CXXOperatorCallExpr, so it should not get caught by the46 // cxxOperatorCallExpr() matcher.47 Finder->addMatcher(48 traverse(49 TK_AsIs,50 cxxForRangeStmt(hasLoopVariable(51 varDecl(52 hasType(qualType(references(qualType(isConstQualified())))),53 hasInitializer(54 expr(anyOf(55 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 64void 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 = Result.Nodes.getNodeAs<Expr>("operator-call");69 70 if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))71 Init = Cleanup->getSubExpr();72 73 const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);74 if (!Materialized)75 return;76 77 // We ignore NoOp casts. Those are generated if the * operator on the78 // iterator returns a value instead of a reference, and the loop variable79 // is a reference. This situation is fine (it probably produces the same80 // code at the end).81 if (isNonTrivialImplicitCast(Materialized->getSubExpr()))82 reportAndFix(Result.Context, VD, OperatorCall);83}84 85void ImplicitConversionInLoopCheck::reportAndFix(const ASTContext *Context,86 const VarDecl *VD,87 const Expr *OperatorCall) {88 // We only match on const ref, so we should print a const ref version of the89 // type.90 const QualType ConstType = OperatorCall->getType().withConst();91 const QualType ConstRefType = Context->getLValueReferenceType(ConstType);92 const char Message[] =93 "the type of the loop variable %0 is different from the one returned "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 "96 "valid option) or remove the reference to make it explicit that you are "97 "creating a new value";98 diag(VD->getBeginLoc(), Message) << VD << ConstRefType;99}100 101} // namespace clang::tidy::performance102