21 using namespace clang;
29 enum Kind { Normal, Unrolled } K;
34 : K(InK), LoopStmt(S), LCtx(L), maxStep(N) {}
50 return K == X.K && LoopStmt == X.LoopStmt;
54 ID.AddPointer(LoopStmt);
56 ID.AddInteger(maxStep);
72 return S && (isa<ForStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S));
76 auto LS = State->get<LoopStack>();
77 if (!LS.isEmpty() && LS.getHead().getLoopStmt() == LoopStmt)
78 State = State->set<LoopStack>(LS.getTail());
84 hasOperatorName(
"<="), hasOperatorName(
">="),
85 hasOperatorName(
"!=")),
87 to(
varDecl(hasType(isInteger())).bind(BindName))))),
90 .bind(
"conditionOperator");
93 static internal::Matcher<Stmt>
97 hasUnaryOperand(ignoringParenImpCasts(
100 hasLHS(ignoringParenImpCasts(
104 static internal::Matcher<Stmt>
106 return callExpr(forEachArgumentWithParam(
111 static internal::Matcher<Stmt>
115 hasInitializer(
anyOf(
120 static internal::Matcher<Stmt>
123 hasOperatorName(
"&"),
147 equalsBoundNode(
"initVarName"))))),
149 equalsBoundNode(
"initVarName"))))),
150 hasRHS(ignoringParenImpCasts(
155 anyOf(hasOperatorName(
"++"), hasOperatorName(
"--")),
158 hasType(isInteger())))))))),
174 if (
const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
175 for (
const Decl *D : DS->decls()) {
177 if (D->getCanonicalDecl() == VD)
194 llvm_unreachable(
"Reached root without finding the declaration of VD");
209 auto CounterVar = Matches[0].getNodeAs<
VarDecl>(
"initVarName");
210 llvm::APInt BoundNum =
212 llvm::APInt InitNum =
214 auto CondOp = Matches[0].getNodeAs<
BinaryOperator>(
"conditionOperator");
215 if (InitNum.getBitWidth() != BoundNum.getBitWidth()) {
216 InitNum = InitNum.zextOrSelf(BoundNum.getBitWidth());
217 BoundNum = BoundNum.zextOrSelf(InitNum.getBitWidth());
220 if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
221 maxStep = (BoundNum - InitNum + 1).abs().getZExtValue();
223 maxStep = (BoundNum - InitNum).
abs().getZExtValue();
230 const Stmt *S =
nullptr;
237 S = BE->getBlock()->getTerminatorStmt();
245 llvm_unreachable(
"Reached root without encountering the previous step");
257 auto LS =
State->get<LoopStack>();
258 if (!LS.isEmpty() && LoopStmt == LS.getHead().getLoopStmt() &&
259 LCtx == LS.getHead().getLocationContext()) {
260 if (LS.getHead().isUnrolled() &&
madeNewBranch(Pred, LoopStmt)) {
262 State = State->add<LoopStack>(
274 unsigned outerStep = (LS.isEmpty() ? 1 : LS.getHead().getMaxStep());
276 unsigned innerMaxStep = maxStep * outerStep;
281 State = State->add<LoopStack>(
287 auto LS = State->get<LoopStack>();
288 if (LS.isEmpty() || !LS.getHead().isUnrolled())
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
static LoopState getUnrolled(const Stmt *S, const LocationContext *L, unsigned N)
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned &maxStep)
Stmt - This represents one statement.
internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, internal::Matcher< Decl >, void(internal::HasDeclarationSupportedTypes)> hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
Decl - This represents one declaration (or definition), e.g.
__DEVICE__ long long abs(long long __n)
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
const ProgramStateRef & getState() const
Represents a variable declaration or definition.
ASTContext & getASTContext() const
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
bool isUnrolledState(ProgramStateRef State)
Returns if the given State indicates that is inside a completely unrolled loop.
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
ProgramStateRef processLoopEnd(const Stmt *LoopStmt, ProgramStateRef State)
Updates the given ProgramState.
const internal::VariadicDynCastAllOfMatcher< Stmt, GotoStmt > gotoStmt
Matches goto statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclStmt > declStmt
Matches declaration statements.
unsigned succ_size() const
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
ProgramStateRef updateLoopStack(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned maxVisitOnPath)
Updates the stack of loops contained by the ProgramState.
const LocationContext * getLocationContext() const
A builtin binary operation expression such as "x + y" or "x <= y".
ExplodedNode * getFirstPred()
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const LocationContext * getLocationContext() const
static internal::Matcher< Stmt > assignedToRef(internal::Matcher< Decl > VarNodeMatcher)
static const int MAXIMUM_STEP_UNROLLED
const internal::VariadicDynCastAllOfMatcher< Stmt, SwitchStmt > switchStmt
Matches switch statements.
#define REGISTER_LIST_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable list type NameTy, suitable for placement into the ProgramState.
static SVal getValue(SVal val, SValBuilder &svalBuilder)
const internal::VariadicDynCastAllOfMatcher< Stmt, ReturnStmt > returnStmt
Matches return statements.
const Stmt * getLoopStmt() const
static const Stmt * getStmt(const ExplodedNode *N)
Given an exploded node, retrieve the statement that should be used for the diagnostic location...
const internal::VariadicDynCastAllOfMatcher< Decl, ParmVarDecl > parmVarDecl
Matches parameter variable declarations.
unsigned getMaxStep() const
bool madeNewBranch(ExplodedNode *N, const Stmt *LoopStmt)
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
const internal::VariadicDynCastAllOfMatcher< Stmt, IntegerLiteral > integerLiteral
Matches integer literals of all sizes / encodings, e.g.
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
const internal::VariadicAllOfMatcher< QualType > qualType
Matches QualTypes in the clang AST.
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
const internal::VariadicDynCastAllOfMatcher< Stmt, ForStmt > forStmt
Matches for statements.
Dataflow Directional Tag Classes.
static LoopState getNormal(const Stmt *S, const LocationContext *L, unsigned N)
static bool isLoopStmt(const Stmt *S)
const internal::VariadicDynCastAllOfMatcher< Stmt, InitListExpr > initListExpr
Matches init list expressions.
static internal::Matcher< Stmt > forLoopMatcher()
internal::Matcher< BinaryOperator > hasEitherOperand(const internal::Matcher< Expr > &InnerMatcher)
Matches if either the left hand side or the right hand side of a binary operator matches.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryOperator > unaryOperator
Matches unary operator expressions.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
static internal::Matcher< Stmt > changeIntBoundNode(internal::Matcher< Decl > VarNodeMatcher)
static internal::Matcher< Stmt > hasSuspiciousStmt(StringRef NodeName)
static internal::Matcher< Stmt > simpleCondition(StringRef BindName)
static internal::Matcher< Stmt > callByRef(internal::Matcher< Decl > VarNodeMatcher)
static bool isPossiblyEscaped(const VarDecl *VD, ExplodedNode *N)
bool operator==(const LoopState &X) const
Optional< T > getAs() const
Convert to the specified ProgramPoint type, returning None if this ProgramPoint is not of the desired...
static internal::Matcher< Stmt > getAddrTo(internal::Matcher< Decl > VarNodeMatcher)
AnalysisDeclContext * getAnalysisDeclContext() const
const AstTypeMatcher< ReferenceType > referenceType
Matches both lvalue and rvalue reference types.
void Profile(llvm::FoldingSetNodeID &ID) const