blob: 04dadfbfad6a457a11ab4e8e903c32ce50cc125f [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
18namespace clang {
19
20using namespace ast_matchers;
21
22namespace tidy {
23namespace performance {
24
25namespace {
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.
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000030bool IsNonTrivialImplicitCast(const Stmt *ST) {
31 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000032 return (ICE->getCastKind() != CK_NoOp) ||
33 IsNonTrivialImplicitCast(ICE->getSubExpr());
34 }
35 return false;
36}
37} // namespace
38
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000039void ImplicitCastInLoopCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko40d307d2016-01-29 15:21:32 +000040 // We look for const ref loop variables that (optionally inside an
41 // ExprWithCleanup) materialize a temporary, and contain a implicit cast. The
42 // check on the implicit cast is done in check() because we can't access
43 // implicit cast subnode via matchers: has() skips casts and materialize!
44 // We also bind on the call to operator* to get the proper type in the
45 // diagnostic message.
46 // Note that when the implicit cast is done through a user defined cast
47 // operator, the node is a CXXMemberCallExpr, not a CXXOperatorCallExpr, so
48 // it should not get caught by the cxxOperatorCallExpr() matcher.
49 Finder->addMatcher(
50 cxxForRangeStmt(hasLoopVariable(
51 varDecl(hasType(qualType(references(qualType(isConstQualified())))),
52 hasInitializer(expr(hasDescendant(cxxOperatorCallExpr().bind(
53 "operator-call")))
54 .bind("init")))
55 .bind("faulty-var"))),
56 this);
57}
58
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000059void ImplicitCastInLoopCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000060 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
61 const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
62 const auto *OperatorCall =
Alexander Kornienko40d307d2016-01-29 15:21:32 +000063 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call");
64
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000065 if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000066 Init = Cleanup->getSubExpr();
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000067
Alexander Kornienkobfee5f72016-01-29 15:22:20 +000068 const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
Alexander Kornienko42e8cf42016-01-29 15:21:43 +000069 if (!Materialized)
Alexander Kornienko40d307d2016-01-29 15:21:32 +000070 return;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000071
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 Kornienko42e8cf42016-01-29 15:21:43 +000076 if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
Alexander Kornienko40d307d2016-01-29 15:21:32 +000077 ReportAndFix(Result.Context, VD, OperatorCall);
Alexander Kornienko40d307d2016-01-29 15:21:32 +000078}
79
80void ImplicitCastInLoopCheck::ReportAndFix(
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 Kramera62e2232016-04-07 14:55:25 +000088 "the type of the loop variable %0 is different from the one returned "
Alexander Kornienko40d307d2016-01-29 15:21:32 +000089 "by the iterator and generates an implicit cast; you can either "
Benjamin Kramera62e2232016-04-07 14:55:25 +000090 "change the type to the correct one (%1 but 'const auto&' is always a "
Alexander Kornienko40d307d2016-01-29 15:21:32 +000091 "valid option) or remove the reference to make it explicit that you are "
92 "creating a new value";
Benjamin Kramera62e2232016-04-07 14:55:25 +000093 diag(VD->getLocStart(), Message) << VD << ConstRefType;
Alexander Kornienko40d307d2016-01-29 15:21:32 +000094}
95
96} // namespace performance
97} // namespace tidy
98} // namespace clang