ExprEngine.h revision bc403861bc4e6f7ad1371e9e129f0f25b38b3a9a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===-- ExprEngine.h - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines a meta-engine for path-sensitive dataflow analysis that 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is built on CoreEngine, but provides the boilerplate to execute transfer 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// functions and build the ExplodedGraph at the expression level. 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_CLANG_GR_EXPRENGINE 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_CLANG_GR_EXPRENGINE 181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Expr.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Type.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Analysis/DomainSpecific/ObjCNoReturn.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h" 251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" 281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace clang { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AnalysisDeclContextManager; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXCatchStmt; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXConstructExpr; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXDeleteExpr; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXNewExpr; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXTemporaryObjectExpr; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXThisExpr; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MaterializeTemporaryExpr; 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ObjCAtSynchronizedStmt; 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ObjCForCollectionStmt; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ento { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AnalysisManager; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CallEvent; 461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass SimpleCall; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXConstructorCall; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ExprEngine : public SubEngine { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// The modes of inlining. 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum InliningModes { 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Do not inline any of the callees. 54 Inline_None = 0, 55 /// Inline all callees. 56 Inline_All = 0x1 57 } ; 58 59private: 60 AnalysisManager &AMgr; 61 62 AnalysisDeclContextManager &AnalysisDeclContexts; 63 64 CoreEngine Engine; 65 66 /// G - the simulation graph. 67 ExplodedGraph& G; 68 69 /// StateMgr - Object that manages the data for all created states. 70 ProgramStateManager StateMgr; 71 72 /// SymMgr - Object that manages the symbol information. 73 SymbolManager& SymMgr; 74 75 /// svalBuilder - SValBuilder object that creates SVals from expressions. 76 SValBuilder &svalBuilder; 77 78 unsigned int currStmtIdx; 79 const NodeBuilderContext *currBldrCtx; 80 81 /// Helper object to determine if an Objective-C message expression 82 /// implicitly never returns. 83 ObjCNoReturn ObjCNoRet; 84 85 /// Whether or not GC is enabled in this analysis. 86 bool ObjCGCEnabled; 87 88 /// The BugReporter associated with this engine. It is important that 89 /// this object be placed at the very end of member variables so that its 90 /// destructor is called before the rest of the ExprEngine is destroyed. 91 GRBugReporter BR; 92 93 /// The functions which have been analyzed through inlining. This is owned by 94 /// AnalysisConsumer. It can be null. 95 SetOfConstDecls *VisitedCallees; 96 97 /// The flag, which specifies the mode of inlining for the engine. 98 InliningModes HowToInline; 99 100public: 101 ExprEngine(AnalysisManager &mgr, bool gcEnabled, 102 SetOfConstDecls *VisitedCalleesIn, 103 FunctionSummariesTy *FS, 104 InliningModes HowToInlineIn); 105 106 ~ExprEngine(); 107 108 /// Returns true if there is still simulation state on the worklist. 109 bool ExecuteWorkList(const LocationContext *L, unsigned Steps = 150000) { 110 return Engine.ExecuteWorkList(L, Steps, 0); 111 } 112 113 /// Execute the work list with an initial state. Nodes that reaches the exit 114 /// of the function are added into the Dst set, which represent the exit 115 /// state of the function call. Returns true if there is still simulation 116 /// state on the worklist. 117 bool ExecuteWorkListWithInitialState(const LocationContext *L, unsigned Steps, 118 ProgramStateRef InitState, 119 ExplodedNodeSet &Dst) { 120 return Engine.ExecuteWorkListWithInitialState(L, Steps, InitState, Dst); 121 } 122 123 /// getContext - Return the ASTContext associated with this analysis. 124 ASTContext &getContext() const { return AMgr.getASTContext(); } 125 126 virtual AnalysisManager &getAnalysisManager() { return AMgr; } 127 128 CheckerManager &getCheckerManager() const { 129 return *AMgr.getCheckerManager(); 130 } 131 132 SValBuilder &getSValBuilder() { return svalBuilder; } 133 134 BugReporter& getBugReporter() { return BR; } 135 136 const NodeBuilderContext &getBuilderContext() { 137 assert(currBldrCtx); 138 return *currBldrCtx; 139 } 140 141 bool isObjCGCEnabled() { return ObjCGCEnabled; } 142 143 const Stmt *getStmt() const; 144 145 void GenerateAutoTransition(ExplodedNode *N); 146 void enqueueEndOfPath(ExplodedNodeSet &S); 147 void GenerateCallExitNode(ExplodedNode *N); 148 149 /// ViewGraph - Visualize the ExplodedGraph created by executing the 150 /// simulation. 151 void ViewGraph(bool trim = false); 152 153 void ViewGraph(ExplodedNode** Beg, ExplodedNode** End); 154 155 /// getInitialState - Return the initial state used for the root vertex 156 /// in the ExplodedGraph. 157 ProgramStateRef getInitialState(const LocationContext *InitLoc); 158 159 ExplodedGraph& getGraph() { return G; } 160 const ExplodedGraph& getGraph() const { return G; } 161 162 /// \brief Run the analyzer's garbage collection - remove dead symbols and 163 /// bindings from the state. 164 /// 165 /// Checkers can participate in this process with two callbacks: 166 /// \c checkLiveSymbols and \c checkDeadSymbols. See the CheckerDocumentation 167 /// class for more information. 168 /// 169 /// \param Node The predecessor node, from which the processing should start. 170 /// \param Out The returned set of output nodes. 171 /// \param ReferenceStmt The statement which is about to be processed. 172 /// Everything needed for this statement should be considered live. 173 /// A null statement means that everything in child LocationContexts 174 /// is dead. 175 /// \param LC The location context of the \p ReferenceStmt. A null location 176 /// context means that we have reached the end of analysis and that 177 /// all statements and local variables should be considered dead. 178 /// \param DiagnosticStmt Used as a location for any warnings that should 179 /// occur while removing the dead (e.g. leaks). By default, the 180 /// \p ReferenceStmt is used. 181 /// \param K Denotes whether this is a pre- or post-statement purge. This 182 /// must only be ProgramPoint::PostStmtPurgeDeadSymbolsKind if an 183 /// entire location context is being cleared, in which case the 184 /// \p ReferenceStmt must either be a ReturnStmt or \c NULL. Otherwise, 185 /// it must be ProgramPoint::PreStmtPurgeDeadSymbolsKind (the default) 186 /// and \p ReferenceStmt must be valid (non-null). 187 void removeDead(ExplodedNode *Node, ExplodedNodeSet &Out, 188 const Stmt *ReferenceStmt, const LocationContext *LC, 189 const Stmt *DiagnosticStmt = 0, 190 ProgramPoint::Kind K = ProgramPoint::PreStmtPurgeDeadSymbolsKind); 191 192 /// processCFGElement - Called by CoreEngine. Used to generate new successor 193 /// nodes by processing the 'effects' of a CFG element. 194 void processCFGElement(const CFGElement E, ExplodedNode *Pred, 195 unsigned StmtIdx, NodeBuilderContext *Ctx); 196 197 void ProcessStmt(const CFGStmt S, ExplodedNode *Pred); 198 199 void ProcessInitializer(const CFGInitializer I, ExplodedNode *Pred); 200 201 void ProcessImplicitDtor(const CFGImplicitDtor D, ExplodedNode *Pred); 202 203 void ProcessAutomaticObjDtor(const CFGAutomaticObjDtor D, 204 ExplodedNode *Pred, ExplodedNodeSet &Dst); 205 void ProcessBaseDtor(const CFGBaseDtor D, 206 ExplodedNode *Pred, ExplodedNodeSet &Dst); 207 void ProcessMemberDtor(const CFGMemberDtor D, 208 ExplodedNode *Pred, ExplodedNodeSet &Dst); 209 void ProcessTemporaryDtor(const CFGTemporaryDtor D, 210 ExplodedNode *Pred, ExplodedNodeSet &Dst); 211 212 /// Called by CoreEngine when processing the entrance of a CFGBlock. 213 virtual void processCFGBlockEntrance(const BlockEdge &L, 214 NodeBuilderWithSinks &nodeBuilder, 215 ExplodedNode *Pred); 216 217 /// ProcessBranch - Called by CoreEngine. Used to generate successor 218 /// nodes by processing the 'effects' of a branch condition. 219 void processBranch(const Stmt *Condition, const Stmt *Term, 220 NodeBuilderContext& BuilderCtx, 221 ExplodedNode *Pred, 222 ExplodedNodeSet &Dst, 223 const CFGBlock *DstT, 224 const CFGBlock *DstF); 225 226 /// processIndirectGoto - Called by CoreEngine. Used to generate successor 227 /// nodes by processing the 'effects' of a computed goto jump. 228 void processIndirectGoto(IndirectGotoNodeBuilder& builder); 229 230 /// ProcessSwitch - Called by CoreEngine. Used to generate successor 231 /// nodes by processing the 'effects' of a switch statement. 232 void processSwitch(SwitchNodeBuilder& builder); 233 234 /// Called by CoreEngine. Used to generate end-of-path 235 /// nodes when the control reaches the end of a function. 236 void processEndOfFunction(NodeBuilderContext& BC, 237 ExplodedNode *Pred); 238 239 /// Remove dead bindings/symbols before exiting a function. 240 void removeDeadOnEndOfFunction(NodeBuilderContext& BC, 241 ExplodedNode *Pred, 242 ExplodedNodeSet &Dst); 243 244 /// Generate the entry node of the callee. 245 void processCallEnter(CallEnter CE, ExplodedNode *Pred); 246 247 /// Generate the sequence of nodes that simulate the call exit and the post 248 /// visit for CallExpr. 249 void processCallExit(ExplodedNode *Pred); 250 251 /// Called by CoreEngine when the analysis worklist has terminated. 252 void processEndWorklist(bool hasWorkRemaining); 253 254 /// evalAssume - Callback function invoked by the ConstraintManager when 255 /// making assumptions about state values. 256 ProgramStateRef processAssume(ProgramStateRef state, SVal cond,bool assumption); 257 258 /// wantsRegionChangeUpdate - Called by ProgramStateManager to determine if a 259 /// region change should trigger a processRegionChanges update. 260 bool wantsRegionChangeUpdate(ProgramStateRef state); 261 262 /// processRegionChanges - Called by ProgramStateManager whenever a change is made 263 /// to the store. Used to update checkers that track region values. 264 ProgramStateRef 265 processRegionChanges(ProgramStateRef state, 266 const InvalidatedSymbols *invalidated, 267 ArrayRef<const MemRegion *> ExplicitRegions, 268 ArrayRef<const MemRegion *> Regions, 269 const CallEvent *Call); 270 271 /// printState - Called by ProgramStateManager to print checker-specific data. 272 void printState(raw_ostream &Out, ProgramStateRef State, 273 const char *NL, const char *Sep); 274 275 virtual ProgramStateManager& getStateManager() { return StateMgr; } 276 277 StoreManager& getStoreManager() { return StateMgr.getStoreManager(); } 278 279 ConstraintManager& getConstraintManager() { 280 return StateMgr.getConstraintManager(); 281 } 282 283 // FIXME: Remove when we migrate over to just using SValBuilder. 284 BasicValueFactory& getBasicVals() { 285 return StateMgr.getBasicVals(); 286 } 287 288 // FIXME: Remove when we migrate over to just using ValueManager. 289 SymbolManager& getSymbolManager() { return SymMgr; } 290 const SymbolManager& getSymbolManager() const { return SymMgr; } 291 292 // Functions for external checking of whether we have unfinished work 293 bool wasBlocksExhausted() const { return Engine.wasBlocksExhausted(); } 294 bool hasEmptyWorkList() const { return !Engine.getWorkList()->hasWork(); } 295 bool hasWorkRemaining() const { return Engine.hasWorkRemaining(); } 296 297 const CoreEngine &getCoreEngine() const { return Engine; } 298 299public: 300 /// Visit - Transfer function logic for all statements. Dispatches to 301 /// other functions that handle specific kinds of statements. 302 void Visit(const Stmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst); 303 304 /// VisitArraySubscriptExpr - Transfer function for array accesses. 305 void VisitLvalArraySubscriptExpr(const ArraySubscriptExpr *Ex, 306 ExplodedNode *Pred, 307 ExplodedNodeSet &Dst); 308 309 /// VisitGCCAsmStmt - Transfer function logic for inline asm. 310 void VisitGCCAsmStmt(const GCCAsmStmt *A, ExplodedNode *Pred, 311 ExplodedNodeSet &Dst); 312 313 /// VisitMSAsmStmt - Transfer function logic for MS inline asm. 314 void VisitMSAsmStmt(const MSAsmStmt *A, ExplodedNode *Pred, 315 ExplodedNodeSet &Dst); 316 317 /// VisitBlockExpr - Transfer function logic for BlockExprs. 318 void VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred, 319 ExplodedNodeSet &Dst); 320 321 /// VisitBinaryOperator - Transfer function logic for binary operators. 322 void VisitBinaryOperator(const BinaryOperator* B, ExplodedNode *Pred, 323 ExplodedNodeSet &Dst); 324 325 326 /// VisitCall - Transfer function for function calls. 327 void VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred, 328 ExplodedNodeSet &Dst); 329 330 /// VisitCast - Transfer function logic for all casts (implicit and explicit). 331 void VisitCast(const CastExpr *CastE, const Expr *Ex, ExplodedNode *Pred, 332 ExplodedNodeSet &Dst); 333 334 /// VisitCompoundLiteralExpr - Transfer function logic for compound literals. 335 void VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL, 336 ExplodedNode *Pred, ExplodedNodeSet &Dst); 337 338 /// Transfer function logic for DeclRefExprs and BlockDeclRefExprs. 339 void VisitCommonDeclRefExpr(const Expr *DR, const NamedDecl *D, 340 ExplodedNode *Pred, ExplodedNodeSet &Dst); 341 342 /// VisitDeclStmt - Transfer function logic for DeclStmts. 343 void VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred, 344 ExplodedNodeSet &Dst); 345 346 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose 347 void VisitGuardedExpr(const Expr *Ex, const Expr *L, const Expr *R, 348 ExplodedNode *Pred, ExplodedNodeSet &Dst); 349 350 void VisitInitListExpr(const InitListExpr *E, ExplodedNode *Pred, 351 ExplodedNodeSet &Dst); 352 353 /// VisitLogicalExpr - Transfer function logic for '&&', '||' 354 void VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred, 355 ExplodedNodeSet &Dst); 356 357 /// VisitMemberExpr - Transfer function for member expressions. 358 void VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred, 359 ExplodedNodeSet &Dst); 360 361 /// Transfer function logic for ObjCAtSynchronizedStmts. 362 void VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, 363 ExplodedNode *Pred, ExplodedNodeSet &Dst); 364 365 /// Transfer function logic for computing the lvalue of an Objective-C ivar. 366 void VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *DR, ExplodedNode *Pred, 367 ExplodedNodeSet &Dst); 368 369 /// VisitObjCForCollectionStmt - Transfer function logic for 370 /// ObjCForCollectionStmt. 371 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, 372 ExplodedNode *Pred, ExplodedNodeSet &Dst); 373 374 void VisitObjCMessage(const ObjCMessageExpr *ME, ExplodedNode *Pred, 375 ExplodedNodeSet &Dst); 376 377 /// VisitReturnStmt - Transfer function logic for return statements. 378 void VisitReturnStmt(const ReturnStmt *R, ExplodedNode *Pred, 379 ExplodedNodeSet &Dst); 380 381 /// VisitOffsetOfExpr - Transfer function for offsetof. 382 void VisitOffsetOfExpr(const OffsetOfExpr *Ex, ExplodedNode *Pred, 383 ExplodedNodeSet &Dst); 384 385 /// VisitUnaryExprOrTypeTraitExpr - Transfer function for sizeof. 386 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex, 387 ExplodedNode *Pred, ExplodedNodeSet &Dst); 388 389 /// VisitUnaryOperator - Transfer function logic for unary operators. 390 void VisitUnaryOperator(const UnaryOperator* B, ExplodedNode *Pred, 391 ExplodedNodeSet &Dst); 392 393 /// Handle ++ and -- (both pre- and post-increment). 394 void VisitIncrementDecrementOperator(const UnaryOperator* U, 395 ExplodedNode *Pred, 396 ExplodedNodeSet &Dst); 397 398 void VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred, 399 ExplodedNodeSet &Dst); 400 401 void VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 402 ExplodedNodeSet & Dst); 403 404 void VisitCXXConstructExpr(const CXXConstructExpr *E, ExplodedNode *Pred, 405 ExplodedNodeSet &Dst); 406 407 void VisitCXXDestructor(QualType ObjectType, const MemRegion *Dest, 408 const Stmt *S, bool IsBaseDtor, 409 ExplodedNode *Pred, ExplodedNodeSet &Dst); 410 411 void VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 412 ExplodedNodeSet &Dst); 413 414 void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, ExplodedNode *Pred, 415 ExplodedNodeSet &Dst); 416 417 /// Create a C++ temporary object for an rvalue. 418 void CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 419 ExplodedNode *Pred, 420 ExplodedNodeSet &Dst); 421 422 /// evalEagerlyAssumeBinOpBifurcation - Given the nodes in 'Src', eagerly assume symbolic 423 /// expressions of the form 'x != 0' and generate new nodes (stored in Dst) 424 /// with those assumptions. 425 void evalEagerlyAssumeBinOpBifurcation(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, 426 const Expr *Ex); 427 428 std::pair<const ProgramPointTag *, const ProgramPointTag*> 429 geteagerlyAssumeBinOpBifurcationTags(); 430 431 SVal evalMinus(SVal X) { 432 return X.isValid() ? svalBuilder.evalMinus(cast<NonLoc>(X)) : X; 433 } 434 435 SVal evalComplement(SVal X) { 436 return X.isValid() ? svalBuilder.evalComplement(cast<NonLoc>(X)) : X; 437 } 438 439public: 440 441 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 442 NonLoc L, NonLoc R, QualType T) { 443 return svalBuilder.evalBinOpNN(state, op, L, R, T); 444 } 445 446 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 447 NonLoc L, SVal R, QualType T) { 448 return R.isValid() ? svalBuilder.evalBinOpNN(state,op,L, cast<NonLoc>(R), T) : R; 449 } 450 451 SVal evalBinOp(ProgramStateRef ST, BinaryOperator::Opcode Op, 452 SVal LHS, SVal RHS, QualType T) { 453 return svalBuilder.evalBinOp(ST, Op, LHS, RHS, T); 454 } 455 456protected: 457 /// evalBind - Handle the semantics of binding a value to a specific location. 458 /// This method is used by evalStore, VisitDeclStmt, and others. 459 void evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE, ExplodedNode *Pred, 460 SVal location, SVal Val, bool atDeclInit = false, 461 const ProgramPoint *PP = 0); 462 463 /// Call PointerEscape callback when a value escapes as a result of bind. 464 ProgramStateRef processPointerEscapedOnBind(ProgramStateRef State, 465 SVal Loc, SVal Val); 466 /// Call PointerEscape callback when a value escapes as a result of 467 /// region invalidation. 468 ProgramStateRef processPointerEscapedOnInvalidateRegions( 469 ProgramStateRef State, 470 const InvalidatedSymbols *Invalidated, 471 ArrayRef<const MemRegion *> ExplicitRegions, 472 ArrayRef<const MemRegion *> Regions, 473 const CallEvent *Call); 474 475public: 476 // FIXME: 'tag' should be removed, and a LocationContext should be used 477 // instead. 478 // FIXME: Comment on the meaning of the arguments, when 'St' may not 479 // be the same as Pred->state, and when 'location' may not be the 480 // same as state->getLValue(Ex). 481 /// Simulate a read of the result of Ex. 482 void evalLoad(ExplodedNodeSet &Dst, 483 const Expr *NodeEx, /* Eventually will be a CFGStmt */ 484 const Expr *BoundExpr, 485 ExplodedNode *Pred, 486 ProgramStateRef St, 487 SVal location, 488 const ProgramPointTag *tag = 0, 489 QualType LoadTy = QualType()); 490 491 // FIXME: 'tag' should be removed, and a LocationContext should be used 492 // instead. 493 void evalStore(ExplodedNodeSet &Dst, const Expr *AssignE, const Expr *StoreE, 494 ExplodedNode *Pred, ProgramStateRef St, SVal TargetLV, SVal Val, 495 const ProgramPointTag *tag = 0); 496 497 /// \brief Create a new state in which the call return value is binded to the 498 /// call origin expression. 499 ProgramStateRef bindReturnValue(const CallEvent &Call, 500 const LocationContext *LCtx, 501 ProgramStateRef State); 502 503 /// Evaluate a call, running pre- and post-call checks and allowing checkers 504 /// to be responsible for handling the evaluation of the call itself. 505 void evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred, 506 const CallEvent &Call); 507 508 /// \brief Default implementation of call evaluation. 509 void defaultEvalCall(NodeBuilder &B, ExplodedNode *Pred, 510 const CallEvent &Call); 511private: 512 void evalLoadCommon(ExplodedNodeSet &Dst, 513 const Expr *NodeEx, /* Eventually will be a CFGStmt */ 514 const Expr *BoundEx, 515 ExplodedNode *Pred, 516 ProgramStateRef St, 517 SVal location, 518 const ProgramPointTag *tag, 519 QualType LoadTy); 520 521 // FIXME: 'tag' should be removed, and a LocationContext should be used 522 // instead. 523 void evalLocation(ExplodedNodeSet &Dst, 524 const Stmt *NodeEx, /* This will eventually be a CFGStmt */ 525 const Stmt *BoundEx, 526 ExplodedNode *Pred, 527 ProgramStateRef St, SVal location, 528 const ProgramPointTag *tag, bool isLoad); 529 530 /// Count the stack depth and determine if the call is recursive. 531 void examineStackFrames(const Decl *D, const LocationContext *LCtx, 532 bool &IsRecursive, unsigned &StackDepth); 533 534 bool shouldInlineDecl(const Decl *D, ExplodedNode *Pred); 535 bool inlineCall(const CallEvent &Call, const Decl *D, NodeBuilder &Bldr, 536 ExplodedNode *Pred, ProgramStateRef State); 537 538 /// \brief Conservatively evaluate call by invalidating regions and binding 539 /// a conjured return value. 540 void conservativeEvalCall(const CallEvent &Call, NodeBuilder &Bldr, 541 ExplodedNode *Pred, ProgramStateRef State); 542 543 /// \brief Either inline or process the call conservatively (or both), based 544 /// on DynamicDispatchBifurcation data. 545 void BifurcateCall(const MemRegion *BifurReg, 546 const CallEvent &Call, const Decl *D, NodeBuilder &Bldr, 547 ExplodedNode *Pred); 548 549 bool replayWithoutInlining(ExplodedNode *P, const LocationContext *CalleeLC); 550 551 /// Models a trivial copy or move constructor call with a simple bind. 552 void performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred, 553 const CXXConstructorCall &Call); 554}; 555 556/// Traits for storing the call processing policy inside GDM. 557/// The GDM stores the corresponding CallExpr pointer. 558// FIXME: This does not use the nice trait macros because it must be accessible 559// from multiple translation units. 560struct ReplayWithoutInlining{}; 561template <> 562struct ProgramStateTrait<ReplayWithoutInlining> : 563 public ProgramStatePartialTrait<const void*> { 564 static void *GDMIndex() { static int index = 0; return &index; } 565}; 566 567} // end ento namespace 568 569} // end clang namespace 570 571#endif 572