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 /// Simplify symbolic expressions within a given SVal. Return an SVal
117 /// that represents the same value, but is hopefully easier to work with
118 /// than the original SVal.
119 virtual SVal simplifySVal(ProgramStateRef State, SVal Val) = 0;
121 /// Constructs a symbolic expression for two non-location values.
122 SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
123 NonLoc lhs, NonLoc rhs, QualType resultTy);
125 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
126 SVal lhs, SVal rhs, QualType type);
128 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
129 DefinedOrUnknownSVal rhs);
131 ASTContext &getContext() { return Context; }
132 const ASTContext &getContext() const { return Context; }
134 ProgramStateManager &getStateManager() { return StateMgr; }
136 QualType getConditionType() const {
137 return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
140 QualType getArrayIndexType() const {
144 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
145 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
147 SymbolManager &getSymbolManager() { return SymMgr; }
148 const SymbolManager &getSymbolManager() const { return SymMgr; }
150 MemRegionManager &getRegionManager() { return MemMgr; }
151 const MemRegionManager &getRegionManager() const { return MemMgr; }
153 // Forwarding methods to SymbolManager.
155 const SymbolConjured* conjureSymbol(const Stmt *stmt,
156 const LocationContext *LCtx,
159 const void *symbolTag = nullptr) {
160 return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
163 const SymbolConjured* conjureSymbol(const Expr *expr,
164 const LocationContext *LCtx,
166 const void *symbolTag = nullptr) {
167 return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
170 /// Construct an SVal representing '0' for the specified type.
171 DefinedOrUnknownSVal makeZeroVal(QualType type);
173 /// Make a unique symbol for value of region.
174 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
176 /// \brief Create a new symbol with a unique 'name'.
178 /// We resort to conjured symbols when we cannot construct a derived symbol.
179 /// The advantage of symbols derived/built from other symbols is that we
180 /// preserve the relation between related(or even equivalent) expressions, so
181 /// conjured symbols should be used sparingly.
182 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
184 const LocationContext *LCtx,
186 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
188 const LocationContext *LCtx,
192 DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
193 const LocationContext *LCtx,
195 unsigned visitCount);
196 /// \brief Conjure a symbol representing heap allocated memory region.
198 /// Note, the expression should represent a location.
199 DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E,
200 const LocationContext *LCtx,
203 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
204 SymbolRef parentSymbol, const TypedValueRegion *region);
206 DefinedSVal getMetadataSymbolVal(const void *symbolTag,
207 const MemRegion *region,
208 const Expr *expr, QualType type,
209 const LocationContext *LCtx,
212 DefinedSVal getMemberPointer(const DeclaratorDecl *DD);
214 DefinedSVal getFunctionPointer(const FunctionDecl *func);
216 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
217 const LocationContext *locContext,
218 unsigned blockCount);
220 /// Returns the value of \p E, if it can be determined in a non-path-sensitive
223 /// If \p E is not a constant or cannot be modeled, returns \c None.
224 Optional<SVal> getConstantVal(const Expr *E);
226 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
227 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
230 NonLoc makeLazyCompoundVal(const StoreRef &store,
231 const TypedValueRegion *region) {
232 return nonloc::LazyCompoundVal(
233 BasicVals.getLazyCompoundValData(store, region));
236 NonLoc makePointerToMember(const DeclaratorDecl *DD) {
237 return nonloc::PointerToMember(DD);
240 NonLoc makePointerToMember(const PointerToMemberData *PTMD) {
241 return nonloc::PointerToMember(PTMD);
244 NonLoc makeZeroArrayIndex() {
245 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
248 NonLoc makeArrayIndex(uint64_t idx) {
249 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
252 SVal convertToArrayIndex(SVal val);
254 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
255 return nonloc::ConcreteInt(
256 BasicVals.getValue(integer->getValue(),
257 integer->getType()->isUnsignedIntegerOrEnumerationType()));
260 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
261 return makeTruthVal(boolean->getValue(), boolean->getType());
264 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
266 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
267 return nonloc::ConcreteInt(BasicVals.getValue(integer));
270 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
271 return loc::ConcreteInt(BasicVals.getValue(integer));
274 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
275 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
278 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
279 if (Loc::isLocType(type))
280 return loc::ConcreteInt(BasicVals.getValue(integer, type));
282 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
285 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
286 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
289 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
290 return nonloc::ConcreteInt(
291 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
294 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
295 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
298 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
299 const llvm::APSInt& rhs, QualType type);
301 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
302 const SymExpr *lhs, QualType type);
304 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
305 const SymExpr *rhs, QualType type);
307 /// \brief Create a NonLoc value for cast.
308 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
310 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
311 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
314 nonloc::ConcreteInt makeTruthVal(bool b) {
315 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
319 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
322 Loc makeLoc(SymbolRef sym) {
323 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
326 Loc makeLoc(const MemRegion* region) {
327 return loc::MemRegionVal(region);
330 Loc makeLoc(const AddrLabelExpr *expr) {
331 return loc::GotoLabel(expr->getLabel());
334 Loc makeLoc(const llvm::APSInt& integer) {
335 return loc::ConcreteInt(BasicVals.getValue(integer));
338 /// Return a memory region for the 'this' object reference.
339 loc::MemRegionVal getCXXThis(const CXXMethodDecl *D,
340 const StackFrameContext *SFC);
342 /// Return a memory region for the 'this' object reference.
343 loc::MemRegionVal getCXXThis(const CXXRecordDecl *D,
344 const StackFrameContext *SFC);
347 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
349 ProgramStateManager &stateMgr);
351 } // end GR namespace
353 } // end clang namespace