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_GR_SVALBUILDER
16 #define LLVM_CLANG_GR_SVALBUILDER
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 virtual SVal evalMinus(NonLoc val) = 0;
89 virtual SVal evalComplement(NonLoc val) = 0;
91 /// Create a new value which represents a binary expression with two non
92 /// location operands.
93 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
94 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
96 /// Create a new value which represents a binary expression with two memory
97 /// location operands.
98 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
99 Loc lhs, Loc rhs, QualType resultTy) = 0;
101 /// Create a new value which represents a binary expression with a memory
102 /// location and non location operands. For example, this would be used to
103 /// evaluate a pointer arithmetic operation.
104 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
105 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
107 /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
108 /// that value is returned. Otherwise, returns NULL.
109 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
111 /// Constructs a symbolic expression for two non-location values.
112 SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
113 NonLoc lhs, NonLoc rhs, QualType resultTy);
115 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
116 SVal lhs, SVal rhs, QualType type);
118 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
119 DefinedOrUnknownSVal rhs);
121 ASTContext &getContext() { return Context; }
122 const ASTContext &getContext() const { return Context; }
124 ProgramStateManager &getStateManager() { return StateMgr; }
126 QualType getConditionType() const {
127 return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
130 QualType getArrayIndexType() const {
134 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
135 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
137 SymbolManager &getSymbolManager() { return SymMgr; }
138 const SymbolManager &getSymbolManager() const { return SymMgr; }
140 MemRegionManager &getRegionManager() { return MemMgr; }
141 const MemRegionManager &getRegionManager() const { return MemMgr; }
143 // Forwarding methods to SymbolManager.
145 const SymbolConjured* conjureSymbol(const Stmt *stmt,
146 const LocationContext *LCtx,
149 const void *symbolTag = 0) {
150 return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
153 const SymbolConjured* conjureSymbol(const Expr *expr,
154 const LocationContext *LCtx,
156 const void *symbolTag = 0) {
157 return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
160 /// Construct an SVal representing '0' for the specified type.
161 DefinedOrUnknownSVal makeZeroVal(QualType type);
163 /// Make a unique symbol for value of region.
164 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
166 /// \brief Create a new symbol with a unique 'name'.
168 /// We resort to conjured symbols when we cannot construct a derived symbol.
169 /// The advantage of symbols derived/built from other symbols is that we
170 /// preserve the relation between related(or even equivalent) expressions, so
171 /// conjured symbols should be used sparingly.
172 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
174 const LocationContext *LCtx,
176 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
178 const LocationContext *LCtx,
182 DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
183 const LocationContext *LCtx,
185 unsigned visitCount);
186 /// \brief Conjure a symbol representing heap allocated memory region.
188 /// Note, the expression should represent a location.
189 DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E,
190 const LocationContext *LCtx,
193 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
194 SymbolRef parentSymbol, const TypedValueRegion *region);
196 DefinedSVal getMetadataSymbolVal(
197 const void *symbolTag, const MemRegion *region,
198 const Expr *expr, QualType type, unsigned count);
200 DefinedSVal getFunctionPointer(const FunctionDecl *func);
202 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
203 const LocationContext *locContext);
205 /// Returns the value of \p E, if it can be determined in a non-path-sensitive
208 /// If \p E is not a constant or cannot be modeled, returns \c None.
209 Optional<SVal> getConstantVal(const Expr *E);
211 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
212 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
215 NonLoc makeLazyCompoundVal(const StoreRef &store,
216 const TypedValueRegion *region) {
217 return nonloc::LazyCompoundVal(
218 BasicVals.getLazyCompoundValData(store, region));
221 NonLoc makeZeroArrayIndex() {
222 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
225 NonLoc makeArrayIndex(uint64_t idx) {
226 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
229 SVal convertToArrayIndex(SVal val);
231 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
232 return nonloc::ConcreteInt(
233 BasicVals.getValue(integer->getValue(),
234 integer->getType()->isUnsignedIntegerOrEnumerationType()));
237 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
238 return makeTruthVal(boolean->getValue(), boolean->getType());
241 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
243 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
244 return nonloc::ConcreteInt(BasicVals.getValue(integer));
247 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
248 return loc::ConcreteInt(BasicVals.getValue(integer));
251 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
252 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
255 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
256 if (Loc::isLocType(type))
257 return loc::ConcreteInt(BasicVals.getValue(integer, type));
259 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
262 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
263 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
266 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
267 return nonloc::ConcreteInt(
268 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
271 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
272 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
275 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
276 const llvm::APSInt& rhs, QualType type);
278 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
279 const SymExpr *lhs, QualType type);
281 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
282 const SymExpr *rhs, QualType type);
284 /// \brief Create a NonLoc value for cast.
285 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
287 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
288 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
291 nonloc::ConcreteInt makeTruthVal(bool b) {
292 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
296 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
299 Loc makeLoc(SymbolRef sym) {
300 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
303 Loc makeLoc(const MemRegion* region) {
304 return loc::MemRegionVal(region);
307 Loc makeLoc(const AddrLabelExpr *expr) {
308 return loc::GotoLabel(expr->getLabel());
311 Loc makeLoc(const llvm::APSInt& integer) {
312 return loc::ConcreteInt(BasicVals.getValue(integer));
315 /// Return a memory region for the 'this' object reference.
316 loc::MemRegionVal getCXXThis(const CXXMethodDecl *D,
317 const StackFrameContext *SFC);
319 /// Return a memory region for the 'this' object reference.
320 loc::MemRegionVal getCXXThis(const CXXRecordDecl *D,
321 const StackFrameContext *SFC);
324 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
326 ProgramStateManager &stateMgr);
328 } // end GR namespace
330 } // end clang namespace