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