SValBuilder.h revision 1a45a5ff5d495cb6cd9a3d4d06317af79c0f634d
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/AST/ExprObjC.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
22#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
23#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
24
25namespace clang {
26
27class CXXBoolLiteralExpr;
28
29namespace ento {
30
31class SValBuilder {
32  virtual void anchor();
33protected:
34  ASTContext &Context;
35
36  /// Manager of APSInt values.
37  BasicValueFactory BasicVals;
38
39  /// Manages the creation of symbols.
40  SymbolManager SymMgr;
41
42  /// Manages the creation of memory regions.
43  MemRegionManager MemMgr;
44
45  ProgramStateManager &StateMgr;
46
47  /// The scalar type to use for array indices.
48  const QualType ArrayIndexTy;
49
50  /// The width of the scalar type used for array indices.
51  const unsigned ArrayIndexWidth;
52
53  virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
54  virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
55
56public:
57  // FIXME: Make these protected again once RegionStoreManager correctly
58  // handles loads from different bound value types.
59  virtual SVal dispatchCast(SVal val, QualType castTy) = 0;
60
61public:
62  SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
63              ProgramStateManager &stateMgr)
64    : Context(context), BasicVals(context, alloc),
65      SymMgr(context, BasicVals, alloc),
66      MemMgr(context, alloc),
67      StateMgr(stateMgr),
68      ArrayIndexTy(context.IntTy),
69      ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
70
71  virtual ~SValBuilder() {}
72
73  bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) {
74    return haveSameType(Sym1->getType(Context), Sym2->getType(Context));
75  }
76
77  bool haveSameType(QualType Ty1, QualType Ty2) {
78    // FIXME: Remove the second disjunct when we support symbolic
79    // truncation/extension.
80    return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) ||
81            (Ty2->isIntegerType() && Ty2->isIntegerType()));
82  }
83
84  SVal evalCast(SVal val, QualType castTy, QualType originalType);
85
86  virtual SVal evalMinus(NonLoc val) = 0;
87
88  virtual SVal evalComplement(NonLoc val) = 0;
89
90  /// Create a new value which represents a binary expression with two non
91  /// location operands.
92  virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
93                           NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
94
95  /// Create a new value which represents a binary expression with two memory
96  /// location operands.
97  virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
98                           Loc lhs, Loc rhs, QualType resultTy) = 0;
99
100  /// Create a new value which represents a binary expression with a memory
101  /// location and non location operands. For example, this would be used to
102  /// evaluate a pointer arithmetic operation.
103  virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
104                           Loc lhs, NonLoc rhs, QualType resultTy) = 0;
105
106  /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
107  /// that value is returned. Otherwise, returns NULL.
108  virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
109
110  /// Handles generation of the value in case the builder is not smart enough to
111  /// handle the given binary expression. Depending on the state, decides to
112  /// either keep the expression or forget the history and generate an
113  /// UnknownVal.
114  SVal makeGenericVal(ProgramStateRef state, BinaryOperator::Opcode op,
115                          NonLoc lhs, NonLoc rhs, QualType resultTy);
116
117  SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
118                 SVal lhs, SVal rhs, QualType type);
119
120  DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
121                              DefinedOrUnknownSVal rhs);
122
123  ASTContext &getContext() { return Context; }
124  const ASTContext &getContext() const { return Context; }
125
126  ProgramStateManager &getStateManager() { return StateMgr; }
127
128  QualType getConditionType() const {
129    return  getContext().IntTy;
130  }
131
132  QualType getArrayIndexType() const {
133    return ArrayIndexTy;
134  }
135
136  BasicValueFactory &getBasicValueFactory() { return BasicVals; }
137  const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
138
139  SymbolManager &getSymbolManager() { return SymMgr; }
140  const SymbolManager &getSymbolManager() const { return SymMgr; }
141
142  MemRegionManager &getRegionManager() { return MemMgr; }
143  const MemRegionManager &getRegionManager() const { return MemMgr; }
144
145  // Forwarding methods to SymbolManager.
146
147  const SymbolConjured* getConjuredSymbol(const Stmt *stmt,
148					  const LocationContext *LCtx,
149					  QualType type,
150                                          unsigned visitCount,
151                                          const void *symbolTag = 0) {
152    return SymMgr.getConjuredSymbol(stmt, LCtx, type, visitCount, symbolTag);
153  }
154
155  const SymbolConjured* getConjuredSymbol(const Expr *expr,
156					  const LocationContext *LCtx,
157					  unsigned visitCount,
158                                          const void *symbolTag = 0) {
159    return SymMgr.getConjuredSymbol(expr, LCtx, visitCount, symbolTag);
160  }
161
162  /// Construct an SVal representing '0' for the specified type.
163  DefinedOrUnknownSVal makeZeroVal(QualType type);
164
165  /// Make a unique symbol for value of region.
166  DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
167
168  /// \brief Create a new symbol with a unique 'name'.
169  ///
170  /// We resort to conjured symbols when we cannot construct a derived symbol.
171  /// The advantage of symbols derived/built from other symbols is that we
172  /// preserve the relation between related(or even equivalent) expressions, so
173  /// conjured symbols should be used sparingly.
174  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
175                                            const Expr *expr,
176					    const LocationContext *LCtx,
177					    unsigned count);
178  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
179                                            const Expr *expr,
180					    const LocationContext *LCtx,
181					    QualType type,
182                                            unsigned count);
183
184  DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
185      SymbolRef parentSymbol, const TypedValueRegion *region);
186
187  DefinedSVal getMetadataSymbolVal(
188      const void *symbolTag, const MemRegion *region,
189      const Expr *expr, QualType type, unsigned count);
190
191  DefinedSVal getFunctionPointer(const FunctionDecl *func);
192
193  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
194                              const LocationContext *locContext);
195
196  NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
197    return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
198  }
199
200  NonLoc makeLazyCompoundVal(const StoreRef &store,
201                             const TypedValueRegion *region) {
202    return nonloc::LazyCompoundVal(
203        BasicVals.getLazyCompoundValData(store, region));
204  }
205
206  NonLoc makeZeroArrayIndex() {
207    return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
208  }
209
210  NonLoc makeArrayIndex(uint64_t idx) {
211    return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
212  }
213
214  SVal convertToArrayIndex(SVal val);
215
216  nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
217    return nonloc::ConcreteInt(
218        BasicVals.getValue(integer->getValue(),
219                     integer->getType()->isUnsignedIntegerOrEnumerationType()));
220  }
221
222  nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
223    return makeTruthVal(boolean->getValue(), boolean->getType());
224  }
225
226  nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
227
228  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
229    return nonloc::ConcreteInt(BasicVals.getValue(integer));
230  }
231
232  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
233    return loc::ConcreteInt(BasicVals.getValue(integer));
234  }
235
236  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
237    return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
238  }
239
240  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
241    if (Loc::isLocType(type))
242      return loc::ConcreteInt(BasicVals.getValue(integer, type));
243
244    return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
245  }
246
247  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
248    return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
249  }
250
251  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
252    return nonloc::ConcreteInt(
253        BasicVals.getIntWithPtrWidth(integer, isUnsigned));
254  }
255
256  NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
257    return nonloc::ConcreteInt(
258        BasicVals.getValue(integer, bitWidth, isUnsigned));
259  }
260
261  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
262    return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
263  }
264
265  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
266                    const llvm::APSInt& rhs, QualType type);
267
268  NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
269                    const SymExpr *lhs, QualType type);
270
271  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
272                    const SymExpr *rhs, QualType type);
273
274  /// \brief Create a NonLoc value for cast.
275  NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
276
277  nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
278    return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
279  }
280
281  nonloc::ConcreteInt makeTruthVal(bool b) {
282    return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
283  }
284
285  Loc makeNull() {
286    return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
287  }
288
289  Loc makeLoc(SymbolRef sym) {
290    return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
291  }
292
293  Loc makeLoc(const MemRegion* region) {
294    return loc::MemRegionVal(region);
295  }
296
297  Loc makeLoc(const AddrLabelExpr *expr) {
298    return loc::GotoLabel(expr->getLabel());
299  }
300
301  Loc makeLoc(const llvm::APSInt& integer) {
302    return loc::ConcreteInt(BasicVals.getValue(integer));
303  }
304
305};
306
307SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
308                                     ASTContext &context,
309                                     ProgramStateManager &stateMgr);
310
311} // end GR namespace
312
313} // end clang namespace
314
315#endif
316