clang-tools 18.0.0git
TooSmallLoopVariableCheck.cpp
Go to the documentation of this file.
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17static constexpr llvm::StringLiteral LoopName =
18 llvm::StringLiteral("forLoopName");
19static constexpr llvm::StringLiteral LoopVarName =
20 llvm::StringLiteral("loopVar");
21static constexpr llvm::StringLiteral LoopVarCastName =
22 llvm::StringLiteral("loopVarCast");
23static constexpr llvm::StringLiteral LoopUpperBoundName =
24 llvm::StringLiteral("loopUpperBound");
25static constexpr llvm::StringLiteral LoopIncrementName =
26 llvm::StringLiteral("loopIncrement");
27
28namespace {
29
30struct MagnitudeBits {
31 unsigned WidthWithoutSignBit = 0U;
32 unsigned BitFieldWidth = 0U;
33
34 bool operator<(const MagnitudeBits &Other) const noexcept {
35 return WidthWithoutSignBit < Other.WidthWithoutSignBit;
36 }
37
38 bool operator!=(const MagnitudeBits &Other) const noexcept {
39 return WidthWithoutSignBit != Other.WidthWithoutSignBit ||
40 BitFieldWidth != Other.BitFieldWidth;
41 }
42};
43
44} // namespace
45
47 ClangTidyContext *Context)
48 : ClangTidyCheck(Name, Context),
49 MagnitudeBitsUpperLimit(Options.get("MagnitudeBitsUpperLimit", 16U)) {}
50
53 Options.store(Opts, "MagnitudeBitsUpperLimit", MagnitudeBitsUpperLimit);
54}
55
56/// The matcher for loops with suspicious integer loop variable.
57///
58/// In this general example, assuming 'j' and 'k' are of integral type:
59/// \code
60/// for (...; j < 3 + 2; ++k) { ... }
61/// \endcode
62/// The following string identifiers are bound to these parts of the AST:
63/// LoopVarName: 'j' (as a VarDecl)
64/// LoopVarCastName: 'j' (after implicit conversion)
65/// LoopUpperBoundName: '3 + 2' (as an Expr)
66/// LoopIncrementName: 'k' (as an Expr)
67/// LoopName: The entire for loop (as a ForStmt)
68///
70 StatementMatcher LoopVarMatcher =
71 expr(ignoringParenImpCasts(
72 anyOf(declRefExpr(to(varDecl(hasType(isInteger())))),
73 memberExpr(member(fieldDecl(hasType(isInteger())))))))
74 .bind(LoopVarName);
75
76 // We need to catch only those comparisons which contain any integer cast.
77 StatementMatcher LoopVarConversionMatcher = traverse(
78 TK_AsIs, implicitCastExpr(hasImplicitDestinationType(isInteger()),
79 has(ignoringParenImpCasts(LoopVarMatcher)))
80 .bind(LoopVarCastName));
81
82 // We are interested in only those cases when the loop bound is a variable
83 // value (not const, enum, etc.).
84 StatementMatcher LoopBoundMatcher =
85 expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
86 unless(integerLiteral()),
87 unless(hasType(isConstQualified())),
88 unless(hasType(enumType())))))
89 .bind(LoopUpperBoundName);
90
91 // We use the loop increment expression only to make sure we found the right
92 // loop variable.
93 StatementMatcher IncrementMatcher =
94 expr(ignoringParenImpCasts(hasType(isInteger()))).bind(LoopIncrementName);
95
96 Finder->addMatcher(
97 forStmt(
98 hasCondition(anyOf(
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))
110 .bind(LoopName),
111 this);
112}
113
114/// Returns the magnitude bits of an integer type.
115static MagnitudeBits calcMagnitudeBits(const ASTContext &Context,
116 const QualType &IntExprType,
117 const Expr *IntExpr) {
118 assert(IntExprType->isIntegerType());
119
120 unsigned SignedBits = IntExprType->isUnsignedIntegerType() ? 0U : 1U;
121
122 if (const auto *BitField = IntExpr->getSourceBitField()) {
123 unsigned BitFieldWidth = BitField->getBitWidthValue(Context);
124 return {BitFieldWidth - SignedBits, BitFieldWidth};
125 }
126
127 unsigned IntWidth = Context.getIntWidth(IntExprType);
128 return {IntWidth - SignedBits, 0U};
129}
130
131/// Calculate the upper bound expression's magnitude bits, but ignore
132/// constant like values to reduce false positives.
133static MagnitudeBits
134calcUpperBoundMagnitudeBits(const ASTContext &Context, const Expr *UpperBound,
135 const QualType &UpperBoundType) {
136 // Ignore casting caused by constant values inside a binary operator.
137 // We are interested in variable values' magnitude bits.
138 if (const auto *BinOperator = dyn_cast<BinaryOperator>(UpperBound)) {
139 const Expr *RHSE = BinOperator->getRHS()->IgnoreParenImpCasts();
140 const Expr *LHSE = BinOperator->getLHS()->IgnoreParenImpCasts();
141
142 QualType RHSEType = RHSE->getType();
143 QualType LHSEType = LHSE->getType();
144
145 if (!RHSEType->isIntegerType() || !LHSEType->isIntegerType())
146 return {};
147
148 bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
149 RHSEType.isConstQualified() ||
150 isa<IntegerLiteral>(RHSE);
151 bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
152 LHSEType.isConstQualified() ||
153 isa<IntegerLiteral>(LHSE);
154
155 // Avoid false positives produced by two constant values.
156 if (RHSEIsConstantValue && LHSEIsConstantValue)
157 return {};
158 if (RHSEIsConstantValue)
159 return calcMagnitudeBits(Context, LHSEType, LHSE);
160 if (LHSEIsConstantValue)
161 return calcMagnitudeBits(Context, RHSEType, RHSE);
162
163 return std::max(calcMagnitudeBits(Context, LHSEType, LHSE),
164 calcMagnitudeBits(Context, RHSEType, RHSE));
165 }
166
167 return calcMagnitudeBits(Context, UpperBoundType, UpperBound);
168}
169
170static std::string formatIntegralType(const QualType &Type,
171 const MagnitudeBits &Info) {
172 std::string Name = Type.getAsString();
173 if (!Info.BitFieldWidth)
174 return Name;
175
176 Name += ':';
177 Name += std::to_string(Info.BitFieldWidth);
178 return Name;
179}
180
181void TooSmallLoopVariableCheck::check(const MatchFinder::MatchResult &Result) {
182 const auto *LoopVar = Result.Nodes.getNodeAs<Expr>(LoopVarName);
183 const auto *UpperBound =
184 Result.Nodes.getNodeAs<Expr>(LoopUpperBoundName)->IgnoreParenImpCasts();
185 const auto *LoopIncrement =
186 Result.Nodes.getNodeAs<Expr>(LoopIncrementName)->IgnoreParenImpCasts();
187
188 // We matched the loop variable incorrectly.
189 if (LoopVar->getType() != LoopIncrement->getType())
190 return;
191
192 ASTContext &Context = *Result.Context;
193
194 const QualType LoopVarType = LoopVar->getType();
195 const MagnitudeBits LoopVarMagnitudeBits =
196 calcMagnitudeBits(Context, LoopVarType, LoopVar);
197
198 const MagnitudeBits LoopIncrementMagnitudeBits =
199 calcMagnitudeBits(Context, LoopIncrement->getType(), LoopIncrement);
200 // We matched the loop variable incorrectly.
201 if (LoopIncrementMagnitudeBits != LoopVarMagnitudeBits)
202 return;
203
204 const QualType UpperBoundType = UpperBound->getType();
205 const MagnitudeBits UpperBoundMagnitudeBits =
206 calcUpperBoundMagnitudeBits(Context, UpperBound, UpperBoundType);
207
208 if ((0U == UpperBoundMagnitudeBits.WidthWithoutSignBit) ||
209 (LoopVarMagnitudeBits.WidthWithoutSignBit > MagnitudeBitsUpperLimit) ||
210 (LoopVarMagnitudeBits.WidthWithoutSignBit >=
211 UpperBoundMagnitudeBits.WidthWithoutSignBit))
212 return;
213
214 diag(LoopVar->getBeginLoc(),
215 "loop variable has narrower type '%0' than iteration's upper bound '%1'")
216 << formatIntegralType(LoopVarType, LoopVarMagnitudeBits)
217 << formatIntegralType(UpperBoundType, UpperBoundMagnitudeBits);
218}
219
220} // namespace clang::tidy::bugprone
FunctionInfo Info
NodeType Type
llvm::StringRef Name
unsigned BitFieldWidth
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