1 //===- SVals.h - Abstract Values for Static Analysis ------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines SVal, Loc, and NonLoc, classes that represent
10 // abstract r-values for use with path-sensitive value tracking.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/Type.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/ImmutableList.h"
23 #include "llvm/ADT/None.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/PointerUnion.h"
26 #include "llvm/Support/Casting.h"
31 //==------------------------------------------------------------------------==//
33 //==------------------------------------------------------------------------==//
37 class CXXBaseSpecifier;
44 class BasicValueFactory;
45 class CompoundValData;
46 class LazyCompoundValData;
48 class PointerToMemberData;
50 class TypedValueRegion;
54 /// Sub-kinds for NonLoc values.
56 #define NONLOC_SVAL(Id, Parent) Id ## Kind,
57 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
64 /// Sub-kinds for Loc values.
66 #define LOC_SVAL(Id, Parent) Id ## Kind,
67 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
72 /// SVal - This represents a symbolic expression, which can be either
73 /// an L-value or an R-value.
78 // The enumerators must be representable using 2 bits.
79 #define BASIC_SVAL(Id, Parent) Id ## Kind,
80 #define ABSTRACT_SVAL_WITH_KIND(Id, Parent) Id ## Kind,
81 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
83 enum { BaseBits = 2, BaseMask = 0x3 };
86 const void *Data = nullptr;
88 /// The lowest 2 bits are a BaseKind (0 -- 3).
89 /// The higher bits are an unsigned "kind" value.
92 explicit SVal(const void *d, bool isLoc, unsigned ValKind)
93 : Data(d), Kind((isLoc ? LocKind : NonLocKind) | (ValKind << BaseBits)) {}
95 explicit SVal(BaseKind k, const void *D = nullptr) : Data(D), Kind(k) {}
98 explicit SVal() = default;
100 /// Convert to the specified SVal type, asserting that this SVal is of
101 /// the desired type.
104 assert(T::isKind(*this));
105 return *static_cast<const T *>(this);
108 /// Convert to the specified SVal type, returning None if this SVal is
109 /// not of the desired type.
111 Optional<T> getAs() const {
112 if (!T::isKind(*this))
114 return *static_cast<const T *>(this);
117 unsigned getRawKind() const { return Kind; }
118 BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
119 unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
121 // This method is required for using SVal in a FoldingSetNode. It
122 // extracts a unique signature for this SVal object.
123 void Profile(llvm::FoldingSetNodeID &ID) const {
124 ID.AddInteger((unsigned) getRawKind());
128 bool operator==(const SVal &R) const {
129 return getRawKind() == R.getRawKind() && Data == R.Data;
132 bool operator!=(const SVal &R) const {
133 return !(*this == R);
136 bool isUnknown() const {
137 return getRawKind() == UnknownValKind;
140 bool isUndef() const {
141 return getRawKind() == UndefinedValKind;
144 bool isUnknownOrUndef() const {
145 return getRawKind() <= UnknownValKind;
148 bool isValid() const {
149 return getRawKind() > UnknownValKind;
152 bool isConstant() const;
154 bool isConstant(int I) const;
156 bool isZeroConstant() const;
158 /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
159 bool hasConjuredSymbol() const;
161 /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
162 /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
163 /// Otherwise return 0.
164 const FunctionDecl *getAsFunctionDecl() const;
166 /// If this SVal is a location and wraps a symbol, return that
167 /// SymbolRef. Otherwise return 0.
169 /// Casts are ignored during lookup.
170 /// \param IncludeBaseRegions The boolean that controls whether the search
171 /// should continue to the base regions if the region is not symbolic.
172 SymbolRef getAsLocSymbol(bool IncludeBaseRegions = false) const;
174 /// Get the symbol in the SVal or its base region.
175 SymbolRef getLocSymbolInBase() const;
177 /// If this SVal wraps a symbol return that SymbolRef.
178 /// Otherwise, return 0.
180 /// Casts are ignored during lookup.
181 /// \param IncludeBaseRegions The boolean that controls whether the search
182 /// should continue to the base regions if the region is not symbolic.
183 SymbolRef getAsSymbol(bool IncludeBaseRegions = false) const;
185 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
186 /// return that expression. Otherwise return NULL.
187 const SymExpr *getAsSymbolicExpression() const;
189 const SymExpr *getAsSymExpr() const;
191 const MemRegion *getAsRegion() const;
193 /// printJson - Pretty-prints in JSON format.
194 void printJson(raw_ostream &Out, bool AddQuotes) const;
196 void dumpToStream(raw_ostream &OS) const;
199 SymExpr::symbol_iterator symbol_begin() const {
200 const SymExpr *SE = getAsSymbol(/*IncludeBaseRegions=*/true);
202 return SE->symbol_begin();
204 return SymExpr::symbol_iterator();
207 SymExpr::symbol_iterator symbol_end() const {
208 return SymExpr::symbol_end();
212 inline raw_ostream &operator<<(raw_ostream &os, clang::ento::SVal V) {
217 class UndefinedVal : public SVal {
219 UndefinedVal() : SVal(UndefinedValKind) {}
224 static bool isKind(const SVal& V) {
225 return V.getBaseKind() == UndefinedValKind;
229 class DefinedOrUnknownSVal : public SVal {
231 // We want calling these methods to be a compiler error since they are
232 // tautologically false.
233 bool isUndef() const = delete;
234 bool isValid() const = delete;
237 DefinedOrUnknownSVal() = default;
238 explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
239 : SVal(d, isLoc, ValKind) {}
240 explicit DefinedOrUnknownSVal(BaseKind k, void *D = nullptr) : SVal(k, D) {}
245 static bool isKind(const SVal& V) {
250 class UnknownVal : public DefinedOrUnknownSVal {
252 explicit UnknownVal() : DefinedOrUnknownSVal(UnknownValKind) {}
257 static bool isKind(const SVal &V) {
258 return V.getBaseKind() == UnknownValKind;
262 class DefinedSVal : public DefinedOrUnknownSVal {
264 // We want calling these methods to be a compiler error since they are
265 // tautologically true/false.
266 bool isUnknown() const = delete;
267 bool isUnknownOrUndef() const = delete;
268 bool isValid() const = delete;
271 DefinedSVal() = default;
272 explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
273 : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
278 static bool isKind(const SVal& V) {
279 return !V.isUnknownOrUndef();
283 /// Represents an SVal that is guaranteed to not be UnknownVal.
284 class KnownSVal : public SVal {
287 KnownSVal() = default;
289 static bool isKind(const SVal &V) {
290 return !V.isUnknown();
294 KnownSVal(const DefinedSVal &V) : SVal(V) {}
295 KnownSVal(const UndefinedVal &V) : SVal(V) {}
298 class NonLoc : public DefinedSVal {
301 explicit NonLoc(unsigned SubKind, const void *d)
302 : DefinedSVal(d, false, SubKind) {}
305 void dumpToStream(raw_ostream &Out) const;
307 static bool isCompoundType(QualType T) {
308 return T->isArrayType() || T->isRecordType() ||
309 T->isAnyComplexType() || T->isVectorType();
315 static bool isKind(const SVal& V) {
316 return V.getBaseKind() == NonLocKind;
320 class Loc : public DefinedSVal {
323 explicit Loc(unsigned SubKind, const void *D)
324 : DefinedSVal(const_cast<void *>(D), true, SubKind) {}
327 void dumpToStream(raw_ostream &Out) const;
329 static bool isLocType(QualType T) {
330 return T->isAnyPointerType() || T->isBlockPointerType() ||
331 T->isReferenceType() || T->isNullPtrType();
337 static bool isKind(const SVal& V) {
338 return V.getBaseKind() == LocKind;
342 //==------------------------------------------------------------------------==//
343 // Subclasses of NonLoc.
344 //==------------------------------------------------------------------------==//
348 /// Represents symbolic expression that isn't a location.
349 class SymbolVal : public NonLoc {
351 SymbolVal() = delete;
352 SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {
354 assert(!Loc::isLocType(sym->getType()));
357 SymbolRef getSymbol() const {
358 return (const SymExpr *) Data;
361 bool isExpression() const {
362 return !isa<SymbolData>(getSymbol());
368 static bool isKind(const SVal& V) {
369 return V.getBaseKind() == NonLocKind &&
370 V.getSubKind() == SymbolValKind;
373 static bool isKind(const NonLoc& V) {
374 return V.getSubKind() == SymbolValKind;
378 /// Value representing integer constant.
379 class ConcreteInt : public NonLoc {
381 explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
383 const llvm::APSInt& getValue() const {
384 return *static_cast<const llvm::APSInt *>(Data);
387 // Transfer functions for binary/unary operations on ConcreteInts.
388 SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
389 const ConcreteInt& R) const;
391 ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
393 ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
398 ConcreteInt() = default;
400 static bool isKind(const SVal& V) {
401 return V.getBaseKind() == NonLocKind &&
402 V.getSubKind() == ConcreteIntKind;
405 static bool isKind(const NonLoc& V) {
406 return V.getSubKind() == ConcreteIntKind;
410 class LocAsInteger : public NonLoc {
411 friend class ento::SValBuilder;
413 explicit LocAsInteger(const std::pair<SVal, uintptr_t> &data)
414 : NonLoc(LocAsIntegerKind, &data) {
415 // We do not need to represent loc::ConcreteInt as LocAsInteger,
416 // as it'd collapse into a nonloc::ConcreteInt instead.
417 assert(data.first.getBaseKind() == LocKind &&
418 (data.first.getSubKind() == loc::MemRegionValKind ||
419 data.first.getSubKind() == loc::GotoLabelKind));
424 const std::pair<SVal, uintptr_t> *D =
425 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
426 return D->first.castAs<Loc>();
429 Loc getPersistentLoc() const {
430 const std::pair<SVal, uintptr_t> *D =
431 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
432 const SVal& V = D->first;
433 return V.castAs<Loc>();
436 unsigned getNumBits() const {
437 const std::pair<SVal, uintptr_t> *D =
438 static_cast<const std::pair<SVal, uintptr_t> *>(Data);
445 LocAsInteger() = default;
447 static bool isKind(const SVal& V) {
448 return V.getBaseKind() == NonLocKind &&
449 V.getSubKind() == LocAsIntegerKind;
452 static bool isKind(const NonLoc& V) {
453 return V.getSubKind() == LocAsIntegerKind;
457 class CompoundVal : public NonLoc {
458 friend class ento::SValBuilder;
460 explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
463 const CompoundValData* getValue() const {
464 return static_cast<const CompoundValData *>(Data);
467 using iterator = llvm::ImmutableList<SVal>::iterator;
469 iterator begin() const;
470 iterator end() const;
475 CompoundVal() = default;
477 static bool isKind(const SVal& V) {
478 return V.getBaseKind() == NonLocKind && V.getSubKind() == CompoundValKind;
481 static bool isKind(const NonLoc& V) {
482 return V.getSubKind() == CompoundValKind;
486 class LazyCompoundVal : public NonLoc {
487 friend class ento::SValBuilder;
489 explicit LazyCompoundVal(const LazyCompoundValData *D)
490 : NonLoc(LazyCompoundValKind, D) {}
493 const LazyCompoundValData *getCVData() const {
494 return static_cast<const LazyCompoundValData *>(Data);
497 const void *getStore() const;
498 const TypedValueRegion *getRegion() const;
503 LazyCompoundVal() = default;
505 static bool isKind(const SVal& V) {
506 return V.getBaseKind() == NonLocKind &&
507 V.getSubKind() == LazyCompoundValKind;
510 static bool isKind(const NonLoc& V) {
511 return V.getSubKind() == LazyCompoundValKind;
515 /// Value representing pointer-to-member.
517 /// This value is qualified as NonLoc because neither loading nor storing
518 /// operations are applied to it. Instead, the analyzer uses the L-value coming
519 /// from pointer-to-member applied to an object.
520 /// This SVal is represented by a DeclaratorDecl which can be a member function
521 /// pointer or a member data pointer and a list of CXXBaseSpecifiers. This list
522 /// is required to accumulate the pointer-to-member cast history to figure out
523 /// the correct subobject field.
524 class PointerToMember : public NonLoc {
525 friend class ento::SValBuilder;
529 llvm::PointerUnion<const DeclaratorDecl *, const PointerToMemberData *>;
531 const PTMDataType getPTMData() const {
532 return PTMDataType::getFromOpaqueValue(const_cast<void *>(Data));
535 bool isNullMemberPointer() const;
537 const DeclaratorDecl *getDecl() const;
539 template<typename AdjustedDecl>
540 const AdjustedDecl *getDeclAs() const {
541 return dyn_cast_or_null<AdjustedDecl>(getDecl());
544 using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
546 iterator begin() const;
547 iterator end() const;
552 PointerToMember() = default;
553 explicit PointerToMember(const PTMDataType D)
554 : NonLoc(PointerToMemberKind, D.getOpaqueValue()) {}
556 static bool isKind(const SVal& V) {
557 return V.getBaseKind() == NonLocKind &&
558 V.getSubKind() == PointerToMemberKind;
561 static bool isKind(const NonLoc& V) {
562 return V.getSubKind() == PointerToMemberKind;
566 } // namespace nonloc
568 //==------------------------------------------------------------------------==//
569 // Subclasses of Loc.
570 //==------------------------------------------------------------------------==//
574 class GotoLabel : public Loc {
576 explicit GotoLabel(const LabelDecl *Label) : Loc(GotoLabelKind, Label) {
580 const LabelDecl *getLabel() const {
581 return static_cast<const LabelDecl *>(Data);
587 GotoLabel() = default;
589 static bool isKind(const SVal& V) {
590 return V.getBaseKind() == LocKind && V.getSubKind() == GotoLabelKind;
593 static bool isKind(const Loc& V) {
594 return V.getSubKind() == GotoLabelKind;
598 class MemRegionVal : public Loc {
600 explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionValKind, r) {
604 /// Get the underlining region.
605 const MemRegion *getRegion() const {
606 return static_cast<const MemRegion *>(Data);
609 /// Get the underlining region and strip casts.
610 const MemRegion* stripCasts(bool StripBaseCasts = true) const;
612 template <typename REGION>
613 const REGION* getRegionAs() const {
614 return dyn_cast<REGION>(getRegion());
617 bool operator==(const MemRegionVal &R) const {
618 return getRegion() == R.getRegion();
621 bool operator!=(const MemRegionVal &R) const {
622 return getRegion() != R.getRegion();
628 MemRegionVal() = default;
630 static bool isKind(const SVal& V) {
631 return V.getBaseKind() == LocKind &&
632 V.getSubKind() == MemRegionValKind;
635 static bool isKind(const Loc& V) {
636 return V.getSubKind() == MemRegionValKind;
640 class ConcreteInt : public Loc {
642 explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
644 const llvm::APSInt &getValue() const {
645 return *static_cast<const llvm::APSInt *>(Data);
648 // Transfer functions for binary/unary operations on ConcreteInts.
649 SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
650 const ConcreteInt& R) const;
655 ConcreteInt() = default;
657 static bool isKind(const SVal& V) {
658 return V.getBaseKind() == LocKind &&
659 V.getSubKind() == ConcreteIntKind;
662 static bool isKind(const Loc& V) {
663 return V.getSubKind() == ConcreteIntKind;
673 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H