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(hasType(isInteger()),
86 unless(integerLiteral()),
87 unless(hasType(isConstQualified())),
88 unless(hasType(enumType())))))
93 StatementMatcher IncrementMatcher =
99 binaryOperator(hasOperatorName(
"<"),
100 hasLHS(LoopVarConversionMatcher),
101 hasRHS(LoopBoundMatcher)),
102 binaryOperator(hasOperatorName(
"<="),
103 hasLHS(LoopVarConversionMatcher),
104 hasRHS(LoopBoundMatcher)),
105 binaryOperator(hasOperatorName(
">"), hasLHS(LoopBoundMatcher),
106 hasRHS(LoopVarConversionMatcher)),
107 binaryOperator(hasOperatorName(
">="), hasLHS(LoopBoundMatcher),
108 hasRHS(LoopVarConversionMatcher)))),
109 hasIncrement(IncrementMatcher))
116 const QualType &IntExprType,
117 const Expr *IntExpr) {
118 assert(IntExprType->isIntegerType());
120 unsigned SignedBits = IntExprType->isUnsignedIntegerType() ? 0U : 1U;
122 if (
const auto *BitField = IntExpr->getSourceBitField()) {
123 unsigned BitFieldWidth = BitField->getBitWidthValue(Context);
127 unsigned IntWidth = Context.getIntWidth(IntExprType);
128 return {IntWidth - SignedBits, 0U};
135 const QualType &UpperBoundType) {
138 if (
const auto *BinOperator = dyn_cast<BinaryOperator>(UpperBound)) {
139 const Expr *RHSE = BinOperator->getRHS()->IgnoreParenImpCasts();
140 const Expr *LHSE = BinOperator->getLHS()->IgnoreParenImpCasts();
142 QualType RHSEType = RHSE->getType();
143 QualType LHSEType = LHSE->getType();
145 if (!RHSEType->isIntegerType() || !LHSEType->isIntegerType())
148 bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
149 RHSEType.isConstQualified() ||
150 isa<IntegerLiteral>(RHSE);
151 bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
152 LHSEType.isConstQualified() ||
153 isa<IntegerLiteral>(LHSE);
156 if (RHSEIsConstantValue && LHSEIsConstantValue)
158 if (RHSEIsConstantValue)
160 if (LHSEIsConstantValue)
171 const MagnitudeBits &
Info) {
172 std::string
Name =
Type.getAsString();
173 if (!
Info.BitFieldWidth)
177 Name += std::to_string(
Info.BitFieldWidth);
182 const auto *LoopVar = Result.Nodes.getNodeAs<Expr>(
LoopVarName);
183 const auto *UpperBound =
185 const auto *LoopIncrement =
189 if (LoopVar->getType() != LoopIncrement->getType())
192 ASTContext &Context = *Result.Context;
194 const QualType LoopVarType = LoopVar->getType();
195 const MagnitudeBits LoopVarMagnitudeBits =
198 const MagnitudeBits LoopIncrementMagnitudeBits =
201 if (LoopIncrementMagnitudeBits != LoopVarMagnitudeBits)
204 const QualType UpperBoundType = UpperBound->getType();
205 const MagnitudeBits UpperBoundMagnitudeBits =
208 if ((0U == UpperBoundMagnitudeBits.WidthWithoutSignBit) ||
209 (LoopVarMagnitudeBits.WidthWithoutSignBit > MagnitudeBitsUpperLimit) ||
210 (LoopVarMagnitudeBits.WidthWithoutSignBit >=
211 UpperBoundMagnitudeBits.WidthWithoutSignBit))
214 diag(LoopVar->getBeginLoc(),
215 "loop variable has narrower type '%0' than iteration's upper bound '%1'")
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.
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