110e25031STed Kremenek //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
210e25031STed Kremenek //
310e25031STed Kremenek //                     The LLVM Compiler Infrastructure
410e25031STed Kremenek //
510e25031STed Kremenek // This file is distributed under the University of Illinois Open Source
610e25031STed Kremenek // License. See LICENSE.TXT for details.
710e25031STed Kremenek //
810e25031STed Kremenek //===----------------------------------------------------------------------===//
910e25031STed Kremenek //
1010e25031STed Kremenek //  This file defines the C++ expression evaluation engine.
1110e25031STed Kremenek //
1210e25031STed Kremenek //===----------------------------------------------------------------------===//
1310e25031STed Kremenek 
1410e25031STed Kremenek #include "clang/StaticAnalyzer/Core/CheckerManager.h"
1510e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
1610e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
174f7df9beSJordan Rose #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
1810e25031STed Kremenek #include "clang/AST/DeclCXX.h"
198fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h"
2010e25031STed Kremenek 
2110e25031STed Kremenek using namespace clang;
2210e25031STed Kremenek using namespace ento;
2310e25031STed Kremenek 
2410e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
2510e25031STed Kremenek                                           ExplodedNode *Pred,
2610e25031STed Kremenek                                           ExplodedNodeSet &Dst) {
27e4e5712cSAnna Zaks   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
282a14c695STed Kremenek   const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
2949b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
30632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
3110e25031STed Kremenek 
3210e25031STed Kremenek   // Bind the temporary object to the value of the expression. Then bind
3310e25031STed Kremenek   // the expression to the location of the object.
34632e3b7eSTed Kremenek   SVal V = state->getSVal(tempExpr, Pred->getLocationContext());
3510e25031STed Kremenek 
3610e25031STed Kremenek   const MemRegion *R =
37632e3b7eSTed Kremenek     svalBuilder.getRegionManager().getCXXTempObjectRegion(ME, LCtx);
3810e25031STed Kremenek 
3910e25031STed Kremenek   state = state->bindLoc(loc::MemRegionVal(R), V);
40632e3b7eSTed Kremenek   Bldr.generateNode(ME, Pred, state->BindExpr(ME, LCtx, loc::MemRegionVal(R)));
4110e25031STed Kremenek }
4210e25031STed Kremenek 
43f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
4410e25031STed Kremenek                                        ExplodedNode *Pred,
4510e25031STed Kremenek                                        ExplodedNodeSet &destNodes) {
46b970505dSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
4705375eb4SJordan Rose   ProgramStateRef State = Pred->getState();
4805375eb4SJordan Rose 
49b970505dSJordan Rose   const MemRegion *Target = 0;
50b970505dSJordan Rose 
51b970505dSJordan Rose   switch (CE->getConstructionKind()) {
52b970505dSJordan Rose   case CXXConstructExpr::CK_Complete: {
5305375eb4SJordan Rose     // See if we're constructing an existing region by looking at the next
5405375eb4SJordan Rose     // element in the CFG.
5505375eb4SJordan Rose     const CFGBlock *B = currentBuilderContext->getBlock();
5605375eb4SJordan Rose     if (currentStmtIdx + 1 < B->size()) {
5705375eb4SJordan Rose       CFGElement Next = (*B)[currentStmtIdx+1];
5805375eb4SJordan Rose 
5905375eb4SJordan Rose       // Is this a constructor for a local variable?
6020edae87SJordan Rose       if (const CFGStmt *StmtElem = dyn_cast<CFGStmt>(&Next)) {
6120edae87SJordan Rose         if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) {
6220edae87SJordan Rose           if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) {
6320edae87SJordan Rose             if (Var->getInit()->IgnoreImplicit() == CE) {
6420edae87SJordan Rose               QualType Ty = Var->getType();
6520edae87SJordan Rose               if (const ArrayType *AT = getContext().getAsArrayType(Ty)) {
6620edae87SJordan Rose                 // FIXME: Handle arrays, which run the same constructor for
6720edae87SJordan Rose                 // every element. This workaround will just run the first
6820edae87SJordan Rose                 // constructor (which should still invalidate the entire array).
6920edae87SJordan Rose                 SVal Base = State->getLValue(Var, LCtx);
7020edae87SJordan Rose                 Target = State->getLValue(AT->getElementType(),
7120edae87SJordan Rose                                           getSValBuilder().makeZeroArrayIndex(),
7220edae87SJordan Rose                                           Base).getAsRegion();
7320edae87SJordan Rose               } else {
7405375eb4SJordan Rose                 Target = State->getLValue(Var, LCtx).getAsRegion();
7520edae87SJordan Rose               }
7620edae87SJordan Rose             }
7720edae87SJordan Rose           }
7820edae87SJordan Rose         }
7920edae87SJordan Rose       }
8005375eb4SJordan Rose 
8154529a34SJordan Rose       // Is this a constructor for a member?
8254529a34SJordan Rose       if (const CFGInitializer *InitElem = dyn_cast<CFGInitializer>(&Next)) {
8354529a34SJordan Rose         const CXXCtorInitializer *Init = InitElem->getInitializer();
8454529a34SJordan Rose         assert(Init->isAnyMemberInitializer());
8554529a34SJordan Rose 
8654529a34SJordan Rose         const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
8754529a34SJordan Rose         Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
8854529a34SJordan Rose                                                   LCtx->getCurrentStackFrame());
8954529a34SJordan Rose         SVal ThisVal = State->getSVal(ThisPtr);
9054529a34SJordan Rose 
9154529a34SJordan Rose         if (Init->isIndirectMemberInitializer()) {
9254529a34SJordan Rose           SVal Field = State->getLValue(Init->getIndirectMember(), ThisVal);
9320edae87SJordan Rose           Target = Field.getAsRegion();
9454529a34SJordan Rose         } else {
9554529a34SJordan Rose           SVal Field = State->getLValue(Init->getMember(), ThisVal);
9620edae87SJordan Rose           Target = Field.getAsRegion();
9754529a34SJordan Rose         }
9854529a34SJordan Rose       }
9954529a34SJordan Rose 
10005375eb4SJordan Rose       // FIXME: This will eventually need to handle new-expressions as well.
10105375eb4SJordan Rose     }
10205375eb4SJordan Rose 
10305375eb4SJordan Rose     // If we couldn't find an existing region to construct into, we'll just
104b970505dSJordan Rose     // generate a symbolic region, which is fine.
10505375eb4SJordan Rose 
106b970505dSJordan Rose     break;
107b970505dSJordan Rose   }
108b970505dSJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
109b970505dSJordan Rose   case CXXConstructExpr::CK_VirtualBase:
110b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
111b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
112b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
113b970505dSJordan Rose                                               LCtx->getCurrentStackFrame());
11405375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
115b970505dSJordan Rose 
116b970505dSJordan Rose     if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
117b970505dSJordan Rose       Target = ThisVal.getAsRegion();
118b970505dSJordan Rose     } else {
119b970505dSJordan Rose       // Cast to the base type.
120b970505dSJordan Rose       QualType BaseTy = CE->getType();
121b970505dSJordan Rose       SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy);
12220edae87SJordan Rose       Target = BaseVal.getAsRegion();
123b970505dSJordan Rose     }
124b970505dSJordan Rose     break;
125b970505dSJordan Rose   }
126b970505dSJordan Rose   }
127b970505dSJordan Rose 
128*fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
129*fcd016e5SJordan Rose   CallEventRef<CXXConstructorCall> Call =
130*fcd016e5SJordan Rose     CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
13110e25031STed Kremenek 
132f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
133f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
134afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
135afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit,
136*fcd016e5SJordan Rose                                             *Call, *this);
13710e25031STed Kremenek 
138f3c12ac3SJordan Rose   ExplodedNodeSet DstInvalidated;
1393136cf9dSAnna Zaks   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext);
140afe7c2c2SJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
141f3c12ac3SJordan Rose        I != E; ++I)
142*fcd016e5SJordan Rose     defaultEvalCall(Bldr, *I, *Call);
14310e25031STed Kremenek 
144afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
145afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstInvalidated,
146*fcd016e5SJordan Rose                                              *Call, *this);
147afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
14810e25031STed Kremenek }
14910e25031STed Kremenek 
150b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
15110e25031STed Kremenek                                     const MemRegion *Dest,
15210e25031STed Kremenek                                     const Stmt *S,
15310e25031STed Kremenek                                     ExplodedNode *Pred,
15410e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
155*fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
156*fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
157*fcd016e5SJordan Rose 
15820edae87SJordan Rose   // FIXME: We need to run the same destructor on every element of the array.
15920edae87SJordan Rose   // This workaround will just run the first destructor (which will still
16020edae87SJordan Rose   // invalidate the entire array).
16120edae87SJordan Rose   if (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) {
16220edae87SJordan Rose     ObjectType = AT->getElementType();
163*fcd016e5SJordan Rose     Dest = State->getLValue(ObjectType, getSValBuilder().makeZeroArrayIndex(),
16420edae87SJordan Rose                             loc::MemRegionVal(Dest)).getAsRegion();
16520edae87SJordan Rose   }
16620edae87SJordan Rose 
167b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
168b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
169b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
170b970505dSJordan Rose 
171*fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
172*fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
173*fcd016e5SJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, State, LCtx);
174142adc49STed Kremenek 
1754ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
1764ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
177*fcd016e5SJordan Rose                                             *Call, *this);
17810e25031STed Kremenek 
1794ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
1803136cf9dSAnna Zaks   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext);
1814ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
1824ee71b8aSJordan Rose        I != E; ++I)
183*fcd016e5SJordan Rose     defaultEvalCall(Bldr, *I, *Call);
1844ee71b8aSJordan Rose 
1854ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
1864ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
187*fcd016e5SJordan Rose                                              *Call, *this);
18810e25031STed Kremenek }
18910e25031STed Kremenek 
19010e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
19110e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
192a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
193a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
194a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
195a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
196e4e5712cSAnna Zaks   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
19710e25031STed Kremenek 
198e594034fSAnna Zaks   unsigned blockCount = currentBuilderContext->getCurrentBlockCount();
199d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
20010e25031STed Kremenek   DefinedOrUnknownSVal symVal =
201a4ee064cSJordan Rose     svalBuilder.getConjuredSymbolVal(0, CNE, LCtx, CNE->getType(), blockCount);
20244320367SJordan Rose   ProgramStateRef State = Pred->getState();
20310e25031STed Kremenek 
204*fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
205*fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
206*fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
207*fcd016e5SJordan Rose 
208a4ee064cSJordan Rose   // Invalidate placement args.
209*fcd016e5SJordan Rose   // FIXME: Once we figure out how we want allocators to work,
210*fcd016e5SJordan Rose   // we should be using the usual pre-/(default-)eval-/post-call checks here.
211*fcd016e5SJordan Rose   State = Call->invalidateRegions(blockCount);
212a4ee064cSJordan Rose 
21310e25031STed Kremenek   if (CNE->isArray()) {
21410e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
21510e25031STed Kremenek     // For now, just return a symbolicated region.
216a4ee064cSJordan Rose     const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion();
217a4ee064cSJordan Rose     QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
218a4ee064cSJordan Rose     const ElementRegion *EleReg =
219a4ee064cSJordan Rose       getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
22044320367SJordan Rose     State = State->BindExpr(CNE, Pred->getLocationContext(),
221632e3b7eSTed Kremenek                             loc::MemRegionVal(EleReg));
22244320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
22310e25031STed Kremenek     return;
22410e25031STed Kremenek   }
22510e25031STed Kremenek 
226a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
227a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
228a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
229a4ee064cSJordan Rose   // the constructor call anyway.)
23044320367SJordan Rose   FunctionDecl *FD = CNE->getOperatorNew();
23144320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
23244320367SJordan Rose     // Non-array placement new should always return the placement location.
23344320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
23444320367SJordan Rose     State = State->BindExpr(CNE, LCtx, PlacementLoc);
23544320367SJordan Rose   } else {
236a4ee064cSJordan Rose     State = State->BindExpr(CNE, LCtx, symVal);
23744320367SJordan Rose   }
23844320367SJordan Rose 
2395089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
2405089f3b3SJordan Rose   // initializer. Copy the value over.
2415089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
2425089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
2435089f3b3SJordan Rose       QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
2445089f3b3SJordan Rose       (void)ObjTy;
2455089f3b3SJordan Rose       assert(!ObjTy->isRecordType());
2465089f3b3SJordan Rose       SVal Location = State->getSVal(CNE, LCtx);
2475089f3b3SJordan Rose       if (isa<Loc>(Location))
2485089f3b3SJordan Rose         State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx));
2495089f3b3SJordan Rose     }
2505089f3b3SJordan Rose   }
2515089f3b3SJordan Rose 
25244320367SJordan Rose   Bldr.generateNode(CNE, Pred, State);
25310e25031STed Kremenek }
25410e25031STed Kremenek 
25510e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
25610e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
25764aea652SErik Verbruggen   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
25864aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
25964aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
26010e25031STed Kremenek }
26110e25031STed Kremenek 
2628fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
2638fdb59f9STed Kremenek                                    ExplodedNode *Pred,
2648fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
2658fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
266b1279b5cSTed Kremenek   if (!VD) {
267b1279b5cSTed Kremenek     Dst.Add(Pred);
268b1279b5cSTed Kremenek     return;
269b1279b5cSTed Kremenek   }
270b1279b5cSTed Kremenek 
2718fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
2728fdb59f9STed Kremenek   SVal V = svalBuilder.getConjuredSymbolVal(CS, LCtx, VD->getType(),
2738fdb59f9STed Kremenek                                  currentBuilderContext->getCurrentBlockCount());
2748fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
2758fdb59f9STed Kremenek   state = state->bindLoc(state->getLValue(VD, LCtx), V);
2768fdb59f9STed Kremenek 
2778fdb59f9STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
2788fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
2798fdb59f9STed Kremenek }
2808fdb59f9STed Kremenek 
28110e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
28210e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
283e4e5712cSAnna Zaks   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
284e594034fSAnna Zaks 
28510e25031STed Kremenek   // Get the this object region from StoreManager.
286632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
28710e25031STed Kremenek   const MemRegion *R =
28810e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
28910e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
290632e3b7eSTed Kremenek                                                     LCtx);
29110e25031STed Kremenek 
29249b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
29310e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
294632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
29510e25031STed Kremenek }
296