]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
Update llvm, clang and lldb to trunk r257626, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / StaticAnalyzer / Core / PathSensitive / SValBuilder.h
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_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
17
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprObjC.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
24
25 namespace clang {
26
27 class CXXBoolLiteralExpr;
28
29 namespace ento {
30
31 class SValBuilder {
32   virtual void anchor();
33 protected:
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
56 public:
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
61 public:
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(), Sym2->getType());
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->isIntegralOrEnumerationType() &&
82              Ty2->isIntegralOrEnumerationType()));
83   }
84
85   SVal evalCast(SVal val, QualType castTy, QualType originalType);
86
87   // Handles casts of type CK_IntegralCast.
88   SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy,
89                         QualType originalType);
90
91   virtual SVal evalMinus(NonLoc val) = 0;
92
93   virtual SVal evalComplement(NonLoc val) = 0;
94
95   /// Create a new value which represents a binary expression with two non-
96   /// location operands.
97   virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
98                            NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
99
100   /// Create a new value which represents a binary expression with two memory
101   /// location operands.
102   virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
103                            Loc lhs, Loc rhs, QualType resultTy) = 0;
104
105   /// Create a new value which represents a binary expression with a memory
106   /// location and non-location operands. For example, this would be used to
107   /// evaluate a pointer arithmetic operation.
108   virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
109                            Loc lhs, NonLoc rhs, QualType resultTy) = 0;
110
111   /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
112   /// that value is returned. Otherwise, returns NULL.
113   virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
114   
115   /// Constructs a symbolic expression for two non-location values.
116   SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op,
117                       NonLoc lhs, NonLoc rhs, QualType resultTy);
118
119   SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
120                  SVal lhs, SVal rhs, QualType type);
121   
122   DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs,
123                               DefinedOrUnknownSVal rhs);
124
125   ASTContext &getContext() { return Context; }
126   const ASTContext &getContext() const { return Context; }
127
128   ProgramStateManager &getStateManager() { return StateMgr; }
129   
130   QualType getConditionType() const {
131     return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
132   }
133   
134   QualType getArrayIndexType() const {
135     return ArrayIndexTy;
136   }
137
138   BasicValueFactory &getBasicValueFactory() { return BasicVals; }
139   const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
140
141   SymbolManager &getSymbolManager() { return SymMgr; }
142   const SymbolManager &getSymbolManager() const { return SymMgr; }
143
144   MemRegionManager &getRegionManager() { return MemMgr; }
145   const MemRegionManager &getRegionManager() const { return MemMgr; }
146
147   // Forwarding methods to SymbolManager.
148
149   const SymbolConjured* conjureSymbol(const Stmt *stmt,
150                                       const LocationContext *LCtx,
151                                       QualType type,
152                                       unsigned visitCount,
153                                       const void *symbolTag = nullptr) {
154     return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
155   }
156
157   const SymbolConjured* conjureSymbol(const Expr *expr,
158                                       const LocationContext *LCtx,
159                                       unsigned visitCount,
160                                       const void *symbolTag = nullptr) {
161     return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
162   }
163
164   /// Construct an SVal representing '0' for the specified type.
165   DefinedOrUnknownSVal makeZeroVal(QualType type);
166
167   /// Make a unique symbol for value of region.
168   DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region);
169
170   /// \brief Create a new symbol with a unique 'name'.
171   ///
172   /// We resort to conjured symbols when we cannot construct a derived symbol.
173   /// The advantage of symbols derived/built from other symbols is that we
174   /// preserve the relation between related(or even equivalent) expressions, so
175   /// conjured symbols should be used sparingly.
176   DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
177                                         const Expr *expr,
178                                         const LocationContext *LCtx,
179                                         unsigned count);
180   DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
181                                         const Expr *expr,
182                                         const LocationContext *LCtx,
183                                         QualType type,
184                                         unsigned count);
185   
186   DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
187                                         const LocationContext *LCtx,
188                                         QualType type,
189                                         unsigned visitCount);
190   /// \brief Conjure a symbol representing heap allocated memory region.
191   ///
192   /// Note, the expression should represent a location.
193   DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E,
194                                                 const LocationContext *LCtx,
195                                                 unsigned Count);
196
197   DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(
198       SymbolRef parentSymbol, const TypedValueRegion *region);
199
200   DefinedSVal getMetadataSymbolVal(
201       const void *symbolTag, const MemRegion *region,
202       const Expr *expr, QualType type, unsigned count);
203
204   DefinedSVal getFunctionPointer(const FunctionDecl *func);
205   
206   DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
207                               const LocationContext *locContext,
208                               unsigned blockCount);
209
210   /// Returns the value of \p E, if it can be determined in a non-path-sensitive
211   /// manner.
212   ///
213   /// If \p E is not a constant or cannot be modeled, returns \c None.
214   Optional<SVal> getConstantVal(const Expr *E);
215
216   NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
217     return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
218   }
219
220   NonLoc makeLazyCompoundVal(const StoreRef &store, 
221                              const TypedValueRegion *region) {
222     return nonloc::LazyCompoundVal(
223         BasicVals.getLazyCompoundValData(store, region));
224   }
225
226   NonLoc makeZeroArrayIndex() {
227     return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
228   }
229
230   NonLoc makeArrayIndex(uint64_t idx) {
231     return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
232   }
233
234   SVal convertToArrayIndex(SVal val);
235
236   nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) {
237     return nonloc::ConcreteInt(
238         BasicVals.getValue(integer->getValue(),
239                      integer->getType()->isUnsignedIntegerOrEnumerationType()));
240   }
241
242   nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) {
243     return makeTruthVal(boolean->getValue(), boolean->getType());
244   }
245
246   nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean);
247
248   nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
249     return nonloc::ConcreteInt(BasicVals.getValue(integer));
250   }
251
252   loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
253     return loc::ConcreteInt(BasicVals.getValue(integer));
254   }
255
256   NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
257     return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
258   }
259
260   DefinedSVal makeIntVal(uint64_t integer, QualType type) {
261     if (Loc::isLocType(type))
262       return loc::ConcreteInt(BasicVals.getValue(integer, type));
263
264     return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
265   }
266
267   NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
268     return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
269   }
270
271   NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
272     return nonloc::ConcreteInt(
273         BasicVals.getIntWithPtrWidth(integer, isUnsigned));
274   }
275
276   NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
277     return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
278   }
279
280   NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
281                     const llvm::APSInt& rhs, QualType type);
282
283   NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
284                     const SymExpr *lhs, QualType type);
285
286   NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
287                     const SymExpr *rhs, QualType type);
288
289   /// \brief Create a NonLoc value for cast.
290   NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
291
292   nonloc::ConcreteInt makeTruthVal(bool b, QualType type) {
293     return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
294   }
295
296   nonloc::ConcreteInt makeTruthVal(bool b) {
297     return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
298   }
299
300   Loc makeNull() {
301     return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
302   }
303
304   Loc makeLoc(SymbolRef sym) {
305     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
306   }
307
308   Loc makeLoc(const MemRegion* region) {
309     return loc::MemRegionVal(region);
310   }
311
312   Loc makeLoc(const AddrLabelExpr *expr) {
313     return loc::GotoLabel(expr->getLabel());
314   }
315
316   Loc makeLoc(const llvm::APSInt& integer) {
317     return loc::ConcreteInt(BasicVals.getValue(integer));
318   }
319
320   /// Return a memory region for the 'this' object reference.
321   loc::MemRegionVal getCXXThis(const CXXMethodDecl *D,
322                                const StackFrameContext *SFC);
323
324   /// Return a memory region for the 'this' object reference.
325   loc::MemRegionVal getCXXThis(const CXXRecordDecl *D,
326                                const StackFrameContext *SFC);
327 };
328
329 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
330                                      ASTContext &context,
331                                      ProgramStateManager &stateMgr);
332
333 } // end GR namespace
334
335 } // end clang namespace
336
337 #endif