SValBuilder.h revision 426a16d4e5efe7efefe76c405207fb170cabad9f
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  /// Construct an SVal representing '0' for the specified type.
144  DefinedOrUnknownSVal makeZeroVal(QualType type);
145
146  /// Make a unique symbol for value of region.
147  DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
148
149  /// \brief Create a new symbol with a unique 'name'.
150  ///
151  /// We resort to conjured symbols when we cannot construct a derived symbol.
152  /// The advantage of symbols derived/built from other symbols is that we
153  /// preserve the relation between related(or even equivalent) expressions, so
154  /// conjured symbols should be used sparingly.
155  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
156                                            const Expr *expr, unsigned count);
157  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
158                                            const Expr *expr, QualType type,
159                                            unsigned count);
160
161  DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
162      SymbolRef parentSymbol, const TypedValueRegion *region);
163
164  DefinedSVal getMetadataSymbolVal(
165      const void *symbolTag, const MemRegion *region,
166      const Expr *expr, QualType type, unsigned count);
167
168  DefinedSVal getFunctionPointer(const FunctionDecl *func);
169
170  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
171                              const LocationContext *locContext);
172
173  NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
174    return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
175  }
176
177  NonLoc makeLazyCompoundVal(const StoreRef &store,
178                             const TypedValueRegion *region) {
179    return nonloc::LazyCompoundVal(
180        BasicVals.getLazyCompoundValData(store, region));
181  }
182
183  NonLoc makeZeroArrayIndex() {
184    return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
185  }
186
187  NonLoc makeArrayIndex(uint64_t idx) {
188    return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
189  }
190
191  SVal convertToArrayIndex(SVal val);
192
193  nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
194    return nonloc::ConcreteInt(
195        BasicVals.getValue(integer->getValue(),
196                     integer->getType()->isUnsignedIntegerOrEnumerationType()));
197  }
198
199  nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean) {
200    return makeTruthVal(boolean->getValue());
201  }
202
203  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
204    return nonloc::ConcreteInt(BasicVals.getValue(integer));
205  }
206
207  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
208    return loc::ConcreteInt(BasicVals.getValue(integer));
209  }
210
211  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
212    return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
213  }
214
215  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
216    if (Loc::isLocType(type))
217      return loc::ConcreteInt(BasicVals.getValue(integer, type));
218
219    return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
220  }
221
222  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
223    return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
224  }
225
226  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
227    return nonloc::ConcreteInt(
228        BasicVals.getIntWithPtrWidth(integer, isUnsigned));
229  }
230
231  NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
232    return nonloc::ConcreteInt(
233        BasicVals.getValue(integer, bitWidth, isUnsigned));
234  }
235
236  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
237    return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
238  }
239
240  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
241                    const llvm::APSInt& rhs, QualType type);
242
243  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
244                    const SymExpr *rhs, QualType type);
245
246  nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
247    return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
248  }
249
250  nonloc::ConcreteInt makeTruthVal(bool b) {
251    return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
252  }
253
254  Loc makeNull() {
255    return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
256  }
257
258  Loc makeLoc(SymbolRef sym) {
259    return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
260  }
261
262  Loc makeLoc(const MemRegion* region) {
263    return loc::MemRegionVal(region);
264  }
265
266  Loc makeLoc(const AddrLabelExpr *expr) {
267    return loc::GotoLabel(expr->getLabel());
268  }
269
270  Loc makeLoc(const llvm::APSInt& integer) {
271    return loc::ConcreteInt(BasicVals.getValue(integer));
272  }
273
274};
275
276SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
277                                     ASTContext &context,
278                                     ProgramStateManager &stateMgr);
279
280} // end GR namespace
281
282} // end clang namespace
283
284#endif
285