clang  3.9.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"
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  /// Constructs a symbolic expression for two non-location values.
118  NonLoc lhs, NonLoc rhs, QualType resultTy);
119 
121  SVal lhs, SVal rhs, QualType type);
122 
125 
127  const ASTContext &getContext() const { return Context; }
128 
130 
132  return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
133  }
134 
136  return ArrayIndexTy;
137  }
138 
141 
143  const SymbolManager &getSymbolManager() const { return SymMgr; }
144 
146  const MemRegionManager &getRegionManager() const { return MemMgr; }
147 
148  // Forwarding methods to SymbolManager.
149 
151  const LocationContext *LCtx,
152  QualType type,
153  unsigned visitCount,
154  const void *symbolTag = nullptr) {
155  return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag);
156  }
157 
159  const LocationContext *LCtx,
160  unsigned visitCount,
161  const void *symbolTag = nullptr) {
162  return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag);
163  }
164 
165  /// Construct an SVal representing '0' for the specified type.
167 
168  /// Make a unique symbol for value of region.
170 
171  /// \brief Create a new symbol with a unique 'name'.
172  ///
173  /// We resort to conjured symbols when we cannot construct a derived symbol.
174  /// The advantage of symbols derived/built from other symbols is that we
175  /// preserve the relation between related(or even equivalent) expressions, so
176  /// conjured symbols should be used sparingly.
177  DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
178  const Expr *expr,
179  const LocationContext *LCtx,
180  unsigned count);
181  DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
182  const Expr *expr,
183  const LocationContext *LCtx,
184  QualType type,
185  unsigned count);
186 
188  const LocationContext *LCtx,
189  QualType type,
190  unsigned visitCount);
191  /// \brief Conjure a symbol representing heap allocated memory region.
192  ///
193  /// Note, the expression should represent a location.
195  const LocationContext *LCtx,
196  unsigned Count);
197 
199  SymbolRef parentSymbol, const TypedValueRegion *region);
200 
202  const void *symbolTag, const MemRegion *region,
203  const Expr *expr, QualType type, unsigned count);
204 
206 
207  DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy,
208  const LocationContext *locContext,
209  unsigned blockCount);
210 
211  /// Returns the value of \p E, if it can be determined in a non-path-sensitive
212  /// manner.
213  ///
214  /// If \p E is not a constant or cannot be modeled, returns \c None.
216 
219  }
220 
222  const TypedValueRegion *region) {
224  BasicVals.getLazyCompoundValData(store, region));
225  }
226 
228  return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy));
229  }
230 
231  NonLoc makeArrayIndex(uint64_t idx) {
232  return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
233  }
234 
236 
238  return nonloc::ConcreteInt(
239  BasicVals.getValue(integer->getValue(),
241  }
242 
244  return makeTruthVal(boolean->getValue(), boolean->getType());
245  }
246 
248 
249  nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
250  return nonloc::ConcreteInt(BasicVals.getValue(integer));
251  }
252 
253  loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
254  return loc::ConcreteInt(BasicVals.getValue(integer));
255  }
256 
257  NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
258  return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
259  }
260 
261  DefinedSVal makeIntVal(uint64_t integer, QualType type) {
262  if (Loc::isLocType(type))
263  return loc::ConcreteInt(BasicVals.getValue(integer, type));
264 
265  return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
266  }
267 
268  NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
269  return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
270  }
271 
272  NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) {
273  return nonloc::ConcreteInt(
274  BasicVals.getIntWithPtrWidth(integer, isUnsigned));
275  }
276 
277  NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
279  }
280 
282  const llvm::APSInt& rhs, QualType type);
283 
284  NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op,
285  const SymExpr *lhs, QualType type);
286 
288  const SymExpr *rhs, QualType type);
289 
290  /// \brief Create a NonLoc value for cast.
291  NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy);
292 
295  }
296 
299  }
300 
303  }
304 
307  }
308 
309  Loc makeLoc(const MemRegion* region) {
310  return loc::MemRegionVal(region);
311  }
312 
314  return loc::GotoLabel(expr->getLabel());
315  }
316 
317  Loc makeLoc(const llvm::APSInt& integer) {
318  return loc::ConcreteInt(BasicVals.getValue(integer));
319  }
320 
321  /// Return a memory region for the 'this' object reference.
323  const StackFrameContext *SFC);
324 
325  /// Return a memory region for the 'this' object reference.
327  const StackFrameContext *SFC);
328 };
329 
330 SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
331  ASTContext &context,
332  ProgramStateManager &stateMgr);
333 
334 } // end GR namespace
335 
336 } // end clang namespace
337 
338 #endif
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Definition: ASTMatchers.h:1367
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:494
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:237
DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, const LocationContext *locContext, unsigned blockCount)
SymbolManager & getSymbolManager()
Definition: SValBuilder.h:142
A (possibly-)qualified type.
Definition: Type.h:598
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:79
QualType getArrayIndexType() const
Definition: SValBuilder.h:135
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:985
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
SVal evalCast(SVal val, QualType castTy, QualType originalType)
Value representing integer constant.
Definition: SVals.h:341
MemRegionManager MemMgr
Manages the creation of memory regions.
Definition: SValBuilder.h:44
const SymbolManager & getSymbolManager() const
Definition: SValBuilder.h:143
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:231
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:145
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy, QualType originalType)
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
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym)
Retrieve or create a "symbolic" memory region.
Definition: MemRegion.cpp:995
static bool isLocType(QualType T)
Definition: SVals.h:291
BinaryOperatorKind
const LangOptions & getLangOpts() const
Definition: ASTContext.h:604
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:309
Loc makeLoc(const AddrLabelExpr *expr)
Definition: SValBuilder.h:313
loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer)
Definition: SValBuilder.h:253
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
Definition: SValBuilder.h:217
DefinedSVal getFunctionPointer(const FunctionDecl *func)
NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:272
bool haveSameType(QualType Ty1, QualType Ty2)
Definition: SValBuilder.h:78
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
virtual QualType getType() const =0
llvm::APInt getValue() const
Definition: Expr.h:1248
nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean)
Definition: SValBuilder.h:243
Loc makeLoc(SymbolRef sym)
Definition: SValBuilder.h:305
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:3456
Expr - This represents one expression.
Definition: Expr.h:105
virtual SVal evalMinus(NonLoc val)=0
const llvm::APSInt & getIntWithPtrWidth(uint64_t X, bool isUnsigned)
nonloc::ConcreteInt makeTruthVal(bool b)
Definition: SValBuilder.h:297
const QualType ArrayIndexTy
The scalar type to use for array indices.
Definition: SValBuilder.h:49
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:249
QualType getConditionType() const
Definition: SValBuilder.h:131
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:1762
NonLoc makeIntVal(uint64_t integer, bool isUnsigned)
Definition: SValBuilder.h:268
const ASTContext & getContext() const
Definition: SValBuilder.h:127
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:5730
bool getValue() const
Definition: ExprObjC.h:71
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 'name'.
virtual SVal evalCastFromLoc(Loc val, QualType castTy)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1736
ASTContext & getContext()
Definition: SValBuilder.h:126
const MemRegionManager & getRegionManager() const
Definition: SValBuilder.h:146
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:46
const BasicValueFactory & getBasicValueFactory() const
Definition: SValBuilder.h:140
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:73
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
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:3339
QualType getType() const
Definition: Expr.h:126
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1983
NonLoc makeIntVal(const llvm::APInt &integer, bool isUnsigned)
Definition: SValBuilder.h:257
const SymbolConjured * conjureSymbol(const Stmt *stmt, const LocationContext *LCtx, QualType type, unsigned visitCount, const void *symbolTag=nullptr)
Definition: SValBuilder.h:150
NonLoc makeLocAsInteger(Loc loc, unsigned bits)
Definition: SValBuilder.h:277
ProgramStateManager & getStateManager()
Definition: SValBuilder.h:129
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:1966
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:139
Loc makeLoc(const llvm::APSInt &integer)
Definition: SValBuilder.h:317
SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
Definition: SValBuilder.h:63
LabelDecl * getLabel() const
Definition: Expr.h:3361
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
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:263
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:158
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
CanQualType IntTy
Definition: ASTContext.h:901
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:60
nonloc::ConcreteInt makeTruthVal(bool b, QualType type)
Definition: SValBuilder.h:293
DefinedSVal makeIntVal(uint64_t integer, QualType type)
Definition: SValBuilder.h:261
NonLoc makeLazyCompoundVal(const StoreRef &store, const TypedValueRegion *region)
Definition: SValBuilder.h:221
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:471
CanQualType BoolTy
Definition: ASTContext.h:894