1 // SValBuilder.h - Construction of SVals from evaluating expressions -*- 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 SValBuilder, a class that defines the interface for
11 // "symbolical evaluators" which construct an SVal from an expression.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprObjC.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
27 class CXXBoolLiteralExpr;
32 virtual void anchor();
36 /// Manager of APSInt values.
37 BasicValueFactory BasicVals;
39 /// Manages the creation of symbols.
42 /// Manages the creation of memory regions.
43 MemRegionManager MemMgr;
45 ProgramStateManager &StateMgr;
47 /// The scalar type to use for array indices.
48 const QualType ArrayIndexTy;
50 /// The width of the scalar type used for array indices.
51 const unsigned ArrayIndexWidth;
53 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
54 virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
57 // FIXME: Make these protected again once RegionStoreManager correctly
58 // handles loads from different bound value types.
59 virtual SVal dispatchCast(SVal val, QualType castTy) = 0;
62 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
63 ProgramStateManager &stateMgr)
64 : Context(context), BasicVals(context, alloc),
65 SymMgr(context, BasicVals, alloc),
66 MemMgr(context, alloc),
68 ArrayIndexTy(context.IntTy),
69 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
71 virtual ~SValBuilder() {}
73 bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) {
74 return haveSameType(Sym1->getType(), Sym2->getType());
77 bool haveSameType(QualType Ty1, QualType Ty2) {
78 // FIXME: Remove the second disjunct when we support symbolic
79 // truncation/extension.
80 return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) ||
81 (Ty1->isIntegralOrEnumerationType() &&
82 Ty2->isIntegralOrEnumerationType()));
85 SVal evalCast(SVal val, QualType castTy, QualType originalType);
87 // Handles casts of type CK_IntegralCast.
88 SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy,
89 QualType originalType);
91 virtual SVal evalMinus(NonLoc val) = 0;
93 virtual SVal evalComplement(NonLoc val) = 0;
95 /// Create a new value which represents a binary expression with two non-
96 /// location operands.
97 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
98 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
100 /// Create a new value which represents a binary expression with two memory
101 /// location operands.
102 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
103 Loc lhs, Loc rhs, QualType resultTy) = 0;
105 /// Create a new value which represents a binary expression with a memory
106 /// location and non-location operands. For example, this would be used to
107 /// evaluate a pointer arithmetic operation.
108 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
109 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
111 /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
112 /// that value is returned. Otherwise, returns NULL.
113 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
115 /// Constructs a symbolic expression for two non-location values.
116 SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
117 NonLoc lhs, NonLoc rhs, QualType resultTy);
119 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
120 SVal lhs, SVal rhs, QualType type);
122 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
123 DefinedOrUnknownSVal rhs);
125 ASTContext &getContext() { return Context; }
126 const ASTContext &getContext() const { return Context; }
128 ProgramStateManager &getStateManager() { return StateMgr; }
130 QualType getConditionType() const {
131 return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
134 QualType getArrayIndexType() const {
138 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
139 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
141 SymbolManager &getSymbolManager() { return SymMgr; }
142 const SymbolManager &getSymbolManager() const { return SymMgr; }
144 MemRegionManager &getRegionManager() { return MemMgr; }
145 const MemRegionManager &getRegionManager() const { return MemMgr; }
147 // Forwarding methods to SymbolManager.
149 const SymbolConjured* conjureSymbol(const Stmt *stmt,
150 const LocationContext *LCtx,
153 const void *symbolTag = nullptr) {
154 return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
157 const SymbolConjured* conjureSymbol(const Expr *expr,
158 const LocationContext *LCtx,
160 const void *symbolTag = nullptr) {
161 return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
164 /// Construct an SVal representing '0' for the specified type.
165 DefinedOrUnknownSVal makeZeroVal(QualType type);
167 /// Make a unique symbol for value of region.
168 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
170 /// \brief Create a new symbol with a unique 'name'.
172 /// We resort to conjured symbols when we cannot construct a derived symbol.
173 /// The advantage of symbols derived/built from other symbols is that we
174 /// preserve the relation between related(or even equivalent) expressions, so
175 /// conjured symbols should be used sparingly.
176 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
178 const LocationContext *LCtx,
180 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
182 const LocationContext *LCtx,
186 DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
187 const LocationContext *LCtx,
189 unsigned visitCount);
190 /// \brief Conjure a symbol representing heap allocated memory region.
192 /// Note, the expression should represent a location.
193 DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E,
194 const LocationContext *LCtx,
197 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
198 SymbolRef parentSymbol, const TypedValueRegion *region);
200 DefinedSVal getMetadataSymbolVal(
201 const void *symbolTag, const MemRegion *region,
202 const Expr *expr, QualType type, unsigned count);
204 DefinedSVal getFunctionPointer(const FunctionDecl *func);
206 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
207 const LocationContext *locContext,
208 unsigned blockCount);
210 /// Returns the value of \p E, if it can be determined in a non-path-sensitive
213 /// If \p E is not a constant or cannot be modeled, returns \c None.
214 Optional<SVal> getConstantVal(const Expr *E);
216 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
217 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
220 NonLoc makeLazyCompoundVal(const StoreRef &store,
221 const TypedValueRegion *region) {
222 return nonloc::LazyCompoundVal(
223 BasicVals.getLazyCompoundValData(store, region));
226 NonLoc makeZeroArrayIndex() {
227 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
230 NonLoc makeArrayIndex(uint64_t idx) {
231 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
234 SVal convertToArrayIndex(SVal val);
236 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
237 return nonloc::ConcreteInt(
238 BasicVals.getValue(integer->getValue(),
239 integer->getType()->isUnsignedIntegerOrEnumerationType()));
242 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
243 return makeTruthVal(boolean->getValue(), boolean->getType());
246 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
248 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
249 return nonloc::ConcreteInt(BasicVals.getValue(integer));
252 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
253 return loc::ConcreteInt(BasicVals.getValue(integer));
256 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
257 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
260 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
261 if (Loc::isLocType(type))
262 return loc::ConcreteInt(BasicVals.getValue(integer, type));
264 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
267 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
268 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
271 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
272 return nonloc::ConcreteInt(
273 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
276 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
277 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
280 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
281 const llvm::APSInt& rhs, QualType type);
283 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
284 const SymExpr *lhs, QualType type);
286 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
287 const SymExpr *rhs, QualType type);
289 /// \brief Create a NonLoc value for cast.
290 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
292 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
293 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
296 nonloc::ConcreteInt makeTruthVal(bool b) {
297 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
301 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
304 Loc makeLoc(SymbolRef sym) {
305 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
308 Loc makeLoc(const MemRegion* region) {
309 return loc::MemRegionVal(region);
312 Loc makeLoc(const AddrLabelExpr *expr) {
313 return loc::GotoLabel(expr->getLabel());
316 Loc makeLoc(const llvm::APSInt& integer) {
317 return loc::ConcreteInt(BasicVals.getValue(integer));
320 /// Return a memory region for the 'this' object reference.
321 loc::MemRegionVal getCXXThis(const CXXMethodDecl *D,
322 const StackFrameContext *SFC);
324 /// Return a memory region for the 'this' object reference.
325 loc::MemRegionVal getCXXThis(const CXXRecordDecl *D,
326 const StackFrameContext *SFC);
329 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
331 ProgramStateManager &stateMgr);
333 } // end GR namespace
335 } // end clang namespace