]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
Update llvm/clang to trunk r126547.
[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_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
24 namespace clang {
25
26 namespace ento {
27
28 class GRState;
29
30 class SValBuilder {
31 protected:
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   GRStateManager &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
51 public:
52   // FIXME: Make these protected again one RegionStoreManager correctly
53   // handles loads from differening bound value types.
54   virtual SVal evalCastNL(NonLoc val, QualType castTy) = 0;
55   virtual SVal evalCastL(Loc val, QualType castTy) = 0;
56
57 public:
58   SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
59               GRStateManager &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 V, QualType castTy, QualType originalType);
70   
71   virtual SVal evalMinus(NonLoc val) = 0;
72
73   virtual SVal evalComplement(NonLoc val) = 0;
74
75   virtual SVal evalBinOpNN(const GRState *state, BinaryOperator::Opcode Op,
76                            NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
77
78   virtual SVal evalBinOpLL(const GRState *state, BinaryOperator::Opcode Op,
79                            Loc lhs, Loc rhs, QualType resultTy) = 0;
80
81   virtual SVal evalBinOpLN(const GRState *state, BinaryOperator::Opcode Op,
82                            Loc lhs, NonLoc rhs, QualType resultTy) = 0;
83
84   /// getKnownValue - evaluates a given SVal. If the SVal has only one possible
85   ///  (integer) value, that value is returned. Otherwise, returns NULL.
86   virtual const llvm::APSInt *getKnownValue(const GRState *state, SVal V) = 0;
87   
88   SVal evalBinOp(const GRState *ST, BinaryOperator::Opcode Op,
89                  SVal L, SVal R, QualType T);
90   
91   DefinedOrUnknownSVal evalEQ(const GRState *ST, DefinedOrUnknownSVal L,
92                               DefinedOrUnknownSVal R);
93
94   ASTContext &getContext() { return Context; }
95   const ASTContext &getContext() const { return Context; }
96
97   GRStateManager &getStateManager() { return StateMgr; }
98   
99   QualType getConditionType() const {
100     return  getContext().IntTy;
101   }
102   
103   QualType getArrayIndexType() const {
104     return ArrayIndexTy;
105   }
106
107   BasicValueFactory &getBasicValueFactory() { return BasicVals; }
108   const BasicValueFactory &getBasicValueFactory() const { return BasicVals; }
109
110   SymbolManager &getSymbolManager() { return SymMgr; }
111   const SymbolManager &getSymbolManager() const { return SymMgr; }
112
113   MemRegionManager &getRegionManager() { return MemMgr; }
114   const MemRegionManager &getRegionManager() const { return MemMgr; }
115
116   // Forwarding methods to SymbolManager.
117
118   const SymbolConjured* getConjuredSymbol(const Stmt* E, QualType T,
119                                           unsigned VisitCount,
120                                           const void* SymbolTag = 0) {
121     return SymMgr.getConjuredSymbol(E, T, VisitCount, SymbolTag);
122   }
123
124   const SymbolConjured* getConjuredSymbol(const Expr* E, unsigned VisitCount,
125                                           const void* SymbolTag = 0) {
126     return SymMgr.getConjuredSymbol(E, VisitCount, SymbolTag);
127   }
128
129   /// makeZeroVal - Construct an SVal representing '0' for the specified type.
130   DefinedOrUnknownSVal makeZeroVal(QualType T);
131
132   /// getRegionValueSymbolVal - make a unique symbol for value of R.
133   DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedRegion *R);
134
135   DefinedOrUnknownSVal getConjuredSymbolVal(const void *SymbolTag,
136                                             const Expr *E, unsigned Count);
137   DefinedOrUnknownSVal getConjuredSymbolVal(const void *SymbolTag,
138                                             const Expr *E, QualType T,
139                                             unsigned Count);
140
141   DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
142                                                       const TypedRegion *R);
143
144   DefinedSVal getMetadataSymbolVal(const void *SymbolTag, const MemRegion *MR,
145                                    const Expr *E, QualType T, unsigned Count);
146
147   DefinedSVal getFunctionPointer(const FunctionDecl *FD);
148   
149   DefinedSVal getBlockPointer(const BlockDecl *BD, CanQualType locTy,
150                               const LocationContext *LC);
151
152   NonLoc makeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals) {
153     return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));
154   }
155
156   NonLoc makeLazyCompoundVal(const void *store, const TypedRegion *R) {
157     return nonloc::LazyCompoundVal(BasicVals.getLazyCompoundValData(store, R));
158   }
159
160   NonLoc makeZeroArrayIndex() {
161     return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
162   }
163
164   NonLoc makeArrayIndex(uint64_t idx) {
165     return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
166   }
167
168   SVal convertToArrayIndex(SVal V);
169
170   nonloc::ConcreteInt makeIntVal(const IntegerLiteral* I) {
171     return nonloc::ConcreteInt(BasicVals.getValue(I->getValue(),
172                                         I->getType()->isUnsignedIntegerType()));
173   }
174
175   nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *E) {
176     return makeTruthVal(E->getValue());
177   }
178
179   nonloc::ConcreteInt makeIntVal(const llvm::APSInt& V) {
180     return nonloc::ConcreteInt(BasicVals.getValue(V));
181   }
182
183   loc::ConcreteInt makeIntLocVal(const llvm::APSInt &v) {
184     return loc::ConcreteInt(BasicVals.getValue(v));
185   }
186
187   NonLoc makeIntVal(const llvm::APInt& V, bool isUnsigned) {
188     return nonloc::ConcreteInt(BasicVals.getValue(V, isUnsigned));
189   }
190
191   DefinedSVal makeIntVal(uint64_t X, QualType T) {
192     if (Loc::isLocType(T))
193       return loc::ConcreteInt(BasicVals.getValue(X, T));
194
195     return nonloc::ConcreteInt(BasicVals.getValue(X, T));
196   }
197
198   NonLoc makeIntVal(uint64_t X, bool isUnsigned) {
199     return nonloc::ConcreteInt(BasicVals.getIntValue(X, isUnsigned));
200   }
201
202   NonLoc makeIntValWithPtrWidth(uint64_t X, bool isUnsigned) {
203     return nonloc::ConcreteInt(BasicVals.getIntWithPtrWidth(X, isUnsigned));
204   }
205
206   NonLoc makeIntVal(uint64_t X, unsigned BitWidth, bool isUnsigned) {
207     return nonloc::ConcreteInt(BasicVals.getValue(X, BitWidth, isUnsigned));
208   }
209
210   NonLoc makeLocAsInteger(Loc V, unsigned Bits) {
211     return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(V, Bits));
212   }
213
214   NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
215                     const llvm::APSInt& rhs, QualType T);
216
217   NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
218                     const SymExpr *rhs, QualType T);
219
220   nonloc::ConcreteInt makeTruthVal(bool b, QualType T) {
221     return nonloc::ConcreteInt(BasicVals.getTruthValue(b, T));
222   }
223
224   nonloc::ConcreteInt makeTruthVal(bool b) {
225     return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
226   }
227
228   Loc makeNull() {
229     return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
230   }
231
232   Loc makeLoc(SymbolRef Sym) {
233     return loc::MemRegionVal(MemMgr.getSymbolicRegion(Sym));
234   }
235
236   Loc makeLoc(const MemRegion* R) {
237     return loc::MemRegionVal(R);
238   }
239
240   Loc makeLoc(const AddrLabelExpr *E) {
241     return loc::GotoLabel(E->getLabel());
242   }
243
244   Loc makeLoc(const llvm::APSInt& V) {
245     return loc::ConcreteInt(BasicVals.getValue(V));
246   }
247
248 };
249
250 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
251                                      ASTContext &context,
252                                      GRStateManager &stateMgr);
253
254 } // end GR namespace
255
256 } // end clang namespace
257
258 #endif