SValBuilder.h revision 5e9ebb3c0fb554d9285aa99c470abdf283272bd9
1// SValBuilder.h - Construction of SVals from evaluating expressions -*- C++ -*-
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines SValBuilder, a class that defines the interface for
11//  "symbolical evaluators" which construct an SVal from an expression.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_GR_SVALBUILDER
16#define LLVM_CLANG_GR_SVALBUILDER
17
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"
23
24namespace clang {
25
26namespace ento {
27
28class GRState;
29
30class SValBuilder {
31protected:
32  ASTContext &Context;
33
34  /// Manager of APSInt values.
35  BasicValueFactory BasicVals;
36
37  /// Manages the creation of symbols.
38  SymbolManager SymMgr;
39
40  /// Manages the creation of memory regions.
41  MemRegionManager MemMgr;
42
43  GRStateManager &StateMgr;
44
45  /// The scalar type to use for array indices.
46  const QualType ArrayIndexTy;
47
48  /// The width of the scalar type used for array indices.
49  const unsigned ArrayIndexWidth;
50
51public:
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;
56
57public:
58  SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
59              GRStateManager &stateMgr)
60    : Context(context), BasicVals(context, alloc),
61      SymMgr(context, BasicVals, alloc),
62      MemMgr(context, alloc),
63      StateMgr(stateMgr),
64      ArrayIndexTy(context.IntTy),
65      ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
66
67  virtual ~SValBuilder() {}
68
69  SVal evalCast(SVal val, QualType castTy, QualType originalType);
70
71  virtual SVal evalMinus(NonLoc val) = 0;
72
73  virtual SVal evalComplement(NonLoc val) = 0;
74
75  virtual SVal evalBinOpNN(const GRState *state, BinaryOperator::Opcode op,
76                           NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
77
78  virtual SVal evalBinOpLL(const GRState *state, BinaryOperator::Opcode op,
79                           Loc lhs, Loc rhs, QualType resultTy) = 0;
80
81  virtual SVal evalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
82                           Loc lhs, NonLoc rhs, QualType resultTy) = 0;
83
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 GRState *state, SVal val) = 0;
87
88  SVal evalBinOp(const GRState *state, BinaryOperator::Opcode op,
89                 SVal lhs, SVal rhs, QualType type);
90
91  DefinedOrUnknownSVal evalEQ(const GRState *state, DefinedOrUnknownSVal lhs,
92                              DefinedOrUnknownSVal rhs);
93
94  ASTContext &getContext() { return Context; }
95  const ASTContext &getContext() const { return Context; }
96
97  GRStateManager &getStateManager() { return StateMgr; }
98
99  QualType getConditionType() const {
100    return  getContext().IntTy;
101  }
102
103  QualType getArrayIndexType() const {
104    return ArrayIndexTy;
105  }
106
107  BasicValueFactory &getBasicValueFactory() { return BasicVals; }
108  const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
109
110  SymbolManager &getSymbolManager() { return SymMgr; }
111  const SymbolManager &getSymbolManager() const { return SymMgr; }
112
113  MemRegionManager &getRegionManager() { return MemMgr; }
114  const MemRegionManager &getRegionManager() const { return MemMgr; }
115
116  // Forwarding methods to SymbolManager.
117
118  const SymbolConjured* getConjuredSymbol(const Stmt* stmt, QualType type,
119                                          unsigned visitCount,
120                                          const void* symbolTag = 0) {
121    return SymMgr.getConjuredSymbol(stmt, type, visitCount, symbolTag);
122  }
123
124  const SymbolConjured* getConjuredSymbol(const Expr* expr, unsigned visitCount,
125                                          const void* symbolTag = 0) {
126    return SymMgr.getConjuredSymbol(expr, visitCount, symbolTag);
127  }
128
129  /// makeZeroVal - Construct an SVal representing '0' for the specified type.
130  DefinedOrUnknownSVal makeZeroVal(QualType type);
131
132  /// getRegionValueSymbolVal - make a unique symbol for value of region.
133  DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedRegion *region);
134
135  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
136                                            const Expr *expr, unsigned count);
137  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
138                                            const Expr *expr, QualType type,
139                                            unsigned count);
140
141  DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
142      SymbolRef parentSymbol, const TypedRegion *region);
143
144  DefinedSVal getMetadataSymbolVal(
145      const void *symbolTag, const MemRegion *region,
146      const Expr *expr, QualType type, unsigned count);
147
148  DefinedSVal getFunctionPointer(const FunctionDecl *func);
149
150  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
151                              const LocationContext *locContext);
152
153  NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
154    return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
155  }
156
157  NonLoc makeLazyCompoundVal(const StoreRef &store, const TypedRegion *region) {
158    return nonloc::LazyCompoundVal(
159        BasicVals.getLazyCompoundValData(store, region));
160  }
161
162  NonLoc makeZeroArrayIndex() {
163    return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
164  }
165
166  NonLoc makeArrayIndex(uint64_t idx) {
167    return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
168  }
169
170  SVal convertToArrayIndex(SVal val);
171
172  nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
173    return nonloc::ConcreteInt(
174        BasicVals.getValue(integer->getValue(),
175                     integer->getType()->isUnsignedIntegerOrEnumerationType()));
176  }
177
178  nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean) {
179    return makeTruthVal(boolean->getValue());
180  }
181
182  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
183    return nonloc::ConcreteInt(BasicVals.getValue(integer));
184  }
185
186  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
187    return loc::ConcreteInt(BasicVals.getValue(integer));
188  }
189
190  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
191    return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
192  }
193
194  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
195    if (Loc::isLocType(type))
196      return loc::ConcreteInt(BasicVals.getValue(integer, type));
197
198    return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
199  }
200
201  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
202    return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
203  }
204
205  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
206    return nonloc::ConcreteInt(
207        BasicVals.getIntWithPtrWidth(integer, isUnsigned));
208  }
209
210  NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
211    return nonloc::ConcreteInt(
212        BasicVals.getValue(integer, bitWidth, isUnsigned));
213  }
214
215  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
216    return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
217  }
218
219  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
220                    const llvm::APSInt& rhs, QualType type);
221
222  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
223                    const SymExpr *rhs, QualType type);
224
225  nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
226    return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
227  }
228
229  nonloc::ConcreteInt makeTruthVal(bool b) {
230    return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
231  }
232
233  Loc makeNull() {
234    return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
235  }
236
237  Loc makeLoc(SymbolRef sym) {
238    return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
239  }
240
241  Loc makeLoc(const MemRegion* region) {
242    return loc::MemRegionVal(region);
243  }
244
245  Loc makeLoc(const AddrLabelExpr *expr) {
246    return loc::GotoLabel(expr->getLabel());
247  }
248
249  Loc makeLoc(const llvm::APSInt& integer) {
250    return loc::ConcreteInt(BasicVals.getValue(integer));
251  }
252
253};
254
255SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
256                                     ASTContext &context,
257                                     GRStateManager &stateMgr);
258
259} // end GR namespace
260
261} // end clang namespace
262
263#endif
264