clang 22.0.0git
LoopWidening.cpp
Go to the documentation of this file.
1//===--- LoopWidening.cpp - Widen loops -------------------------*- C++ -*-===//
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///
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.
13///
14//===----------------------------------------------------------------------===//
15
19
20using namespace clang;
21using namespace ento;
22using namespace clang::ast_matchers;
23
24const auto MatchRef = "matchref";
25
26namespace clang {
27namespace ento {
28
30 const LocationContext *LCtx,
31 unsigned BlockCount,
32 ConstCFGElementRef Elem) {
33 // Invalidate values in the current state.
34 // TODO Make this more conservative by only invalidating values that might
35 // be modified by the body of the loop.
36 // TODO Nested loops are currently widened as a result of the invalidation
37 // being so inprecise. When the invalidation is improved, the handling
38 // of nested loops will also need to be improved.
40 const StackFrameContext *STC = LCtx->getStackFrame();
41 MemRegionManager &MRMgr = PrevState->getStateManager().getRegionManager();
42 const MemRegion *Regions[] = {MRMgr.getStackLocalsRegion(STC),
43 MRMgr.getStackArgumentsRegion(STC),
44 MRMgr.getGlobalsRegion()};
46 for (auto *Region : Regions) {
47 ITraits.setTrait(Region,
49 }
50
51 // References should not be invalidated.
52 auto Matches = match(
54 varDecl(hasType(hasCanonicalType(referenceType()))).bind(MatchRef)))),
55 *LCtx->getDecl()->getBody(), ASTCtx);
56 for (BoundNodes Match : Matches) {
57 const VarDecl *VD = Match.getNodeAs<VarDecl>(MatchRef);
58 assert(VD);
59 const VarRegion *VarMem = MRMgr.getVarRegion(VD, LCtx);
60 ITraits.setTrait(VarMem,
62 }
63
64
65 // 'this' pointer is not an lvalue, we should not invalidate it. If the loop
66 // is located in a method, constructor or destructor, the value of 'this'
67 // pointer should remain unchanged. Ignore static methods, since they do not
68 // have 'this' pointers.
69 const CXXMethodDecl *CXXMD = dyn_cast<CXXMethodDecl>(STC->getDecl());
70 if (CXXMD && CXXMD->isImplicitObjectMemberFunction()) {
71 const CXXThisRegion *ThisR =
72 MRMgr.getCXXThisRegion(CXXMD->getThisType(), STC);
73 ITraits.setTrait(ThisR,
75 }
76
77 return PrevState->invalidateRegions(Regions, Elem, BlockCount, LCtx, true,
78 nullptr, nullptr, &ITraits);
79}
80
81} // end namespace ento
82} // end namespace clang
const auto MatchRef
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:188
ASTContext & getASTContext() const
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2129
bool isImplicitObjectMemberFunction() const
[C++2b][dcl.fct]/p7 An implicit object member function is a non-static member function without an exp...
Definition: DeclCXX.cpp:2710
QualType getThisType() const
Return the type of the this pointer.
Definition: DeclCXX.cpp:2809
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:1087
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).
Represents a variable declaration or definition.
Definition: Decl.h:925
Maps string IDs to AST nodes matched by parts of a matcher.
Definition: ASTMatchers.h:111
CXXThisRegion - Represents the region for the implicit 'this' parameter in a call to a C++ method.
Definition: MemRegion.h:1102
const StackArgumentsSpaceRegion * getStackArgumentsRegion(const StackFrameContext *STC)
getStackArgumentsRegion - Retrieve the memory region associated with function/method arguments of the...
Definition: MemRegion.cpp:937
const CXXThisRegion * getCXXThisRegion(QualType thisPointerTy, const LocationContext *LC)
getCXXThisRegion - Retrieve the [artificial] region associated with the parameter 'this'.
Definition: MemRegion.cpp:1357
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:1041
const StackLocalsSpaceRegion * getStackLocalsRegion(const StackFrameContext *STC)
getStackLocalsRegion - Retrieve the memory region associated with the specified stack frame.
Definition: MemRegion.cpp:925
const GlobalsSpaceRegion * getGlobalsRegion(MemRegion::Kind K=MemRegion::GlobalInternalSpaceRegionKind, const CodeTextRegion *R=nullptr)
getGlobalsRegion - Retrieve the memory region associated with global variables.
Definition: MemRegion.cpp:949
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:98
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1657
@ TK_PreserveContents
Tells that a region's contents is not changed.
Definition: MemRegion.h:1672
@ TK_EntireMemSpace
When applied to a MemSpaceRegion, indicates the entire memory space should be invalidated.
Definition: MemRegion.h:1682
void setTrait(SymbolRef Sym, InvalidationKinds IK)
Definition: MemRegion.cpp:1847
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:3675
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState, const LocationContext *LCtx, unsigned BlockCount, ConstCFGElementRef Elem)
Get the states that result from widening the loop.
The JSON file list parser is used to communicate input to InstallAPI.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition: CFG.h:1199