ImplicitConversionInLoopCheck.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===//
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "ImplicitConversionInLoopCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/AST/Decl.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/ASTMatchers/ASTMatchers.h"
  13. #include "clang/Lex/Lexer.h"
  14. using namespace clang::ast_matchers;
  15. namespace clang::tidy::performance {
  16. // Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp.
  17. // The subtlety is that in some cases (user defined conversions), we can
  18. // get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this
  19. // case we skip the first cast expr.
  20. static bool isNonTrivialImplicitCast(const Stmt *ST) {
  21. if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
  22. return (ICE->getCastKind() != CK_NoOp) ||
  23. isNonTrivialImplicitCast(ICE->getSubExpr());
  24. }
  25. return false;
  26. }
  27. void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {
  28. // We look for const ref loop variables that (optionally inside an
  29. // ExprWithCleanup) materialize a temporary, and contain a implicit
  30. // conversion. The check on the implicit conversion is done in check() because
  31. // we can't access implicit conversion subnode via matchers: has() skips casts
  32. // and materialize! We also bind on the call to operator* to get the proper
  33. // type in the diagnostic message. We use both cxxOperatorCallExpr for user
  34. // defined operator and unaryOperator when the iterator is a pointer, like
  35. // for arrays or std::array.
  36. //
  37. // Note that when the implicit conversion is done through a user defined
  38. // conversion operator, the node is a CXXMemberCallExpr, not a
  39. // CXXOperatorCallExpr, so it should not get caught by the
  40. // cxxOperatorCallExpr() matcher.
  41. Finder->addMatcher(
  42. traverse(
  43. TK_AsIs,
  44. cxxForRangeStmt(hasLoopVariable(
  45. varDecl(
  46. hasType(qualType(references(qualType(isConstQualified())))),
  47. hasInitializer(
  48. expr(anyOf(
  49. hasDescendant(
  50. cxxOperatorCallExpr().bind("operator-call")),
  51. hasDescendant(unaryOperator(hasOperatorName("*"))
  52. .bind("operator-call"))))
  53. .bind("init")))
  54. .bind("faulty-var")))),
  55. this);
  56. }
  57. void ImplicitConversionInLoopCheck::check(
  58. 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<Expr>("operator-call");
  63. if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
  64. Init = Cleanup->getSubExpr();
  65. const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
  66. if (!Materialized)
  67. return;
  68. // We ignore NoOp casts. Those are generated if the * operator on the
  69. // iterator returns a value instead of a reference, and the loop variable
  70. // is a reference. This situation is fine (it probably produces the same
  71. // code at the end).
  72. if (isNonTrivialImplicitCast(Materialized->getSubExpr()))
  73. reportAndFix(Result.Context, VD, OperatorCall);
  74. }
  75. void ImplicitConversionInLoopCheck::reportAndFix(const ASTContext *Context,
  76. const VarDecl *VD,
  77. const Expr *OperatorCall) {
  78. // We only match on const ref, so we should print a const ref version of the
  79. // type.
  80. QualType ConstType = OperatorCall->getType().withConst();
  81. QualType ConstRefType = Context->getLValueReferenceType(ConstType);
  82. const char Message[] =
  83. "the type of the loop variable %0 is different from the one returned "
  84. "by the iterator and generates an implicit conversion; you can either "
  85. "change the type to the matching one (%1 but 'const auto&' is always a "
  86. "valid option) or remove the reference to make it explicit that you are "
  87. "creating a new value";
  88. diag(VD->getBeginLoc(), Message) << VD << ConstRefType;
  89. }
  90. } // namespace clang::tidy::performance