clang  6.0.0svn
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"
25 
26 namespace clang {
27 
28 class CXXBoolLiteralExpr;
29 
30 namespace ento {
31 
32 class SValBuilder {
33  virtual void anchor();
34 protected:
36 
37  /// Manager of APSInt values.
39 
40  /// Manages the creation of symbols.
42 
43  /// Manages the creation of memory regions.
45 
47 
48  /// The scalar type to use for array indices.
50 
51  /// The width of the scalar type used for array indices.
52  const unsigned ArrayIndexWidth;
53 
54  virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
55  virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
56 
57 public:
58  // FIXME: Make these protected again once RegionStoreManager correctly
59  // handles loads from different bound value types.
60  virtual SVal dispatchCast(SVal val, QualType castTy) = 0;
61 
62 public:
63  SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
64  ProgramStateManager &stateMgr)
65  : Context(context), BasicVals(context, alloc),
66  SymMgr(context, BasicVals, alloc),
67  MemMgr(context, alloc),
68  StateMgr(stateMgr),
69  ArrayIndexTy(context.LongLongTy),
70  ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
71 
72  virtual ~SValBuilder() {}
73 
74  bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) {
75  return haveSameType(Sym1->getType(), Sym2->getType());
76  }
77 
78  bool haveSameType(QualType Ty1, QualType Ty2) {
79  // FIXME: Remove the second disjunct when we support symbolic
80  // truncation/extension.
81  return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) ||
84  }
85 
86  SVal evalCast(SVal val, QualType castTy, QualType originalType);
87 
88  // Handles casts of type CK_IntegralCast.
90  QualType originalType);
91 
92  virtual SVal evalMinus(NonLoc val) = 0;
93 
94  virtual SVal evalComplement(NonLoc val) = 0;
95 
96  /// Create a new value which represents a binary expression with two non-
97  /// location operands.
99  NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
100 
101  /// Create a new value which represents a binary expression with two memory
102  /// location operands.
104  Loc lhs, Loc rhs, QualType resultTy) = 0;
105 
106  /// Create a new value which represents a binary expression with a memory
107  /// location and non-location operands. For example, this would be used to
108  /// evaluate a pointer arithmetic operation.
110  Loc lhs, NonLoc rhs, QualType resultTy) = 0;
111 
112  /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
113  /// that value is returned. Otherwise, returns NULL.
114  virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
115 
116  /// Simplify symbolic expressions within a given SVal. Return an SVal
117  /// that represents the same value, but is hopefully easier to work with
118  /// than the original SVal.
119  virtual SVal simplifySVal(ProgramStateRef State, SVal Val) = 0;
120 
121  /// Constructs a symbolic expression for two non-location values.
123  NonLoc lhs, NonLoc rhs, QualType resultTy);
124 
126  SVal lhs, SVal rhs, QualType type);
127 
130 
132  const ASTContext &getContext() const { return Context; }
133 
135 
137  return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
138  }
139 
141  return ArrayIndexTy;
142  }
143 
146 
148  const SymbolManager &getSymbolManager() const { return SymMgr; }
149 
151  const MemRegionManager &getRegionManager() const { return MemMgr; }
152 
153  // Forwarding methods to SymbolManager.
154 
156  const LocationContext *LCtx,
157  QualType type,
158  unsigned visitCount,
159  const void *symbolTag = nullptr) {
160  return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
161  }
162 
164  const LocationContext *LCtx,
165  unsigned visitCount,
166  const void *symbolTag = nullptr) {
167  return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
168  }
169 
170  /// Construct an SVal representing '0' for the specified type.
172 
173  /// Make a unique symbol for value of region.
175 
176  /// \brief Create a new symbol with a unique 'name'.
177  ///
178  /// We resort to conjured symbols when we cannot construct a derived symbol.
179  /// The advantage of symbols derived/built from other symbols is that we
180  /// preserve the relation between related(or even equivalent) expressions, so
181  /// conjured symbols should be used sparingly.
182  DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
183  const Expr *expr,
184  const LocationContext *LCtx,
185  unsigned count);
186  DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
187  const Expr *expr,
188  const LocationContext *LCtx,
189  QualType type,
190  unsigned count);
191 
193  const LocationContext *LCtx,
194  QualType type,
195  unsigned visitCount);
196  /// \brief Conjure a symbol representing heap allocated memory region.
197  ///
198  /// Note, the expression should represent a location.
200  const LocationContext *LCtx,
201  unsigned Count);
202 
204  SymbolRef parentSymbol, const TypedValueRegion *region);
205 
206  DefinedSVal getMetadataSymbolVal(const void *symbolTag,
207  const MemRegion *region,
208  const Expr *expr, QualType type,
209  const LocationContext *LCtx,
210  unsigned count);
211 
213 
215 
216  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
217  const LocationContext *locContext,
218  unsigned blockCount);
219 
220  /// Returns the value of \p E, if it can be determined in a non-path-sensitive
221  /// manner.
222  ///
223  /// If \p E is not a constant or cannot be modeled, returns \c None.
225 
227  return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
228  }
229 
231  const TypedValueRegion *region) {
233  BasicVals.getLazyCompoundValData(store, region));
234  }
235 
237  return nonloc::PointerToMember(DD);
238  }
239 
241  return nonloc::PointerToMember(PTMD);
242  }
243 
245  return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
246  }
247 
248  NonLoc makeArrayIndex(uint64_t idx) {
249  return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
250  }
251 
253 
255  return nonloc::ConcreteInt(
256  BasicVals.getValue(integer->getValue(),
258  }
259 
261  return makeTruthVal(boolean->getValue(), boolean->getType());
262  }
263 
265 
266  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
267  return nonloc::ConcreteInt(BasicVals.getValue(integer));
268  }
269 
270  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
271  return loc::ConcreteInt(BasicVals.getValue(integer));
272  }
273 
274  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
275  return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
276  }
277 
278  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
279  if (Loc::isLocType(type))
280  return loc::ConcreteInt(BasicVals.getValue(integer, type));
281 
282  return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
283  }
284 
285  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
286  return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
287  }
288 
289  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
290  return nonloc::ConcreteInt(
291  BasicVals.getIntWithPtrWidth(integer, isUnsigned));
292  }
293 
294  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
295  return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
296  }
297 
299  const llvm::APSInt& rhs, QualType type);
300 
301  NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
302  const SymExpr *lhs, QualType type);
303 
305  const SymExpr *rhs, QualType type);
306 
307  /// \brief Create a NonLoc value for cast.
308  NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
309 
311  return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
312  }
313 
315  return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
316  }
317 
318  /// Create NULL pointer, with proper pointer bit-width for given address
319  /// space.
320  /// \param type pointer type.
322  return loc::ConcreteInt(BasicVals.getZeroWithTypeSize(type));
323  }
324 
326  return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
327  }
328 
330  return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
331  }
332 
333  Loc makeLoc(const MemRegion* region) {
334  return loc::MemRegionVal(region);
335  }
336 
337  Loc makeLoc(const AddrLabelExpr *expr) {
338  return loc::GotoLabel(expr->getLabel());
339  }
340 
341  Loc makeLoc(const llvm::APSInt& integer) {
342  return loc::ConcreteInt(BasicVals.getValue(integer));
343  }
344 
345  /// Return a memory region for the 'this' object reference.
347  const StackFrameContext *SFC);
348 
349  /// Return a memory region for the 'this' object reference.
351  const StackFrameContext *SFC);
352 };
353 
354 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
355  ASTContext &context,
356  ProgramStateManager &stateMgr);
357 
358 } // end GR namespace
359 
360 } // end clang namespace
361 
362 #endif
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1629
bool getValue() const
Definition: ExprObjC.h:71
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Definition: ASTMatchers.h:1538
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:511
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:254
DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, const LocationContext *locContext, unsigned blockCount)
SymbolManager & getSymbolManager()
Definition: SValBuilder.h:147
QualType getArrayIndexType() const
Definition: SValBuilder.h:140
A (possibly-)qualified type.
Definition: Type.h:614
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:79
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:1801
const CompoundValData * getCompoundValData(QualType T, llvm::ImmutableList< SVal > Vals)
SValBuilder * createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
Stmt - This represents one statement.
Definition: Stmt.h:60
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
Definition: ASTMatchers.h:1068
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:46
const llvm::APSInt & getZeroWithTypeSize(QualType T)
virtual SVal simplifySVal(ProgramStateRef State, SVal Val)=0
Simplify symbolic expressions within a given SVal.
SVal evalCast(SVal val, QualType castTy, QualType originalType)
Value representing integer constant.
Definition: SVals.h:352
MemRegionManager MemMgr
Manages the creation of memory regions.
Definition: SValBuilder.h:44
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 &#39;this&#39; object reference.
NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, const llvm::APSInt &rhs, QualType type)
Definition: SValBuilder.cpp:47
NonLoc makeArrayIndex(uint64_t idx)
Definition: SValBuilder.h:248
BasicValueFactory BasicVals
Manager of APSInt values.
Definition: SValBuilder.h:38
Symbolic value.
Definition: SymExpr.h:29
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:150
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
LineState State
SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy, QualType originalType)
Loc makeNullWithType(QualType type)
Create NULL pointer, with proper pointer bit-width for given address space.
Definition: SValBuilder.h:321
DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, const TypedValueRegion *region)
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:5966
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym)
Retrieve or create a "symbolic" memory region.
Definition: MemRegion.cpp:957
static bool isLocType(QualType T)
Definition: SVals.h:307
BinaryOperatorKind
NonLoc makePointerToMember(const DeclaratorDecl *DD)
Definition: SValBuilder.h:236
DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region)
Make a unique symbol for value of region.
Definition: SValBuilder.cpp:98
Loc makeLoc(const MemRegion *region)
Definition: SValBuilder.h:333
Loc makeLoc(const AddrLabelExpr *expr)
Definition: SValBuilder.h:337
loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:270
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
Definition: SValBuilder.h:226
DefinedSVal getFunctionPointer(const FunctionDecl *func)
NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:289
bool haveSameType(QualType Ty1, QualType Ty2)
Definition: SValBuilder.h:78
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
virtual QualType getType() const =0
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:647
nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean)
Definition: SValBuilder.h:260
Loc makeLoc(SymbolRef sym)
Definition: SValBuilder.h:329
DefinedOrUnknownSVal makeZeroVal(QualType type)
Construct an SVal representing &#39;0&#39; for the specified type.
Definition: SValBuilder.cpp:32
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3582
Expr - This represents one expression.
Definition: Expr.h:106
virtual SVal evalMinus(NonLoc val)=0
const llvm::APSInt & getIntWithPtrWidth(uint64_t X, bool isUnsigned)
nonloc::ConcreteInt makeTruthVal(bool b)
Definition: SValBuilder.h:314
const QualType ArrayIndexTy
The scalar type to use for array indices.
Definition: SValBuilder.h:49
QualType getConditionType() const
Definition: SValBuilder.h:136
QualType getType() const
Definition: Expr.h:128
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)
const BasicValueFactory & getBasicValueFactory() const
Definition: SValBuilder.h:145
nonloc::ConcreteInt makeIntVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:266
NonLoc makeIntVal(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:285
SymbolManager SymMgr
Manages the creation of symbols.
Definition: SValBuilder.h:41
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, const Expr *expr, const LocationContext *LCtx, unsigned count)
Create a new symbol with a unique &#39;name&#39;.
virtual SVal evalCastFromLoc(Loc val, QualType castTy)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1918
ASTContext & getContext()
Definition: SValBuilder.h:131
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
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...
Definition: SymbolManager.h:76
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:52
const MemRegionManager & getRegionManager() const
Definition: SValBuilder.h:151
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:3439
Dataflow Directional Tag Classes.
NonLoc makeIntVal(const llvm::APInt &integer, bool isUnsigned)
Definition: SValBuilder.h:274
const SymbolConjured * conjureSymbol(const Stmt *stmt, const LocationContext *LCtx, QualType type, unsigned visitCount, const void *symbolTag=nullptr)
Definition: SValBuilder.h:155
NonLoc makeLocAsInteger(Loc loc, unsigned bits)
Definition: SValBuilder.h:294
ProgramStateManager & getStateManager()
Definition: SValBuilder.h:134
SVal convertToArrayIndex(SVal val)
Definition: SValBuilder.cpp:79
const SymbolConjured * conjureSymbol(const Stmt *E, const LocationContext *LCtx, QualType T, unsigned VisitCount, const void *SymbolTag=nullptr)
DefinedSVal getMetadataSymbolVal(const void *symbolTag, const MemRegion *region, const Expr *expr, QualType type, const LocationContext *LCtx, unsigned count)
llvm::APInt getValue() const
Definition: Expr.h:1279
LabelDecl * getLabel() const
Definition: Expr.h:3461
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:144
Loc makeLoc(const llvm::APSInt &integer)
Definition: SValBuilder.h:341
DefinedSVal getMemberPointer(const DeclaratorDecl *DD)
SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
Definition: SValBuilder.h:63
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2106
bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2)
Definition: SValBuilder.h:74
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:266
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:163
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
CanQualType IntTy
Definition: ASTContext.h:971
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:60
NonLoc makePointerToMember(const PointerToMemberData *PTMD)
Definition: SValBuilder.h:240
const ASTContext & getContext() const
Definition: SValBuilder.h:132
nonloc::ConcreteInt makeTruthVal(bool b, QualType type)
Definition: SValBuilder.h:310
DefinedSVal makeIntVal(uint64_t integer, QualType type)
Definition: SValBuilder.h:278
NonLoc makeLazyCompoundVal(const StoreRef &store, const TypedValueRegion *region)
Definition: SValBuilder.h:230
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:486
CanQualType BoolTy
Definition: ASTContext.h:964
const SymbolManager & getSymbolManager() const
Definition: SValBuilder.h:148
const LangOptions & getLangOpts() const
Definition: ASTContext.h:659