clang  7.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 ConditionTruthVal;
33 
34 class SValBuilder {
35  virtual void anchor();
36 protected:
38 
39  /// Manager of APSInt values.
41 
42  /// Manages the creation of symbols.
44 
45  /// Manages the creation of memory regions.
47 
49 
50  /// The scalar type to use for array indices.
52 
53  /// The width of the scalar type used for array indices.
54  const unsigned ArrayIndexWidth;
55 
56  virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0;
57  virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0;
58 
59 public:
60  // FIXME: Make these protected again once RegionStoreManager correctly
61  // handles loads from different bound value types.
62  virtual SVal dispatchCast(SVal val, QualType castTy) = 0;
63 
64 public:
65  SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
66  ProgramStateManager &stateMgr)
67  : Context(context), BasicVals(context, alloc),
68  SymMgr(context, BasicVals, alloc),
69  MemMgr(context, alloc),
70  StateMgr(stateMgr),
71  ArrayIndexTy(context.LongLongTy),
72  ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
73 
74  virtual ~SValBuilder() {}
75 
76  bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) {
77  return haveSameType(Sym1->getType(), Sym2->getType());
78  }
79 
80  bool haveSameType(QualType Ty1, QualType Ty2) {
81  // FIXME: Remove the second disjunct when we support symbolic
82  // truncation/extension.
83  return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) ||
86  }
87 
88  SVal evalCast(SVal val, QualType castTy, QualType originalType);
89 
90  // Handles casts of type CK_IntegralCast.
92  QualType originalType);
93 
94  virtual SVal evalMinus(NonLoc val) = 0;
95 
96  virtual SVal evalComplement(NonLoc val) = 0;
97 
98  /// Create a new value which represents a binary expression with two non-
99  /// location operands.
101  NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
102 
103  /// Create a new value which represents a binary expression with two memory
104  /// location operands.
106  Loc lhs, Loc rhs, QualType resultTy) = 0;
107 
108  /// Create a new value which represents a binary expression with a memory
109  /// location and non-location operands. For example, this would be used to
110  /// evaluate a pointer arithmetic operation.
112  Loc lhs, NonLoc rhs, QualType resultTy) = 0;
113 
114  /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
115  /// that value is returned. Otherwise, returns NULL.
116  virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
117 
118  /// Simplify symbolic expressions within a given SVal. Return an SVal
119  /// that represents the same value, but is hopefully easier to work with
120  /// than the original SVal.
121  virtual SVal simplifySVal(ProgramStateRef State, SVal Val) = 0;
122 
123  /// Constructs a symbolic expression for two non-location values.
125  NonLoc lhs, NonLoc rhs, QualType resultTy);
126 
128  SVal lhs, SVal rhs, QualType type);
129 
130  /// \return Whether values in \p lhs and \p rhs are equal at \p state.
132 
133  SVal evalEQ(ProgramStateRef state, SVal lhs, SVal rhs);
134 
137 
139  const ASTContext &getContext() const { return Context; }
140 
142 
144  return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
145  }
146 
148  return ArrayIndexTy;
149  }
150 
153 
155  const SymbolManager &getSymbolManager() const { return SymMgr; }
156 
158  const MemRegionManager &getRegionManager() const { return MemMgr; }
159 
160  // Forwarding methods to SymbolManager.
161 
163  const LocationContext *LCtx,
164  QualType type,
165  unsigned visitCount,
166  const void *symbolTag = nullptr) {
167  return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
168  }
169 
171  const LocationContext *LCtx,
172  unsigned visitCount,
173  const void *symbolTag = nullptr) {
174  return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
175  }
176 
177  /// Construct an SVal representing '0' for the specified type.
179 
180  /// Make a unique symbol for value of region.
182 
183  /// \brief Create a new symbol with a unique 'name'.
184  ///
185  /// We resort to conjured symbols when we cannot construct a derived symbol.
186  /// The advantage of symbols derived/built from other symbols is that we
187  /// preserve the relation between related(or even equivalent) expressions, so
188  /// conjured symbols should be used sparingly.
189  DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
190  const Expr *expr,
191  const LocationContext *LCtx,
192  unsigned count);
193  DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
194  const Expr *expr,
195  const LocationContext *LCtx,
196  QualType type,
197  unsigned count);
198 
200  const LocationContext *LCtx,
201  QualType type,
202  unsigned visitCount);
203  /// \brief Conjure a symbol representing heap allocated memory region.
204  ///
205  /// Note, the expression should represent a location.
207  const LocationContext *LCtx,
208  unsigned Count);
209 
211  SymbolRef parentSymbol, const TypedValueRegion *region);
212 
213  DefinedSVal getMetadataSymbolVal(const void *symbolTag,
214  const MemRegion *region,
215  const Expr *expr, QualType type,
216  const LocationContext *LCtx,
217  unsigned count);
218 
220 
222 
223  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
224  const LocationContext *locContext,
225  unsigned blockCount);
226 
227  /// Returns the value of \p E, if it can be determined in a non-path-sensitive
228  /// manner.
229  ///
230  /// If \p E is not a constant or cannot be modeled, returns \c None.
232 
234  return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
235  }
236 
238  const TypedValueRegion *region) {
240  BasicVals.getLazyCompoundValData(store, region));
241  }
242 
244  return nonloc::PointerToMember(DD);
245  }
246 
248  return nonloc::PointerToMember(PTMD);
249  }
250 
252  return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
253  }
254 
255  NonLoc makeArrayIndex(uint64_t idx) {
256  return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
257  }
258 
260 
262  return nonloc::ConcreteInt(
263  BasicVals.getValue(integer->getValue(),
265  }
266 
268  return makeTruthVal(boolean->getValue(), boolean->getType());
269  }
270 
272 
273  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
274  return nonloc::ConcreteInt(BasicVals.getValue(integer));
275  }
276 
277  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
278  return loc::ConcreteInt(BasicVals.getValue(integer));
279  }
280 
281  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
282  return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
283  }
284 
285  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
286  if (Loc::isLocType(type))
287  return loc::ConcreteInt(BasicVals.getValue(integer, type));
288 
289  return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
290  }
291 
292  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
293  return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
294  }
295 
296  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
297  return nonloc::ConcreteInt(
298  BasicVals.getIntWithPtrWidth(integer, isUnsigned));
299  }
300 
301  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
302  return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
303  }
304 
306  const llvm::APSInt& rhs, QualType type);
307 
308  NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
309  const SymExpr *lhs, QualType type);
310 
312  const SymExpr *rhs, QualType type);
313 
314  /// \brief Create a NonLoc value for cast.
315  NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
316 
318  return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
319  }
320 
322  return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
323  }
324 
325  /// Create NULL pointer, with proper pointer bit-width for given address
326  /// space.
327  /// \param type pointer type.
329  return loc::ConcreteInt(BasicVals.getZeroWithTypeSize(type));
330  }
331 
333  return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
334  }
335 
337  return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
338  }
339 
340  Loc makeLoc(const MemRegion* region) {
341  return loc::MemRegionVal(region);
342  }
343 
344  Loc makeLoc(const AddrLabelExpr *expr) {
345  return loc::GotoLabel(expr->getLabel());
346  }
347 
348  Loc makeLoc(const llvm::APSInt& integer) {
349  return loc::ConcreteInt(BasicVals.getValue(integer));
350  }
351 
352  /// Return a memory region for the 'this' object reference.
354  const StackFrameContext *SFC);
355 
356  /// Return a memory region for the 'this' object reference.
358  const StackFrameContext *SFC);
359 };
360 
361 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
362  ASTContext &context,
363  ProgramStateManager &stateMgr);
364 
365 } // end GR namespace
366 
367 } // end clang namespace
368 
369 #endif
Defines the clang::ASTContext interface.
Represents a function declaration or definition.
Definition: Decl.h:1696
bool getValue() const
Definition: ExprObjC.h:94
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:512
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:261
DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, const LocationContext *locContext, unsigned blockCount)
SymbolManager & getSymbolManager()
Definition: SValBuilder.h:154
QualType getArrayIndexType() const
Definition: SValBuilder.h:147
A (possibly-)qualified type.
Definition: Type.h:653
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:1856
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
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:66
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:48
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:353
MemRegionManager MemMgr
Manages the creation of memory regions.
Definition: SValBuilder.h:46
const llvm::APSInt & getTruthValue(bool b, QualType T)
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
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:255
BasicValueFactory BasicVals
Manager of APSInt values.
Definition: SValBuilder.h:40
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:157
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
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:328
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:6231
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym)
Retrieve or create a "symbolic" memory region.
Definition: MemRegion.cpp:959
static bool isLocType(QualType T)
Definition: SVals.h:308
BinaryOperatorKind
NonLoc makePointerToMember(const DeclaratorDecl *DD)
Definition: SValBuilder.h:243
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:340
Loc makeLoc(const AddrLabelExpr *expr)
Definition: SValBuilder.h:344
loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:277
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
Definition: SValBuilder.h:233
DefinedSVal getFunctionPointer(const FunctionDecl *func)
NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:296
bool haveSameType(QualType Ty1, QualType Ty2)
Definition: SValBuilder.h:80
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:688
nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean)
Definition: SValBuilder.h:267
ConditionTruthVal areEqual(ProgramStateRef state, SVal lhs, SVal rhs)
Loc makeLoc(SymbolRef sym)
Definition: SValBuilder.h:336
DefinedOrUnknownSVal makeZeroVal(QualType type)
Construct an SVal representing &#39;0&#39; for the specified type.
Definition: SValBuilder.cpp:32
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3713
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:321
const QualType ArrayIndexTy
The scalar type to use for array indices.
Definition: SValBuilder.h:51
QualType getConditionType() const
Definition: SValBuilder.h:143
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:152
nonloc::ConcreteInt makeIntVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:273
NonLoc makeIntVal(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:292
SymbolManager SymMgr
Manages the creation of symbols.
Definition: SValBuilder.h:43
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:2019
ASTContext & getContext()
Definition: SValBuilder.h:138
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:54
const MemRegionManager & getRegionManager() const
Definition: SValBuilder.h:158
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:3457
Dataflow Directional Tag Classes.
NonLoc makeIntVal(const llvm::APInt &integer, bool isUnsigned)
Definition: SValBuilder.h:281
const SymbolConjured * conjureSymbol(const Stmt *stmt, const LocationContext *LCtx, QualType type, unsigned visitCount, const void *symbolTag=nullptr)
Definition: SValBuilder.h:162
NonLoc makeLocAsInteger(Loc loc, unsigned bits)
Definition: SValBuilder.h:301
ProgramStateManager & getStateManager()
Definition: SValBuilder.h:141
SVal evalEQ(ProgramStateRef state, SVal lhs, SVal rhs)
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:3479
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:151
Loc makeLoc(const llvm::APSInt &integer)
Definition: SValBuilder.h:348
DefinedSVal getMemberPointer(const DeclaratorDecl *DD)
SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
Definition: SValBuilder.h:65
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:2185
bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2)
Definition: SValBuilder.h:76
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
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:170
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
CanQualType IntTy
Definition: ASTContext.h:1004
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:82
NonLoc makePointerToMember(const PointerToMemberData *PTMD)
Definition: SValBuilder.h:247
const ASTContext & getContext() const
Definition: SValBuilder.h:139
nonloc::ConcreteInt makeTruthVal(bool b, QualType type)
Definition: SValBuilder.h:317
DefinedSVal makeIntVal(uint64_t integer, QualType type)
Definition: SValBuilder.h:285
NonLoc makeLazyCompoundVal(const StoreRef &store, const TypedValueRegion *region)
Definition: SValBuilder.h:237
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:530
CanQualType BoolTy
Definition: ASTContext.h:997
const SymbolManager & getSymbolManager() const
Definition: SValBuilder.h:155
const LangOptions & getLangOpts() const
Definition: ASTContext.h:688