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) {}
74 assert(T::isKind(*this));
82 llvm::Optional<T> getAs() const {
83 if (!T::isKind(*this))
84 return llvm::Optional<T>();
91 /// BufferTy - A temporary buffer to hold a set of SVals.
92 typedef SmallVector<SVal,5> BufferTy;
94 inline unsigned getRawKind() const { return Kind; }
95 inline BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
96 inline unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
98 // This method is required for using SVal in a FoldingSetNode. It
99 // extracts a unique signature for this SVal object.
100 inline void Profile(llvm::FoldingSetNodeID& ID) const {
101 ID.AddInteger((unsigned) getRawKind());
105 inline bool operator==(const SVal& R) const {
106 return getRawKind() == R.getRawKind() && Data == R.Data;
109 inline bool operator!=(const SVal& R) const {
110 return !(*this == R);
113 inline bool isUnknown() const {
114 return getRawKind() == UnknownKind;
117 inline bool isUndef() const {
118 return getRawKind() == UndefinedKind;
121 inline bool isUnknownOrUndef() const {
122 return getRawKind() <= UnknownKind;
125 inline bool isValid() const {
126 return getRawKind() > UnknownKind;
129 bool isConstant() const;
131 bool isConstant(int I) const;
133 bool isZeroConstant() const;
135 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
136 bool hasConjuredSymbol() const;
138 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
139 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
140 /// Otherwise return 0.
141 const FunctionDecl *getAsFunctionDecl() const;
143 /// If this SVal is a location (subclasses Loc) and
144 /// wraps a symbol, return that SymbolRef. Otherwise return 0.
145 SymbolRef getAsLocSymbol() const;
147 /// Get the symbol in the SVal or its base region.
148 SymbolRef getLocSymbolInBase() const;
150 /// If this SVal wraps a symbol return that SymbolRef.
151 /// Otherwise, return 0.
152 SymbolRef getAsSymbol() const;
154 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
155 /// return that expression. Otherwise return NULL.
156 const SymExpr *getAsSymbolicExpression() const;
158 const SymExpr* getAsSymExpr() const;
160 const MemRegion *getAsRegion() const;
162 void dumpToStream(raw_ostream &OS) const;
165 SymExpr::symbol_iterator symbol_begin() const {
166 const SymExpr *SE = getAsSymbolicExpression();
168 return SE->symbol_begin();
170 return SymExpr::symbol_iterator();
173 SymExpr::symbol_iterator symbol_end() const {
174 return SymExpr::symbol_end();
179 class UndefinedVal : public SVal {
181 UndefinedVal() : SVal(UndefinedKind) {}
185 static bool isKind(const SVal& V) {
186 return V.getBaseKind() == UndefinedKind;
190 class DefinedOrUnknownSVal : public SVal {
192 // We want calling these methods to be a compiler error since they are
193 // tautologically false.
194 bool isUndef() const LLVM_DELETED_FUNCTION;
195 bool isValid() const LLVM_DELETED_FUNCTION;
198 DefinedOrUnknownSVal() {}
199 explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
200 : SVal(d, isLoc, ValKind) {}
202 explicit DefinedOrUnknownSVal(BaseKind k, void *D = NULL)
207 static bool isKind(const SVal& V) {
212 class UnknownVal : public DefinedOrUnknownSVal {
214 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownKind) {}
218 static bool isKind(const SVal& V) {
219 return V.getBaseKind() == UnknownKind;
223 class DefinedSVal : public DefinedOrUnknownSVal {
225 // We want calling these methods to be a compiler error since they are
226 // tautologically true/false.
227 bool isUnknown() const LLVM_DELETED_FUNCTION;
228 bool isUnknownOrUndef() const LLVM_DELETED_FUNCTION;
229 bool isValid() const LLVM_DELETED_FUNCTION;
232 explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
233 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
236 static bool isKind(const SVal& V) {
237 return !V.isUnknownOrUndef();
241 class NonLoc : public DefinedSVal {
244 explicit NonLoc(unsigned SubKind, const void *d)
245 : DefinedSVal(d, false, SubKind) {}
248 void dumpToStream(raw_ostream &Out) const;
252 static bool isKind(const SVal& V) {
253 return V.getBaseKind() == NonLocKind;
257 class Loc : public DefinedSVal {
260 explicit Loc(unsigned SubKind, const void *D)
261 : DefinedSVal(const_cast<void*>(D), true, SubKind) {}
264 void dumpToStream(raw_ostream &Out) const;
266 static inline bool isLocType(QualType T) {
267 return T->isAnyPointerType() || T->isBlockPointerType() ||
268 T->isReferenceType();
273 static bool isKind(const SVal& V) {
274 return V.getBaseKind() == LocKind;
278 //==------------------------------------------------------------------------==//
279 // Subclasses of NonLoc.
280 //==------------------------------------------------------------------------==//
284 enum Kind { ConcreteIntKind, SymbolValKind,
285 LocAsIntegerKind, CompoundValKind, LazyCompoundValKind };
287 /// \brief Represents symbolic expression.
288 class SymbolVal : public NonLoc {
290 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {}
292 SymbolRef getSymbol() const {
293 return (const SymExpr*) Data;
296 bool isExpression() const {
297 return !isa<SymbolData>(getSymbol());
303 static bool isKind(const SVal& V) {
304 return V.getBaseKind() == NonLocKind &&
305 V.getSubKind() == SymbolValKind;
308 static bool isKind(const NonLoc& V) {
309 return V.getSubKind() == SymbolValKind;
313 /// \brief Value representing integer constant.
314 class ConcreteInt : public NonLoc {
316 explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
318 const llvm::APSInt& getValue() const {
319 return *static_cast<const llvm::APSInt*>(Data);
322 // Transfer functions for binary/unary operations on ConcreteInts.
323 SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
324 const ConcreteInt& R) const;
326 ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
328 ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
333 static bool isKind(const SVal& V) {
334 return V.getBaseKind() == NonLocKind &&
335 V.getSubKind() == ConcreteIntKind;
338 static bool isKind(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 (data.first.getAs<Loc>());
354 const std::pair<SVal, uintptr_t> *D =
355 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
356 return D->first.castAs<Loc>();
359 Loc getPersistentLoc() const {
360 const std::pair<SVal, uintptr_t> *D =
361 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
362 const SVal& V = D->first;
363 return V.castAs<Loc>();
366 unsigned getNumBits() const {
367 const std::pair<SVal, uintptr_t> *D =
368 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
375 static bool isKind(const SVal& V) {
376 return V.getBaseKind() == NonLocKind &&
377 V.getSubKind() == LocAsIntegerKind;
380 static bool isKind(const NonLoc& V) {
381 return V.getSubKind() == LocAsIntegerKind;
385 class CompoundVal : public NonLoc {
386 friend class ento::SValBuilder;
388 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
391 const CompoundValData* getValue() const {
392 return static_cast<const CompoundValData*>(Data);
395 typedef llvm::ImmutableList<SVal>::iterator iterator;
396 iterator begin() const;
397 iterator end() const;
402 static bool isKind(const SVal& V) {
403 return V.getBaseKind() == NonLocKind && V.getSubKind() == CompoundValKind;
406 static bool isKind(const NonLoc& V) {
407 return V.getSubKind() == CompoundValKind;
411 class LazyCompoundVal : public NonLoc {
412 friend class ento::SValBuilder;
414 explicit LazyCompoundVal(const LazyCompoundValData *D)
415 : NonLoc(LazyCompoundValKind, D) {}
417 const LazyCompoundValData *getCVData() const {
418 return static_cast<const LazyCompoundValData*>(Data);
420 const void *getStore() const;
421 const TypedValueRegion *getRegion() const;
426 static bool isKind(const SVal& V) {
427 return V.getBaseKind() == NonLocKind &&
428 V.getSubKind() == LazyCompoundValKind;
430 static bool isKind(const NonLoc& V) {
431 return V.getSubKind() == LazyCompoundValKind;
435 } // end namespace ento::nonloc
437 //==------------------------------------------------------------------------==//
438 // Subclasses of Loc.
439 //==------------------------------------------------------------------------==//
443 enum Kind { GotoLabelKind, MemRegionKind, ConcreteIntKind };
445 class GotoLabel : public Loc {
447 explicit GotoLabel(LabelDecl *Label) : Loc(GotoLabelKind, Label) {}
449 const LabelDecl *getLabel() const {
450 return static_cast<const LabelDecl*>(Data);
456 static bool isKind(const SVal& V) {
457 return V.getBaseKind() == LocKind && V.getSubKind() == GotoLabelKind;
460 static bool isKind(const Loc& V) {
461 return V.getSubKind() == GotoLabelKind;
466 class MemRegionVal : public Loc {
468 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionKind, r) {}
470 /// \brief Get the underlining region.
471 const MemRegion* getRegion() const {
472 return static_cast<const MemRegion*>(Data);
475 /// \brief Get the underlining region and strip casts.
476 const MemRegion* stripCasts(bool StripBaseCasts = true) const;
478 template <typename REGION>
479 const REGION* getRegionAs() const {
480 return dyn_cast<REGION>(getRegion());
483 inline bool operator==(const MemRegionVal& R) const {
484 return getRegion() == R.getRegion();
487 inline bool operator!=(const MemRegionVal& R) const {
488 return getRegion() != R.getRegion();
494 static bool isKind(const SVal& V) {
495 return V.getBaseKind() == LocKind &&
496 V.getSubKind() == MemRegionKind;
499 static bool isKind(const Loc& V) {
500 return V.getSubKind() == MemRegionKind;
504 class ConcreteInt : public Loc {
506 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
508 const llvm::APSInt& getValue() const {
509 return *static_cast<const llvm::APSInt*>(Data);
512 // Transfer functions for binary/unary operations on ConcreteInts.
513 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
514 const ConcreteInt& R) const;
519 static bool isKind(const SVal& V) {
520 return V.getBaseKind() == LocKind &&
521 V.getSubKind() == ConcreteIntKind;
524 static bool isKind(const Loc& V) {
525 return V.getSubKind() == ConcreteIntKind;
529 } // end ento::loc namespace
530 } // end GR namespace
532 } // end clang namespace
535 static inline raw_ostream &operator<<(raw_ostream &os,
536 clang::ento::SVal V) {
541 } // end llvm namespace