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 #ifndef LLVM_CLANG_GR_SYMMGR_H
16 #define LLVM_CLANG_GR_SYMMGR_H
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/Analysis/AnalysisContext.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/DenseMap.h"
29 class BumpPtrAllocator;
34 class StackFrameContext;
37 class BasicValueFactory;
40 class TypedValueRegion;
43 /// \brief Symbolic value. These values used to capture symbolic execution of
45 class SymExpr : public llvm::FoldingSetNode {
46 virtual void anchor();
48 enum Kind { RegionValueKind, ConjuredKind, DerivedKind, ExtentKind,
50 BEGIN_SYMBOLS = RegionValueKind,
51 END_SYMBOLS = MetadataKind,
52 SymIntKind, IntSymKind, SymSymKind, CastSymbolKind };
57 SymExpr(Kind k) : K(k) {}
62 Kind getKind() const { return K; }
66 virtual void dumpToStream(raw_ostream &os) const = 0;
68 virtual QualType getType(ASTContext&) const = 0;
69 virtual void Profile(llvm::FoldingSetNodeID& profile) = 0;
71 // Implement isa<T> support.
72 static inline bool classof(const SymExpr*) { return true; }
74 /// \brief Iterator over symbols that the current symbol depends on.
76 /// For SymbolData, it's the symbol itself; for expressions, it's the
77 /// expression symbol and all the operands in it. Note, SymbolDerived is
78 /// treated as SymbolData - the iterator will NOT visit the parent region.
79 class symbol_iterator {
80 SmallVector<const SymExpr*, 5> itr;
84 symbol_iterator(const SymExpr *SE);
86 symbol_iterator &operator++();
87 const SymExpr* operator*();
89 bool operator==(const symbol_iterator &X) const;
90 bool operator!=(const symbol_iterator &X) const;
93 symbol_iterator symbol_begin() const {
94 return symbol_iterator(this);
96 static symbol_iterator symbol_end() { return symbol_iterator(); }
99 typedef const SymExpr* SymbolRef;
100 typedef llvm::SmallVector<SymbolRef, 2> SymbolRefSmallVectorTy;
102 typedef unsigned SymbolID;
103 /// \brief A symbol representing data which can be stored in a memory location
105 class SymbolData : public SymExpr {
106 virtual void anchor();
110 SymbolData(Kind k, SymbolID sym) : SymExpr(k), Sym(sym) {}
113 virtual ~SymbolData() {}
115 SymbolID getSymbolID() const { return Sym; }
117 // Implement isa<T> support.
118 static inline bool classof(const SymExpr *SE) {
119 Kind k = SE->getKind();
120 return k >= BEGIN_SYMBOLS && k <= END_SYMBOLS;
124 ///\brief A symbol representing the value stored at a MemRegion.
125 class SymbolRegionValue : public SymbolData {
126 const TypedValueRegion *R;
129 SymbolRegionValue(SymbolID sym, const TypedValueRegion *r)
130 : SymbolData(RegionValueKind, sym), R(r) {}
132 const TypedValueRegion* getRegion() const { return R; }
134 static void Profile(llvm::FoldingSetNodeID& profile, const TypedValueRegion* R) {
135 profile.AddInteger((unsigned) RegionValueKind);
136 profile.AddPointer(R);
139 virtual void Profile(llvm::FoldingSetNodeID& profile) {
143 void dumpToStream(raw_ostream &os) const;
145 QualType getType(ASTContext&) const;
147 // Implement isa<T> support.
148 static inline bool classof(const SymExpr *SE) {
149 return SE->getKind() == RegionValueKind;
153 /// A symbol representing the result of an expression in the case when we do
154 /// not know anything about what the expression is.
155 class SymbolConjured : public SymbolData {
159 const void *SymbolTag;
162 SymbolConjured(SymbolID sym, const Stmt *s, QualType t, unsigned count,
163 const void *symbolTag)
164 : SymbolData(ConjuredKind, sym), S(s), T(t), Count(count),
165 SymbolTag(symbolTag) {}
167 const Stmt *getStmt() const { return S; }
168 unsigned getCount() const { return Count; }
169 const void *getTag() const { return SymbolTag; }
171 QualType getType(ASTContext&) const;
173 void dumpToStream(raw_ostream &os) const;
175 static void Profile(llvm::FoldingSetNodeID& profile, const Stmt *S,
176 QualType T, unsigned Count, const void *SymbolTag) {
177 profile.AddInteger((unsigned) ConjuredKind);
178 profile.AddPointer(S);
180 profile.AddInteger(Count);
181 profile.AddPointer(SymbolTag);
184 virtual void Profile(llvm::FoldingSetNodeID& profile) {
185 Profile(profile, S, T, Count, SymbolTag);
188 // Implement isa<T> support.
189 static inline bool classof(const SymExpr *SE) {
190 return SE->getKind() == ConjuredKind;
194 /// A symbol representing the value of a MemRegion whose parent region has
196 class SymbolDerived : public SymbolData {
197 SymbolRef parentSymbol;
198 const TypedValueRegion *R;
201 SymbolDerived(SymbolID sym, SymbolRef parent, const TypedValueRegion *r)
202 : SymbolData(DerivedKind, sym), parentSymbol(parent), R(r) {}
204 SymbolRef getParentSymbol() const { return parentSymbol; }
205 const TypedValueRegion *getRegion() const { return R; }
207 QualType getType(ASTContext&) const;
209 void dumpToStream(raw_ostream &os) const;
211 static void Profile(llvm::FoldingSetNodeID& profile, SymbolRef parent,
212 const TypedValueRegion *r) {
213 profile.AddInteger((unsigned) DerivedKind);
214 profile.AddPointer(r);
215 profile.AddPointer(parent);
218 virtual void Profile(llvm::FoldingSetNodeID& profile) {
219 Profile(profile, parentSymbol, R);
222 // Implement isa<T> support.
223 static inline bool classof(const SymExpr *SE) {
224 return SE->getKind() == DerivedKind;
228 /// SymbolExtent - Represents the extent (size in bytes) of a bounded region.
229 /// Clients should not ask the SymbolManager for a region's extent. Always use
230 /// SubRegion::getExtent instead -- the value returned may not be a symbol.
231 class SymbolExtent : public SymbolData {
235 SymbolExtent(SymbolID sym, const SubRegion *r)
236 : SymbolData(ExtentKind, sym), R(r) {}
238 const SubRegion *getRegion() const { return R; }
240 QualType getType(ASTContext&) const;
242 void dumpToStream(raw_ostream &os) const;
244 static void Profile(llvm::FoldingSetNodeID& profile, const SubRegion *R) {
245 profile.AddInteger((unsigned) ExtentKind);
246 profile.AddPointer(R);
249 virtual void Profile(llvm::FoldingSetNodeID& profile) {
253 // Implement isa<T> support.
254 static inline bool classof(const SymExpr *SE) {
255 return SE->getKind() == ExtentKind;
259 /// SymbolMetadata - Represents path-dependent metadata about a specific region.
260 /// Metadata symbols remain live as long as they are marked as in use before
261 /// dead-symbol sweeping AND their associated regions are still alive.
262 /// Intended for use by checkers.
263 class SymbolMetadata : public SymbolData {
270 SymbolMetadata(SymbolID sym, const MemRegion* r, const Stmt *s, QualType t,
271 unsigned count, const void *tag)
272 : SymbolData(MetadataKind, sym), R(r), S(s), T(t), Count(count), Tag(tag) {}
274 const MemRegion *getRegion() const { return R; }
275 const Stmt *getStmt() const { return S; }
276 unsigned getCount() const { return Count; }
277 const void *getTag() const { return Tag; }
279 QualType getType(ASTContext&) const;
281 void dumpToStream(raw_ostream &os) const;
283 static void Profile(llvm::FoldingSetNodeID& profile, const MemRegion *R,
284 const Stmt *S, QualType T, unsigned Count,
286 profile.AddInteger((unsigned) MetadataKind);
287 profile.AddPointer(R);
288 profile.AddPointer(S);
290 profile.AddInteger(Count);
291 profile.AddPointer(Tag);
294 virtual void Profile(llvm::FoldingSetNodeID& profile) {
295 Profile(profile, R, S, T, Count, Tag);
298 // Implement isa<T> support.
299 static inline bool classof(const SymExpr *SE) {
300 return SE->getKind() == MetadataKind;
304 /// \brief Represents a cast expression.
305 class SymbolCast : public SymExpr {
306 const SymExpr *Operand;
307 /// Type of the operand.
309 /// The type of the result.
313 SymbolCast(const SymExpr *In, QualType From, QualType To) :
314 SymExpr(CastSymbolKind), Operand(In), FromTy(From), ToTy(To) { }
316 QualType getType(ASTContext &C) const { return ToTy; }
318 const SymExpr *getOperand() const { return Operand; }
320 void dumpToStream(raw_ostream &os) const;
322 static void Profile(llvm::FoldingSetNodeID& ID,
323 const SymExpr *In, QualType From, QualType To) {
324 ID.AddInteger((unsigned) CastSymbolKind);
330 void Profile(llvm::FoldingSetNodeID& ID) {
331 Profile(ID, Operand, FromTy, ToTy);
334 // Implement isa<T> support.
335 static inline bool classof(const SymExpr *SE) {
336 return SE->getKind() == CastSymbolKind;
340 /// SymIntExpr - Represents symbolic expression like 'x' + 3.
341 class SymIntExpr : public SymExpr {
343 BinaryOperator::Opcode Op;
344 const llvm::APSInt& RHS;
348 SymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
349 const llvm::APSInt& rhs, QualType t)
350 : SymExpr(SymIntKind), LHS(lhs), Op(op), RHS(rhs), T(t) {}
352 // FIXME: We probably need to make this out-of-line to avoid redundant
353 // generation of virtual functions.
354 QualType getType(ASTContext &C) const { return T; }
356 BinaryOperator::Opcode getOpcode() const { return Op; }
358 void dumpToStream(raw_ostream &os) const;
360 const SymExpr *getLHS() const { return LHS; }
361 const llvm::APSInt &getRHS() const { return RHS; }
363 static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
364 BinaryOperator::Opcode op, const llvm::APSInt& rhs,
366 ID.AddInteger((unsigned) SymIntKind);
373 void Profile(llvm::FoldingSetNodeID& ID) {
374 Profile(ID, LHS, Op, RHS, T);
377 // Implement isa<T> support.
378 static inline bool classof(const SymExpr *SE) {
379 return SE->getKind() == SymIntKind;
383 /// IntSymExpr - Represents symbolic expression like 3 - 'x'.
384 class IntSymExpr : public SymExpr {
385 const llvm::APSInt& LHS;
386 BinaryOperator::Opcode Op;
391 IntSymExpr(const llvm::APSInt& lhs, BinaryOperator::Opcode op,
392 const SymExpr *rhs, QualType t)
393 : SymExpr(IntSymKind), LHS(lhs), Op(op), RHS(rhs), T(t) {}
395 QualType getType(ASTContext &C) const { return T; }
397 BinaryOperator::Opcode getOpcode() const { return Op; }
399 void dumpToStream(raw_ostream &os) const;
401 const SymExpr *getRHS() const { return RHS; }
402 const llvm::APSInt &getLHS() const { return LHS; }
404 static void Profile(llvm::FoldingSetNodeID& ID, const llvm::APSInt& lhs,
405 BinaryOperator::Opcode op, const SymExpr *rhs,
407 ID.AddInteger((unsigned) IntSymKind);
414 void Profile(llvm::FoldingSetNodeID& ID) {
415 Profile(ID, LHS, Op, RHS, T);
418 // Implement isa<T> support.
419 static inline bool classof(const SymExpr *SE) {
420 return SE->getKind() == IntSymKind;
424 /// SymSymExpr - Represents symbolic expression like 'x' + 'y'.
425 class SymSymExpr : public SymExpr {
427 BinaryOperator::Opcode Op;
432 SymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs,
434 : SymExpr(SymSymKind), LHS(lhs), Op(op), RHS(rhs), T(t) {}
436 BinaryOperator::Opcode getOpcode() const { return Op; }
437 const SymExpr *getLHS() const { return LHS; }
438 const SymExpr *getRHS() const { return RHS; }
440 // FIXME: We probably need to make this out-of-line to avoid redundant
441 // generation of virtual functions.
442 QualType getType(ASTContext &C) const { return T; }
444 void dumpToStream(raw_ostream &os) const;
446 static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
447 BinaryOperator::Opcode op, const SymExpr *rhs, QualType t) {
448 ID.AddInteger((unsigned) SymSymKind);
455 void Profile(llvm::FoldingSetNodeID& ID) {
456 Profile(ID, LHS, Op, RHS, T);
459 // Implement isa<T> support.
460 static inline bool classof(const SymExpr *SE) {
461 return SE->getKind() == SymSymKind;
465 class SymbolManager {
466 typedef llvm::FoldingSet<SymExpr> DataSetTy;
467 typedef llvm::DenseMap<SymbolRef, SymbolRefSmallVectorTy*> SymbolDependTy;
470 /// Stores the extra dependencies between symbols: the data should be kept
471 /// alive as long as the key is live.
472 SymbolDependTy SymbolDependencies;
473 unsigned SymbolCounter;
474 llvm::BumpPtrAllocator& BPAlloc;
475 BasicValueFactory &BV;
479 SymbolManager(ASTContext &ctx, BasicValueFactory &bv,
480 llvm::BumpPtrAllocator& bpalloc)
481 : SymbolDependencies(16), SymbolCounter(0),
482 BPAlloc(bpalloc), BV(bv), Ctx(ctx) {}
486 static bool canSymbolicate(QualType T);
488 /// \brief Make a unique symbol for MemRegion R according to its kind.
489 const SymbolRegionValue* getRegionValueSymbol(const TypedValueRegion* R);
491 const SymbolConjured* getConjuredSymbol(const Stmt *E, QualType T,
493 const void *SymbolTag = 0);
495 const SymbolConjured* getConjuredSymbol(const Expr *E, unsigned VisitCount,
496 const void *SymbolTag = 0) {
497 return getConjuredSymbol(E, E->getType(), VisitCount, SymbolTag);
500 const SymbolDerived *getDerivedSymbol(SymbolRef parentSymbol,
501 const TypedValueRegion *R);
503 const SymbolExtent *getExtentSymbol(const SubRegion *R);
505 /// \brief Creates a metadata symbol associated with a specific region.
507 /// VisitCount can be used to differentiate regions corresponding to
508 /// different loop iterations, thus, making the symbol path-dependent.
509 const SymbolMetadata* getMetadataSymbol(const MemRegion* R, const Stmt *S,
510 QualType T, unsigned VisitCount,
511 const void *SymbolTag = 0);
513 const SymbolCast* getCastSymbol(const SymExpr *Operand,
514 QualType From, QualType To);
516 const SymIntExpr *getSymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
517 const llvm::APSInt& rhs, QualType t);
519 const SymIntExpr *getSymIntExpr(const SymExpr &lhs, BinaryOperator::Opcode op,
520 const llvm::APSInt& rhs, QualType t) {
521 return getSymIntExpr(&lhs, op, rhs, t);
524 const IntSymExpr *getIntSymExpr(const llvm::APSInt& lhs,
525 BinaryOperator::Opcode op,
526 const SymExpr *rhs, QualType t);
528 const SymSymExpr *getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
529 const SymExpr *rhs, QualType t);
531 QualType getType(const SymExpr *SE) const {
532 return SE->getType(Ctx);
535 /// \brief Add artificial symbol dependency.
537 /// The dependent symbol should stay alive as long as the primary is alive.
538 void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent);
540 const SymbolRefSmallVectorTy *getDependentSymbols(const SymbolRef Primary);
542 ASTContext &getContext() { return Ctx; }
543 BasicValueFactory &getBasicVals() { return BV; }
552 typedef llvm::DenseSet<SymbolRef> SymbolSetTy;
553 typedef llvm::DenseMap<SymbolRef, SymbolStatus> SymbolMapTy;
554 typedef llvm::DenseSet<const MemRegion *> RegionSetTy;
556 SymbolMapTy TheLiving;
557 SymbolSetTy MetadataInUse;
560 RegionSetTy RegionRoots;
562 const LocationContext *LCtx;
564 SymbolManager& SymMgr;
565 StoreRef reapedStore;
566 llvm::DenseMap<const MemRegion *, unsigned> includedRegionCache;
569 SymbolReaper(const LocationContext *ctx, const Stmt *s, SymbolManager& symmgr,
570 StoreManager &storeMgr)
571 : LCtx(ctx), Loc(s), SymMgr(symmgr), reapedStore(0, storeMgr) {}
575 const LocationContext *getLocationContext() const { return LCtx; }
576 const Stmt *getCurrentStatement() const { return Loc; }
578 bool isLive(SymbolRef sym);
579 bool isLiveRegion(const MemRegion *region);
580 bool isLive(const Stmt *ExprVal, const LocationContext *LCtx) const;
581 bool isLive(const VarRegion *VR, bool includeStoreBindings = false) const;
583 /// \brief Unconditionally marks a symbol as live.
585 /// This should never be
586 /// used by checkers, only by the state infrastructure such as the store and
587 /// environment. Checkers should instead use metadata symbols and markInUse.
588 void markLive(SymbolRef sym);
590 /// \brief Marks a symbol as important to a checker.
592 /// For metadata symbols,
593 /// this will keep the symbol alive as long as its associated region is also
594 /// live. For other symbols, this has no effect; checkers are not permitted
595 /// to influence the life of other symbols. This should be used before any
596 /// symbol marking has occurred, i.e. in the MarkLiveSymbols callback.
597 void markInUse(SymbolRef sym);
599 /// \brief If a symbol is known to be live, marks the symbol as live.
601 /// Otherwise, if the symbol cannot be proven live, it is marked as dead.
602 /// Returns true if the symbol is dead, false if live.
603 bool maybeDead(SymbolRef sym);
605 typedef SymbolSetTy::const_iterator dead_iterator;
606 dead_iterator dead_begin() const { return TheDead.begin(); }
607 dead_iterator dead_end() const { return TheDead.end(); }
609 bool hasDeadSymbols() const {
610 return !TheDead.empty();
613 typedef RegionSetTy::const_iterator region_iterator;
614 region_iterator region_begin() const { return RegionRoots.begin(); }
615 region_iterator region_end() const { return RegionRoots.end(); }
617 /// \brief Returns whether or not a symbol has been confirmed dead.
619 /// This should only be called once all marking of dead symbols has completed.
620 /// (For checkers, this means only in the evalDeadSymbols callback.)
621 bool isDead(SymbolRef sym) const {
622 return TheDead.count(sym);
625 void markLive(const MemRegion *region);
627 /// \brief Set to the value of the symbolic store after
628 /// StoreManager::removeDeadBindings has been called.
629 void setReapedStore(StoreRef st) { reapedStore = st; }
632 /// Mark the symbols dependent on the input symbol as live.
633 void markDependentsLive(SymbolRef sym);
636 class SymbolVisitor {
638 /// \brief A visitor method invoked by ProgramStateManager::scanReachableSymbols.
640 /// The method returns \c true if symbols should continue be scanned and \c
642 virtual bool VisitSymbol(SymbolRef sym) = 0;
643 virtual bool VisitMemRegion(const MemRegion *region) { return true; }
644 virtual ~SymbolVisitor();
647 } // end GR namespace
649 } // end clang namespace
652 static inline raw_ostream &operator<<(raw_ostream &os,
653 const clang::ento::SymExpr *SE) {
654 SE->dumpToStream(os);
657 } // end llvm namespace