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"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
28 class CXXBoolLiteralExpr;
33 virtual void anchor();
37 /// Manager of APSInt values.
38 BasicValueFactory BasicVals;
40 /// Manages the creation of symbols.
43 /// Manages the creation of memory regions.
44 MemRegionManager MemMgr;
46 ProgramStateManager &StateMgr;
48 /// The scalar type to use for array indices.
49 const QualType ArrayIndexTy;
51 /// The width of the scalar type used for array indices.
52 const unsigned ArrayIndexWidth;
54 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
55 virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
58 // FIXME: Make these protected again once RegionStoreManager correctly
59 // handles loads from different bound value types.
60 virtual SVal dispatchCast(SVal val, QualType castTy) = 0;
63 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
64 ProgramStateManager &stateMgr)
65 : Context(context), BasicVals(context, alloc),
66 SymMgr(context, BasicVals, alloc),
67 MemMgr(context, alloc),
69 ArrayIndexTy(context.LongLongTy),
70 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
72 virtual ~SValBuilder() {}
74 bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) {
75 return haveSameType(Sym1->getType(), Sym2->getType());
78 bool haveSameType(QualType Ty1, QualType Ty2) {
79 // FIXME: Remove the second disjunct when we support symbolic
80 // truncation/extension.
81 return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) ||
82 (Ty1->isIntegralOrEnumerationType() &&
83 Ty2->isIntegralOrEnumerationType()));
86 SVal evalCast(SVal val, QualType castTy, QualType originalType);
88 // Handles casts of type CK_IntegralCast.
89 SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy,
90 QualType originalType);
92 virtual SVal evalMinus(NonLoc val) = 0;
94 virtual SVal evalComplement(NonLoc val) = 0;
96 /// Create a new value which represents a binary expression with two non-
97 /// location operands.
98 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
99 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
101 /// Create a new value which represents a binary expression with two memory
102 /// location operands.
103 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
104 Loc lhs, Loc rhs, QualType resultTy) = 0;
106 /// Create a new value which represents a binary expression with a memory
107 /// location and non-location operands. For example, this would be used to
108 /// evaluate a pointer arithmetic operation.
109 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
110 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
112 /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
113 /// that value is returned. Otherwise, returns NULL.
114 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
116 /// Constructs a symbolic expression for two non-location values.
117 SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
118 NonLoc lhs, NonLoc rhs, QualType resultTy);
120 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
121 SVal lhs, SVal rhs, QualType type);
123 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
124 DefinedOrUnknownSVal rhs);
126 ASTContext &getContext() { return Context; }
127 const ASTContext &getContext() const { return Context; }
129 ProgramStateManager &getStateManager() { return StateMgr; }
131 QualType getConditionType() const {
132 return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
135 QualType getArrayIndexType() const {
139 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
140 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
142 SymbolManager &getSymbolManager() { return SymMgr; }
143 const SymbolManager &getSymbolManager() const { return SymMgr; }
145 MemRegionManager &getRegionManager() { return MemMgr; }
146 const MemRegionManager &getRegionManager() const { return MemMgr; }
148 // Forwarding methods to SymbolManager.
150 const SymbolConjured* conjureSymbol(const Stmt *stmt,
151 const LocationContext *LCtx,
154 const void *symbolTag = nullptr) {
155 return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
158 const SymbolConjured* conjureSymbol(const Expr *expr,
159 const LocationContext *LCtx,
161 const void *symbolTag = nullptr) {
162 return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
165 /// Construct an SVal representing '0' for the specified type.
166 DefinedOrUnknownSVal makeZeroVal(QualType type);
168 /// Make a unique symbol for value of region.
169 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
171 /// \brief Create a new symbol with a unique 'name'.
173 /// We resort to conjured symbols when we cannot construct a derived symbol.
174 /// The advantage of symbols derived/built from other symbols is that we
175 /// preserve the relation between related(or even equivalent) expressions, so
176 /// conjured symbols should be used sparingly.
177 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
179 const LocationContext *LCtx,
181 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
183 const LocationContext *LCtx,
187 DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
188 const LocationContext *LCtx,
190 unsigned visitCount);
191 /// \brief Conjure a symbol representing heap allocated memory region.
193 /// Note, the expression should represent a location.
194 DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E,
195 const LocationContext *LCtx,
198 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
199 SymbolRef parentSymbol, const TypedValueRegion *region);
201 DefinedSVal getMetadataSymbolVal(
202 const void *symbolTag, const MemRegion *region,
203 const Expr *expr, QualType type, unsigned count);
205 DefinedSVal getFunctionPointer(const FunctionDecl *func);
207 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
208 const LocationContext *locContext,
209 unsigned blockCount);
211 /// Returns the value of \p E, if it can be determined in a non-path-sensitive
214 /// If \p E is not a constant or cannot be modeled, returns \c None.
215 Optional<SVal> getConstantVal(const Expr *E);
217 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
218 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
221 NonLoc makeLazyCompoundVal(const StoreRef &store,
222 const TypedValueRegion *region) {
223 return nonloc::LazyCompoundVal(
224 BasicVals.getLazyCompoundValData(store, region));
227 NonLoc makeZeroArrayIndex() {
228 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
231 NonLoc makeArrayIndex(uint64_t idx) {
232 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
235 SVal convertToArrayIndex(SVal val);
237 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
238 return nonloc::ConcreteInt(
239 BasicVals.getValue(integer->getValue(),
240 integer->getType()->isUnsignedIntegerOrEnumerationType()));
243 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
244 return makeTruthVal(boolean->getValue(), boolean->getType());
247 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
249 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
250 return nonloc::ConcreteInt(BasicVals.getValue(integer));
253 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
254 return loc::ConcreteInt(BasicVals.getValue(integer));
257 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
258 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
261 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
262 if (Loc::isLocType(type))
263 return loc::ConcreteInt(BasicVals.getValue(integer, type));
265 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
268 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
269 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
272 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
273 return nonloc::ConcreteInt(
274 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
277 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
278 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
281 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
282 const llvm::APSInt& rhs, QualType type);
284 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
285 const SymExpr *lhs, QualType type);
287 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
288 const SymExpr *rhs, QualType type);
290 /// \brief Create a NonLoc value for cast.
291 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
293 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
294 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
297 nonloc::ConcreteInt makeTruthVal(bool b) {
298 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
302 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
305 Loc makeLoc(SymbolRef sym) {
306 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
309 Loc makeLoc(const MemRegion* region) {
310 return loc::MemRegionVal(region);
313 Loc makeLoc(const AddrLabelExpr *expr) {
314 return loc::GotoLabel(expr->getLabel());
317 Loc makeLoc(const llvm::APSInt& integer) {
318 return loc::ConcreteInt(BasicVals.getValue(integer));
321 /// Return a memory region for the 'this' object reference.
322 loc::MemRegionVal getCXXThis(const CXXMethodDecl *D,
323 const StackFrameContext *SFC);
325 /// Return a memory region for the 'this' object reference.
326 loc::MemRegionVal getCXXThis(const CXXRecordDecl *D,
327 const StackFrameContext *SFC);
330 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
332 ProgramStateManager &stateMgr);
334 } // end GR namespace
336 } // end clang namespace