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/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/ExprObjC.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.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(Context), Sym2->getType(Context));
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 (Ty2->isIntegerType() && Ty2->isIntegerType()));
84 SVal evalCast(SVal val, QualType castTy, QualType originalType);
86 virtual SVal evalMinus(NonLoc val) = 0;
88 virtual SVal evalComplement(NonLoc val) = 0;
90 /// Create a new value which represents a binary expression with two non
91 /// location operands.
92 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
93 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
95 /// Create a new value which represents a binary expression with two memory
96 /// location operands.
97 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
98 Loc lhs, Loc rhs, QualType resultTy) = 0;
100 /// Create a new value which represents a binary expression with a memory
101 /// location and non location operands. For example, this would be used to
102 /// evaluate a pointer arithmetic operation.
103 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
104 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
106 /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
107 /// that value is returned. Otherwise, returns NULL.
108 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
110 /// Handles generation of the value in case the builder is not smart enough to
111 /// handle the given binary expression. Depending on the state, decides to
112 /// either keep the expression or forget the history and generate an
114 SVal makeGenericVal(ProgramStateRef state, BinaryOperator::Opcode op,
115 NonLoc lhs, NonLoc rhs, QualType resultTy);
117 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
118 SVal lhs, SVal rhs, QualType type);
120 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
121 DefinedOrUnknownSVal rhs);
123 ASTContext &getContext() { return Context; }
124 const ASTContext &getContext() const { return Context; }
126 ProgramStateManager &getStateManager() { return StateMgr; }
128 QualType getConditionType() const {
129 return getContext().IntTy;
132 QualType getArrayIndexType() const {
136 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
137 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
139 SymbolManager &getSymbolManager() { return SymMgr; }
140 const SymbolManager &getSymbolManager() const { return SymMgr; }
142 MemRegionManager &getRegionManager() { return MemMgr; }
143 const MemRegionManager &getRegionManager() const { return MemMgr; }
145 // Forwarding methods to SymbolManager.
147 const SymbolConjured* getConjuredSymbol(const Stmt *stmt,
148 const LocationContext *LCtx,
151 const void *symbolTag = 0) {
152 return SymMgr.getConjuredSymbol(stmt, LCtx, type, visitCount, symbolTag);
155 const SymbolConjured* getConjuredSymbol(const Expr *expr,
156 const LocationContext *LCtx,
158 const void *symbolTag = 0) {
159 return SymMgr.getConjuredSymbol(expr, LCtx, visitCount, symbolTag);
162 /// Construct an SVal representing '0' for the specified type.
163 DefinedOrUnknownSVal makeZeroVal(QualType type);
165 /// Make a unique symbol for value of region.
166 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
168 /// \brief Create a new symbol with a unique 'name'.
170 /// We resort to conjured symbols when we cannot construct a derived symbol.
171 /// The advantage of symbols derived/built from other symbols is that we
172 /// preserve the relation between related(or even equivalent) expressions, so
173 /// conjured symbols should be used sparingly.
174 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
176 const LocationContext *LCtx,
178 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
180 const LocationContext *LCtx,
184 DefinedOrUnknownSVal getConjuredSymbolVal(const Stmt *stmt,
185 const LocationContext *LCtx,
187 unsigned visitCount);
189 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
190 SymbolRef parentSymbol, const TypedValueRegion *region);
192 DefinedSVal getMetadataSymbolVal(
193 const void *symbolTag, const MemRegion *region,
194 const Expr *expr, QualType type, unsigned count);
196 DefinedSVal getFunctionPointer(const FunctionDecl *func);
198 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
199 const LocationContext *locContext);
201 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
202 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
205 NonLoc makeLazyCompoundVal(const StoreRef &store,
206 const TypedValueRegion *region) {
207 return nonloc::LazyCompoundVal(
208 BasicVals.getLazyCompoundValData(store, region));
211 NonLoc makeZeroArrayIndex() {
212 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
215 NonLoc makeArrayIndex(uint64_t idx) {
216 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
219 SVal convertToArrayIndex(SVal val);
221 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
222 return nonloc::ConcreteInt(
223 BasicVals.getValue(integer->getValue(),
224 integer->getType()->isUnsignedIntegerOrEnumerationType()));
227 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
228 return makeTruthVal(boolean->getValue(), boolean->getType());
231 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
233 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
234 return nonloc::ConcreteInt(BasicVals.getValue(integer));
237 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
238 return loc::ConcreteInt(BasicVals.getValue(integer));
241 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
242 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
245 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
246 if (Loc::isLocType(type))
247 return loc::ConcreteInt(BasicVals.getValue(integer, type));
249 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
252 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
253 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
256 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
257 return nonloc::ConcreteInt(
258 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
261 NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
262 return nonloc::ConcreteInt(
263 BasicVals.getValue(integer, bitWidth, isUnsigned));
266 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
267 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
270 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
271 const llvm::APSInt& rhs, QualType type);
273 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
274 const SymExpr *lhs, QualType type);
276 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
277 const SymExpr *rhs, QualType type);
279 /// \brief Create a NonLoc value for cast.
280 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
282 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
283 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
286 nonloc::ConcreteInt makeTruthVal(bool b) {
287 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
291 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
294 Loc makeLoc(SymbolRef sym) {
295 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
298 Loc makeLoc(const MemRegion* region) {
299 return loc::MemRegionVal(region);
302 Loc makeLoc(const AddrLabelExpr *expr) {
303 return loc::GotoLabel(expr->getLabel());
306 Loc makeLoc(const llvm::APSInt& integer) {
307 return loc::ConcreteInt(BasicVals.getValue(integer));
312 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
314 ProgramStateManager &stateMgr);
316 } // end GR namespace
318 } // end clang namespace