25#include "llvm/ADT/FoldingSet.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/Compiler.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
36void SymExpr::anchor() {}
53 const llvm::APSInt &
Value) {
54 if (
Value.isUnsigned())
58 if (
Value.isUnsigned())
68 os <<
'(' << ToTy <<
") (";
75 bool Binary = isa<BinarySymExpr>(Operand);
89 os <<
", #" << Count <<
'}';
105void SymbolData::anchor() {}
124 assert(!itr.empty() &&
"attempting to iterate on an 'end' iterator");
130 assert(!itr.empty() &&
"attempting to dereference an 'end' iterator");
134void SymExpr::symbol_iterator::expand() {
135 const SymExpr *SE = itr.pop_back_val();
138 case SymExpr::SymbolRegionValueKind:
139 case SymExpr::SymbolConjuredKind:
140 case SymExpr::SymbolDerivedKind:
141 case SymExpr::SymbolExtentKind:
142 case SymExpr::SymbolMetadataKind:
144 case SymExpr::SymbolCastKind:
145 itr.push_back(cast<SymbolCast>(SE)->getOperand());
147 case SymExpr::UnarySymExprKind:
148 itr.push_back(cast<UnarySymExpr>(SE)->getOperand());
150 case SymExpr::SymIntExprKind:
151 itr.push_back(cast<SymIntExpr>(SE)->getLHS());
153 case SymExpr::IntSymExprKind:
154 itr.push_back(cast<IntSymExpr>(SE)->getRHS());
156 case SymExpr::SymSymExprKind: {
157 const auto *x = cast<SymSymExpr>(SE);
158 itr.push_back(x->getLHS());
159 itr.push_back(x->getRHS());
163 llvm_unreachable(
"unhandled expansion case");
168 llvm::FoldingSetNodeID profile;
171 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
175 DataSet.InsertNode(SD, InsertPos);
179 return cast<SymbolRegionValue>(SD);
186 const void *SymbolTag) {
187 llvm::FoldingSetNodeID profile;
190 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
193 new (SD)
SymbolConjured(SymbolCounter, E, LCtx, T, Count, SymbolTag);
194 DataSet.InsertNode(SD, InsertPos);
198 return cast<SymbolConjured>(SD);
204 llvm::FoldingSetNodeID profile;
207 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
211 DataSet.InsertNode(SD, InsertPos);
215 return cast<SymbolDerived>(SD);
220 llvm::FoldingSetNodeID profile;
223 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
227 DataSet.InsertNode(SD, InsertPos);
231 return cast<SymbolExtent>(SD);
237 unsigned Count,
const void *SymbolTag) {
238 llvm::FoldingSetNodeID profile;
241 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
244 new (SD)
SymbolMetadata(SymbolCounter, R, S, T, LCtx, Count, SymbolTag);
245 DataSet.InsertNode(SD, InsertPos);
249 return cast<SymbolMetadata>(SD);
255 llvm::FoldingSetNodeID ID;
258 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
262 DataSet.InsertNode(data, InsertPos);
265 return cast<SymbolCast>(data);
270 const llvm::APSInt&
v,
272 llvm::FoldingSetNodeID ID;
275 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
280 DataSet.InsertNode(data, InsertPos);
283 return cast<SymIntExpr>(data);
290 llvm::FoldingSetNodeID ID;
293 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
298 DataSet.InsertNode(data, InsertPos);
301 return cast<IntSymExpr>(data);
308 llvm::FoldingSetNodeID ID;
311 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
316 DataSet.InsertNode(data, InsertPos);
319 return cast<SymSymExpr>(data);
325 llvm::FoldingSetNodeID ID;
328 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
332 DataSet.InsertNode(data, InsertPos);
335 return cast<UnarySymExpr>(data);
343 return R->getValueType();
347 ASTContext &Ctx = R->getMemRegionManager().getContext();
356 return R->getValueType();
376 auto &dependencies = SymbolDependencies[Primary];
378 dependencies = std::make_unique<SymbolRefSmallVectorTy>();
380 dependencies->push_back(Dependent);
385 SymbolDependTy::const_iterator I = SymbolDependencies.find(Primary);
386 if (I == SymbolDependencies.end())
388 return I->second.get();
391void SymbolReaper::markDependentsLive(
SymbolRef sym) {
393 SymbolMapTy::iterator LI = TheLiving.find(sym);
394 assert(LI != TheLiving.end() &&
"The primary symbol is not live.");
395 if (LI->second == HaveMarkedDependents)
397 LI->second = HaveMarkedDependents;
400 for (
const auto I : *Deps) {
401 if (TheLiving.contains(I))
409 TheLiving[sym] = NotProcessed;
410 markDependentsLive(sym);
415 markElementIndicesLive(region);
423 for (
auto SR = dyn_cast<SubRegion>(region); SR;
424 SR = dyn_cast<SubRegion>(SR->getSuperRegion())) {
425 if (
const auto ER = dyn_cast<ElementRegion>(SR)) {
426 SVal Idx = ER->getIndex();
434 if (isa<SymbolMetadata>(sym))
435 MetadataInUse.insert(sym);
444 if (LiveRegionRoots.count(MR))
447 if (
const auto *SR = dyn_cast<SymbolicRegion>(MR))
448 return isLive(SR->getSymbol());
450 if (
const auto *VR = dyn_cast<VarRegion>(MR))
451 return isLive(VR,
true);
457 return isa<AllocaRegion, CXXThisRegion, MemSpaceRegion, CodeTextRegion>(MR);
460bool SymbolReaper::isLazilyCopiedRegion(
const MemRegion *MR)
const {
465bool SymbolReaper::isReadableRegion(
const MemRegion *MR) {
466 return isLiveRegion(MR) || isLazilyCopiedRegion(MR);
470 if (TheLiving.count(sym)) {
471 markDependentsLive(sym);
478 case SymExpr::SymbolRegionValueKind:
479 KnownLive = isReadableRegion(cast<SymbolRegionValue>(sym)->getRegion());
481 case SymExpr::SymbolConjuredKind:
484 case SymExpr::SymbolDerivedKind:
485 KnownLive = isLive(cast<SymbolDerived>(sym)->getParentSymbol());
487 case SymExpr::SymbolExtentKind:
488 KnownLive = isLiveRegion(cast<SymbolExtent>(sym)->getRegion());
490 case SymExpr::SymbolMetadataKind:
491 KnownLive = MetadataInUse.count(sym) &&
492 isLiveRegion(cast<SymbolMetadata>(sym)->getRegion());
494 MetadataInUse.erase(sym);
496 case SymExpr::SymIntExprKind:
497 KnownLive = isLive(cast<SymIntExpr>(sym)->getLHS());
499 case SymExpr::IntSymExprKind:
500 KnownLive = isLive(cast<IntSymExpr>(sym)->getRHS());
502 case SymExpr::SymSymExprKind:
503 KnownLive = isLive(cast<SymSymExpr>(sym)->getLHS()) &&
504 isLive(cast<SymSymExpr>(sym)->getRHS());
506 case SymExpr::SymbolCastKind:
507 KnownLive = isLive(cast<SymbolCast>(sym)->getOperand());
509 case SymExpr::UnarySymExprKind:
510 KnownLive = isLive(cast<UnarySymExpr>(sym)->getOperand());
528 if (LCtx->isParentOf(ELCtx))
551 if (VarContext == CurrentContext) {
558 if (isa<CXXInheritedCtorInitExpr>(
Loc))
564 if (!includeStoreBindings)
567 unsigned &cachedQuery =
568 const_cast<SymbolReaper *
>(
this)->includedRegionCache[VR];
571 return cachedQuery == 1;
575 if (
Store store = reapedStore.getStore()) {
577 reapedStore.getStoreManager().includedInBindings(store, VR);
578 cachedQuery = hasRegion ? 1 : 2;
585 return VarContext->
isParentOf(CurrentContext);
Defines the clang::ASTContext interface.
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the Objective-C statement AST node classes.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
StringRef getOpcodeStr() const
ASTContext & getASTContext() const LLVM_READONLY
This represents one expression.
bool isLive(const CFGBlock *B, const VarDecl *D)
Return true if a variable is live at the end of a specified block.
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
bool isParentOf(const LocationContext *LC) const
const Decl * getDecl() const
const StackFrameContext * getStackFrame() const
A (possibly-)qualified type.
QualType getCanonicalType() const
It represents a stack frame of the call stack (based on CallEvent).
Stmt - This represents one statement.
int64_t getID(const ASTContext &Context) const
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
bool isRecordType() const
static StringRef getOpcodeStr(Opcode Op)
getOpcodeStr - Turn an Opcode enum value into the punctuation char it corresponds to,...
Template implementation for all binary symbolic expressions.
static void Profile(llvm::FoldingSetNodeID &ID, LHSTYPE lhs, BinaryOperator::Opcode op, RHSTYPE rhs, QualType t)
static void dumpToStreamImpl(raw_ostream &os, const SymExpr *Value)
static bool isLocType(QualType T)
MemRegion - The root abstract class for all memory regions.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
SymExpr::symbol_iterator symbol_begin() const
SymExpr::symbol_iterator symbol_end() const
SubRegion - A region that subsets another larger region.
Iterator over symbols that the current symbol depends on.
symbol_iterator()=default
bool operator!=(const symbol_iterator &X) const
symbol_iterator & operator++()
bool operator==(const symbol_iterator &X) const
const SymExpr * operator*()
virtual void dumpToStream(raw_ostream &os) const
virtual void dump() const
Represents a cast expression.
static void Profile(llvm::FoldingSetNodeID &ID, const SymExpr *In, QualType From, QualType To)
void dumpToStream(raw_ostream &os) const override
A symbol representing the result of an expression in the case when we do not know anything about what...
StringRef getKindStr() const override
Get a string representation of the kind of the region.
static void Profile(llvm::FoldingSetNodeID &profile, const Stmt *S, QualType T, unsigned Count, const LocationContext *LCtx, const void *SymbolTag)
void dumpToStream(raw_ostream &os) const override
QualType getType() const override
SymbolID getSymbolID() const
A symbol representing the value of a MemRegion whose parent region has symbolic value.
LLVM_ATTRIBUTE_RETURNS_NONNULL SymbolRef getParentSymbol() const
StringRef getKindStr() const override
Get a string representation of the kind of the region.
void dumpToStream(raw_ostream &os) const override
static void Profile(llvm::FoldingSetNodeID &profile, SymbolRef parent, const TypedValueRegion *r)
QualType getType() const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
SymbolExtent - Represents the extent (size in bytes) of a bounded region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const SubRegion * getRegion() const
void dumpToStream(raw_ostream &os) const override
QualType getType() const override
static void Profile(llvm::FoldingSetNodeID &profile, const SubRegion *R)
StringRef getKindStr() const override
Get a string representation of the kind of the region.
const SymbolExtent * getExtentSymbol(const SubRegion *R)
const SymbolDerived * getDerivedSymbol(SymbolRef parentSymbol, const TypedValueRegion *R)
const SymbolMetadata * getMetadataSymbol(const MemRegion *R, const Stmt *S, QualType T, const LocationContext *LCtx, unsigned VisitCount, const void *SymbolTag=nullptr)
Creates a metadata symbol associated with a specific region.
const SymbolRegionValue * getRegionValueSymbol(const TypedValueRegion *R)
Make a unique symbol for MemRegion R according to its kind.
const SymbolConjured * conjureSymbol(const Stmt *E, const LocationContext *LCtx, QualType T, unsigned VisitCount, const void *SymbolTag=nullptr)
void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent)
Add artificial symbol dependency.
const SymIntExpr * getSymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const llvm::APSInt &rhs, QualType t)
const SymbolCast * getCastSymbol(const SymExpr *Operand, QualType From, QualType To)
const UnarySymExpr * getUnarySymExpr(const SymExpr *operand, UnaryOperator::Opcode op, QualType t)
const SymbolRefSmallVectorTy * getDependentSymbols(const SymbolRef Primary)
static bool canSymbolicate(QualType T)
const SymSymExpr * getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs, QualType t)
const IntSymExpr * getIntSymExpr(const llvm::APSInt &lhs, BinaryOperator::Opcode op, const SymExpr *rhs, QualType t)
A class responsible for cleaning up unused symbols.
void markLive(SymbolRef sym)
Unconditionally marks a symbol as live.
void markElementIndicesLive(const MemRegion *region)
void markInUse(SymbolRef sym)
Marks a symbol as important to a checker.
bool isLiveRegion(const MemRegion *region)
void markLazilyCopied(const MemRegion *region)
bool isLive(SymbolRef sym)
A symbol representing the value stored at a MemRegion.
void dumpToStream(raw_ostream &os) const override
static void Profile(llvm::FoldingSetNodeID &profile, const TypedValueRegion *R)
QualType getType() const override
StringRef getKindStr() const override
Get a string representation of the kind of the region.
TypedValueRegion - An abstract class representing regions having a typed value.
Represents a symbolic expression involving a unary operator.
void dumpToStream(raw_ostream &os) const override
static void Profile(llvm::FoldingSetNodeID &ID, const SymExpr *In, UnaryOperator::Opcode Op, QualType T)
const VarDecl * getDecl() const override=0
const StackFrameContext * getStackFrame() const
It might return null.
BinarySymExprImpl< const SymExpr *, const llvm::APSInt &, SymExpr::Kind::SymIntExprKind > SymIntExpr
Represents a symbolic expression like 'x' + 3.
BinarySymExprImpl< const llvm::APSInt &, const SymExpr *, SymExpr::Kind::IntSymExprKind > IntSymExpr
Represents a symbolic expression like 3 - 'x'.
BinarySymExprImpl< const SymExpr *, const SymExpr *, SymExpr::Kind::SymSymExprKind > SymSymExpr
Represents a symbolic expression like 'x' + 'y'.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.