10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
18 llvm::StringLiteral(
"forLoopName");
20 llvm::StringLiteral(
"loopVar");
22 llvm::StringLiteral(
"loopVarCast");
24 llvm::StringLiteral(
"loopUpperBound");
26 llvm::StringLiteral(
"loopIncrement");
34 bool operator<(
const MagnitudeBits &Other)
const noexcept {
38 bool operator!=(
const MagnitudeBits &Other)
const noexcept {
49 MagnitudeBitsUpperLimit(Options.get(
"MagnitudeBitsUpperLimit", 16U)) {}
53 Options.
store(Opts,
"MagnitudeBitsUpperLimit", MagnitudeBitsUpperLimit);
70 StatementMatcher LoopVarMatcher =
71 expr(ignoringParenImpCasts(
72 anyOf(declRefExpr(to(varDecl(hasType(isInteger())))),
73 memberExpr(member(fieldDecl(hasType(isInteger())))))))
77 StatementMatcher LoopVarConversionMatcher = traverse(
78 TK_AsIs, implicitCastExpr(hasImplicitDestinationType(isInteger()),
79 has(ignoringParenImpCasts(LoopVarMatcher)))
84 StatementMatcher LoopBoundMatcher =
85 expr(ignoringParenImpCasts(allOf(
86 hasType(isInteger()), unless(integerLiteral()),
88 hasType(isConstQualified()),
89 declRefExpr(to(varDecl(anyOf(
90 hasInitializer(ignoringParenImpCasts(integerLiteral())),
91 isConstexpr(), isConstinit())))))),
92 unless(hasType(enumType())))))
97 StatementMatcher IncrementMatcher =
103 binaryOperator(hasOperatorName(
"<"),
104 hasLHS(LoopVarConversionMatcher),
105 hasRHS(LoopBoundMatcher)),
106 binaryOperator(hasOperatorName(
"<="),
107 hasLHS(LoopVarConversionMatcher),
108 hasRHS(LoopBoundMatcher)),
109 binaryOperator(hasOperatorName(
">"), hasLHS(LoopBoundMatcher),
110 hasRHS(LoopVarConversionMatcher)),
111 binaryOperator(hasOperatorName(
">="), hasLHS(LoopBoundMatcher),
112 hasRHS(LoopVarConversionMatcher)))),
113 hasIncrement(IncrementMatcher))
120 const QualType &IntExprType,
121 const Expr *IntExpr) {
122 assert(IntExprType->isIntegerType());
124 unsigned SignedBits = IntExprType->isUnsignedIntegerType() ? 0U : 1U;
126 if (
const auto *BitField = IntExpr->getSourceBitField()) {
127 unsigned BitFieldWidth = BitField->getBitWidthValue(Context);
131 unsigned IntWidth = Context.getIntWidth(IntExprType);
132 return {IntWidth - SignedBits, 0U};
139 const QualType &UpperBoundType) {
142 if (
const auto *BinOperator = dyn_cast<BinaryOperator>(UpperBound)) {
143 const Expr *RHSE = BinOperator->getRHS()->IgnoreParenImpCasts();
144 const Expr *LHSE = BinOperator->getLHS()->IgnoreParenImpCasts();
146 QualType RHSEType = RHSE->getType();
147 QualType LHSEType = LHSE->getType();
149 if (!RHSEType->isIntegerType() || !LHSEType->isIntegerType())
152 bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
153 RHSEType.isConstQualified() ||
154 isa<IntegerLiteral>(RHSE);
155 bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
156 LHSEType.isConstQualified() ||
157 isa<IntegerLiteral>(LHSE);
160 if (RHSEIsConstantValue && LHSEIsConstantValue)
162 if (RHSEIsConstantValue)
164 if (LHSEIsConstantValue)
175 const MagnitudeBits &
Info) {
176 std::string
Name =
Type.getAsString();
177 if (!
Info.BitFieldWidth)
181 Name += std::to_string(
Info.BitFieldWidth);
186 const auto *LoopVar = Result.Nodes.getNodeAs<Expr>(
LoopVarName);
187 const auto *UpperBound =
189 const auto *LoopIncrement =
193 if (LoopVar->getType() != LoopIncrement->getType())
196 ASTContext &Context = *Result.Context;
198 const QualType LoopVarType = LoopVar->getType();
199 const MagnitudeBits LoopVarMagnitudeBits =
202 const MagnitudeBits LoopIncrementMagnitudeBits =
205 if (LoopIncrementMagnitudeBits != LoopVarMagnitudeBits)
208 const QualType UpperBoundType = UpperBound->getType();
209 const MagnitudeBits UpperBoundMagnitudeBits =
212 if ((0U == UpperBoundMagnitudeBits.WidthWithoutSignBit) ||
213 (LoopVarMagnitudeBits.WidthWithoutSignBit > MagnitudeBitsUpperLimit) ||
214 (LoopVarMagnitudeBits.WidthWithoutSignBit >=
215 UpperBoundMagnitudeBits.WidthWithoutSignBit))
218 diag(LoopVar->getBeginLoc(),
219 "loop variable has narrower type '%0' than iteration's upper bound '%1'")
llvm::SmallString< 256U > Name
unsigned WidthWithoutSignBit
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
TooSmallLoopVariableCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
The matcher for loops with suspicious integer loop variable.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
bool operator!=(const SymbolRange &LHS, const SymbolRange &RHS)
static MagnitudeBits calcMagnitudeBits(const ASTContext &Context, const QualType &IntExprType, const Expr *IntExpr)
Returns the magnitude bits of an integer type.
static std::string formatIntegralType(const QualType &Type, const MagnitudeBits &Info)
static constexpr llvm::StringLiteral LoopVarName
static constexpr llvm::StringLiteral LoopIncrementName
static constexpr llvm::StringLiteral LoopUpperBoundName
static MagnitudeBits calcUpperBoundMagnitudeBits(const ASTContext &Context, const Expr *UpperBound, const QualType &UpperBoundType)
Calculate the upper bound expression's magnitude bits, but ignore constant like values to reduce fals...
static constexpr llvm::StringLiteral LoopVarCastName
static constexpr llvm::StringLiteral LoopName
llvm::StringMap< ClangTidyValue > OptionMap