clang  3.8.0
SValBuilder.h
Go to the documentation of this file.
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"
24 
25 namespace clang {
26 
27 class CXXBoolLiteralExpr;
28 
29 namespace ento {
30 
31 class SValBuilder {
32  virtual void anchor();
33 protected:
35 
36  /// Manager of APSInt values.
38 
39  /// Manages the creation of symbols.
41 
42  /// Manages the creation of memory regions.
44 
46 
47  /// The scalar type to use for array indices.
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),
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) ||
83  }
84 
85  SVal evalCast(SVal val, QualType castTy, QualType originalType);
86 
87  // Handles casts of type CK_IntegralCast.
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.
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.
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.
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.
117  NonLoc lhs, NonLoc rhs, QualType resultTy);
118 
120  SVal lhs, SVal rhs, QualType type);
121 
124 
126  const ASTContext &getContext() const { return Context; }
127 
129 
131  return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
132  }
133 
135  return ArrayIndexTy;
136  }
137 
140 
142  const SymbolManager &getSymbolManager() const { return SymMgr; }
143 
145  const MemRegionManager &getRegionManager() const { return MemMgr; }
146 
147  // Forwarding methods to SymbolManager.
148 
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 
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.
166 
167  /// Make a unique symbol for value of 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 
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.
194  const LocationContext *LCtx,
195  unsigned Count);
196 
198  SymbolRef parentSymbol, const TypedValueRegion *region);
199 
201  const void *symbolTag, const MemRegion *region,
202  const Expr *expr, QualType type, unsigned count);
203 
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.
215 
218  }
219 
221  const TypedValueRegion *region) {
223  BasicVals.getLazyCompoundValData(store, region));
224  }
225 
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 
235 
237  return nonloc::ConcreteInt(
238  BasicVals.getValue(integer->getValue(),
240  }
241 
243  return makeTruthVal(boolean->getValue(), boolean->getType());
244  }
245 
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) {
278  }
279 
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 
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 
294  }
295 
298  }
299 
302  }
303 
306  }
307 
308  Loc makeLoc(const MemRegion* region) {
309  return loc::MemRegionVal(region);
310  }
311 
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.
322  const StackFrameContext *SFC);
323 
324  /// Return a memory region for the 'this' object reference.
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
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1483
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Definition: ASTMatchers.h:1192
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:510
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:236
DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, const LocationContext *locContext, unsigned blockCount)
SymbolManager & getSymbolManager()
Definition: SValBuilder.h:141
A (possibly-)qualified type.
Definition: Type.h:575
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:78
QualType getArrayIndexType() const
Definition: SValBuilder.h:134
const CompoundValData * getCompoundValData(QualType T, llvm::ImmutableList< SVal > Vals)
SValBuilder * createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
Definition: ASTMatchers.h:876
SVal makeSymExprValNN(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)
Constructs a symbolic expression for two non-location values.
ProgramStateManager & StateMgr
Definition: SValBuilder.h:45
SVal evalCast(SVal val, QualType castTy, QualType originalType)
Value representing integer constant.
Definition: SVals.h:339
MemRegionManager MemMgr
Manages the creation of memory regions.
Definition: SValBuilder.h:43
const SymbolManager & getSymbolManager() const
Definition: SValBuilder.h:142
const llvm::APSInt & getTruthValue(bool b, QualType T)
virtual SVal dispatchCast(SVal val, QualType castTy)=0
loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, const StackFrameContext *SFC)
Return a memory region for the 'this' object reference.
NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, const llvm::APSInt &rhs, QualType type)
Definition: SValBuilder.cpp:44
NonLoc makeArrayIndex(uint64_t idx)
Definition: SValBuilder.h:230
BasicValueFactory BasicVals
Manager of APSInt values.
Definition: SValBuilder.h:37
static llvm::Value * getTypeSize(CodeGenFunction &CGF, QualType Ty)
Symbolic value.
Definition: SymbolManager.h:42
virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy)=0
virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, Loc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with a memory location and non-location opera...
MemRegionManager & getRegionManager()
Definition: SValBuilder.h:144
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:91
SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy, QualType originalType)
DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, const TypedValueRegion *region)
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym)
Retrieve or create a "symbolic" memory region.
Definition: MemRegion.cpp:945
static bool isLocType(QualType T)
Definition: SVals.h:291
BinaryOperatorKind
const LangOptions & getLangOpts() const
Definition: ASTContext.h:596
DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region)
Make a unique symbol for value of region.
Definition: SValBuilder.cpp:95
Loc makeLoc(const MemRegion *region)
Definition: SValBuilder.h:308
Loc makeLoc(const AddrLabelExpr *expr)
Definition: SValBuilder.h:312
loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:252
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
Definition: SValBuilder.h:216
DefinedSVal getFunctionPointer(const FunctionDecl *func)
NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:271
bool haveSameType(QualType Ty1, QualType Ty2)
Definition: SValBuilder.h:77
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
virtual QualType getType() const =0
llvm::APInt getValue() const
Definition: Expr.h:1234
nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean)
Definition: SValBuilder.h:242
Loc makeLoc(SymbolRef sym)
Definition: SValBuilder.h:304
DefinedOrUnknownSVal makeZeroVal(QualType type)
Construct an SVal representing '0' for the specified type.
Definition: SValBuilder.cpp:32
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3369
Expr - This represents one expression.
Definition: Expr.h:104
virtual SVal evalMinus(NonLoc val)=0
const llvm::APSInt & getIntWithPtrWidth(uint64_t X, bool isUnsigned)
nonloc::ConcreteInt makeTruthVal(bool b)
Definition: SValBuilder.h:296
const QualType ArrayIndexTy
The scalar type to use for array indices.
Definition: SValBuilder.h:48
virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, Loc lhs, Loc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two memory location operands.
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
nonloc::ConcreteInt makeIntVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:248
QualType getConditionType() const
Definition: SValBuilder.h:130
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:1756
NonLoc makeIntVal(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:267
const ASTContext & getContext() const
Definition: SValBuilder.h:126
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:5596
bool getValue() const
Definition: ExprObjC.h:71
SymbolManager SymMgr
Manages the creation of symbols.
Definition: SValBuilder.h:40
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, const Expr *expr, const LocationContext *LCtx, unsigned count)
Create a new symbol with a unique 'name'.
virtual SVal evalCastFromLoc(Loc val, QualType castTy)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1701
ASTContext & getContext()
Definition: SValBuilder.h:125
const MemRegionManager & getRegionManager() const
Definition: SValBuilder.h:145
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:44
const BasicValueFactory & getBasicValueFactory() const
Definition: SValBuilder.h:139
const llvm::APSInt & getZeroWithPtrWidth(bool isUnsigned=true)
DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E, const LocationContext *LCtx, unsigned Count)
Conjure a symbol representing heap allocated memory region.
A symbol representing the result of an expression in the case when we do not know anything about what...
virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two non- location operands.
const unsigned ArrayIndexWidth
The width of the scalar type used for array indices.
Definition: SValBuilder.h:51
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:3317
QualType getType() const
Definition: Expr.h:125
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1723
NonLoc makeIntVal(const llvm::APInt &integer, bool isUnsigned)
Definition: SValBuilder.h:256
const SymbolConjured * conjureSymbol(const Stmt *stmt, const LocationContext *LCtx, QualType type, unsigned visitCount, const void *symbolTag=nullptr)
Definition: SValBuilder.h:149
NonLoc makeLocAsInteger(Loc loc, unsigned bits)
Definition: SValBuilder.h:276
ProgramStateManager & getStateManager()
Definition: SValBuilder.h:128
DefinedSVal getMetadataSymbolVal(const void *symbolTag, const MemRegion *region, const Expr *expr, QualType type, unsigned count)
detail::InMemoryDirectory::const_iterator E
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:1946
SVal convertToArrayIndex(SVal val)
Definition: SValBuilder.cpp:76
const SymbolConjured * conjureSymbol(const Stmt *E, const LocationContext *LCtx, QualType T, unsigned VisitCount, const void *SymbolTag=nullptr)
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:138
Loc makeLoc(const llvm::APSInt &integer)
Definition: SValBuilder.h:316
SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
Definition: SValBuilder.h:62
LabelDecl * getLabel() const
Definition: Expr.h:3339
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2)
Definition: SValBuilder.h:73
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs, DefinedOrUnknownSVal rhs)
virtual SVal evalComplement(NonLoc val)=0
Optional< SVal > getConstantVal(const Expr *E)
Returns the value of E, if it can be determined in a non-path-sensitive manner.
const SymbolConjured * conjureSymbol(const Expr *expr, const LocationContext *LCtx, unsigned visitCount, const void *symbolTag=nullptr)
Definition: SValBuilder.h:157
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
CanQualType IntTy
Definition: ASTContext.h:889
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:60
nonloc::ConcreteInt makeTruthVal(bool b, QualType type)
Definition: SValBuilder.h:292
DefinedSVal makeIntVal(uint64_t integer, QualType type)
Definition: SValBuilder.h:260
NonLoc makeLazyCompoundVal(const StoreRef &store, const TypedValueRegion *region)
Definition: SValBuilder.h:220
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:455
CanQualType BoolTy
Definition: ASTContext.h:882