clang-tools  4.0.0
ImplicitCastInLoopCheck.cpp
Go to the documentation of this file.
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 
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 using namespace clang::ast_matchers;
19 
20 namespace clang {
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.
29 bool IsNonTrivialImplicitCast(const Stmt *ST) {
30  if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
31  return (ICE->getCastKind() != CK_NoOp) ||
32  IsNonTrivialImplicitCast(ICE->getSubExpr());
33  }
34  return false;
35 }
36 } // namespace
37 
38 void ImplicitCastInLoopCheck::registerMatchers(MatchFinder *Finder) {
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 
58 void ImplicitCastInLoopCheck::check(const MatchFinder::MatchResult &Result) {
59  const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
60  const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
61  const auto *OperatorCall =
62  Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call");
63 
64  if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
65  Init = Cleanup->getSubExpr();
66 
67  const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
68  if (!Materialized)
69  return;
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).
75  if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
76  ReportAndFix(Result.Context, VD, OperatorCall);
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[] =
87  "the type of the loop variable %0 is different from the one returned "
88  "by the iterator and generates an implicit cast; you can either "
89  "change the type to the correct one (%1 but 'const auto&' is always a "
90  "valid option) or remove the reference to make it explicit that you are "
91  "creating a new value";
92  diag(VD->getLocStart(), Message) << VD << ConstRefType;
93 }
94 
95 } // namespace performance
96 } // namespace tidy
97 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
const NamedDecl * Result
Definition: USRFinder.cpp:162