SValBuilder.h revision 0d339d06f8721d14befd6311bd306ac485772188
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 ProgramState;
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  ProgramStateManager &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              ProgramStateManager &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  /// Create a new value which represents a binary expression with two non
76  /// location operands.
77  virtual SVal evalBinOpNN(const ProgramState *state, BinaryOperator::Opcode op,
78                           NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
79
80  /// Create a new value which represents a binary expression with two memory
81  /// location operands.
82  virtual SVal evalBinOpLL(const ProgramState *state, BinaryOperator::Opcode op,
83                           Loc lhs, Loc rhs, QualType resultTy) = 0;
84
85  /// Create a new value which represents a binary expression with a memory
86  /// location and non location operands. For example, this would be used to
87  /// evaluate a pointer arithmetic operation.
88  virtual SVal evalBinOpLN(const ProgramState *state, BinaryOperator::Opcode op,
89                           Loc lhs, NonLoc rhs, QualType resultTy) = 0;
90
91  /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
92  /// that value is returned. Otherwise, returns NULL.
93  virtual const llvm::APSInt *getKnownValue(const ProgramState *state, SVal val) = 0;
94
95  /// Handles generation of the value in case the builder is not smart enough to
96  /// handle the given binary expression. Depending on the state, decides to
97  /// either keep the expression or forget the history and generate an
98  /// UnknownVal.
99  SVal generateUnknownVal(const ProgramState *state, BinaryOperator::Opcode op,
100                          NonLoc lhs, NonLoc rhs, QualType resultTy);
101
102  SVal evalBinOp(const ProgramState *state, BinaryOperator::Opcode op,
103                 SVal lhs, SVal rhs, QualType type);
104
105  DefinedOrUnknownSVal evalEQ(const ProgramState *state, DefinedOrUnknownSVal lhs,
106                              DefinedOrUnknownSVal rhs);
107
108  ASTContext &getContext() { return Context; }
109  const ASTContext &getContext() const { return Context; }
110
111  ProgramStateManager &getStateManager() { return StateMgr; }
112
113  QualType getConditionType() const {
114    return  getContext().IntTy;
115  }
116
117  QualType getArrayIndexType() const {
118    return ArrayIndexTy;
119  }
120
121  BasicValueFactory &getBasicValueFactory() { return BasicVals; }
122  const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
123
124  SymbolManager &getSymbolManager() { return SymMgr; }
125  const SymbolManager &getSymbolManager() const { return SymMgr; }
126
127  MemRegionManager &getRegionManager() { return MemMgr; }
128  const MemRegionManager &getRegionManager() const { return MemMgr; }
129
130  // Forwarding methods to SymbolManager.
131
132  const SymbolConjured* getConjuredSymbol(const Stmt *stmt, QualType type,
133                                          unsigned visitCount,
134                                          const void *symbolTag = 0) {
135    return SymMgr.getConjuredSymbol(stmt, type, visitCount, symbolTag);
136  }
137
138  const SymbolConjured* getConjuredSymbol(const Expr *expr, unsigned visitCount,
139                                          const void *symbolTag = 0) {
140    return SymMgr.getConjuredSymbol(expr, visitCount, symbolTag);
141  }
142
143  /// makeZeroVal - Construct an SVal representing '0' for the specified type.
144  DefinedOrUnknownSVal makeZeroVal(QualType type);
145
146  /// getRegionValueSymbolVal - make a unique symbol for value of region.
147  DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
148
149  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
150                                            const Expr *expr, unsigned count);
151  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
152                                            const Expr *expr, QualType type,
153                                            unsigned count);
154
155  DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
156      SymbolRef parentSymbol, const TypedValueRegion *region);
157
158  DefinedSVal getMetadataSymbolVal(
159      const void *symbolTag, const MemRegion *region,
160      const Expr *expr, QualType type, unsigned count);
161
162  DefinedSVal getFunctionPointer(const FunctionDecl *func);
163
164  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
165                              const LocationContext *locContext);
166
167  NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
168    return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
169  }
170
171  NonLoc makeLazyCompoundVal(const StoreRef &store,
172                             const TypedValueRegion *region) {
173    return nonloc::LazyCompoundVal(
174        BasicVals.getLazyCompoundValData(store, region));
175  }
176
177  NonLoc makeZeroArrayIndex() {
178    return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
179  }
180
181  NonLoc makeArrayIndex(uint64_t idx) {
182    return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
183  }
184
185  SVal convertToArrayIndex(SVal val);
186
187  nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
188    return nonloc::ConcreteInt(
189        BasicVals.getValue(integer->getValue(),
190                     integer->getType()->isUnsignedIntegerOrEnumerationType()));
191  }
192
193  nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean) {
194    return makeTruthVal(boolean->getValue());
195  }
196
197  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
198    return nonloc::ConcreteInt(BasicVals.getValue(integer));
199  }
200
201  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
202    return loc::ConcreteInt(BasicVals.getValue(integer));
203  }
204
205  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
206    return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
207  }
208
209  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
210    if (Loc::isLocType(type))
211      return loc::ConcreteInt(BasicVals.getValue(integer, type));
212
213    return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
214  }
215
216  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
217    return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
218  }
219
220  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
221    return nonloc::ConcreteInt(
222        BasicVals.getIntWithPtrWidth(integer, isUnsigned));
223  }
224
225  NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
226    return nonloc::ConcreteInt(
227        BasicVals.getValue(integer, bitWidth, isUnsigned));
228  }
229
230  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
231    return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
232  }
233
234  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
235                    const llvm::APSInt& rhs, QualType type);
236
237  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
238                    const SymExpr *rhs, QualType type);
239
240  nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
241    return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
242  }
243
244  nonloc::ConcreteInt makeTruthVal(bool b) {
245    return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
246  }
247
248  Loc makeNull() {
249    return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
250  }
251
252  Loc makeLoc(SymbolRef sym) {
253    return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
254  }
255
256  Loc makeLoc(const MemRegion* region) {
257    return loc::MemRegionVal(region);
258  }
259
260  Loc makeLoc(const AddrLabelExpr *expr) {
261    return loc::GotoLabel(expr->getLabel());
262  }
263
264  Loc makeLoc(const llvm::APSInt& integer) {
265    return loc::ConcreteInt(BasicVals.getValue(integer));
266  }
267
268};
269
270SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
271                                     ASTContext &context,
272                                     ProgramStateManager &stateMgr);
273
274} // end GR namespace
275
276} // end clang namespace
277
278#endif
279