1 //== SVals.h - Abstract Values for Static Analysis ---------*- 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 SVal, Loc, and NonLoc, classes that represent
11 // abstract r-values for use with path-sensitive value tracking.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_GR_RVALUE_H
16 #define LLVM_CLANG_GR_RVALUE_H
18 #include "clang/Basic/LLVM.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
20 #include "llvm/ADT/ImmutableList.h"
22 //==------------------------------------------------------------------------==//
24 //==------------------------------------------------------------------------==//
30 class CompoundValData;
31 class LazyCompoundValData;
33 class BasicValueFactory;
36 class MemRegionManager;
37 class ProgramStateManager;
40 /// SVal - This represents a symbolic expression, which can be either
41 /// an L-value or an R-value.
46 // The enumerators must be representable using 2 bits.
47 UndefinedKind = 0, // for subclass UndefinedVal (an uninitialized value)
48 UnknownKind = 1, // for subclass UnknownVal (a void value)
49 LocKind = 2, // for subclass Loc (an L-value)
50 NonLocKind = 3 // for subclass NonLoc (an R-value that's not
53 enum { BaseBits = 2, BaseMask = 0x3 };
58 /// The lowest 2 bits are a BaseKind (0 -- 3).
59 /// The higher bits are an unsigned "kind" value.
62 explicit SVal(const void *d, bool isLoc, unsigned ValKind)
63 : Data(d), Kind((isLoc ? LocKind : NonLocKind) | (ValKind << BaseBits)) {}
65 explicit SVal(BaseKind k, const void *D = NULL)
69 explicit SVal() : Data(0), Kind(0) {}
72 /// BufferTy - A temporary buffer to hold a set of SVals.
73 typedef SmallVector<SVal,5> BufferTy;
75 inline unsigned getRawKind() const { return Kind; }
76 inline BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
77 inline unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
79 // This method is required for using SVal in a FoldingSetNode. It
80 // extracts a unique signature for this SVal object.
81 inline void Profile(llvm::FoldingSetNodeID& ID) const {
82 ID.AddInteger((unsigned) getRawKind());
86 inline bool operator==(const SVal& R) const {
87 return getRawKind() == R.getRawKind() && Data == R.Data;
90 inline bool operator!=(const SVal& R) const {
94 inline bool isUnknown() const {
95 return getRawKind() == UnknownKind;
98 inline bool isUndef() const {
99 return getRawKind() == UndefinedKind;
102 inline bool isUnknownOrUndef() const {
103 return getRawKind() <= UnknownKind;
106 inline bool isValid() const {
107 return getRawKind() > UnknownKind;
110 bool isConstant() const;
112 bool isConstant(int I) const;
114 bool isZeroConstant() const;
116 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
117 bool hasConjuredSymbol() const;
119 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
120 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
121 /// Otherwise return 0.
122 const FunctionDecl *getAsFunctionDecl() const;
124 /// If this SVal is a location (subclasses Loc) and
125 /// wraps a symbol, return that SymbolRef. Otherwise return 0.
126 SymbolRef getAsLocSymbol() const;
128 /// Get the symbol in the SVal or its base region.
129 SymbolRef getLocSymbolInBase() const;
131 /// If this SVal wraps a symbol return that SymbolRef.
132 /// Otherwise, return 0.
133 SymbolRef getAsSymbol() const;
135 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
136 /// return that expression. Otherwise return NULL.
137 const SymExpr *getAsSymbolicExpression() const;
139 const SymExpr* getAsSymExpr() const;
141 const MemRegion *getAsRegion() const;
143 void dumpToStream(raw_ostream &OS) const;
147 class symbol_iterator {
148 SmallVector<const SymExpr*, 5> itr;
152 symbol_iterator(const SymExpr *SE);
154 symbol_iterator &operator++();
155 SymbolRef operator*();
157 bool operator==(const symbol_iterator &X) const;
158 bool operator!=(const symbol_iterator &X) const;
161 symbol_iterator symbol_begin() const {
162 const SymExpr *SE = getAsSymbolicExpression();
164 return symbol_iterator(SE);
166 return symbol_iterator();
169 symbol_iterator symbol_end() const { return symbol_iterator(); }
171 // Implement isa<T> support.
172 static inline bool classof(const SVal*) { return true; }
176 class UndefinedVal : public SVal {
178 UndefinedVal() : SVal(UndefinedKind) {}
179 UndefinedVal(const void *D) : SVal(UndefinedKind, D) {}
181 static inline bool classof(const SVal* V) {
182 return V->getBaseKind() == UndefinedKind;
185 const void *getData() const { return Data; }
188 class DefinedOrUnknownSVal : public SVal {
190 // Do not implement. We want calling these methods to be a compiler
191 // error since they are tautologically false.
192 bool isUndef() const;
193 bool isValid() const;
196 explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
197 : SVal(d, isLoc, ValKind) {}
199 explicit DefinedOrUnknownSVal(BaseKind k, void *D = NULL)
203 // Implement isa<T> support.
204 static inline bool classof(const SVal *V) {
205 return !V->isUndef();
209 class UnknownVal : public DefinedOrUnknownSVal {
211 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownKind) {}
213 static inline bool classof(const SVal *V) {
214 return V->getBaseKind() == UnknownKind;
218 class DefinedSVal : public DefinedOrUnknownSVal {
220 // Do not implement. We want calling these methods to be a compiler
221 // error since they are tautologically true/false.
222 bool isUnknown() const;
223 bool isUnknownOrUndef() const;
224 bool isValid() const;
226 explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
227 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
229 // Implement isa<T> support.
230 static inline bool classof(const SVal *V) {
231 return !V->isUnknownOrUndef();
235 class NonLoc : public DefinedSVal {
237 explicit NonLoc(unsigned SubKind, const void *d)
238 : DefinedSVal(d, false, SubKind) {}
241 void dumpToStream(raw_ostream &Out) const;
243 // Implement isa<T> support.
244 static inline bool classof(const SVal* V) {
245 return V->getBaseKind() == NonLocKind;
249 class Loc : public DefinedSVal {
251 explicit Loc(unsigned SubKind, const void *D)
252 : DefinedSVal(const_cast<void*>(D), true, SubKind) {}
255 void dumpToStream(raw_ostream &Out) const;
257 Loc(const Loc& X) : DefinedSVal(X.Data, true, X.getSubKind()) {}
259 // Implement isa<T> support.
260 static inline bool classof(const SVal* V) {
261 return V->getBaseKind() == LocKind;
264 static inline bool isLocType(QualType T) {
265 return T->isAnyPointerType() || T->isBlockPointerType() ||
266 T->isReferenceType();
270 //==------------------------------------------------------------------------==//
271 // Subclasses of NonLoc.
272 //==------------------------------------------------------------------------==//
276 enum Kind { ConcreteIntKind, SymbolValKind, SymExprValKind,
277 LocAsIntegerKind, CompoundValKind, LazyCompoundValKind };
279 class SymbolVal : public NonLoc {
281 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {}
283 SymbolRef getSymbol() const {
284 return (const SymExpr*) Data;
287 static inline bool classof(const SVal* V) {
288 return V->getBaseKind() == NonLocKind &&
289 V->getSubKind() == SymbolValKind;
292 static inline bool classof(const NonLoc* V) {
293 return V->getSubKind() == SymbolValKind;
297 class SymExprVal : public NonLoc {
299 explicit SymExprVal(const SymExpr *SE)
300 : NonLoc(SymExprValKind, reinterpret_cast<const void*>(SE)) {}
302 const SymExpr *getSymbolicExpression() const {
303 return reinterpret_cast<const SymExpr*>(Data);
306 static inline bool classof(const SVal* V) {
307 return V->getBaseKind() == NonLocKind &&
308 V->getSubKind() == SymExprValKind;
311 static inline bool classof(const NonLoc* V) {
312 return V->getSubKind() == SymExprValKind;
316 class ConcreteInt : public NonLoc {
318 explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
320 const llvm::APSInt& getValue() const {
321 return *static_cast<const llvm::APSInt*>(Data);
324 // Transfer functions for binary/unary operations on ConcreteInts.
325 SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
326 const ConcreteInt& R) const;
328 ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
330 ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
332 // Implement isa<T> support.
333 static inline bool classof(const SVal* V) {
334 return V->getBaseKind() == NonLocKind &&
335 V->getSubKind() == ConcreteIntKind;
338 static inline bool classof(const NonLoc* V) {
339 return V->getSubKind() == ConcreteIntKind;
343 class LocAsInteger : public NonLoc {
344 friend class ento::SValBuilder;
346 explicit LocAsInteger(const std::pair<SVal, uintptr_t>& data) :
347 NonLoc(LocAsIntegerKind, &data) {
348 assert (isa<Loc>(data.first));
354 return cast<Loc>(((std::pair<SVal, uintptr_t>*) Data)->first);
357 const Loc& getPersistentLoc() const {
358 const SVal& V = ((std::pair<SVal, uintptr_t>*) Data)->first;
362 unsigned getNumBits() const {
363 return ((std::pair<SVal, unsigned>*) Data)->second;
366 // Implement isa<T> support.
367 static inline bool classof(const SVal* V) {
368 return V->getBaseKind() == NonLocKind &&
369 V->getSubKind() == LocAsIntegerKind;
372 static inline bool classof(const NonLoc* V) {
373 return V->getSubKind() == LocAsIntegerKind;
377 class CompoundVal : public NonLoc {
378 friend class ento::SValBuilder;
380 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
383 const CompoundValData* getValue() const {
384 return static_cast<const CompoundValData*>(Data);
387 typedef llvm::ImmutableList<SVal>::iterator iterator;
388 iterator begin() const;
389 iterator end() const;
391 static bool classof(const SVal* V) {
392 return V->getBaseKind() == NonLocKind && V->getSubKind() == CompoundValKind;
395 static bool classof(const NonLoc* V) {
396 return V->getSubKind() == CompoundValKind;
400 class LazyCompoundVal : public NonLoc {
401 friend class ento::SValBuilder;
403 explicit LazyCompoundVal(const LazyCompoundValData *D)
404 : NonLoc(LazyCompoundValKind, D) {}
406 const LazyCompoundValData *getCVData() const {
407 return static_cast<const LazyCompoundValData*>(Data);
409 const void *getStore() const;
410 const TypedRegion *getRegion() const;
412 static bool classof(const SVal *V) {
413 return V->getBaseKind() == NonLocKind &&
414 V->getSubKind() == LazyCompoundValKind;
416 static bool classof(const NonLoc *V) {
417 return V->getSubKind() == LazyCompoundValKind;
421 } // end namespace ento::nonloc
423 //==------------------------------------------------------------------------==//
424 // Subclasses of Loc.
425 //==------------------------------------------------------------------------==//
429 enum Kind { GotoLabelKind, MemRegionKind, ConcreteIntKind, ObjCPropRefKind };
431 class GotoLabel : public Loc {
433 explicit GotoLabel(LabelDecl *Label) : Loc(GotoLabelKind, Label) {}
435 const LabelDecl *getLabel() const {
436 return static_cast<const LabelDecl*>(Data);
439 static inline bool classof(const SVal* V) {
440 return V->getBaseKind() == LocKind && V->getSubKind() == GotoLabelKind;
443 static inline bool classof(const Loc* V) {
444 return V->getSubKind() == GotoLabelKind;
449 class MemRegionVal : public Loc {
451 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionKind, r) {}
453 const MemRegion* getRegion() const {
454 return static_cast<const MemRegion*>(Data);
457 const MemRegion* stripCasts() const;
459 template <typename REGION>
460 const REGION* getRegionAs() const {
461 return llvm::dyn_cast<REGION>(getRegion());
464 inline bool operator==(const MemRegionVal& R) const {
465 return getRegion() == R.getRegion();
468 inline bool operator!=(const MemRegionVal& R) const {
469 return getRegion() != R.getRegion();
472 // Implement isa<T> support.
473 static inline bool classof(const SVal* V) {
474 return V->getBaseKind() == LocKind &&
475 V->getSubKind() == MemRegionKind;
478 static inline bool classof(const Loc* V) {
479 return V->getSubKind() == MemRegionKind;
483 class ConcreteInt : public Loc {
485 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
487 const llvm::APSInt& getValue() const {
488 return *static_cast<const llvm::APSInt*>(Data);
491 // Transfer functions for binary/unary operations on ConcreteInts.
492 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
493 const ConcreteInt& R) const;
495 // Implement isa<T> support.
496 static inline bool classof(const SVal* V) {
497 return V->getBaseKind() == LocKind &&
498 V->getSubKind() == ConcreteIntKind;
501 static inline bool classof(const Loc* V) {
502 return V->getSubKind() == ConcreteIntKind;
506 /// \brief Pseudo-location SVal used by the ExprEngine to simulate a "load" or
507 /// "store" of an ObjC property for the dot syntax.
508 class ObjCPropRef : public Loc {
510 explicit ObjCPropRef(const ObjCPropertyRefExpr *E)
511 : Loc(ObjCPropRefKind, E) {}
513 const ObjCPropertyRefExpr *getPropRefExpr() const {
514 return static_cast<const ObjCPropertyRefExpr *>(Data);
517 // Implement isa<T> support.
518 static inline bool classof(const SVal* V) {
519 return V->getBaseKind() == LocKind &&
520 V->getSubKind() == ObjCPropRefKind;
523 static inline bool classof(const Loc* V) {
524 return V->getSubKind() == ObjCPropRefKind;
528 } // end ento::loc namespace
529 } // end GR namespace
531 } // end clang namespace
534 static inline raw_ostream &operator<<(raw_ostream &os,
535 clang::ento::SVal V) {
540 } // end llvm namespace