clang 17.0.0git
Go to the documentation of this file.
1//===--- LoopWidening.cpp - Widen loops -------------------------*- C++ -*-===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9/// This file contains functions which are used to widen loops. A loop may be
10/// widened to approximate the exit state(s), without analyzing every
11/// iteration. The widening is done by invalidating anything which might be
12/// modified by the body of the loop.
16#include "clang/AST/AST.h"
21using namespace clang;
22using namespace ento;
23using namespace clang::ast_matchers;
25const auto MatchRef = "matchref";
27/// Return the loops condition Stmt or NULL if LoopStmt is not a loop
28static const Expr *getLoopCondition(const Stmt *LoopStmt) {
29 switch (LoopStmt->getStmtClass()) {
30 default:
31 return nullptr;
32 case Stmt::ForStmtClass:
33 return cast<ForStmt>(LoopStmt)->getCond();
34 case Stmt::WhileStmtClass:
35 return cast<WhileStmt>(LoopStmt)->getCond();
36 case Stmt::DoStmtClass:
37 return cast<DoStmt>(LoopStmt)->getCond();
38 }
41namespace clang {
42namespace ento {
45 const LocationContext *LCtx,
46 unsigned BlockCount, const Stmt *LoopStmt) {
48 assert((isa<ForStmt, WhileStmt, DoStmt>(LoopStmt)));
50 // Invalidate values in the current state.
51 // TODO Make this more conservative by only invalidating values that might
52 // be modified by the body of the loop.
53 // TODO Nested loops are currently widened as a result of the invalidation
54 // being so inprecise. When the invalidation is improved, the handling
55 // of nested loops will also need to be improved.
57 const StackFrameContext *STC = LCtx->getStackFrame();
58 MemRegionManager &MRMgr = PrevState->getStateManager().getRegionManager();
59 const MemRegion *Regions[] = {MRMgr.getStackLocalsRegion(STC),
60 MRMgr.getStackArgumentsRegion(STC),
61 MRMgr.getGlobalsRegion()};
63 for (auto *Region : Regions) {
64 ITraits.setTrait(Region,
66 }
68 // References should not be invalidated.
69 auto Matches = match(
71 varDecl(hasType(hasCanonicalType(referenceType()))).bind(MatchRef)))),
72 *LCtx->getDecl()->getBody(), ASTCtx);
73 for (BoundNodes Match : Matches) {
74 const VarDecl *VD = Match.getNodeAs<VarDecl>(MatchRef);
75 assert(VD);
76 const VarRegion *VarMem = MRMgr.getVarRegion(VD, LCtx);
77 ITraits.setTrait(VarMem,
79 }
82 // 'this' pointer is not an lvalue, we should not invalidate it. If the loop
83 // is located in a method, constructor or destructor, the value of 'this'
84 // pointer should remain unchanged. Ignore static methods, since they do not
85 // have 'this' pointers.
86 const CXXMethodDecl *CXXMD = dyn_cast<CXXMethodDecl>(STC->getDecl());
87 if (CXXMD && !CXXMD->isStatic()) {
88 const CXXThisRegion *ThisR =
89 MRMgr.getCXXThisRegion(CXXMD->getThisType(), STC);
90 ITraits.setTrait(ThisR,
92 }
94 return PrevState->invalidateRegions(Regions, getLoopCondition(LoopStmt),
95 BlockCount, LCtx, true, nullptr, nullptr,
96 &ITraits);
99} // end namespace ento
100} // end namespace clang
const auto MatchRef
static const Expr * getLoopCondition(const Stmt *LoopStmt)
Return the loops condition Stmt or NULL if LoopStmt is not a loop.
This header contains the declarations of functions which are used to widen loops which do not otherwi...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
ASTContext & getASTContext() const
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2018
QualType getThisType() const
Return the type of the this pointer.
Definition: DeclCXX.cpp:2502
bool isStatic() const
Definition: DeclCXX.cpp:2142
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:1052
This represents one expression.
Definition: Expr.h:110
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
const Decl * getDecl() const
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
const StackFrameContext * getStackFrame() const
It represents a stack frame of the call stack (based on CallEvent).
Stmt - This represents one statement.
Definition: Stmt.h:72
StmtClass getStmtClass() const
Definition: Stmt.h:1177
Represents a variable declaration or definition.
Definition: Decl.h:913
Maps string IDs to AST nodes matched by parts of a matcher.
Definition: ASTMatchers.h:108
CXXThisRegion - Represents the region for the implicit 'this' parameter in a call to a C++ method.
Definition: MemRegion.h:1064
const StackArgumentsSpaceRegion * getStackArgumentsRegion(const StackFrameContext *STC)
getStackArgumentsRegion - Retrieve the memory region associated with function/method arguments of the...
Definition: MemRegion.cpp:872
const CXXThisRegion * getCXXThisRegion(QualType thisPointerTy, const LocationContext *LC)
getCXXThisRegion - Retrieve the [artificial] region associated with the parameter 'this'.
Definition: MemRegion.cpp:1249
const VarRegion * getVarRegion(const VarDecl *VD, const LocationContext *LC)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and LocationC...
Definition: MemRegion.cpp:964
const StackLocalsSpaceRegion * getStackLocalsRegion(const StackFrameContext *STC)
getStackLocalsRegion - Retrieve the memory region associated with the specified stack frame.
Definition: MemRegion.cpp:859
const GlobalsSpaceRegion * getGlobalsRegion(MemRegion::Kind K=MemRegion::GlobalInternalSpaceRegionKind, const CodeTextRegion *R=nullptr)
getGlobalsRegion - Retrieve the memory region associated with global variables.
Definition: MemRegion.cpp:885
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:95
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1570
@ TK_PreserveContents
Tells that a region's contents is not changed.
Definition: MemRegion.h:1585
@ TK_EntireMemSpace
When applied to a MemSpaceRegion, indicates the entire memory space should be invalidated.
Definition: MemRegion.h:1595
void setTrait(SymbolRef Sym, InvalidationKinds IK)
Definition: MemRegion.cpp:1732
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const AstTypeMatcher< ReferenceType > referenceType
Matches both lvalue and rvalue reference types.
internal::Matcher< T > findAll(const internal::Matcher< T > &Matcher)
Matches if the node or any descendant matches.
Definition: ASTMatchers.h:3501
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState, const LocationContext *LCtx, unsigned BlockCount, const Stmt *LoopStmt)
Get the states that result from widening the loop.