1 //== SymbolManager.h - Management of Symbolic Values ------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines SymbolManager, a class that manages symbolic values
11 // created for use by ExprEngine and related classes.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
16 #include "clang/Analysis/Analyses/LiveVariables.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
24 void SymExpr::dump() const {
25 dumpToStream(llvm::errs());
28 static void print(raw_ostream &os, BinaryOperator::Opcode Op) {
31 llvm_unreachable("operator printing not implemented");
32 case BO_Mul: os << '*' ; break;
33 case BO_Div: os << '/' ; break;
34 case BO_Rem: os << '%' ; break;
35 case BO_Add: os << '+' ; break;
36 case BO_Sub: os << '-' ; break;
37 case BO_Shl: os << "<<" ; break;
38 case BO_Shr: os << ">>" ; break;
39 case BO_LT: os << "<" ; break;
40 case BO_GT: os << '>' ; break;
41 case BO_LE: os << "<=" ; break;
42 case BO_GE: os << ">=" ; break;
43 case BO_EQ: os << "==" ; break;
44 case BO_NE: os << "!=" ; break;
45 case BO_And: os << '&' ; break;
46 case BO_Xor: os << '^' ; break;
47 case BO_Or: os << '|' ; break;
51 void SymIntExpr::dumpToStream(raw_ostream &os) const {
53 getLHS()->dumpToStream(os);
55 print(os, getOpcode());
56 os << ' ' << getRHS().getZExtValue();
57 if (getRHS().isUnsigned()) os << 'U';
60 void SymSymExpr::dumpToStream(raw_ostream &os) const {
62 getLHS()->dumpToStream(os);
65 getRHS()->dumpToStream(os);
69 void SymbolConjured::dumpToStream(raw_ostream &os) const {
70 os << "conj_$" << getSymbolID() << '{' << T.getAsString() << '}';
73 void SymbolDerived::dumpToStream(raw_ostream &os) const {
74 os << "derived_$" << getSymbolID() << '{'
75 << getParentSymbol() << ',' << getRegion() << '}';
78 void SymbolExtent::dumpToStream(raw_ostream &os) const {
79 os << "extent_$" << getSymbolID() << '{' << getRegion() << '}';
82 void SymbolMetadata::dumpToStream(raw_ostream &os) const {
83 os << "meta_$" << getSymbolID() << '{'
84 << getRegion() << ',' << T.getAsString() << '}';
87 void SymbolRegionValue::dumpToStream(raw_ostream &os) const {
88 os << "reg_$" << getSymbolID() << "<" << R << ">";
91 const SymbolRegionValue*
92 SymbolManager::getRegionValueSymbol(const TypedValueRegion* R) {
93 llvm::FoldingSetNodeID profile;
94 SymbolRegionValue::Profile(profile, R);
96 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
98 SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>();
99 new (SD) SymbolRegionValue(SymbolCounter, R);
100 DataSet.InsertNode(SD, InsertPos);
104 return cast<SymbolRegionValue>(SD);
107 const SymbolConjured*
108 SymbolManager::getConjuredSymbol(const Stmt *E, QualType T, unsigned Count,
109 const void *SymbolTag) {
111 llvm::FoldingSetNodeID profile;
112 SymbolConjured::Profile(profile, E, T, Count, SymbolTag);
114 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
116 SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>();
117 new (SD) SymbolConjured(SymbolCounter, E, T, Count, SymbolTag);
118 DataSet.InsertNode(SD, InsertPos);
122 return cast<SymbolConjured>(SD);
126 SymbolManager::getDerivedSymbol(SymbolRef parentSymbol,
127 const TypedValueRegion *R) {
129 llvm::FoldingSetNodeID profile;
130 SymbolDerived::Profile(profile, parentSymbol, R);
132 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
134 SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>();
135 new (SD) SymbolDerived(SymbolCounter, parentSymbol, R);
136 DataSet.InsertNode(SD, InsertPos);
140 return cast<SymbolDerived>(SD);
144 SymbolManager::getExtentSymbol(const SubRegion *R) {
145 llvm::FoldingSetNodeID profile;
146 SymbolExtent::Profile(profile, R);
148 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
150 SD = (SymExpr*) BPAlloc.Allocate<SymbolExtent>();
151 new (SD) SymbolExtent(SymbolCounter, R);
152 DataSet.InsertNode(SD, InsertPos);
156 return cast<SymbolExtent>(SD);
159 const SymbolMetadata*
160 SymbolManager::getMetadataSymbol(const MemRegion* R, const Stmt *S, QualType T,
161 unsigned Count, const void *SymbolTag) {
163 llvm::FoldingSetNodeID profile;
164 SymbolMetadata::Profile(profile, R, S, T, Count, SymbolTag);
166 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
168 SD = (SymExpr*) BPAlloc.Allocate<SymbolMetadata>();
169 new (SD) SymbolMetadata(SymbolCounter, R, S, T, Count, SymbolTag);
170 DataSet.InsertNode(SD, InsertPos);
174 return cast<SymbolMetadata>(SD);
177 const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs,
178 BinaryOperator::Opcode op,
179 const llvm::APSInt& v,
181 llvm::FoldingSetNodeID ID;
182 SymIntExpr::Profile(ID, lhs, op, v, t);
184 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
187 data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>();
188 new (data) SymIntExpr(lhs, op, v, t);
189 DataSet.InsertNode(data, InsertPos);
192 return cast<SymIntExpr>(data);
195 const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs,
196 BinaryOperator::Opcode op,
199 llvm::FoldingSetNodeID ID;
200 SymSymExpr::Profile(ID, lhs, op, rhs, t);
202 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
205 data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>();
206 new (data) SymSymExpr(lhs, op, rhs, t);
207 DataSet.InsertNode(data, InsertPos);
210 return cast<SymSymExpr>(data);
213 QualType SymbolConjured::getType(ASTContext&) const {
217 QualType SymbolDerived::getType(ASTContext &Ctx) const {
218 return R->getValueType();
221 QualType SymbolExtent::getType(ASTContext &Ctx) const {
222 return Ctx.getSizeType();
225 QualType SymbolMetadata::getType(ASTContext&) const {
229 QualType SymbolRegionValue::getType(ASTContext &C) const {
230 return R->getValueType();
233 SymbolManager::~SymbolManager() {
234 for (SymbolDependTy::const_iterator I = SymbolDependencies.begin(),
235 E = SymbolDependencies.end(); I != E; ++I) {
241 bool SymbolManager::canSymbolicate(QualType T) {
242 T = T.getCanonicalType();
244 if (Loc::isLocType(T))
247 if (T->isIntegerType())
248 return T->isScalarType();
250 if (T->isRecordType() && !T->isUnionType())
256 void SymbolManager::addSymbolDependency(const SymbolRef Primary,
257 const SymbolRef Dependent) {
258 SymbolDependTy::iterator I = SymbolDependencies.find(Primary);
259 SymbolRefSmallVectorTy *dependencies = 0;
260 if (I == SymbolDependencies.end()) {
261 dependencies = new SymbolRefSmallVectorTy();
262 SymbolDependencies[Primary] = dependencies;
264 dependencies = I->second;
266 dependencies->push_back(Dependent);
269 const SymbolRefSmallVectorTy *SymbolManager::getDependentSymbols(
270 const SymbolRef Primary) {
271 SymbolDependTy::const_iterator I = SymbolDependencies.find(Primary);
272 if (I == SymbolDependencies.end())
277 void SymbolReaper::markDependentsLive(SymbolRef sym) {
278 // Do not mark dependents more then once.
279 SymbolMapTy::iterator LI = TheLiving.find(sym);
280 assert(LI != TheLiving.end() && "The primary symbol is not live.");
281 if (LI->second == HaveMarkedDependents)
283 LI->second = HaveMarkedDependents;
285 if (const SymbolRefSmallVectorTy *Deps = SymMgr.getDependentSymbols(sym)) {
286 for (SymbolRefSmallVectorTy::const_iterator I = Deps->begin(),
287 E = Deps->end(); I != E; ++I) {
288 if (TheLiving.find(*I) != TheLiving.end())
295 void SymbolReaper::markLive(SymbolRef sym) {
296 TheLiving[sym] = NotProcessed;
298 markDependentsLive(sym);
301 void SymbolReaper::markLive(const MemRegion *region) {
302 RegionRoots.insert(region);
305 void SymbolReaper::markInUse(SymbolRef sym) {
306 if (isa<SymbolMetadata>(sym))
307 MetadataInUse.insert(sym);
310 bool SymbolReaper::maybeDead(SymbolRef sym) {
318 bool SymbolReaper::isLiveRegion(const MemRegion *MR) {
319 if (RegionRoots.count(MR))
322 MR = MR->getBaseRegion();
324 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
325 return isLive(SR->getSymbol());
327 if (const VarRegion *VR = dyn_cast<VarRegion>(MR))
328 return isLive(VR, true);
330 // FIXME: This is a gross over-approximation. What we really need is a way to
331 // tell if anything still refers to this region. Unlike SymbolicRegions,
332 // AllocaRegions don't have associated symbols, though, so we don't actually
333 // have a way to track their liveness.
334 if (isa<AllocaRegion>(MR))
337 if (isa<CXXThisRegion>(MR))
340 if (isa<MemSpaceRegion>(MR))
346 bool SymbolReaper::isLive(SymbolRef sym) {
347 if (TheLiving.count(sym)) {
348 markDependentsLive(sym);
352 if (const SymbolDerived *derived = dyn_cast<SymbolDerived>(sym)) {
353 if (isLive(derived->getParentSymbol())) {
360 if (const SymbolExtent *extent = dyn_cast<SymbolExtent>(sym)) {
361 if (isLiveRegion(extent->getRegion())) {
368 if (const SymbolMetadata *metadata = dyn_cast<SymbolMetadata>(sym)) {
369 if (MetadataInUse.count(sym)) {
370 if (isLiveRegion(metadata->getRegion())) {
372 MetadataInUse.erase(sym);
379 // Interogate the symbol. It may derive from an input value to
380 // the analyzed function/method.
381 return isa<SymbolRegionValue>(sym);
384 bool SymbolReaper::isLive(const Stmt *ExprVal) const {
385 return LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, ExprVal);
388 bool SymbolReaper::isLive(const VarRegion *VR, bool includeStoreBindings) const{
389 const StackFrameContext *VarContext = VR->getStackFrame();
390 const StackFrameContext *CurrentContext = LCtx->getCurrentStackFrame();
392 if (VarContext == CurrentContext) {
393 if (LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, VR->getDecl()))
396 if (!includeStoreBindings)
399 unsigned &cachedQuery =
400 const_cast<SymbolReaper*>(this)->includedRegionCache[VR];
403 return cachedQuery == 1;
406 // Query the store to see if the region occurs in any live bindings.
407 if (Store store = reapedStore.getStore()) {
409 reapedStore.getStoreManager().includedInBindings(store, VR);
410 cachedQuery = hasRegion ? 1 : 2;
417 return VarContext->isParentOf(CurrentContext);
420 SymbolVisitor::~SymbolVisitor() {}