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/ProgramState_Fwd.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
21 #include "llvm/ADT/ImmutableList.h"
23 //==------------------------------------------------------------------------==//
25 //==------------------------------------------------------------------------==//
31 class CompoundValData;
32 class LazyCompoundValData;
34 class BasicValueFactory;
36 class TypedValueRegion;
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) {}
72 /// \brief Convert to the specified SVal type, asserting that this SVal is of
76 assert(T::isKind(*this));
83 /// \brief Convert to the specified SVal type, returning None if this SVal is
84 /// not of the desired type.
86 Optional<T> getAs() const {
87 if (!T::isKind(*this))
95 /// BufferTy - A temporary buffer to hold a set of SVals.
96 typedef SmallVector<SVal,5> BufferTy;
98 inline unsigned getRawKind() const { return Kind; }
99 inline BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
100 inline unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
102 // This method is required for using SVal in a FoldingSetNode. It
103 // extracts a unique signature for this SVal object.
104 inline void Profile(llvm::FoldingSetNodeID& ID) const {
105 ID.AddInteger((unsigned) getRawKind());
109 inline bool operator==(const SVal& R) const {
110 return getRawKind() == R.getRawKind() && Data == R.Data;
113 inline bool operator!=(const SVal& R) const {
114 return !(*this == R);
117 inline bool isUnknown() const {
118 return getRawKind() == UnknownKind;
121 inline bool isUndef() const {
122 return getRawKind() == UndefinedKind;
125 inline bool isUnknownOrUndef() const {
126 return getRawKind() <= UnknownKind;
129 inline bool isValid() const {
130 return getRawKind() > UnknownKind;
133 bool isConstant() const;
135 bool isConstant(int I) const;
137 bool isZeroConstant() const;
139 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
140 bool hasConjuredSymbol() const;
142 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
143 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
144 /// Otherwise return 0.
145 const FunctionDecl *getAsFunctionDecl() const;
147 /// If this SVal is a location (subclasses Loc) and
148 /// wraps a symbol, return that SymbolRef. Otherwise return 0.
149 SymbolRef getAsLocSymbol() const;
151 /// Get the symbol in the SVal or its base region.
152 SymbolRef getLocSymbolInBase() const;
154 /// If this SVal wraps a symbol return that SymbolRef.
155 /// Otherwise, return 0.
156 SymbolRef getAsSymbol() const;
158 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
159 /// return that expression. Otherwise return NULL.
160 const SymExpr *getAsSymbolicExpression() const;
162 const SymExpr* getAsSymExpr() const;
164 const MemRegion *getAsRegion() const;
166 void dumpToStream(raw_ostream &OS) const;
169 SymExpr::symbol_iterator symbol_begin() const {
170 const SymExpr *SE = getAsSymbolicExpression();
172 return SE->symbol_begin();
174 return SymExpr::symbol_iterator();
177 SymExpr::symbol_iterator symbol_end() const {
178 return SymExpr::symbol_end();
183 class UndefinedVal : public SVal {
185 UndefinedVal() : SVal(UndefinedKind) {}
189 static bool isKind(const SVal& V) {
190 return V.getBaseKind() == UndefinedKind;
194 class DefinedOrUnknownSVal : public SVal {
196 // We want calling these methods to be a compiler error since they are
197 // tautologically false.
198 bool isUndef() const LLVM_DELETED_FUNCTION;
199 bool isValid() const LLVM_DELETED_FUNCTION;
202 DefinedOrUnknownSVal() {}
203 explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
204 : SVal(d, isLoc, ValKind) {}
206 explicit DefinedOrUnknownSVal(BaseKind k, void *D = NULL)
211 static bool isKind(const SVal& V) {
216 class UnknownVal : public DefinedOrUnknownSVal {
218 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownKind) {}
222 static bool isKind(const SVal& V) {
223 return V.getBaseKind() == UnknownKind;
227 class DefinedSVal : public DefinedOrUnknownSVal {
229 // We want calling these methods to be a compiler error since they are
230 // tautologically true/false.
231 bool isUnknown() const LLVM_DELETED_FUNCTION;
232 bool isUnknownOrUndef() const LLVM_DELETED_FUNCTION;
233 bool isValid() const LLVM_DELETED_FUNCTION;
236 explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
237 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
240 static bool isKind(const SVal& V) {
241 return !V.isUnknownOrUndef();
245 class NonLoc : public DefinedSVal {
248 explicit NonLoc(unsigned SubKind, const void *d)
249 : DefinedSVal(d, false, SubKind) {}
252 void dumpToStream(raw_ostream &Out) const;
256 static bool isKind(const SVal& V) {
257 return V.getBaseKind() == NonLocKind;
261 class Loc : public DefinedSVal {
264 explicit Loc(unsigned SubKind, const void *D)
265 : DefinedSVal(const_cast<void*>(D), true, SubKind) {}
268 void dumpToStream(raw_ostream &Out) const;
270 static inline bool isLocType(QualType T) {
271 return T->isAnyPointerType() || T->isBlockPointerType() ||
272 T->isReferenceType();
277 static bool isKind(const SVal& V) {
278 return V.getBaseKind() == LocKind;
282 //==------------------------------------------------------------------------==//
283 // Subclasses of NonLoc.
284 //==------------------------------------------------------------------------==//
288 enum Kind { ConcreteIntKind, SymbolValKind,
289 LocAsIntegerKind, CompoundValKind, LazyCompoundValKind };
291 /// \brief Represents symbolic expression.
292 class SymbolVal : public NonLoc {
294 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {}
296 SymbolRef getSymbol() const {
297 return (const SymExpr*) Data;
300 bool isExpression() const {
301 return !isa<SymbolData>(getSymbol());
307 static bool isKind(const SVal& V) {
308 return V.getBaseKind() == NonLocKind &&
309 V.getSubKind() == SymbolValKind;
312 static bool isKind(const NonLoc& V) {
313 return V.getSubKind() == SymbolValKind;
317 /// \brief Value representing integer constant.
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;
337 static bool isKind(const SVal& V) {
338 return V.getBaseKind() == NonLocKind &&
339 V.getSubKind() == ConcreteIntKind;
342 static bool isKind(const NonLoc& V) {
343 return V.getSubKind() == ConcreteIntKind;
347 class LocAsInteger : public NonLoc {
348 friend class ento::SValBuilder;
350 explicit LocAsInteger(const std::pair<SVal, uintptr_t> &data)
351 : NonLoc(LocAsIntegerKind, &data) {
352 assert (data.first.getAs<Loc>());
358 const std::pair<SVal, uintptr_t> *D =
359 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
360 return D->first.castAs<Loc>();
363 Loc getPersistentLoc() const {
364 const std::pair<SVal, uintptr_t> *D =
365 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
366 const SVal& V = D->first;
367 return V.castAs<Loc>();
370 unsigned getNumBits() const {
371 const std::pair<SVal, uintptr_t> *D =
372 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
379 static bool isKind(const SVal& V) {
380 return V.getBaseKind() == NonLocKind &&
381 V.getSubKind() == LocAsIntegerKind;
384 static bool isKind(const NonLoc& V) {
385 return V.getSubKind() == LocAsIntegerKind;
389 class CompoundVal : public NonLoc {
390 friend class ento::SValBuilder;
392 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
395 const CompoundValData* getValue() const {
396 return static_cast<const CompoundValData*>(Data);
399 typedef llvm::ImmutableList<SVal>::iterator iterator;
400 iterator begin() const;
401 iterator end() const;
406 static bool isKind(const SVal& V) {
407 return V.getBaseKind() == NonLocKind && V.getSubKind() == CompoundValKind;
410 static bool isKind(const NonLoc& V) {
411 return V.getSubKind() == CompoundValKind;
415 class LazyCompoundVal : public NonLoc {
416 friend class ento::SValBuilder;
418 explicit LazyCompoundVal(const LazyCompoundValData *D)
419 : NonLoc(LazyCompoundValKind, D) {}
421 const LazyCompoundValData *getCVData() const {
422 return static_cast<const LazyCompoundValData*>(Data);
424 const void *getStore() const;
425 const TypedValueRegion *getRegion() const;
430 static bool isKind(const SVal& V) {
431 return V.getBaseKind() == NonLocKind &&
432 V.getSubKind() == LazyCompoundValKind;
434 static bool isKind(const NonLoc& V) {
435 return V.getSubKind() == LazyCompoundValKind;
439 } // end namespace ento::nonloc
441 //==------------------------------------------------------------------------==//
442 // Subclasses of Loc.
443 //==------------------------------------------------------------------------==//
447 enum Kind { GotoLabelKind, MemRegionKind, ConcreteIntKind };
449 class GotoLabel : public Loc {
451 explicit GotoLabel(LabelDecl *Label) : Loc(GotoLabelKind, Label) {}
453 const LabelDecl *getLabel() const {
454 return static_cast<const LabelDecl*>(Data);
460 static bool isKind(const SVal& V) {
461 return V.getBaseKind() == LocKind && V.getSubKind() == GotoLabelKind;
464 static bool isKind(const Loc& V) {
465 return V.getSubKind() == GotoLabelKind;
470 class MemRegionVal : public Loc {
472 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionKind, r) {}
474 /// \brief Get the underlining region.
475 const MemRegion* getRegion() const {
476 return static_cast<const MemRegion*>(Data);
479 /// \brief Get the underlining region and strip casts.
480 const MemRegion* stripCasts(bool StripBaseCasts = true) const;
482 template <typename REGION>
483 const REGION* getRegionAs() const {
484 return dyn_cast<REGION>(getRegion());
487 inline bool operator==(const MemRegionVal& R) const {
488 return getRegion() == R.getRegion();
491 inline bool operator!=(const MemRegionVal& R) const {
492 return getRegion() != R.getRegion();
498 static bool isKind(const SVal& V) {
499 return V.getBaseKind() == LocKind &&
500 V.getSubKind() == MemRegionKind;
503 static bool isKind(const Loc& V) {
504 return V.getSubKind() == MemRegionKind;
508 class ConcreteInt : public Loc {
510 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
512 const llvm::APSInt& getValue() const {
513 return *static_cast<const llvm::APSInt*>(Data);
516 // Transfer functions for binary/unary operations on ConcreteInts.
517 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
518 const ConcreteInt& R) const;
523 static bool isKind(const SVal& V) {
524 return V.getBaseKind() == LocKind &&
525 V.getSubKind() == ConcreteIntKind;
528 static bool isKind(const Loc& V) {
529 return V.getSubKind() == ConcreteIntKind;
533 } // end ento::loc namespace
534 } // end GR namespace
536 } // end clang namespace
539 static inline raw_ostream &operator<<(raw_ostream &os,
540 clang::ento::SVal V) {
545 } // end llvm namespace