blob: 3fcfdba5033a4e8a2d0cde36d17e52ef4c2f8adc [file] [log] [blame]
Alexander Kornienko40d307d2016-01-29 15:21:32 +00001//===--- 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 Bergeron456177b2016-05-02 18:00:29 +000018using namespace clang::ast_matchers;
19
Alexander Kornienko40d307d2016-01-29 15:21:32 +000020namespace clang {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000021namespace tidy {
22namespace performance {
23
24namespace {
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 Kornienkobfee5f72016-01-29 15:22:20 +000029bool IsNonTrivialImplicitCast(const Stmt *ST) {
30 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000031 return (ICE->getCastKind() != CK_NoOp) ||
32 IsNonTrivialImplicitCast(ICE->getSubExpr());
33 }
34 return false;
35}
36} // namespace
37
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000038void ImplicitCastInLoopCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000039 // 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 Kornienko42e8cf42016-01-29 15:21:43 +000058void ImplicitCastInLoopCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000059 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
60 const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
61 const auto *OperatorCall =
Alexander Kornienko40d307d2016-01-29 15:21:32 +000062 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call");
63
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000064 if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000065 Init = Cleanup->getSubExpr();
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000066
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000067 const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000068 if (!Materialized)
Alexander Kornienko40d307d2016-01-29 15:21:32 +000069 return;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000070
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 Kornienko42e8cf42016-01-29 15:21:43 +000075 if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000076 ReportAndFix(Result.Context, VD, OperatorCall);
Alexander Kornienko40d307d2016-01-29 15:21:32 +000077}
78
79void 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 Kramera62e2232016-04-07 14:55:25 +000087 "the type of the loop variable %0 is different from the one returned "
Alexander Kornienko40d307d2016-01-29 15:21:32 +000088 "by the iterator and generates an implicit cast; you can either "
Benjamin Kramera62e2232016-04-07 14:55:25 +000089 "change the type to the correct one (%1 but 'const auto&' is always a "
Alexander Kornienko40d307d2016-01-29 15:21:32 +000090 "valid option) or remove the reference to make it explicit that you are "
91 "creating a new value";
Benjamin Kramera62e2232016-04-07 14:55:25 +000092 diag(VD->getLocStart(), Message) << VD << ConstRefType;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000093}
94
95} // namespace performance
96} // namespace tidy
97} // namespace clang