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/StaticAnalyzer/Core/PathSensitive/SVals.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
34 /// Manager of APSInt values.
35 BasicValueFactory BasicVals;
37 /// Manages the creation of symbols.
40 /// Manages the creation of memory regions.
41 MemRegionManager MemMgr;
43 ProgramStateManager &StateMgr;
45 /// The scalar type to use for array indices.
46 const QualType ArrayIndexTy;
48 /// The width of the scalar type used for array indices.
49 const unsigned ArrayIndexWidth;
52 // FIXME: Make these protected again once RegionStoreManager correctly
53 // handles loads from different bound value types.
54 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
55 virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
58 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
59 ProgramStateManager &stateMgr)
60 : Context(context), BasicVals(context, alloc),
61 SymMgr(context, BasicVals, alloc),
62 MemMgr(context, alloc),
64 ArrayIndexTy(context.IntTy),
65 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
67 virtual ~SValBuilder() {}
69 SVal evalCast(SVal val, QualType castTy, QualType originalType);
71 virtual SVal evalMinus(NonLoc val) = 0;
73 virtual SVal evalComplement(NonLoc val) = 0;
75 virtual SVal evalBinOpNN(const ProgramState *state, BinaryOperator::Opcode op,
76 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
78 virtual SVal evalBinOpLL(const ProgramState *state, BinaryOperator::Opcode op,
79 Loc lhs, Loc rhs, QualType resultTy) = 0;
81 virtual SVal evalBinOpLN(const ProgramState *state, BinaryOperator::Opcode op,
82 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
84 /// getKnownValue - evaluates a given SVal. If the SVal has only one possible
85 /// (integer) value, that value is returned. Otherwise, returns NULL.
86 virtual const llvm::APSInt *getKnownValue(const ProgramState *state, SVal val) = 0;
88 SVal evalBinOp(const ProgramState *state, BinaryOperator::Opcode op,
89 SVal lhs, SVal rhs, QualType type);
91 DefinedOrUnknownSVal evalEQ(const ProgramState *state, DefinedOrUnknownSVal lhs,
92 DefinedOrUnknownSVal rhs);
94 ASTContext &getContext() { return Context; }
95 const ASTContext &getContext() const { return Context; }
97 ProgramStateManager &getStateManager() { return StateMgr; }
99 QualType getConditionType() const {
100 return getContext().IntTy;
103 QualType getArrayIndexType() const {
107 BasicValueFactory &getBasicValueFactory() { return BasicVals; }
108 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
110 SymbolManager &getSymbolManager() { return SymMgr; }
111 const SymbolManager &getSymbolManager() const { return SymMgr; }
113 MemRegionManager &getRegionManager() { return MemMgr; }
114 const MemRegionManager &getRegionManager() const { return MemMgr; }
116 // Forwarding methods to SymbolManager.
118 const SymbolConjured* getConjuredSymbol(const Stmt *stmt, QualType type,
120 const void *symbolTag = 0) {
121 return SymMgr.getConjuredSymbol(stmt, type, visitCount, symbolTag);
124 const SymbolConjured* getConjuredSymbol(const Expr *expr, unsigned visitCount,
125 const void *symbolTag = 0) {
126 return SymMgr.getConjuredSymbol(expr, visitCount, symbolTag);
129 /// makeZeroVal - Construct an SVal representing '0' for the specified type.
130 DefinedOrUnknownSVal makeZeroVal(QualType type);
132 /// getRegionValueSymbolVal - make a unique symbol for value of region.
133 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
135 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
136 const Expr *expr, unsigned count);
137 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
138 const Expr *expr, QualType type,
141 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
142 SymbolRef parentSymbol, const TypedValueRegion *region);
144 DefinedSVal getMetadataSymbolVal(
145 const void *symbolTag, const MemRegion *region,
146 const Expr *expr, QualType type, unsigned count);
148 DefinedSVal getFunctionPointer(const FunctionDecl *func);
150 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
151 const LocationContext *locContext);
153 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
154 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
157 NonLoc makeLazyCompoundVal(const StoreRef &store,
158 const TypedValueRegion *region) {
159 return nonloc::LazyCompoundVal(
160 BasicVals.getLazyCompoundValData(store, region));
163 NonLoc makeZeroArrayIndex() {
164 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
167 NonLoc makeArrayIndex(uint64_t idx) {
168 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
171 SVal convertToArrayIndex(SVal val);
173 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
174 return nonloc::ConcreteInt(
175 BasicVals.getValue(integer->getValue(),
176 integer->getType()->isUnsignedIntegerOrEnumerationType()));
179 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean) {
180 return makeTruthVal(boolean->getValue());
183 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
184 return nonloc::ConcreteInt(BasicVals.getValue(integer));
187 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
188 return loc::ConcreteInt(BasicVals.getValue(integer));
191 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
192 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
195 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
196 if (Loc::isLocType(type))
197 return loc::ConcreteInt(BasicVals.getValue(integer, type));
199 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
202 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
203 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
206 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
207 return nonloc::ConcreteInt(
208 BasicVals.getIntWithPtrWidth(integer, isUnsigned));
211 NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
212 return nonloc::ConcreteInt(
213 BasicVals.getValue(integer, bitWidth, isUnsigned));
216 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
217 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
220 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
221 const llvm::APSInt& rhs, QualType type);
223 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
224 const SymExpr *rhs, QualType type);
226 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
227 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
230 nonloc::ConcreteInt makeTruthVal(bool b) {
231 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
235 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
238 Loc makeLoc(SymbolRef sym) {
239 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
242 Loc makeLoc(const MemRegion* region) {
243 return loc::MemRegionVal(region);
246 Loc makeLoc(const AddrLabelExpr *expr) {
247 return loc::GotoLabel(expr->getLabel());
250 Loc makeLoc(const llvm::APSInt& integer) {
251 return loc::ConcreteInt(BasicVals.getValue(integer));
256 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
258 ProgramStateManager &stateMgr);
260 } // end GR namespace
262 } // end clang namespace