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/DeclarationName.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprObjC.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/Basic/LangOptions.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
30 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
31 #include "llvm/ADT/ImmutableList.h"
32 #include "llvm/ADT/Optional.h"
38 class CXXBoolLiteralExpr;
43 class LocationContext;
44 class StackFrameContext;
49 class ConditionTruthVal;
50 class ProgramStateManager;
54 virtual void anchor();
59 /// Manager of APSInt values.
60 BasicValueFactory BasicVals;
62 /// Manages the creation of symbols.
65 /// Manages the creation of memory regions.
66 MemRegionManager MemMgr;
68 ProgramStateManager &StateMgr;
70 /// The scalar type to use for array indices.
71 const QualType ArrayIndexTy;
73 /// The width of the scalar type used for array indices.
74 const unsigned ArrayIndexWidth;
76 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
77 virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
80 // FIXME: Make these protected again once RegionStoreManager correctly
81 // handles loads from different bound value types.
82 virtual SVal dispatchCast(SVal val, QualType castTy) = 0;
85 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
86 ProgramStateManager &stateMgr)
87 : Context(context), BasicVals(context, alloc),
88 SymMgr(context, BasicVals, alloc), MemMgr(context, alloc),
89 StateMgr(stateMgr), ArrayIndexTy(context.LongLongTy),
90 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
92 virtual ~SValBuilder() = default;
94 bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) {
95 return haveSameType(Sym1->getType(), Sym2->getType());
98 bool haveSameType(QualType Ty1, QualType Ty2) {
99 // FIXME: Remove the second disjunct when we support symbolic
100 // truncation/extension.
101 return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) ||
102 (Ty1->isIntegralOrEnumerationType() &&
103 Ty2->isIntegralOrEnumerationType()));
106 SVal evalCast(SVal val, QualType castTy, QualType originalType);
108 // Handles casts of type CK_IntegralCast.
109 SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy,
110 QualType originalType);
112 virtual SVal evalMinus(NonLoc val) = 0;
114 virtual SVal evalComplement(NonLoc val) = 0;
116 /// Create a new value which represents a binary expression with two non-
117 /// location operands.
118 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
119 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
121 /// Create a new value which represents a binary expression with two memory
122 /// location operands.
123 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
124 Loc lhs, Loc rhs, QualType resultTy) = 0;
126 /// Create a new value which represents a binary expression with a memory
127 /// location and non-location operands. For example, this would be used to
128 /// evaluate a pointer arithmetic operation.
129 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
130 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
132 /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
133 /// that value is returned. Otherwise, returns NULL.
134 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
136 /// Simplify symbolic expressions within a given SVal. Return an SVal
137 /// that represents the same value, but is hopefully easier to work with
138 /// than the original SVal.
139 virtual SVal simplifySVal(ProgramStateRef State, SVal Val) = 0;
141 /// Constructs a symbolic expression for two non-location values.
142 SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
143 NonLoc lhs, NonLoc rhs, QualType resultTy);
145 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
146 SVal lhs, SVal rhs, QualType type);
148 /// \return Whether values in \p lhs and \p rhs are equal at \p state.
149 ConditionTruthVal areEqual(ProgramStateRef state, SVal lhs, SVal rhs);
151 SVal evalEQ(ProgramStateRef state, SVal lhs, SVal rhs);
153 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
154 DefinedOrUnknownSVal rhs);
156 ASTContext &getContext() { return Context; }
157 const ASTContext &getContext() const { return Context; }
159 ProgramStateManager &getStateManager() { return StateMgr; }
161 QualType getConditionType() const {
162 return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
165 QualType getArrayIndexType() const {
169 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
170 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
172 SymbolManager &getSymbolManager() { return SymMgr; }
173 const SymbolManager &getSymbolManager() const { return SymMgr; }
175 MemRegionManager &getRegionManager() { return MemMgr; }
176 const MemRegionManager &getRegionManager() const { return MemMgr; }
178 // Forwarding methods to SymbolManager.
180 const SymbolConjured* conjureSymbol(const Stmt *stmt,
181 const LocationContext *LCtx,
184 const void *symbolTag = nullptr) {
185 return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
188 const SymbolConjured* conjureSymbol(const Expr *expr,
189 const LocationContext *LCtx,
191 const void *symbolTag = nullptr) {
192 return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
195 /// Construct an SVal representing '0' for the specified type.
196 DefinedOrUnknownSVal makeZeroVal(QualType type);
198 /// Make a unique symbol for value of region.
199 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
201 /// Create a new symbol with a unique 'name'.
203 /// We resort to conjured symbols when we cannot construct a derived symbol.
204 /// The advantage of symbols derived/built from other symbols is that we
205 /// preserve the relation between related(or even equivalent) expressions, so
206 /// conjured symbols should be used sparingly.
207 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
209 const LocationContext *LCtx,
211 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
213 const LocationContext *LCtx,
216 DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
217 const LocationContext *LCtx,
219 unsigned visitCount);
221 /// Conjure a symbol representing heap allocated memory region.
223 /// Note, the expression should represent a location.
224 DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E,
225 const LocationContext *LCtx,
228 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
229 SymbolRef parentSymbol, const TypedValueRegion *region);
231 DefinedSVal getMetadataSymbolVal(const void *symbolTag,
232 const MemRegion *region,
233 const Expr *expr, QualType type,
234 const LocationContext *LCtx,
237 DefinedSVal getMemberPointer(const DeclaratorDecl *DD);
239 DefinedSVal getFunctionPointer(const FunctionDecl *func);
241 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
242 const LocationContext *locContext,
243 unsigned blockCount);
245 /// Returns the value of \p E, if it can be determined in a non-path-sensitive
248 /// If \p E is not a constant or cannot be modeled, returns \c None.
249 Optional<SVal> getConstantVal(const Expr *E);
251 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
252 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
255 NonLoc makeLazyCompoundVal(const StoreRef &store,
256 const TypedValueRegion *region) {
257 return nonloc::LazyCompoundVal(
258 BasicVals.getLazyCompoundValData(store, region));
261 NonLoc makePointerToMember(const DeclaratorDecl *DD) {
262 return nonloc::PointerToMember(DD);
265 NonLoc makePointerToMember(const PointerToMemberData *PTMD) {
266 return nonloc::PointerToMember(PTMD);
269 NonLoc makeZeroArrayIndex() {
270 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
273 NonLoc makeArrayIndex(uint64_t idx) {
274 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
277 SVal convertToArrayIndex(SVal val);
279 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
280 return nonloc::ConcreteInt(
281 BasicVals.getValue(integer->getValue(),
282 integer->getType()->isUnsignedIntegerOrEnumerationType()));
285 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
286 return makeTruthVal(boolean->getValue(), boolean->getType());
289 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
291 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
292 return nonloc::ConcreteInt(BasicVals.getValue(integer));
295 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
296 return loc::ConcreteInt(BasicVals.getValue(integer));
299 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
300 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
303 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
304 if (Loc::isLocType(type))
305 return loc::ConcreteInt(BasicVals.getValue(integer, type));
307 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
310 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
311 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
314 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
315 return nonloc::ConcreteInt(
316 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
319 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
320 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
323 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
324 const llvm::APSInt& rhs, QualType type);
326 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
327 const SymExpr *lhs, QualType type);
329 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
330 const SymExpr *rhs, QualType type);
332 /// Create a NonLoc value for cast.
333 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
335 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
336 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
339 nonloc::ConcreteInt makeTruthVal(bool b) {
340 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
343 /// Create NULL pointer, with proper pointer bit-width for given address
345 /// \param type pointer type.
346 Loc makeNullWithType(QualType type) {
347 return loc::ConcreteInt(BasicVals.getZeroWithTypeSize(type));
351 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
354 Loc makeLoc(SymbolRef sym) {
355 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
358 Loc makeLoc(const MemRegion* region) {
359 return loc::MemRegionVal(region);
362 Loc makeLoc(const AddrLabelExpr *expr) {
363 return loc::GotoLabel(expr->getLabel());
366 Loc makeLoc(const llvm::APSInt& integer) {
367 return loc::ConcreteInt(BasicVals.getValue(integer));
370 /// Make an SVal that represents the given symbol. This follows the convention
371 /// of representing Loc-type symbols (symbolic pointers and references)
372 /// as Loc values wrapping the symbol rather than as plain symbol values.
373 SVal makeSymbolVal(SymbolRef Sym) {
374 if (Loc::isLocType(Sym->getType()))
376 return nonloc::SymbolVal(Sym);
379 /// Return a memory region for the 'this' object reference.
380 loc::MemRegionVal getCXXThis(const CXXMethodDecl *D,
381 const StackFrameContext *SFC);
383 /// Return a memory region for the 'this' object reference.
384 loc::MemRegionVal getCXXThis(const CXXRecordDecl *D,
385 const StackFrameContext *SFC);
388 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
390 ProgramStateManager &stateMgr);
396 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H