TooSmallLoopVariableCheck.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //===--- TooSmallLoopVariableCheck.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 "TooSmallLoopVariableCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::bugprone {
  13. static constexpr llvm::StringLiteral LoopName =
  14. llvm::StringLiteral("forLoopName");
  15. static constexpr llvm::StringLiteral LoopVarName =
  16. llvm::StringLiteral("loopVar");
  17. static constexpr llvm::StringLiteral LoopVarCastName =
  18. llvm::StringLiteral("loopVarCast");
  19. static constexpr llvm::StringLiteral LoopUpperBoundName =
  20. llvm::StringLiteral("loopUpperBound");
  21. static constexpr llvm::StringLiteral LoopIncrementName =
  22. llvm::StringLiteral("loopIncrement");
  23. TooSmallLoopVariableCheck::TooSmallLoopVariableCheck(StringRef Name,
  24. ClangTidyContext *Context)
  25. : ClangTidyCheck(Name, Context),
  26. MagnitudeBitsUpperLimit(Options.get("MagnitudeBitsUpperLimit", 16U)) {}
  27. void TooSmallLoopVariableCheck::storeOptions(
  28. ClangTidyOptions::OptionMap &Opts) {
  29. Options.store(Opts, "MagnitudeBitsUpperLimit", MagnitudeBitsUpperLimit);
  30. }
  31. /// The matcher for loops with suspicious integer loop variable.
  32. ///
  33. /// In this general example, assuming 'j' and 'k' are of integral type:
  34. /// \code
  35. /// for (...; j < 3 + 2; ++k) { ... }
  36. /// \endcode
  37. /// The following string identifiers are bound to these parts of the AST:
  38. /// LoopVarName: 'j' (as a VarDecl)
  39. /// LoopVarCastName: 'j' (after implicit conversion)
  40. /// LoopUpperBoundName: '3 + 2' (as an Expr)
  41. /// LoopIncrementName: 'k' (as an Expr)
  42. /// LoopName: The entire for loop (as a ForStmt)
  43. ///
  44. void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
  45. StatementMatcher LoopVarMatcher =
  46. expr(
  47. ignoringParenImpCasts(declRefExpr(to(varDecl(hasType(isInteger()))))))
  48. .bind(LoopVarName);
  49. // We need to catch only those comparisons which contain any integer cast.
  50. StatementMatcher LoopVarConversionMatcher = traverse(
  51. TK_AsIs, implicitCastExpr(hasImplicitDestinationType(isInteger()),
  52. has(ignoringParenImpCasts(LoopVarMatcher)))
  53. .bind(LoopVarCastName));
  54. // We are interested in only those cases when the loop bound is a variable
  55. // value (not const, enum, etc.).
  56. StatementMatcher LoopBoundMatcher =
  57. expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
  58. unless(integerLiteral()),
  59. unless(hasType(isConstQualified())),
  60. unless(hasType(enumType())))))
  61. .bind(LoopUpperBoundName);
  62. // We use the loop increment expression only to make sure we found the right
  63. // loop variable.
  64. StatementMatcher IncrementMatcher =
  65. expr(ignoringParenImpCasts(hasType(isInteger()))).bind(LoopIncrementName);
  66. Finder->addMatcher(
  67. forStmt(
  68. hasCondition(anyOf(
  69. binaryOperator(hasOperatorName("<"),
  70. hasLHS(LoopVarConversionMatcher),
  71. hasRHS(LoopBoundMatcher)),
  72. binaryOperator(hasOperatorName("<="),
  73. hasLHS(LoopVarConversionMatcher),
  74. hasRHS(LoopBoundMatcher)),
  75. binaryOperator(hasOperatorName(">"), hasLHS(LoopBoundMatcher),
  76. hasRHS(LoopVarConversionMatcher)),
  77. binaryOperator(hasOperatorName(">="), hasLHS(LoopBoundMatcher),
  78. hasRHS(LoopVarConversionMatcher)))),
  79. hasIncrement(IncrementMatcher))
  80. .bind(LoopName),
  81. this);
  82. }
  83. /// Returns the magnitude bits of an integer type.
  84. static unsigned calcMagnitudeBits(const ASTContext &Context,
  85. const QualType &IntExprType) {
  86. assert(IntExprType->isIntegerType());
  87. return IntExprType->isUnsignedIntegerType()
  88. ? Context.getIntWidth(IntExprType)
  89. : Context.getIntWidth(IntExprType) - 1;
  90. }
  91. /// Calculate the upper bound expression's magnitude bits, but ignore
  92. /// constant like values to reduce false positives.
  93. static unsigned calcUpperBoundMagnitudeBits(const ASTContext &Context,
  94. const Expr *UpperBound,
  95. const QualType &UpperBoundType) {
  96. // Ignore casting caused by constant values inside a binary operator.
  97. // We are interested in variable values' magnitude bits.
  98. if (const auto *BinOperator = dyn_cast<BinaryOperator>(UpperBound)) {
  99. const Expr *RHSE = BinOperator->getRHS()->IgnoreParenImpCasts();
  100. const Expr *LHSE = BinOperator->getLHS()->IgnoreParenImpCasts();
  101. QualType RHSEType = RHSE->getType();
  102. QualType LHSEType = LHSE->getType();
  103. if (!RHSEType->isIntegerType() || !LHSEType->isIntegerType())
  104. return 0;
  105. bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
  106. RHSEType.isConstQualified() ||
  107. isa<IntegerLiteral>(RHSE);
  108. bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
  109. LHSEType.isConstQualified() ||
  110. isa<IntegerLiteral>(LHSE);
  111. // Avoid false positives produced by two constant values.
  112. if (RHSEIsConstantValue && LHSEIsConstantValue)
  113. return 0;
  114. if (RHSEIsConstantValue)
  115. return calcMagnitudeBits(Context, LHSEType);
  116. if (LHSEIsConstantValue)
  117. return calcMagnitudeBits(Context, RHSEType);
  118. return std::max(calcMagnitudeBits(Context, LHSEType),
  119. calcMagnitudeBits(Context, RHSEType));
  120. }
  121. return calcMagnitudeBits(Context, UpperBoundType);
  122. }
  123. void TooSmallLoopVariableCheck::check(const MatchFinder::MatchResult &Result) {
  124. const auto *LoopVar = Result.Nodes.getNodeAs<Expr>(LoopVarName);
  125. const auto *UpperBound =
  126. Result.Nodes.getNodeAs<Expr>(LoopUpperBoundName)->IgnoreParenImpCasts();
  127. const auto *LoopIncrement =
  128. Result.Nodes.getNodeAs<Expr>(LoopIncrementName)->IgnoreParenImpCasts();
  129. // We matched the loop variable incorrectly.
  130. if (LoopVar->getType() != LoopIncrement->getType())
  131. return;
  132. QualType LoopVarType = LoopVar->getType();
  133. QualType UpperBoundType = UpperBound->getType();
  134. ASTContext &Context = *Result.Context;
  135. unsigned LoopVarMagnitudeBits = calcMagnitudeBits(Context, LoopVarType);
  136. unsigned UpperBoundMagnitudeBits =
  137. calcUpperBoundMagnitudeBits(Context, UpperBound, UpperBoundType);
  138. if (UpperBoundMagnitudeBits == 0)
  139. return;
  140. if (LoopVarMagnitudeBits > MagnitudeBitsUpperLimit)
  141. return;
  142. if (LoopVarMagnitudeBits < UpperBoundMagnitudeBits)
  143. diag(LoopVar->getBeginLoc(), "loop variable has narrower type %0 than "
  144. "iteration's upper bound %1")
  145. << LoopVarType << UpperBoundType;
  146. }
  147. } // namespace clang::tidy::bugprone