SValBuilder.h revision 009072fd2223c823032b804c64ca7b4c7c3badc4
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            (Ty1->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  /// Constructs a symbolic expression for two non-location values.
111  SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
112                      NonLoc lhs, NonLoc rhs, QualType resultTy);
113
114  SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
115                 SVal lhs, SVal rhs, QualType type);
116
117  DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
118                              DefinedOrUnknownSVal rhs);
119
120  ASTContext &getContext() { return Context; }
121  const ASTContext &getContext() const { return Context; }
122
123  ProgramStateManager &getStateManager() { return StateMgr; }
124
125  QualType getConditionType() const {
126    return  getContext().IntTy;
127  }
128
129  QualType getArrayIndexType() const {
130    return ArrayIndexTy;
131  }
132
133  BasicValueFactory &getBasicValueFactory() { return BasicVals; }
134  const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
135
136  SymbolManager &getSymbolManager() { return SymMgr; }
137  const SymbolManager &getSymbolManager() const { return SymMgr; }
138
139  MemRegionManager &getRegionManager() { return MemMgr; }
140  const MemRegionManager &getRegionManager() const { return MemMgr; }
141
142  // Forwarding methods to SymbolManager.
143
144  const SymbolConjured* getConjuredSymbol(const Stmt *stmt,
145                                          const LocationContext *LCtx,
146                                          QualType type,
147                                          unsigned visitCount,
148                                          const void *symbolTag = 0) {
149    return SymMgr.getConjuredSymbol(stmt, LCtx, type, visitCount, symbolTag);
150  }
151
152  const SymbolConjured* getConjuredSymbol(const Expr *expr,
153                                          const LocationContext *LCtx,
154                                          unsigned visitCount,
155                                          const void *symbolTag = 0) {
156    return SymMgr.getConjuredSymbol(expr, LCtx, visitCount, symbolTag);
157  }
158
159  /// Construct an SVal representing '0' for the specified type.
160  DefinedOrUnknownSVal makeZeroVal(QualType type);
161
162  /// Make a unique symbol for value of region.
163  DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
164
165  /// \brief Create a new symbol with a unique 'name'.
166  ///
167  /// We resort to conjured symbols when we cannot construct a derived symbol.
168  /// The advantage of symbols derived/built from other symbols is that we
169  /// preserve the relation between related(or even equivalent) expressions, so
170  /// conjured symbols should be used sparingly.
171  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
172                                            const Expr *expr,
173                                            const LocationContext *LCtx,
174                                            unsigned count);
175  DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag,
176                                            const Expr *expr,
177                                            const LocationContext *LCtx,
178                                            QualType type,
179                                            unsigned count);
180
181  DefinedOrUnknownSVal getConjuredSymbolVal(const Stmt *stmt,
182                                            const LocationContext *LCtx,
183                                            QualType type,
184                                            unsigned visitCount);
185
186  DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
187      SymbolRef parentSymbol, const TypedValueRegion *region);
188
189  DefinedSVal getMetadataSymbolVal(
190      const void *symbolTag, const MemRegion *region,
191      const Expr *expr, QualType type, unsigned count);
192
193  DefinedSVal getFunctionPointer(const FunctionDecl *func);
194
195  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
196                              const LocationContext *locContext);
197
198  NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
199    return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
200  }
201
202  NonLoc makeLazyCompoundVal(const StoreRef &store,
203                             const TypedValueRegion *region) {
204    return nonloc::LazyCompoundVal(
205        BasicVals.getLazyCompoundValData(store, region));
206  }
207
208  NonLoc makeZeroArrayIndex() {
209    return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
210  }
211
212  NonLoc makeArrayIndex(uint64_t idx) {
213    return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
214  }
215
216  SVal convertToArrayIndex(SVal val);
217
218  nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
219    return nonloc::ConcreteInt(
220        BasicVals.getValue(integer->getValue(),
221                     integer->getType()->isUnsignedIntegerOrEnumerationType()));
222  }
223
224  nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
225    return makeTruthVal(boolean->getValue(), boolean->getType());
226  }
227
228  nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
229
230  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
231    return nonloc::ConcreteInt(BasicVals.getValue(integer));
232  }
233
234  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
235    return loc::ConcreteInt(BasicVals.getValue(integer));
236  }
237
238  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
239    return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
240  }
241
242  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
243    if (Loc::isLocType(type))
244      return loc::ConcreteInt(BasicVals.getValue(integer, type));
245
246    return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
247  }
248
249  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
250    return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
251  }
252
253  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
254    return nonloc::ConcreteInt(
255        BasicVals.getIntWithPtrWidth(integer, isUnsigned));
256  }
257
258  NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) {
259    return nonloc::ConcreteInt(
260        BasicVals.getValue(integer, bitWidth, isUnsigned));
261  }
262
263  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
264    return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
265  }
266
267  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
268                    const llvm::APSInt& rhs, QualType type);
269
270  NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
271                    const SymExpr *lhs, QualType type);
272
273  NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
274                    const SymExpr *rhs, QualType type);
275
276  /// \brief Create a NonLoc value for cast.
277  NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
278
279  nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
280    return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
281  }
282
283  nonloc::ConcreteInt makeTruthVal(bool b) {
284    return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
285  }
286
287  Loc makeNull() {
288    return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
289  }
290
291  Loc makeLoc(SymbolRef sym) {
292    return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
293  }
294
295  Loc makeLoc(const MemRegion* region) {
296    return loc::MemRegionVal(region);
297  }
298
299  Loc makeLoc(const AddrLabelExpr *expr) {
300    return loc::GotoLabel(expr->getLabel());
301  }
302
303  Loc makeLoc(const llvm::APSInt& integer) {
304    return loc::ConcreteInt(BasicVals.getValue(integer));
305  }
306
307};
308
309SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
310                                     ASTContext &context,
311                                     ProgramStateManager &stateMgr);
312
313} // end GR namespace
314
315} // end clang namespace
316
317#endif
318