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(
"MagnitudeBitsUpperLimit", 16U)) {}
37 Options.
store(Opts,
"MagnitudeBitsUpperLimit", MagnitudeBitsUpperLimit);
54 StatementMatcher LoopVarMatcher =
56 ignoringParenImpCasts(declRefExpr(to(varDecl(hasType(isInteger()))))))
60 StatementMatcher LoopVarConversionMatcher =
61 traverse(ast_type_traits::TK_AsIs,
62 implicitCastExpr(hasImplicitDestinationType(isInteger()),
63 has(ignoringParenImpCasts(LoopVarMatcher)))
68 StatementMatcher LoopBoundMatcher =
69 expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
70 unless(integerLiteral()),
71 unless(hasType(isConstQualified())),
72 unless(hasType(enumType())))))
77 StatementMatcher IncrementMatcher =
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;