10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 19 static constexpr llvm::StringLiteral
LoopName =
20 llvm::StringLiteral(
"forLoopName");
22 llvm::StringLiteral(
"loopVar");
24 llvm::StringLiteral(
"loopVarCast");
26 llvm::StringLiteral(
"loopUpperBound");
28 llvm::StringLiteral(
"loopIncrement");
30 TooSmallLoopVariableCheck::TooSmallLoopVariableCheck(StringRef
Name,
33 MagnitudeBitsUpperLimit(Options.get<unsigned>(
34 "MagnitudeBitsUpperLimit", 16)) {}
38 Options.
store(Opts,
"MagnitudeBitsUpperLimit", MagnitudeBitsUpperLimit);
55 StatementMatcher LoopVarMatcher =
57 ignoringParenImpCasts(declRefExpr(to(varDecl(hasType(isInteger()))))))
61 StatementMatcher LoopVarConversionMatcher =
62 implicitCastExpr(hasImplicitDestinationType(isInteger()),
63 has(ignoringParenImpCasts(LoopVarMatcher)))
64 .bind(LoopVarCastName);
68 StatementMatcher LoopBoundMatcher =
69 expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
70 unless(integerLiteral()),
71 unless(hasType(isConstQualified())),
72 unless(hasType(enumType())))))
73 .bind(LoopUpperBoundName);
77 StatementMatcher IncrementMatcher =
78 expr(ignoringParenImpCasts(hasType(isInteger()))).bind(LoopIncrementName);
83 binaryOperator(hasOperatorName(
"<"),
84 hasLHS(LoopVarConversionMatcher),
85 hasRHS(LoopBoundMatcher)),
86 binaryOperator(hasOperatorName(
"<="),
87 hasLHS(LoopVarConversionMatcher),
88 hasRHS(LoopBoundMatcher)),
89 binaryOperator(hasOperatorName(
">"), hasLHS(LoopBoundMatcher),
90 hasRHS(LoopVarConversionMatcher)),
91 binaryOperator(hasOperatorName(
">="), hasLHS(LoopBoundMatcher),
92 hasRHS(LoopVarConversionMatcher)))),
93 hasIncrement(IncrementMatcher))
100 const QualType &IntExprType) {
101 assert(IntExprType->isIntegerType());
103 return IntExprType->isUnsignedIntegerType()
104 ? Context.getIntWidth(IntExprType)
105 : Context.getIntWidth(IntExprType) - 1;
111 const Expr *UpperBound,
112 const QualType &UpperBoundType) {
115 if (
const auto *BinOperator = dyn_cast<BinaryOperator>(UpperBound)) {
116 const Expr *RHSE = BinOperator->getRHS()->IgnoreParenImpCasts();
117 const Expr *LHSE = BinOperator->getLHS()->IgnoreParenImpCasts();
119 QualType RHSEType = RHSE->getType();
120 QualType LHSEType = LHSE->getType();
122 if (!RHSEType->isIntegerType() || !LHSEType->isIntegerType())
125 bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
126 RHSEType.isConstQualified() ||
127 isa<IntegerLiteral>(RHSE);
128 bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
129 LHSEType.isConstQualified() ||
130 isa<IntegerLiteral>(LHSE);
133 if (RHSEIsConstantValue && LHSEIsConstantValue)
135 if (RHSEIsConstantValue)
137 if (LHSEIsConstantValue)
148 const auto *LoopVar = Result.Nodes.getNodeAs<Expr>(
LoopVarName);
149 const auto *UpperBound =
151 const auto *LoopIncrement =
155 if (LoopVar->getType() != LoopIncrement->getType())
158 QualType LoopVarType = LoopVar->getType();
159 QualType UpperBoundType = UpperBound->getType();
161 ASTContext &Context = *Result.Context;
164 unsigned UpperBoundMagnitudeBits =
167 if (UpperBoundMagnitudeBits == 0)
170 if (LoopVarMagnitudeBits > MagnitudeBitsUpperLimit)
173 if (LoopVarMagnitudeBits < UpperBoundMagnitudeBits)
174 diag(LoopVar->getBeginLoc(),
"loop variable has narrower type %0 than " 175 "iteration's upper bound %1")
176 << LoopVarType << UpperBoundType;
static constexpr llvm::StringLiteral LoopVarName
void registerMatchers(ast_matchers::MatchFinder *Finder) override
The matcher for loops with suspicious integer loop variable.
static constexpr llvm::StringLiteral LoopName
Base class for all clang-tidy checks.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static constexpr llvm::StringLiteral LoopVarCastName
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.
static constexpr llvm::StringLiteral LoopUpperBoundName
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static unsigned calcMagnitudeBits(const ASTContext &Context, const QualType &IntExprType)
Returns the magnitude bits of an integer type.
static unsigned 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...
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
static constexpr llvm::StringLiteral LoopIncrementName