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_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/ImmutableList.h"
25 //==------------------------------------------------------------------------==//
27 //==------------------------------------------------------------------------==//
33 class CompoundValData;
34 class LazyCompoundValData;
35 class PointerToMemberData;
37 class BasicValueFactory;
39 class TypedValueRegion;
40 class MemRegionManager;
41 class ProgramStateManager;
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 #define BASIC_SVAL(Id, Parent) Id ## Kind,
52 #define ABSTRACT_SVAL_WITH_KIND(Id, Parent) Id ## Kind,
53 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
55 enum { BaseBits = 2, BaseMask = 0x3 };
60 /// The lowest 2 bits are a BaseKind (0 -- 3).
61 /// The higher bits are an unsigned "kind" value.
64 explicit SVal(const void *d, bool isLoc, unsigned ValKind)
65 : Data(d), Kind((isLoc ? LocKind : NonLocKind) | (ValKind << BaseBits)) {}
67 explicit SVal(BaseKind k, const void *D = nullptr)
71 explicit SVal() : Data(nullptr), Kind(0) {}
73 /// \brief Convert to the specified SVal type, asserting that this SVal is of
77 assert(T::isKind(*this));
84 /// \brief Convert to the specified SVal type, returning None if this SVal is
85 /// not of the desired type.
87 Optional<T> getAs() const {
88 if (!T::isKind(*this))
96 /// BufferTy - A temporary buffer to hold a set of SVals.
97 typedef SmallVector<SVal,5> BufferTy;
99 inline unsigned getRawKind() const { return Kind; }
100 inline BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
101 inline unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
103 // This method is required for using SVal in a FoldingSetNode. It
104 // extracts a unique signature for this SVal object.
105 inline void Profile(llvm::FoldingSetNodeID& ID) const {
106 ID.AddInteger((unsigned) getRawKind());
110 inline bool operator==(const SVal& R) const {
111 return getRawKind() == R.getRawKind() && Data == R.Data;
114 inline bool operator!=(const SVal& R) const {
115 return !(*this == R);
118 inline bool isUnknown() const {
119 return getRawKind() == UnknownValKind;
122 inline bool isUndef() const {
123 return getRawKind() == UndefinedValKind;
126 inline bool isUnknownOrUndef() const {
127 return getRawKind() <= UnknownValKind;
130 inline bool isValid() const {
131 return getRawKind() > UnknownValKind;
134 bool isConstant() const;
136 bool isConstant(int I) const;
138 bool isZeroConstant() const;
140 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
141 bool hasConjuredSymbol() const;
143 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
144 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
145 /// Otherwise return 0.
146 const FunctionDecl *getAsFunctionDecl() const;
148 /// \brief If this SVal is a location and wraps a symbol, return that
149 /// SymbolRef. Otherwise return 0.
151 /// Casts are ignored during lookup.
152 /// \param IncludeBaseRegions The boolean that controls whether the search
153 /// should continue to the base regions if the region is not symbolic.
154 SymbolRef getAsLocSymbol(bool IncludeBaseRegions = false) const;
156 /// Get the symbol in the SVal or its base region.
157 SymbolRef getLocSymbolInBase() const;
159 /// \brief If this SVal wraps a symbol return that SymbolRef.
160 /// Otherwise, return 0.
162 /// Casts are ignored during lookup.
163 /// \param IncludeBaseRegions The boolean that controls whether the search
164 /// should continue to the base regions if the region is not symbolic.
165 SymbolRef getAsSymbol(bool IncludeBaseRegions = false) const;
167 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
168 /// return that expression. Otherwise return NULL.
169 const SymExpr *getAsSymbolicExpression() const;
171 const SymExpr* getAsSymExpr() const;
173 const MemRegion *getAsRegion() const;
175 void dumpToStream(raw_ostream &OS) const;
178 SymExpr::symbol_iterator symbol_begin() const {
179 const SymExpr *SE = getAsSymbolicExpression();
181 return SE->symbol_begin();
183 return SymExpr::symbol_iterator();
186 SymExpr::symbol_iterator symbol_end() const {
187 return SymExpr::symbol_end();
192 class UndefinedVal : public SVal {
194 UndefinedVal() : SVal(UndefinedValKind) {}
198 static bool isKind(const SVal& V) {
199 return V.getBaseKind() == UndefinedValKind;
203 class DefinedOrUnknownSVal : public SVal {
205 // We want calling these methods to be a compiler error since they are
206 // tautologically false.
207 bool isUndef() const = delete;
208 bool isValid() const = delete;
211 DefinedOrUnknownSVal() {}
212 explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
213 : SVal(d, isLoc, ValKind) {}
215 explicit DefinedOrUnknownSVal(BaseKind k, void *D = nullptr)
220 static bool isKind(const SVal& V) {
225 class UnknownVal : public DefinedOrUnknownSVal {
227 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownValKind) {}
231 static bool isKind(const SVal &V) {
232 return V.getBaseKind() == UnknownValKind;
236 class DefinedSVal : public DefinedOrUnknownSVal {
238 // We want calling these methods to be a compiler error since they are
239 // tautologically true/false.
240 bool isUnknown() const = delete;
241 bool isUnknownOrUndef() const = delete;
242 bool isValid() const = delete;
245 explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
246 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
249 static bool isKind(const SVal& V) {
250 return !V.isUnknownOrUndef();
255 /// \brief Represents an SVal that is guaranteed to not be UnknownVal.
256 class KnownSVal : public SVal {
259 static bool isKind(const SVal &V) {
260 return !V.isUnknown();
263 KnownSVal(const DefinedSVal &V) : SVal(V) {}
264 KnownSVal(const UndefinedVal &V) : SVal(V) {}
267 class NonLoc : public DefinedSVal {
270 explicit NonLoc(unsigned SubKind, const void *d)
271 : DefinedSVal(d, false, SubKind) {}
274 void dumpToStream(raw_ostream &Out) const;
278 static bool isKind(const SVal& V) {
279 return V.getBaseKind() == NonLocKind;
283 class Loc : public DefinedSVal {
286 explicit Loc(unsigned SubKind, const void *D)
287 : DefinedSVal(const_cast<void*>(D), true, SubKind) {}
290 void dumpToStream(raw_ostream &Out) const;
292 static inline bool isLocType(QualType T) {
293 return T->isAnyPointerType() || T->isBlockPointerType() ||
294 T->isReferenceType() || T->isNullPtrType();
299 static bool isKind(const SVal& V) {
300 return V.getBaseKind() == LocKind;
304 //==------------------------------------------------------------------------==//
305 // Subclasses of NonLoc.
306 //==------------------------------------------------------------------------==//
311 #define NONLOC_SVAL(Id, Parent) Id ## Kind,
312 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
315 /// \brief Represents symbolic expression.
316 class SymbolVal : public NonLoc {
318 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {}
320 SymbolRef getSymbol() const {
321 return (const SymExpr*) Data;
324 bool isExpression() const {
325 return !isa<SymbolData>(getSymbol());
331 static bool isKind(const SVal& V) {
332 return V.getBaseKind() == NonLocKind &&
333 V.getSubKind() == SymbolValKind;
336 static bool isKind(const NonLoc& V) {
337 return V.getSubKind() == SymbolValKind;
341 /// \brief Value representing integer constant.
342 class ConcreteInt : public NonLoc {
344 explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
346 const llvm::APSInt& getValue() const {
347 return *static_cast<const llvm::APSInt*>(Data);
350 // Transfer functions for binary/unary operations on ConcreteInts.
351 SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
352 const ConcreteInt& R) const;
354 ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
356 ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
361 static bool isKind(const SVal& V) {
362 return V.getBaseKind() == NonLocKind &&
363 V.getSubKind() == ConcreteIntKind;
366 static bool isKind(const NonLoc& V) {
367 return V.getSubKind() == ConcreteIntKind;
371 class LocAsInteger : public NonLoc {
372 friend class ento::SValBuilder;
374 explicit LocAsInteger(const std::pair<SVal, uintptr_t> &data)
375 : NonLoc(LocAsIntegerKind, &data) {
376 assert (data.first.getAs<Loc>());
382 const std::pair<SVal, uintptr_t> *D =
383 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
384 return D->first.castAs<Loc>();
387 Loc getPersistentLoc() const {
388 const std::pair<SVal, uintptr_t> *D =
389 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
390 const SVal& V = D->first;
391 return V.castAs<Loc>();
394 unsigned getNumBits() const {
395 const std::pair<SVal, uintptr_t> *D =
396 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
403 static bool isKind(const SVal& V) {
404 return V.getBaseKind() == NonLocKind &&
405 V.getSubKind() == LocAsIntegerKind;
408 static bool isKind(const NonLoc& V) {
409 return V.getSubKind() == LocAsIntegerKind;
413 class CompoundVal : public NonLoc {
414 friend class ento::SValBuilder;
416 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
419 const CompoundValData* getValue() const {
420 return static_cast<const CompoundValData*>(Data);
423 typedef llvm::ImmutableList<SVal>::iterator iterator;
424 iterator begin() const;
425 iterator end() const;
430 static bool isKind(const SVal& V) {
431 return V.getBaseKind() == NonLocKind && V.getSubKind() == CompoundValKind;
434 static bool isKind(const NonLoc& V) {
435 return V.getSubKind() == CompoundValKind;
439 class LazyCompoundVal : public NonLoc {
440 friend class ento::SValBuilder;
442 explicit LazyCompoundVal(const LazyCompoundValData *D)
443 : NonLoc(LazyCompoundValKind, D) {}
445 const LazyCompoundValData *getCVData() const {
446 return static_cast<const LazyCompoundValData*>(Data);
448 const void *getStore() const;
449 const TypedValueRegion *getRegion() const;
454 static bool isKind(const SVal& V) {
455 return V.getBaseKind() == NonLocKind &&
456 V.getSubKind() == LazyCompoundValKind;
458 static bool isKind(const NonLoc& V) {
459 return V.getSubKind() == LazyCompoundValKind;
463 /// \brief Value representing pointer-to-member.
465 /// This value is qualified as NonLoc because neither loading nor storing
466 /// operations are aplied to it. Instead, the analyzer uses the L-value coming
467 /// from pointer-to-member applied to an object.
468 /// This SVal is represented by a DeclaratorDecl which can be a member function
469 /// pointer or a member data pointer and a list of CXXBaseSpecifiers. This list
470 /// is required to accumulate the pointer-to-member cast history to figure out
471 /// the correct subobject field.
472 class PointerToMember : public NonLoc {
473 friend class ento::SValBuilder;
476 typedef llvm::PointerUnion<const DeclaratorDecl *,
477 const PointerToMemberData *> PTMDataType;
478 const PTMDataType getPTMData() const {
479 return PTMDataType::getFromOpaqueValue(const_cast<void *>(Data));
481 bool isNullMemberPointer() const {
482 return getPTMData().isNull();
484 const DeclaratorDecl *getDecl() const;
485 template<typename AdjustedDecl>
486 const AdjustedDecl* getDeclAs() const {
487 return dyn_cast_or_null<AdjustedDecl>(getDecl());
489 typedef llvm::ImmutableList<const CXXBaseSpecifier *>::iterator iterator;
490 iterator begin() const;
491 iterator end() const;
494 explicit PointerToMember(const PTMDataType D)
495 : NonLoc(PointerToMemberKind, D.getOpaqueValue()) {}
498 static bool isKind(const SVal& V) {
499 return V.getBaseKind() == NonLocKind &&
500 V.getSubKind() == PointerToMemberKind;
503 static bool isKind(const NonLoc& V) {
504 return V.getSubKind() == PointerToMemberKind;
508 } // end namespace ento::nonloc
510 //==------------------------------------------------------------------------==//
511 // Subclasses of Loc.
512 //==------------------------------------------------------------------------==//
517 #define LOC_SVAL(Id, Parent) Id ## Kind,
518 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
521 class GotoLabel : public Loc {
523 explicit GotoLabel(LabelDecl *Label) : Loc(GotoLabelKind, Label) {}
525 const LabelDecl *getLabel() const {
526 return static_cast<const LabelDecl*>(Data);
532 static bool isKind(const SVal& V) {
533 return V.getBaseKind() == LocKind && V.getSubKind() == GotoLabelKind;
536 static bool isKind(const Loc& V) {
537 return V.getSubKind() == GotoLabelKind;
542 class MemRegionVal : public Loc {
544 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionValKind, r) {}
546 /// \brief Get the underlining region.
547 const MemRegion* getRegion() const {
548 return static_cast<const MemRegion*>(Data);
551 /// \brief Get the underlining region and strip casts.
552 const MemRegion* stripCasts(bool StripBaseCasts = true) const;
554 template <typename REGION>
555 const REGION* getRegionAs() const {
556 return dyn_cast<REGION>(getRegion());
559 inline bool operator==(const MemRegionVal& R) const {
560 return getRegion() == R.getRegion();
563 inline bool operator!=(const MemRegionVal& R) const {
564 return getRegion() != R.getRegion();
570 static bool isKind(const SVal& V) {
571 return V.getBaseKind() == LocKind &&
572 V.getSubKind() == MemRegionValKind;
575 static bool isKind(const Loc& V) {
576 return V.getSubKind() == MemRegionValKind;
580 class ConcreteInt : public Loc {
582 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
584 const llvm::APSInt& getValue() const {
585 return *static_cast<const llvm::APSInt*>(Data);
588 // Transfer functions for binary/unary operations on ConcreteInts.
589 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
590 const ConcreteInt& R) const;
595 static bool isKind(const SVal& V) {
596 return V.getBaseKind() == LocKind &&
597 V.getSubKind() == ConcreteIntKind;
600 static bool isKind(const Loc& V) {
601 return V.getSubKind() == ConcreteIntKind;
605 } // end ento::loc namespace
607 } // end ento namespace
609 } // end clang namespace
612 static inline raw_ostream &operator<<(raw_ostream &os,
613 clang::ento::SVal V) {
618 template <typename T> struct isPodLike;
619 template <> struct isPodLike<clang::ento::SVal> {
620 static const bool value = true;
623 } // end llvm namespace