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/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/ADT/ImmutableList.h"
26 //==------------------------------------------------------------------------==//
28 //==------------------------------------------------------------------------==//
34 class CompoundValData;
35 class LazyCompoundValData;
37 class BasicValueFactory;
40 class MemRegionManager;
44 /// SVal - This represents a symbolic expression, which can be either
45 /// an L-value or an R-value.
50 // The enumerators must be representable using 2 bits.
51 UndefinedKind = 0, // for subclass UndefinedVal (an uninitialized value)
52 UnknownKind = 1, // for subclass UnknownVal (a void value)
53 LocKind = 2, // for subclass Loc (an L-value)
54 NonLocKind = 3 // for subclass NonLoc (an R-value that's not
57 enum { BaseBits = 2, BaseMask = 0x3 };
62 /// The lowest 2 bits are a BaseKind (0 -- 3).
63 /// The higher bits are an unsigned "kind" value.
66 explicit SVal(const void* d, bool isLoc, unsigned ValKind)
67 : Data(d), Kind((isLoc ? LocKind : NonLocKind) | (ValKind << BaseBits)) {}
69 explicit SVal(BaseKind k, const void* D = NULL)
73 explicit SVal() : Data(0), Kind(0) {}
76 /// BufferTy - A temporary buffer to hold a set of SVals.
77 typedef SmallVector<SVal,5> BufferTy;
79 inline unsigned getRawKind() const { return Kind; }
80 inline BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
81 inline unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
83 // This method is required for using SVal in a FoldingSetNode. It
84 // extracts a unique signature for this SVal object.
85 inline void Profile(llvm::FoldingSetNodeID& ID) const {
86 ID.AddInteger((unsigned) getRawKind());
90 inline bool operator==(const SVal& R) const {
91 return getRawKind() == R.getRawKind() && Data == R.Data;
94 inline bool operator!=(const SVal& R) const {
98 inline bool isUnknown() const {
99 return getRawKind() == UnknownKind;
102 inline bool isUndef() const {
103 return getRawKind() == UndefinedKind;
106 inline bool isUnknownOrUndef() const {
107 return getRawKind() <= UnknownKind;
110 inline bool isValid() const {
111 return getRawKind() > UnknownKind;
114 bool isConstant() const;
116 bool isConstant(int I) const;
118 bool isZeroConstant() const;
120 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
121 bool hasConjuredSymbol() const;
123 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
124 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
125 /// Otherwise return 0.
126 const FunctionDecl* getAsFunctionDecl() const;
128 /// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
129 /// wraps a symbol, return that SymbolRef. Otherwise return NULL.
130 SymbolRef getAsLocSymbol() const;
132 /// Get the symbol in the SVal or its base region.
133 SymbolRef getLocSymbolInBase() const;
135 /// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
136 /// Otherwise return a SymbolRef where 'isValid()' returns false.
137 SymbolRef getAsSymbol() const;
139 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
140 /// return that expression. Otherwise return NULL.
141 const SymExpr *getAsSymbolicExpression() const;
143 const MemRegion *getAsRegion() const;
145 void dumpToStream(llvm::raw_ostream& OS) const;
149 class symbol_iterator {
150 SmallVector<const SymExpr*, 5> itr;
154 symbol_iterator(const SymExpr* SE);
156 symbol_iterator& operator++();
157 SymbolRef operator*();
159 bool operator==(const symbol_iterator& X) const;
160 bool operator!=(const symbol_iterator& X) const;
163 symbol_iterator symbol_begin() const {
164 const SymExpr *SE = getAsSymbolicExpression();
166 return symbol_iterator(SE);
168 return symbol_iterator();
171 symbol_iterator symbol_end() const { return symbol_iterator(); }
173 // Implement isa<T> support.
174 static inline bool classof(const SVal*) { return true; }
178 class UndefinedVal : public SVal {
180 UndefinedVal() : SVal(UndefinedKind) {}
181 UndefinedVal(const void* D) : SVal(UndefinedKind, D) {}
183 static inline bool classof(const SVal* V) {
184 return V->getBaseKind() == UndefinedKind;
187 const void* getData() const { return Data; }
190 class DefinedOrUnknownSVal : public SVal {
192 // Do not implement. We want calling these methods to be a compiler
193 // error since they are tautologically false.
194 bool isUndef() const;
195 bool isValid() const;
198 explicit DefinedOrUnknownSVal(const void* d, bool isLoc, unsigned ValKind)
199 : SVal(d, isLoc, ValKind) {}
201 explicit DefinedOrUnknownSVal(BaseKind k, void *D = NULL)
205 // Implement isa<T> support.
206 static inline bool classof(const SVal *V) {
207 return !V->isUndef();
211 class UnknownVal : public DefinedOrUnknownSVal {
213 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownKind) {}
215 static inline bool classof(const SVal *V) {
216 return V->getBaseKind() == UnknownKind;
220 class DefinedSVal : public DefinedOrUnknownSVal {
222 // Do not implement. We want calling these methods to be a compiler
223 // error since they are tautologically true/false.
224 bool isUnknown() const;
225 bool isUnknownOrUndef() const;
226 bool isValid() const;
228 explicit DefinedSVal(const void* d, bool isLoc, unsigned ValKind)
229 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
231 // Implement isa<T> support.
232 static inline bool classof(const SVal *V) {
233 return !V->isUnknownOrUndef();
237 class NonLoc : public DefinedSVal {
239 explicit NonLoc(unsigned SubKind, const void* d)
240 : DefinedSVal(d, false, SubKind) {}
243 void dumpToStream(llvm::raw_ostream& Out) const;
245 // Implement isa<T> support.
246 static inline bool classof(const SVal* V) {
247 return V->getBaseKind() == NonLocKind;
251 class Loc : public DefinedSVal {
253 explicit Loc(unsigned SubKind, const void* D)
254 : DefinedSVal(const_cast<void*>(D), true, SubKind) {}
257 void dumpToStream(llvm::raw_ostream& Out) const;
259 Loc(const Loc& X) : DefinedSVal(X.Data, true, X.getSubKind()) {}
261 // Implement isa<T> support.
262 static inline bool classof(const SVal* V) {
263 return V->getBaseKind() == LocKind;
266 static inline bool isLocType(QualType T) {
267 return T->isAnyPointerType() || T->isBlockPointerType() ||
268 T->isReferenceType();
272 //==------------------------------------------------------------------------==//
273 // Subclasses of NonLoc.
274 //==------------------------------------------------------------------------==//
278 enum Kind { ConcreteIntKind, SymbolValKind, SymExprValKind,
279 LocAsIntegerKind, CompoundValKind, LazyCompoundValKind };
281 class SymbolVal : public NonLoc {
283 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {}
285 SymbolRef getSymbol() const {
286 return (const SymbolData*) Data;
289 static inline bool classof(const SVal* V) {
290 return V->getBaseKind() == NonLocKind &&
291 V->getSubKind() == SymbolValKind;
294 static inline bool classof(const NonLoc* V) {
295 return V->getSubKind() == SymbolValKind;
299 class SymExprVal : public NonLoc {
301 explicit SymExprVal(const SymExpr *SE)
302 : NonLoc(SymExprValKind, reinterpret_cast<const void*>(SE)) {}
304 const SymExpr *getSymbolicExpression() const {
305 return reinterpret_cast<const SymExpr*>(Data);
308 static inline bool classof(const SVal* V) {
309 return V->getBaseKind() == NonLocKind &&
310 V->getSubKind() == SymExprValKind;
313 static inline bool classof(const NonLoc* V) {
314 return V->getSubKind() == SymExprValKind;
318 class ConcreteInt : public NonLoc {
320 explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
322 const llvm::APSInt& getValue() const {
323 return *static_cast<const llvm::APSInt*>(Data);
326 // Transfer functions for binary/unary operations on ConcreteInts.
327 SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
328 const ConcreteInt& R) const;
330 ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
332 ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
334 // Implement isa<T> support.
335 static inline bool classof(const SVal* V) {
336 return V->getBaseKind() == NonLocKind &&
337 V->getSubKind() == ConcreteIntKind;
340 static inline bool classof(const NonLoc* V) {
341 return V->getSubKind() == ConcreteIntKind;
345 class LocAsInteger : public NonLoc {
346 friend class ento::SValBuilder;
348 explicit LocAsInteger(const std::pair<SVal, uintptr_t>& data) :
349 NonLoc(LocAsIntegerKind, &data) {
350 assert (isa<Loc>(data.first));
356 return cast<Loc>(((std::pair<SVal, uintptr_t>*) Data)->first);
359 const Loc& getPersistentLoc() const {
360 const SVal& V = ((std::pair<SVal, uintptr_t>*) Data)->first;
364 unsigned getNumBits() const {
365 return ((std::pair<SVal, unsigned>*) Data)->second;
368 // Implement isa<T> support.
369 static inline bool classof(const SVal* V) {
370 return V->getBaseKind() == NonLocKind &&
371 V->getSubKind() == LocAsIntegerKind;
374 static inline bool classof(const NonLoc* V) {
375 return V->getSubKind() == LocAsIntegerKind;
379 class CompoundVal : public NonLoc {
380 friend class ento::SValBuilder;
382 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
385 const CompoundValData* getValue() const {
386 return static_cast<const CompoundValData*>(Data);
389 typedef llvm::ImmutableList<SVal>::iterator iterator;
390 iterator begin() const;
391 iterator end() const;
393 static bool classof(const SVal* V) {
394 return V->getBaseKind() == NonLocKind && V->getSubKind() == CompoundValKind;
397 static bool classof(const NonLoc* V) {
398 return V->getSubKind() == CompoundValKind;
402 class LazyCompoundVal : public NonLoc {
403 friend class ento::SValBuilder;
405 explicit LazyCompoundVal(const LazyCompoundValData *D)
406 : NonLoc(LazyCompoundValKind, D) {}
408 const LazyCompoundValData *getCVData() const {
409 return static_cast<const LazyCompoundValData*>(Data);
411 const void *getStore() const;
412 const TypedRegion *getRegion() const;
414 static bool classof(const SVal *V) {
415 return V->getBaseKind() == NonLocKind &&
416 V->getSubKind() == LazyCompoundValKind;
418 static bool classof(const NonLoc *V) {
419 return V->getSubKind() == LazyCompoundValKind;
423 } // end namespace ento::nonloc
425 //==------------------------------------------------------------------------==//
426 // Subclasses of Loc.
427 //==------------------------------------------------------------------------==//
431 enum Kind { GotoLabelKind, MemRegionKind, ConcreteIntKind, ObjCPropRefKind };
433 class GotoLabel : public Loc {
435 explicit GotoLabel(LabelDecl *Label) : Loc(GotoLabelKind, Label) {}
437 const LabelDecl *getLabel() const {
438 return static_cast<const LabelDecl*>(Data);
441 static inline bool classof(const SVal* V) {
442 return V->getBaseKind() == LocKind && V->getSubKind() == GotoLabelKind;
445 static inline bool classof(const Loc* V) {
446 return V->getSubKind() == GotoLabelKind;
451 class MemRegionVal : public Loc {
453 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionKind, r) {}
455 const MemRegion* getRegion() const {
456 return static_cast<const MemRegion*>(Data);
459 const MemRegion* stripCasts() const;
461 template <typename REGION>
462 const REGION* getRegionAs() const {
463 return llvm::dyn_cast<REGION>(getRegion());
466 inline bool operator==(const MemRegionVal& R) const {
467 return getRegion() == R.getRegion();
470 inline bool operator!=(const MemRegionVal& R) const {
471 return getRegion() != R.getRegion();
474 // Implement isa<T> support.
475 static inline bool classof(const SVal* V) {
476 return V->getBaseKind() == LocKind &&
477 V->getSubKind() == MemRegionKind;
480 static inline bool classof(const Loc* V) {
481 return V->getSubKind() == MemRegionKind;
485 class ConcreteInt : public Loc {
487 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
489 const llvm::APSInt& getValue() const {
490 return *static_cast<const llvm::APSInt*>(Data);
493 // Transfer functions for binary/unary operations on ConcreteInts.
494 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
495 const ConcreteInt& R) const;
497 // Implement isa<T> support.
498 static inline bool classof(const SVal* V) {
499 return V->getBaseKind() == LocKind &&
500 V->getSubKind() == ConcreteIntKind;
503 static inline bool classof(const Loc* V) {
504 return V->getSubKind() == ConcreteIntKind;
508 /// \brief Pseudo-location SVal used by the ExprEngine to simulate a "load" or
509 /// "store" of an ObjC property for the dot syntax.
510 class ObjCPropRef : public Loc {
512 explicit ObjCPropRef(const ObjCPropertyRefExpr *E)
513 : Loc(ObjCPropRefKind, E) {}
515 const ObjCPropertyRefExpr *getPropRefExpr() const {
516 return static_cast<const ObjCPropertyRefExpr *>(Data);
519 // Implement isa<T> support.
520 static inline bool classof(const SVal* V) {
521 return V->getBaseKind() == LocKind &&
522 V->getSubKind() == ObjCPropRefKind;
525 static inline bool classof(const Loc* V) {
526 return V->getSubKind() == ObjCPropRefKind;
530 } // end ento::loc namespace
531 } // end GR namespace
533 } // end clang namespace
536 static inline llvm::raw_ostream& operator<<(llvm::raw_ostream& os,
537 clang::ento::SVal V) {
542 } // end llvm namespace