clang  7.0.0svn
ExprEngine.h
Go to the documentation of this file.
1 //===- ExprEngine.h - Path-Sensitive Expression-Level Dataflow --*- 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 a meta-engine for path-sensitive dataflow analysis that
11 // is built on CoreEngine, but provides the boilerplate to execute transfer
12 // functions and build the ExplodedGraph at the expression level.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_EXPRENGINE_H
17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_EXPRENGINE_H
18 
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Analysis/CFG.h"
24 #include "clang/Basic/LLVM.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include <cassert>
38 #include <utility>
39 
40 namespace clang {
41 
42 class AnalysisDeclContextManager;
43 class AnalyzerOptions;
44 class ASTContext;
45 class ConstructionContext;
46 class CXXBindTemporaryExpr;
47 class CXXCatchStmt;
48 class CXXConstructExpr;
49 class CXXDeleteExpr;
50 class CXXNewExpr;
51 class CXXThisExpr;
52 class Decl;
53 class DeclStmt;
54 class GCCAsmStmt;
55 class LambdaExpr;
56 class LocationContext;
57 class MaterializeTemporaryExpr;
58 class MSAsmStmt;
59 class NamedDecl;
60 class ObjCAtSynchronizedStmt;
61 class ObjCForCollectionStmt;
62 class ObjCIvarRefExpr;
63 class ObjCMessageExpr;
64 class ReturnStmt;
65 class Stmt;
66 
67 namespace cross_tu {
68 
69 class CrossTranslationUnitContext;
70 
71 } // namespace cross_tu
72 
73 namespace ento {
74 
75 class BasicValueFactory;
76 class CallEvent;
77 class CheckerManager;
78 class ConstraintManager;
79 class CXXTempObjectRegion;
80 class MemRegion;
81 class RegionAndSymbolInvalidationTraits;
82 class SymbolManager;
83 
84 class ExprEngine : public SubEngine {
85 public:
86  /// The modes of inlining, which override the default analysis-wide settings.
88  /// Follow the default settings for inlining callees.
89  Inline_Regular = 0,
90 
91  /// Do minimal inlining of callees.
92  Inline_Minimal = 0x1
93  };
94 
95  /// Hints for figuring out of a call should be inlined during evalCall().
96  struct EvalCallOptions {
97  /// This call is a constructor or a destructor for which we do not currently
98  /// compute the this-region correctly.
99  bool IsCtorOrDtorWithImproperlyModeledTargetRegion = false;
100 
101  /// This call is a constructor or a destructor for a single element within
102  /// an array, a part of array construction or destruction.
103  bool IsArrayCtorOrDtor = false;
104 
105  /// This call is a constructor or a destructor of a temporary value.
106  bool IsTemporaryCtorOrDtor = false;
107 
108  /// This call is a constructor for a temporary that is lifetime-extended
109  /// by binding a smaller object within it to a reference, for example
110  /// 'const int &x = C().x;'.
111  bool IsTemporaryLifetimeExtendedViaSubobject = false;
112 
113  /// This call is a constructor for a temporary that is lifetime-extended
114  /// by binding it to a reference-type field within an aggregate,
115  /// for example 'A { const C &c; }; A a = { C() };'
116  bool IsTemporaryLifetimeExtendedViaAggregate = false;
117 
119  };
120 
121 private:
123 
124  AnalysisManager &AMgr;
125 
126  AnalysisDeclContextManager &AnalysisDeclContexts;
127 
128  CoreEngine Engine;
129 
130  /// G - the simulation graph.
131  ExplodedGraph &G;
132 
133  /// StateMgr - Object that manages the data for all created states.
134  ProgramStateManager StateMgr;
135 
136  /// SymMgr - Object that manages the symbol information.
137  SymbolManager &SymMgr;
138 
139  /// svalBuilder - SValBuilder object that creates SVals from expressions.
140  SValBuilder &svalBuilder;
141 
142  unsigned int currStmtIdx = 0;
143  const NodeBuilderContext *currBldrCtx = nullptr;
144 
145  /// Helper object to determine if an Objective-C message expression
146  /// implicitly never returns.
147  ObjCNoReturn ObjCNoRet;
148 
149  /// Whether or not GC is enabled in this analysis.
150  bool ObjCGCEnabled;
151 
152  /// The BugReporter associated with this engine. It is important that
153  /// this object be placed at the very end of member variables so that its
154  /// destructor is called before the rest of the ExprEngine is destroyed.
155  GRBugReporter BR;
156 
157  /// The functions which have been analyzed through inlining. This is owned by
158  /// AnalysisConsumer. It can be null.
159  SetOfConstDecls *VisitedCallees;
160 
161  /// The flag, which specifies the mode of inlining for the engine.
162  InliningModes HowToInline;
163 
164 public:
166  bool gcEnabled, SetOfConstDecls *VisitedCalleesIn,
167  FunctionSummariesTy *FS, InliningModes HowToInlineIn);
168 
169  ~ExprEngine() override;
170 
171  /// Returns true if there is still simulation state on the worklist.
172  bool ExecuteWorkList(const LocationContext *L, unsigned Steps = 150000) {
173  return Engine.ExecuteWorkList(L, Steps, nullptr);
174  }
175 
176  /// Execute the work list with an initial state. Nodes that reaches the exit
177  /// of the function are added into the Dst set, which represent the exit
178  /// state of the function call. Returns true if there is still simulation
179  /// state on the worklist.
180  bool ExecuteWorkListWithInitialState(const LocationContext *L, unsigned Steps,
181  ProgramStateRef InitState,
182  ExplodedNodeSet &Dst) {
183  return Engine.ExecuteWorkListWithInitialState(L, Steps, InitState, Dst);
184  }
185 
186  /// getContext - Return the ASTContext associated with this analysis.
187  ASTContext &getContext() const { return AMgr.getASTContext(); }
188 
189  AnalysisManager &getAnalysisManager() override { return AMgr; }
190 
192  return *AMgr.getCheckerManager();
193  }
194 
195  SValBuilder &getSValBuilder() { return svalBuilder; }
196 
197  BugReporter &getBugReporter() { return BR; }
198 
201  return &CTU;
202  }
203 
205  assert(currBldrCtx);
206  return *currBldrCtx;
207  }
208 
209  bool isObjCGCEnabled() { return ObjCGCEnabled; }
210 
211  const Stmt *getStmt() const;
212 
213  void GenerateAutoTransition(ExplodedNode *N);
214  void enqueueEndOfPath(ExplodedNodeSet &S);
215  void GenerateCallExitNode(ExplodedNode *N);
216 
217  /// Visualize the ExplodedGraph created by executing the simulation.
218  void ViewGraph(bool trim = false);
219 
220  /// Visualize a trimmed ExplodedGraph that only contains paths to the given
221  /// nodes.
222  void ViewGraph(ArrayRef<const ExplodedNode *> Nodes);
223 
224  /// getInitialState - Return the initial state used for the root vertex
225  /// in the ExplodedGraph.
226  ProgramStateRef getInitialState(const LocationContext *InitLoc) override;
227 
228  ExplodedGraph &getGraph() { return G; }
229  const ExplodedGraph &getGraph() const { return G; }
230 
231  /// Run the analyzer's garbage collection - remove dead symbols and
232  /// bindings from the state.
233  ///
234  /// Checkers can participate in this process with two callbacks:
235  /// \c checkLiveSymbols and \c checkDeadSymbols. See the CheckerDocumentation
236  /// class for more information.
237  ///
238  /// \param Node The predecessor node, from which the processing should start.
239  /// \param Out The returned set of output nodes.
240  /// \param ReferenceStmt The statement which is about to be processed.
241  /// Everything needed for this statement should be considered live.
242  /// A null statement means that everything in child LocationContexts
243  /// is dead.
244  /// \param LC The location context of the \p ReferenceStmt. A null location
245  /// context means that we have reached the end of analysis and that
246  /// all statements and local variables should be considered dead.
247  /// \param DiagnosticStmt Used as a location for any warnings that should
248  /// occur while removing the dead (e.g. leaks). By default, the
249  /// \p ReferenceStmt is used.
250  /// \param K Denotes whether this is a pre- or post-statement purge. This
251  /// must only be ProgramPoint::PostStmtPurgeDeadSymbolsKind if an
252  /// entire location context is being cleared, in which case the
253  /// \p ReferenceStmt must either be a ReturnStmt or \c NULL. Otherwise,
254  /// it must be ProgramPoint::PreStmtPurgeDeadSymbolsKind (the default)
255  /// and \p ReferenceStmt must be valid (non-null).
256  void removeDead(ExplodedNode *Node, ExplodedNodeSet &Out,
257  const Stmt *ReferenceStmt, const LocationContext *LC,
258  const Stmt *DiagnosticStmt = nullptr,
260 
261  /// processCFGElement - Called by CoreEngine. Used to generate new successor
262  /// nodes by processing the 'effects' of a CFG element.
263  void processCFGElement(const CFGElement E, ExplodedNode *Pred,
264  unsigned StmtIdx, NodeBuilderContext *Ctx) override;
265 
266  void ProcessStmt(const Stmt *S, ExplodedNode *Pred);
267 
268  void ProcessLoopExit(const Stmt* S, ExplodedNode *Pred);
269 
270  void ProcessInitializer(const CFGInitializer I, ExplodedNode *Pred);
271 
272  void ProcessImplicitDtor(const CFGImplicitDtor D, ExplodedNode *Pred);
273 
274  void ProcessNewAllocator(const CXXNewExpr *NE, ExplodedNode *Pred);
275 
276  void ProcessAutomaticObjDtor(const CFGAutomaticObjDtor D,
277  ExplodedNode *Pred, ExplodedNodeSet &Dst);
278  void ProcessDeleteDtor(const CFGDeleteDtor D,
279  ExplodedNode *Pred, ExplodedNodeSet &Dst);
280  void ProcessBaseDtor(const CFGBaseDtor D,
281  ExplodedNode *Pred, ExplodedNodeSet &Dst);
282  void ProcessMemberDtor(const CFGMemberDtor D,
283  ExplodedNode *Pred, ExplodedNodeSet &Dst);
284  void ProcessTemporaryDtor(const CFGTemporaryDtor D,
285  ExplodedNode *Pred, ExplodedNodeSet &Dst);
286 
287  /// Called by CoreEngine when processing the entrance of a CFGBlock.
288  void processCFGBlockEntrance(const BlockEdge &L,
289  NodeBuilderWithSinks &nodeBuilder,
290  ExplodedNode *Pred) override;
291 
292  /// ProcessBranch - Called by CoreEngine. Used to generate successor
293  /// nodes by processing the 'effects' of a branch condition.
294  void processBranch(const Stmt *Condition, const Stmt *Term,
295  NodeBuilderContext& BuilderCtx,
296  ExplodedNode *Pred,
297  ExplodedNodeSet &Dst,
298  const CFGBlock *DstT,
299  const CFGBlock *DstF) override;
300 
301  /// Called by CoreEngine.
302  /// Used to generate successor nodes for temporary destructors depending
303  /// on whether the corresponding constructor was visited.
304  void processCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
305  NodeBuilderContext &BldCtx,
306  ExplodedNode *Pred, ExplodedNodeSet &Dst,
307  const CFGBlock *DstT,
308  const CFGBlock *DstF) override;
309 
310  /// Called by CoreEngine. Used to processing branching behavior
311  /// at static initializers.
312  void processStaticInitializer(const DeclStmt *DS,
313  NodeBuilderContext& BuilderCtx,
314  ExplodedNode *Pred,
315  ExplodedNodeSet &Dst,
316  const CFGBlock *DstT,
317  const CFGBlock *DstF) override;
318 
319  /// processIndirectGoto - Called by CoreEngine. Used to generate successor
320  /// nodes by processing the 'effects' of a computed goto jump.
321  void processIndirectGoto(IndirectGotoNodeBuilder& builder) override;
322 
323  /// ProcessSwitch - Called by CoreEngine. Used to generate successor
324  /// nodes by processing the 'effects' of a switch statement.
325  void processSwitch(SwitchNodeBuilder& builder) override;
326 
327  /// Called by CoreEngine. Used to notify checkers that processing a
328  /// function has begun. Called for both inlined and and top-level functions.
329  void processBeginOfFunction(NodeBuilderContext &BC,
330  ExplodedNode *Pred, ExplodedNodeSet &Dst,
331  const BlockEdge &L) override;
332 
333  /// Called by CoreEngine. Used to notify checkers that processing a
334  /// function has ended. Called for both inlined and and top-level functions.
335  void processEndOfFunction(NodeBuilderContext& BC,
336  ExplodedNode *Pred,
337  const ReturnStmt *RS = nullptr) override;
338 
339  /// Remove dead bindings/symbols before exiting a function.
340  void removeDeadOnEndOfFunction(NodeBuilderContext& BC,
341  ExplodedNode *Pred,
342  ExplodedNodeSet &Dst);
343 
344  /// Generate the entry node of the callee.
345  void processCallEnter(NodeBuilderContext& BC, CallEnter CE,
346  ExplodedNode *Pred) override;
347 
348  /// Generate the sequence of nodes that simulate the call exit and the post
349  /// visit for CallExpr.
350  void processCallExit(ExplodedNode *Pred) override;
351 
352  /// Called by CoreEngine when the analysis worklist has terminated.
353  void processEndWorklist(bool hasWorkRemaining) override;
354 
355  /// evalAssume - Callback function invoked by the ConstraintManager when
356  /// making assumptions about state values.
357  ProgramStateRef processAssume(ProgramStateRef state, SVal cond,
358  bool assumption) override;
359 
360  /// processRegionChanges - Called by ProgramStateManager whenever a change is made
361  /// to the store. Used to update checkers that track region values.
363  processRegionChanges(ProgramStateRef state,
364  const InvalidatedSymbols *invalidated,
365  ArrayRef<const MemRegion *> ExplicitRegions,
367  const LocationContext *LCtx,
368  const CallEvent *Call) override;
369 
370  /// printState - Called by ProgramStateManager to print checker-specific data.
371  void printState(raw_ostream &Out, ProgramStateRef State, const char *NL,
372  const char *Sep,
373  const LocationContext *LCtx = nullptr) override;
374 
375  ProgramStateManager &getStateManager() override { return StateMgr; }
376 
377  StoreManager &getStoreManager() { return StateMgr.getStoreManager(); }
378 
380  return StateMgr.getConstraintManager();
381  }
382 
383  // FIXME: Remove when we migrate over to just using SValBuilder.
385  return StateMgr.getBasicVals();
386  }
387 
388  // FIXME: Remove when we migrate over to just using ValueManager.
389  SymbolManager &getSymbolManager() { return SymMgr; }
390  const SymbolManager &getSymbolManager() const { return SymMgr; }
391 
392  // Functions for external checking of whether we have unfinished work
393  bool wasBlocksExhausted() const { return Engine.wasBlocksExhausted(); }
394  bool hasEmptyWorkList() const { return !Engine.getWorkList()->hasWork(); }
395  bool hasWorkRemaining() const { return Engine.hasWorkRemaining(); }
396 
397  const CoreEngine &getCoreEngine() const { return Engine; }
398 
399 public:
400  /// Visit - Transfer function logic for all statements. Dispatches to
401  /// other functions that handle specific kinds of statements.
402  void Visit(const Stmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst);
403 
404  /// VisitArraySubscriptExpr - Transfer function for array accesses.
405  void VisitArraySubscriptExpr(const ArraySubscriptExpr *Ex,
406  ExplodedNode *Pred,
407  ExplodedNodeSet &Dst);
408 
409  /// VisitGCCAsmStmt - Transfer function logic for inline asm.
410  void VisitGCCAsmStmt(const GCCAsmStmt *A, ExplodedNode *Pred,
411  ExplodedNodeSet &Dst);
412 
413  /// VisitMSAsmStmt - Transfer function logic for MS inline asm.
414  void VisitMSAsmStmt(const MSAsmStmt *A, ExplodedNode *Pred,
415  ExplodedNodeSet &Dst);
416 
417  /// VisitBlockExpr - Transfer function logic for BlockExprs.
418  void VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
419  ExplodedNodeSet &Dst);
420 
421  /// VisitLambdaExpr - Transfer function logic for LambdaExprs.
422  void VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
423  ExplodedNodeSet &Dst);
424 
425  /// VisitBinaryOperator - Transfer function logic for binary operators.
426  void VisitBinaryOperator(const BinaryOperator* B, ExplodedNode *Pred,
427  ExplodedNodeSet &Dst);
428 
429 
430  /// VisitCall - Transfer function for function calls.
431  void VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred,
432  ExplodedNodeSet &Dst);
433 
434  /// VisitCast - Transfer function logic for all casts (implicit and explicit).
435  void VisitCast(const CastExpr *CastE, const Expr *Ex, ExplodedNode *Pred,
436  ExplodedNodeSet &Dst);
437 
438  /// VisitCompoundLiteralExpr - Transfer function logic for compound literals.
439  void VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
440  ExplodedNode *Pred, ExplodedNodeSet &Dst);
441 
442  /// Transfer function logic for DeclRefExprs and BlockDeclRefExprs.
443  void VisitCommonDeclRefExpr(const Expr *DR, const NamedDecl *D,
444  ExplodedNode *Pred, ExplodedNodeSet &Dst);
445 
446  /// VisitDeclStmt - Transfer function logic for DeclStmts.
447  void VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
448  ExplodedNodeSet &Dst);
449 
450  /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
451  void VisitGuardedExpr(const Expr *Ex, const Expr *L, const Expr *R,
452  ExplodedNode *Pred, ExplodedNodeSet &Dst);
453 
454  void VisitInitListExpr(const InitListExpr *E, ExplodedNode *Pred,
455  ExplodedNodeSet &Dst);
456 
457  /// VisitLogicalExpr - Transfer function logic for '&&', '||'
458  void VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
459  ExplodedNodeSet &Dst);
460 
461  /// VisitMemberExpr - Transfer function for member expressions.
462  void VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred,
463  ExplodedNodeSet &Dst);
464 
465  /// VisitAtomicExpr - Transfer function for builtin atomic expressions
466  void VisitAtomicExpr(const AtomicExpr *E, ExplodedNode *Pred,
467  ExplodedNodeSet &Dst);
468 
469  /// Transfer function logic for ObjCAtSynchronizedStmts.
470  void VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S,
471  ExplodedNode *Pred, ExplodedNodeSet &Dst);
472 
473  /// Transfer function logic for computing the lvalue of an Objective-C ivar.
474  void VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *DR, ExplodedNode *Pred,
475  ExplodedNodeSet &Dst);
476 
477  /// VisitObjCForCollectionStmt - Transfer function logic for
478  /// ObjCForCollectionStmt.
479  void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S,
480  ExplodedNode *Pred, ExplodedNodeSet &Dst);
481 
482  void VisitObjCMessage(const ObjCMessageExpr *ME, ExplodedNode *Pred,
483  ExplodedNodeSet &Dst);
484 
485  /// VisitReturnStmt - Transfer function logic for return statements.
486  void VisitReturnStmt(const ReturnStmt *R, ExplodedNode *Pred,
487  ExplodedNodeSet &Dst);
488 
489  /// VisitOffsetOfExpr - Transfer function for offsetof.
490  void VisitOffsetOfExpr(const OffsetOfExpr *Ex, ExplodedNode *Pred,
491  ExplodedNodeSet &Dst);
492 
493  /// VisitUnaryExprOrTypeTraitExpr - Transfer function for sizeof.
494  void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
495  ExplodedNode *Pred, ExplodedNodeSet &Dst);
496 
497  /// VisitUnaryOperator - Transfer function logic for unary operators.
498  void VisitUnaryOperator(const UnaryOperator* B, ExplodedNode *Pred,
499  ExplodedNodeSet &Dst);
500 
501  /// Handle ++ and -- (both pre- and post-increment).
502  void VisitIncrementDecrementOperator(const UnaryOperator* U,
503  ExplodedNode *Pred,
504  ExplodedNodeSet &Dst);
505 
506  void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE,
507  ExplodedNodeSet &PreVisit,
508  ExplodedNodeSet &Dst);
509 
510  void VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred,
511  ExplodedNodeSet &Dst);
512 
513  void VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
514  ExplodedNodeSet & Dst);
515 
516  void VisitCXXConstructExpr(const CXXConstructExpr *E, ExplodedNode *Pred,
517  ExplodedNodeSet &Dst);
518 
519  void VisitCXXDestructor(QualType ObjectType, const MemRegion *Dest,
520  const Stmt *S, bool IsBaseDtor,
521  ExplodedNode *Pred, ExplodedNodeSet &Dst,
522  const EvalCallOptions &Options);
523 
524  void VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
525  ExplodedNode *Pred,
526  ExplodedNodeSet &Dst);
527 
528  void VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
529  ExplodedNodeSet &Dst);
530 
531  void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, ExplodedNode *Pred,
532  ExplodedNodeSet &Dst);
533 
534  /// Create a C++ temporary object for an rvalue.
535  void CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
536  ExplodedNode *Pred,
537  ExplodedNodeSet &Dst);
538 
539  /// evalEagerlyAssumeBinOpBifurcation - Given the nodes in 'Src', eagerly assume symbolic
540  /// expressions of the form 'x != 0' and generate new nodes (stored in Dst)
541  /// with those assumptions.
542  void evalEagerlyAssumeBinOpBifurcation(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
543  const Expr *Ex);
544 
545  std::pair<const ProgramPointTag *, const ProgramPointTag *>
546  geteagerlyAssumeBinOpBifurcationTags();
547 
549  return X.isValid() ? svalBuilder.evalMinus(X.castAs<NonLoc>()) : X;
550  }
551 
553  return X.isValid() ? svalBuilder.evalComplement(X.castAs<NonLoc>()) : X;
554  }
555 
556  ProgramStateRef handleLValueBitCast(ProgramStateRef state, const Expr *Ex,
557  const LocationContext *LCtx, QualType T,
558  QualType ExTy, const CastExpr *CastE,
559  StmtNodeBuilder &Bldr,
560  ExplodedNode *Pred);
561 
562  ProgramStateRef handleLVectorSplat(ProgramStateRef state,
563  const LocationContext *LCtx,
564  const CastExpr *CastE,
565  StmtNodeBuilder &Bldr,
566  ExplodedNode *Pred);
567 
568  void handleUOExtension(ExplodedNodeSet::iterator I,
569  const UnaryOperator* U,
570  StmtNodeBuilder &Bldr);
571 
572 public:
574  NonLoc L, NonLoc R, QualType T) {
575  return svalBuilder.evalBinOpNN(state, op, L, R, T);
576  }
577 
579  NonLoc L, SVal R, QualType T) {
580  return R.isValid() ? svalBuilder.evalBinOpNN(state, op, L,
581  R.castAs<NonLoc>(), T) : R;
582  }
583 
585  SVal LHS, SVal RHS, QualType T) {
586  return svalBuilder.evalBinOp(ST, Op, LHS, RHS, T);
587  }
588 
589 protected:
590  /// evalBind - Handle the semantics of binding a value to a specific location.
591  /// This method is used by evalStore, VisitDeclStmt, and others.
592  void evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE, ExplodedNode *Pred,
593  SVal location, SVal Val, bool atDeclInit = false,
594  const ProgramPoint *PP = nullptr);
595 
596  /// Call PointerEscape callback when a value escapes as a result of bind.
597  ProgramStateRef processPointerEscapedOnBind(ProgramStateRef State,
598  SVal Loc,
599  SVal Val,
600  const LocationContext *LCtx) override;
601  /// Call PointerEscape callback when a value escapes as a result of
602  /// region invalidation.
603  /// \param[in] ITraits Specifies invalidation traits for regions/symbols.
604  ProgramStateRef notifyCheckersOfPointerEscape(
605  ProgramStateRef State,
606  const InvalidatedSymbols *Invalidated,
607  ArrayRef<const MemRegion *> ExplicitRegions,
609  const CallEvent *Call,
610  RegionAndSymbolInvalidationTraits &ITraits) override;
611 
612  /// A simple wrapper when you only need to notify checkers of pointer-escape
613  /// of a single value.
614  ProgramStateRef escapeValue(ProgramStateRef State, SVal V,
615  PointerEscapeKind K) const;
616 
617 public:
618  // FIXME: 'tag' should be removed, and a LocationContext should be used
619  // instead.
620  // FIXME: Comment on the meaning of the arguments, when 'St' may not
621  // be the same as Pred->state, and when 'location' may not be the
622  // same as state->getLValue(Ex).
623  /// Simulate a read of the result of Ex.
624  void evalLoad(ExplodedNodeSet &Dst,
625  const Expr *NodeEx, /* Eventually will be a CFGStmt */
626  const Expr *BoundExpr,
627  ExplodedNode *Pred,
628  ProgramStateRef St,
629  SVal location,
630  const ProgramPointTag *tag = nullptr,
631  QualType LoadTy = QualType());
632 
633  // FIXME: 'tag' should be removed, and a LocationContext should be used
634  // instead.
635  void evalStore(ExplodedNodeSet &Dst, const Expr *AssignE, const Expr *StoreE,
636  ExplodedNode *Pred, ProgramStateRef St, SVal TargetLV, SVal Val,
637  const ProgramPointTag *tag = nullptr);
638 
639  /// Return the CFG element corresponding to the worklist element
640  /// that is currently being processed by ExprEngine.
642  return (*currBldrCtx->getBlock())[currStmtIdx];
643  }
644 
645  /// Create a new state in which the call return value is binded to the
646  /// call origin expression.
647  ProgramStateRef bindReturnValue(const CallEvent &Call,
648  const LocationContext *LCtx,
649  ProgramStateRef State);
650 
651  /// Evaluate a call, running pre- and post-call checks and allowing checkers
652  /// to be responsible for handling the evaluation of the call itself.
653  void evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred,
654  const CallEvent &Call);
655 
656  /// Default implementation of call evaluation.
657  void defaultEvalCall(NodeBuilder &B, ExplodedNode *Pred,
658  const CallEvent &Call,
659  const EvalCallOptions &CallOpts = {});
660 
661 private:
662  void evalLoadCommon(ExplodedNodeSet &Dst,
663  const Expr *NodeEx, /* Eventually will be a CFGStmt */
664  const Expr *BoundEx,
665  ExplodedNode *Pred,
666  ProgramStateRef St,
667  SVal location,
668  const ProgramPointTag *tag,
669  QualType LoadTy);
670 
671  // FIXME: 'tag' should be removed, and a LocationContext should be used
672  // instead.
673  void evalLocation(ExplodedNodeSet &Dst,
674  const Stmt *NodeEx, /* This will eventually be a CFGStmt */
675  const Stmt *BoundEx,
676  ExplodedNode *Pred,
677  ProgramStateRef St, SVal location,
678  const ProgramPointTag *tag, bool isLoad);
679 
680  /// Count the stack depth and determine if the call is recursive.
681  void examineStackFrames(const Decl *D, const LocationContext *LCtx,
682  bool &IsRecursive, unsigned &StackDepth);
683 
684  enum CallInlinePolicy {
685  CIP_Allowed,
686  CIP_DisallowedOnce,
687  CIP_DisallowedAlways
688  };
689 
690  /// See if a particular call should be inlined, by only looking
691  /// at the call event and the current state of analysis.
692  CallInlinePolicy mayInlineCallKind(const CallEvent &Call,
693  const ExplodedNode *Pred,
694  AnalyzerOptions &Opts,
695  const EvalCallOptions &CallOpts);
696 
697  /// Checks our policies and decides weither the given call should be inlined.
698  bool shouldInlineCall(const CallEvent &Call, const Decl *D,
699  const ExplodedNode *Pred,
700  const EvalCallOptions &CallOpts = {});
701 
702  bool inlineCall(const CallEvent &Call, const Decl *D, NodeBuilder &Bldr,
703  ExplodedNode *Pred, ProgramStateRef State);
704 
705  /// Conservatively evaluate call by invalidating regions and binding
706  /// a conjured return value.
707  void conservativeEvalCall(const CallEvent &Call, NodeBuilder &Bldr,
708  ExplodedNode *Pred, ProgramStateRef State);
709 
710  /// Either inline or process the call conservatively (or both), based
711  /// on DynamicDispatchBifurcation data.
712  void BifurcateCall(const MemRegion *BifurReg,
713  const CallEvent &Call, const Decl *D, NodeBuilder &Bldr,
714  ExplodedNode *Pred);
715 
716  bool replayWithoutInlining(ExplodedNode *P, const LocationContext *CalleeLC);
717 
718  /// Models a trivial copy or move constructor or trivial assignment operator
719  /// call with a simple bind.
720  void performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
721  const CallEvent &Call);
722 
723  /// If the value of the given expression \p InitWithAdjustments is a NonLoc,
724  /// copy it into a new temporary object region, and replace the value of the
725  /// expression with that.
726  ///
727  /// If \p Result is provided, the new region will be bound to this expression
728  /// instead of \p InitWithAdjustments.
729  ProgramStateRef createTemporaryRegionIfNeeded(ProgramStateRef State,
730  const LocationContext *LC,
731  const Expr *InitWithAdjustments,
732  const Expr *Result = nullptr);
733 
734  /// Returns a region representing the first element of a (possibly
735  /// multi-dimensional) array, for the purposes of element construction or
736  /// destruction.
737  ///
738  /// On return, \p Ty will be set to the base type of the array.
739  ///
740  /// If the type is not an array type at all, the original value is returned.
741  /// Otherwise the "IsArray" flag is set.
742  static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue,
743  QualType &Ty, bool &IsArray);
744 
745  /// For a DeclStmt or CXXInitCtorInitializer, walk backward in the current CFG
746  /// block to find the constructor expression that directly constructed into
747  /// the storage for this statement. Returns null if the constructor for this
748  /// statement created a temporary object region rather than directly
749  /// constructing into an existing region.
750  const CXXConstructExpr *findDirectConstructorForCurrentCFGElement();
751 
752  /// For a given constructor, look forward in the current CFG block to
753  /// determine the region into which an object will be constructed by \p CE.
754  /// When the lookahead fails, a temporary region is returned, and the
755  /// IsConstructorWithImproperlyModeledTargetRegion flag is set in \p CallOpts.
756  const MemRegion *getRegionForConstructedObject(const CXXConstructExpr *CE,
757  ExplodedNode *Pred,
758  const ConstructionContext *CC,
759  EvalCallOptions &CallOpts);
760 
761  /// Store the region of a C++ temporary object corresponding to a
762  /// CXXBindTemporaryExpr for later destruction.
763  static ProgramStateRef addInitializedTemporary(
764  ProgramStateRef State, const CXXBindTemporaryExpr *BTE,
765  const LocationContext *LC, const CXXTempObjectRegion *R);
766 
767  /// Check if all initialized temporary regions are clear for the given
768  /// context range (including FromLC, not including ToLC).
769  /// This is useful for assertions.
770  static bool areInitializedTemporariesClear(ProgramStateRef State,
771  const LocationContext *FromLC,
772  const LocationContext *ToLC);
773 
774  /// Store the region of a C++ temporary object corresponding to a
775  /// CXXBindTemporaryExpr for later destruction.
776  static ProgramStateRef addTemporaryMaterialization(
777  ProgramStateRef State, const MaterializeTemporaryExpr *MTE,
778  const LocationContext *LC, const CXXTempObjectRegion *R);
779 
780  /// Check if all temporary materialization regions are clear for the given
781  /// context range (including FromLC, not including ToLC).
782  /// This is useful for assertions.
783  static bool areTemporaryMaterializationsClear(ProgramStateRef State,
784  const LocationContext *FromLC,
785  const LocationContext *ToLC);
786 
787  /// Adds an initialized temporary and/or a materialization, whichever is
788  /// necessary, by looking at the whole construction context. Handles
789  /// function return values, which need the construction context of the parent
790  /// stack frame, automagically.
791  ProgramStateRef addAllNecessaryTemporaryInfo(
792  ProgramStateRef State, const ConstructionContext *CC,
793  const LocationContext *LC, const MemRegion *R);
794 
795  /// Store the region returned by operator new() so that the constructor
796  /// that follows it knew what location to initialize. The value should be
797  /// cleared once the respective CXXNewExpr CFGStmt element is processed.
798  static ProgramStateRef
799  setCXXNewAllocatorValue(ProgramStateRef State, const CXXNewExpr *CNE,
800  const LocationContext *CallerLC, SVal V);
801 
802  /// Retrieve the location returned by the current operator new().
803  static SVal
804  getCXXNewAllocatorValue(ProgramStateRef State, const CXXNewExpr *CNE,
805  const LocationContext *CallerLC);
806 
807  /// Clear the location returned by the respective operator new(). This needs
808  /// to be done as soon as CXXNewExpr CFG block is evaluated.
809  static ProgramStateRef
810  clearCXXNewAllocatorValue(ProgramStateRef State, const CXXNewExpr *CNE,
811  const LocationContext *CallerLC);
812 
813  /// Check if all allocator values are clear for the given context range
814  /// (including FromLC, not including ToLC). This is useful for assertions.
815  static bool areCXXNewAllocatorValuesClear(ProgramStateRef State,
816  const LocationContext *FromLC,
817  const LocationContext *ToLC);
818 };
819 
820 /// Traits for storing the call processing policy inside GDM.
821 /// The GDM stores the corresponding CallExpr pointer.
822 // FIXME: This does not use the nice trait macros because it must be accessible
823 // from multiple translation units.
825 template <>
827  public ProgramStatePartialTrait<const void*> {
828  static void *GDMIndex() { static int index = 0; return &index; }
829 };
830 
831 } // namespace ento
832 
833 } // namespace clang
834 
835 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_EXPRENGINE_H
This represents a GCC inline-assembly statement extension.
Definition: Stmt.h:1636
A (possibly-)qualified type.
Definition: Type.h:654
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
CheckerManager * getCheckerManager() const
bool ExecuteWorkList(const LocationContext *L, unsigned Steps, ProgramStateRef InitState)
ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
Definition: CoreEngine.cpp:79
Stmt - This represents one statement.
Definition: Stmt.h:66
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1384
BasicValueFactory & getBasicVals()
Definition: ProgramState.h:526
This builder class is useful for generating nodes that resulted from visiting a statement.
Definition: CoreEngine.h:370
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Represents a point when we begin processing an inlined call.
Definition: ProgramPoint.h:600
StringRef P
bool ExecuteWorkListWithInitialState(const LocationContext *L, unsigned Steps, ProgramStateRef InitState, ExplodedNodeSet &Dst)
Execute the work list with an initial state.
Definition: ExprEngine.h:180
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc L, NonLoc R, QualType T)
Definition: ExprEngine.h:573
CFGDeleteDtor - Represents C++ object destructor generated from a call to delete. ...
Definition: CFG.h:405
Hints for figuring out of a call should be inlined during evalCall().
Definition: ExprEngine.h:96
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1239
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
Definition: ExprCXX.h:4039
cross_tu::CrossTranslationUnitContext * getCrossTranslationUnitContext() override
Definition: ExprEngine.h:200
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2674
const NodeBuilderContext & getBuilderContext()
Definition: ExprEngine.h:204
bool ExecuteWorkListWithInitialState(const LocationContext *L, unsigned Steps, ProgramStateRef InitState, ExplodedNodeSet &Dst)
Returns true if there is still simulation state on the worklist.
Definition: CoreEngine.cpp:196
BoundNodesTreeBuilder Nodes
bool hasWorkRemaining() const
Definition: CoreEngine.h:155
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
LineState State
CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated for automatic object or t...
Definition: CFG.h:379
bool hasEmptyWorkList() const
Definition: ExprEngine.h:394
bool wasBlocksExhausted() const
Definition: ExprEngine.h:393
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
Describes an C or C++ initializer list.
Definition: Expr.h:3911
BinaryOperatorKind
WorkList * getWorkList() const
Definition: CoreEngine.h:165
CFGElement getCurrentCFGElement()
Return the CFG element corresponding to the worklist element that is currently being processed by Exp...
Definition: ExprEngine.h:641
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3024
const CoreEngine & getCoreEngine() const
Definition: ExprEngine.h:397
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2747
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1196
SVal evalComplement(SVal X)
Definition: ExprEngine.h:552
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1583
ASTContext & getASTContext() override
Represents the this expression in C++.
Definition: ExprCXX.h:945
CheckerManager & getCheckerManager() const
Definition: ExprEngine.h:191
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand...
Definition: Expr.h:2064
SVal evalBinOp(ProgramStateRef ST, BinaryOperator::Opcode Op, SVal LHS, SVal RHS, QualType T)
Definition: ExprEngine.h:584
CFGBlock - Represents a single basic block in a source-level CFG.
Definition: CFG.h:548
InliningModes
The modes of inlining, which override the default analysis-wide settings.
Definition: ExprEngine.h:87
Expr - This represents one expression.
Definition: Expr.h:106
virtual SVal evalMinus(NonLoc val)=0
const FunctionProtoType * T
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4890
This is the simplest builder which generates nodes in the ExplodedGraph.
Definition: CoreEngine.h:228
bool isValid() const
Definition: SVals.h:149
Represents Objective-C&#39;s @synchronized statement.
Definition: StmtObjC.h:262
ASTContext & getContext() const
getContext - Return the ASTContext associated with this analysis.
Definition: ExprEngine.h:187
Traits for storing the call processing policy inside GDM.
Definition: ExprEngine.h:824
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
Definition: Stmt.h:1433
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:903
This represents a Microsoft inline-assembly statement extension.
Definition: Stmt.h:1811
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1741
CFGBaseDtor - Represents C++ object destructor implicitly generated for base object in destructor...
Definition: CFG.h:431
SymbolManager & getSymbolManager()
Definition: ExprEngine.h:389
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:416
ProgramPoints can be "tagged" as representing points specific to a given analysis entity...
Definition: ProgramPoint.h:40
AnalysisManager & getAnalysisManager() override
Definition: ExprEngine.h:189
bool wasBlocksExhausted() const
Definition: CoreEngine.h:154
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:1845
virtual bool hasWork() const =0
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Definition: Stmt.h:499
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:76
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the similarly-named C++11 instructions, and __c11 variants for <stdatomic.h>, and corresponding __opencl_atomic_* for OpenCL 2.0.
Definition: Expr.h:5142
BugReporter & getBugReporter()
Definition: ExprEngine.h:197
BasicValueFactory & getBasicVals()
Definition: ExprEngine.h:384
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.
bool ExecuteWorkList(const LocationContext *L, unsigned Steps=150000)
Returns true if there is still simulation state on the worklist.
Definition: ExprEngine.h:172
ast_type_traits::DynTypedNode Node
CoreEngine - Implements the core logic of the graph-reachability analysis.
Definition: CoreEngine.h:55
const SymbolManager & getSymbolManager() const
Definition: ExprEngine.h:390
Dataflow Directional Tag Classes.
SValBuilder & getSValBuilder()
Definition: ExprEngine.h:195
Represents a delete expression for memory deallocation and destructor calls, e.g. ...
Definition: ExprCXX.h:2074
StoreManager & getStoreManager()
Definition: ExprEngine.h:377
bool hasWorkRemaining() const
Definition: ExprEngine.h:395
This node builder keeps track of the generated sink nodes.
Definition: CoreEngine.h:333
const ExplodedGraph & getGraph() const
Definition: ExprEngine.h:229
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:164
ProgramStateManager & getStateManager() override
Definition: ExprEngine.h:375
This class is used for tools that requires cross translation unit capability.
PointerEscapeKind
Describes the different reasons a pointer escapes during analysis.
ConstraintManager & getConstraintManager()
Definition: ProgramState.h:553
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:104
SVal evalMinus(SVal X)
Definition: ExprEngine.h:548
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2154
Represents Objective-C&#39;s collection statement.
Definition: StmtObjC.h:24
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:513
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13503
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:2424
ConstructionContext&#39;s subclasses describe different ways of constructing an object in C++...
virtual SVal evalComplement(NonLoc val)=0
CFGImplicitDtor - Represents C++ object destructor implicitly generated by compiler on various occasi...
Definition: CFG.h:354
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc L, SVal R, QualType T)
Definition: ExprEngine.h:578
CXXCatchStmt - This represents a C++ catch block.
Definition: StmtCXX.h:29
CFGElement - Represents a top-level expression in a basic block.
Definition: CFG.h:55
CFGMemberDtor - Represents C++ object destructor implicitly generated for member object in destructor...
Definition: CFG.h:452
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2242
CFGInitializer - Represents C++ base or member initializer from constructor&#39;s initialization list...
Definition: CFG.h:215
ExplodedGraph & getGraph()
Definition: ExprEngine.h:228
This represents a decl that may have a name.
Definition: Decl.h:248
ConstraintManager & getConstraintManager()
Definition: ExprEngine.h:379
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type, member-designator).
Definition: Expr.h:1959
CFGTemporaryDtor - Represents C++ object destructor implicitly generated at the end of full expressio...
Definition: CFG.h:473
const CFGBlock * getBlock() const
Return the CFGBlock associated with this builder.
Definition: CoreEngine.h:208