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 "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
21 #include "llvm/ADT/ImmutableList.h"
23 //==------------------------------------------------------------------------==//
25 //==------------------------------------------------------------------------==//
31 class CompoundValData;
32 class LazyCompoundValData;
34 class BasicValueFactory;
37 class MemRegionManager;
38 class ProgramStateManager;
41 /// SVal - This represents a symbolic expression, which can be either
42 /// an L-value or an R-value.
47 // The enumerators must be representable using 2 bits.
48 UndefinedKind = 0, // for subclass UndefinedVal (an uninitialized value)
49 UnknownKind = 1, // for subclass UnknownVal (a void value)
50 LocKind = 2, // for subclass Loc (an L-value)
51 NonLocKind = 3 // for subclass NonLoc (an R-value that's not
54 enum { BaseBits = 2, BaseMask = 0x3 };
59 /// The lowest 2 bits are a BaseKind (0 -- 3).
60 /// The higher bits are an unsigned "kind" value.
63 explicit SVal(const void *d, bool isLoc, unsigned ValKind)
64 : Data(d), Kind((isLoc ? LocKind : NonLocKind) | (ValKind << BaseBits)) {}
66 explicit SVal(BaseKind k, const void *D = NULL)
70 explicit SVal() : Data(0), Kind(0) {}
73 /// BufferTy - A temporary buffer to hold a set of SVals.
74 typedef SmallVector<SVal,5> BufferTy;
76 inline unsigned getRawKind() const { return Kind; }
77 inline BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
78 inline unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
80 // This method is required for using SVal in a FoldingSetNode. It
81 // extracts a unique signature for this SVal object.
82 inline void Profile(llvm::FoldingSetNodeID& ID) const {
83 ID.AddInteger((unsigned) getRawKind());
87 inline bool operator==(const SVal& R) const {
88 return getRawKind() == R.getRawKind() && Data == R.Data;
91 inline bool operator!=(const SVal& R) const {
95 inline bool isUnknown() const {
96 return getRawKind() == UnknownKind;
99 inline bool isUndef() const {
100 return getRawKind() == UndefinedKind;
103 inline bool isUnknownOrUndef() const {
104 return getRawKind() <= UnknownKind;
107 inline bool isValid() const {
108 return getRawKind() > UnknownKind;
111 bool isConstant() const;
113 bool isConstant(int I) const;
115 bool isZeroConstant() const;
117 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
118 bool hasConjuredSymbol() const;
120 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
121 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
122 /// Otherwise return 0.
123 const FunctionDecl *getAsFunctionDecl() const;
125 /// If this SVal is a location (subclasses Loc) and
126 /// wraps a symbol, return that SymbolRef. Otherwise return 0.
127 SymbolRef getAsLocSymbol() const;
129 /// Get the symbol in the SVal or its base region.
130 SymbolRef getLocSymbolInBase() const;
132 /// If this SVal wraps a symbol return that SymbolRef.
133 /// Otherwise, return 0.
134 SymbolRef getAsSymbol() const;
136 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
137 /// return that expression. Otherwise return NULL.
138 const SymExpr *getAsSymbolicExpression() const;
140 const SymExpr* getAsSymExpr() const;
142 const MemRegion *getAsRegion() const;
144 void dumpToStream(raw_ostream &OS) const;
147 SymExpr::symbol_iterator symbol_begin() const {
148 const SymExpr *SE = getAsSymbolicExpression();
150 return SE->symbol_begin();
152 return SymExpr::symbol_iterator();
155 SymExpr::symbol_iterator symbol_end() const {
156 return SymExpr::symbol_end();
159 // Implement isa<T> support.
160 static inline bool classof(const SVal*) { return true; }
164 class UndefinedVal : public SVal {
166 UndefinedVal() : SVal(UndefinedKind) {}
167 UndefinedVal(const void *D) : SVal(UndefinedKind, D) {}
169 static inline bool classof(const SVal* V) {
170 return V->getBaseKind() == UndefinedKind;
173 const void *getData() const { return Data; }
176 class DefinedOrUnknownSVal : public SVal {
178 // Do not implement. We want calling these methods to be a compiler
179 // error since they are tautologically false.
180 bool isUndef() const;
181 bool isValid() const;
184 explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
185 : SVal(d, isLoc, ValKind) {}
187 explicit DefinedOrUnknownSVal(BaseKind k, void *D = NULL)
191 // Implement isa<T> support.
192 static inline bool classof(const SVal *V) {
193 return !V->isUndef();
197 class UnknownVal : public DefinedOrUnknownSVal {
199 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownKind) {}
201 static inline bool classof(const SVal *V) {
202 return V->getBaseKind() == UnknownKind;
206 class DefinedSVal : public DefinedOrUnknownSVal {
208 // Do not implement. We want calling these methods to be a compiler
209 // error since they are tautologically true/false.
210 bool isUnknown() const;
211 bool isUnknownOrUndef() const;
212 bool isValid() const;
214 explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
215 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
217 // Implement isa<T> support.
218 static inline bool classof(const SVal *V) {
219 return !V->isUnknownOrUndef();
223 class NonLoc : public DefinedSVal {
225 explicit NonLoc(unsigned SubKind, const void *d)
226 : DefinedSVal(d, false, SubKind) {}
229 void dumpToStream(raw_ostream &Out) const;
231 // Implement isa<T> support.
232 static inline bool classof(const SVal* V) {
233 return V->getBaseKind() == NonLocKind;
237 class Loc : public DefinedSVal {
239 explicit Loc(unsigned SubKind, const void *D)
240 : DefinedSVal(const_cast<void*>(D), true, SubKind) {}
243 void dumpToStream(raw_ostream &Out) const;
245 Loc(const Loc& X) : DefinedSVal(X.Data, true, X.getSubKind()) {}
247 // Implement isa<T> support.
248 static inline bool classof(const SVal* V) {
249 return V->getBaseKind() == LocKind;
252 static inline bool isLocType(QualType T) {
253 return T->isAnyPointerType() || T->isBlockPointerType() ||
254 T->isReferenceType();
258 //==------------------------------------------------------------------------==//
259 // Subclasses of NonLoc.
260 //==------------------------------------------------------------------------==//
264 enum Kind { ConcreteIntKind, SymbolValKind, SymExprValKind,
265 LocAsIntegerKind, CompoundValKind, LazyCompoundValKind };
267 /// \brief Represents symbolic expression.
268 class SymbolVal : public NonLoc {
270 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {}
272 SymbolRef getSymbol() const {
273 return (const SymExpr*) Data;
276 bool isExpression() {
277 return !isa<SymbolData>(getSymbol());
280 static inline bool classof(const SVal* V) {
281 return V->getBaseKind() == NonLocKind &&
282 V->getSubKind() == SymbolValKind;
285 static inline bool classof(const NonLoc* V) {
286 return V->getSubKind() == SymbolValKind;
290 /// \brief Value representing integer constant.
291 class ConcreteInt : public NonLoc {
293 explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
295 const llvm::APSInt& getValue() const {
296 return *static_cast<const llvm::APSInt*>(Data);
299 // Transfer functions for binary/unary operations on ConcreteInts.
300 SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
301 const ConcreteInt& R) const;
303 ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
305 ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
307 // Implement isa<T> support.
308 static inline bool classof(const SVal* V) {
309 return V->getBaseKind() == NonLocKind &&
310 V->getSubKind() == ConcreteIntKind;
313 static inline bool classof(const NonLoc* V) {
314 return V->getSubKind() == ConcreteIntKind;
318 class LocAsInteger : public NonLoc {
319 friend class ento::SValBuilder;
321 explicit LocAsInteger(const std::pair<SVal, uintptr_t>& data) :
322 NonLoc(LocAsIntegerKind, &data) {
323 assert (isa<Loc>(data.first));
329 const std::pair<SVal, uintptr_t> *D =
330 static_cast<const std::pair<SVal, unsigned> *>(Data);
331 return cast<Loc>(D->first);
334 const Loc& getPersistentLoc() const {
335 const std::pair<SVal, uintptr_t> *D =
336 static_cast<const std::pair<SVal, unsigned> *>(Data);
337 const SVal& V = D->first;
341 unsigned getNumBits() const {
342 const std::pair<SVal, uintptr_t> *D =
343 static_cast<const std::pair<SVal, unsigned> *>(Data);
347 // Implement isa<T> support.
348 static inline bool classof(const SVal* V) {
349 return V->getBaseKind() == NonLocKind &&
350 V->getSubKind() == LocAsIntegerKind;
353 static inline bool classof(const NonLoc* V) {
354 return V->getSubKind() == LocAsIntegerKind;
358 class CompoundVal : public NonLoc {
359 friend class ento::SValBuilder;
361 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
364 const CompoundValData* getValue() const {
365 return static_cast<const CompoundValData*>(Data);
368 typedef llvm::ImmutableList<SVal>::iterator iterator;
369 iterator begin() const;
370 iterator end() const;
372 static bool classof(const SVal* V) {
373 return V->getBaseKind() == NonLocKind && V->getSubKind() == CompoundValKind;
376 static bool classof(const NonLoc* V) {
377 return V->getSubKind() == CompoundValKind;
381 class LazyCompoundVal : public NonLoc {
382 friend class ento::SValBuilder;
384 explicit LazyCompoundVal(const LazyCompoundValData *D)
385 : NonLoc(LazyCompoundValKind, D) {}
387 const LazyCompoundValData *getCVData() const {
388 return static_cast<const LazyCompoundValData*>(Data);
390 const void *getStore() const;
391 const TypedRegion *getRegion() const;
393 static bool classof(const SVal *V) {
394 return V->getBaseKind() == NonLocKind &&
395 V->getSubKind() == LazyCompoundValKind;
397 static bool classof(const NonLoc *V) {
398 return V->getSubKind() == LazyCompoundValKind;
402 } // end namespace ento::nonloc
404 //==------------------------------------------------------------------------==//
405 // Subclasses of Loc.
406 //==------------------------------------------------------------------------==//
410 enum Kind { GotoLabelKind, MemRegionKind, ConcreteIntKind, ObjCPropRefKind };
412 class GotoLabel : public Loc {
414 explicit GotoLabel(LabelDecl *Label) : Loc(GotoLabelKind, Label) {}
416 const LabelDecl *getLabel() const {
417 return static_cast<const LabelDecl*>(Data);
420 static inline bool classof(const SVal* V) {
421 return V->getBaseKind() == LocKind && V->getSubKind() == GotoLabelKind;
424 static inline bool classof(const Loc* V) {
425 return V->getSubKind() == GotoLabelKind;
430 class MemRegionVal : public Loc {
432 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionKind, r) {}
434 /// \brief Get the underlining region.
435 const MemRegion* getRegion() const {
436 return static_cast<const MemRegion*>(Data);
439 /// \brief Get the underlining region and strip casts.
440 const MemRegion* stripCasts() const;
442 template <typename REGION>
443 const REGION* getRegionAs() const {
444 return llvm::dyn_cast<REGION>(getRegion());
447 inline bool operator==(const MemRegionVal& R) const {
448 return getRegion() == R.getRegion();
451 inline bool operator!=(const MemRegionVal& R) const {
452 return getRegion() != R.getRegion();
455 // Implement isa<T> support.
456 static inline bool classof(const SVal* V) {
457 return V->getBaseKind() == LocKind &&
458 V->getSubKind() == MemRegionKind;
461 static inline bool classof(const Loc* V) {
462 return V->getSubKind() == MemRegionKind;
466 class ConcreteInt : public Loc {
468 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
470 const llvm::APSInt& getValue() const {
471 return *static_cast<const llvm::APSInt*>(Data);
474 // Transfer functions for binary/unary operations on ConcreteInts.
475 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
476 const ConcreteInt& R) const;
478 // Implement isa<T> support.
479 static inline bool classof(const SVal* V) {
480 return V->getBaseKind() == LocKind &&
481 V->getSubKind() == ConcreteIntKind;
484 static inline bool classof(const Loc* V) {
485 return V->getSubKind() == ConcreteIntKind;
489 /// \brief Pseudo-location SVal used by the ExprEngine to simulate a "load" or
490 /// "store" of an ObjC property for the dot syntax.
491 class ObjCPropRef : public Loc {
493 explicit ObjCPropRef(const ObjCPropertyRefExpr *E)
494 : Loc(ObjCPropRefKind, E) {}
496 const ObjCPropertyRefExpr *getPropRefExpr() const {
497 return static_cast<const ObjCPropertyRefExpr *>(Data);
500 // Implement isa<T> support.
501 static inline bool classof(const SVal* V) {
502 return V->getBaseKind() == LocKind &&
503 V->getSubKind() == ObjCPropRefKind;
506 static inline bool classof(const Loc* V) {
507 return V->getSubKind() == ObjCPropRefKind;
511 } // end ento::loc namespace
512 } // end GR namespace
514 } // end clang namespace
517 static inline raw_ostream &operator<<(raw_ostream &os,
518 clang::ento::SVal V) {
523 } // end llvm namespace