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"
17*4f7df9beSJordan 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 
12805375eb4SJordan Rose   CXXConstructorCall Call(CE, Target, State, LCtx);
12910e25031STed Kremenek 
130f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
131f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
132afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
133afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit,
134afe7c2c2SJordan Rose                                             Call, *this);
13510e25031STed Kremenek 
136f3c12ac3SJordan Rose   ExplodedNodeSet DstInvalidated;
1373136cf9dSAnna Zaks   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext);
138afe7c2c2SJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
139f3c12ac3SJordan Rose        I != E; ++I)
1403136cf9dSAnna Zaks     defaultEvalCall(Bldr, *I, Call);
14110e25031STed Kremenek 
142afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
143afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstInvalidated,
144afe7c2c2SJordan Rose                                              Call, *this);
145afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
14610e25031STed Kremenek }
14710e25031STed Kremenek 
148b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
14910e25031STed Kremenek                                     const MemRegion *Dest,
15010e25031STed Kremenek                                     const Stmt *S,
15110e25031STed Kremenek                                     ExplodedNode *Pred,
15210e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
15320edae87SJordan Rose   // FIXME: We need to run the same destructor on every element of the array.
15420edae87SJordan Rose   // This workaround will just run the first destructor (which will still
15520edae87SJordan Rose   // invalidate the entire array).
15620edae87SJordan Rose   if (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) {
15720edae87SJordan Rose     ObjectType = AT->getElementType();
15820edae87SJordan Rose     Dest = Pred->getState()->getLValue(ObjectType,
15920edae87SJordan Rose                                        getSValBuilder().makeZeroArrayIndex(),
16020edae87SJordan Rose                                        loc::MemRegionVal(Dest)).getAsRegion();
16120edae87SJordan Rose   }
16220edae87SJordan Rose 
163b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
164b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
165b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
166b970505dSJordan Rose 
167b970505dSJordan Rose   CXXDestructorCall Call(DtorDecl, S, Dest, Pred->getState(),
1684ee71b8aSJordan Rose                          Pred->getLocationContext());
169142adc49STed Kremenek 
1704ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
1714ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
1724ee71b8aSJordan Rose                                             Call, *this);
17310e25031STed Kremenek 
1744ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
1753136cf9dSAnna Zaks   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext);
1764ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
1774ee71b8aSJordan Rose        I != E; ++I)
1783136cf9dSAnna Zaks     defaultEvalCall(Bldr, *I, Call);
1794ee71b8aSJordan Rose 
1804ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
1814ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
1824ee71b8aSJordan Rose                                              Call, *this);
18310e25031STed Kremenek }
18410e25031STed Kremenek 
18510e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
18610e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
187a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
188a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
189a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
190a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
191e4e5712cSAnna Zaks   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
19210e25031STed Kremenek 
193e594034fSAnna Zaks   unsigned blockCount = currentBuilderContext->getCurrentBlockCount();
194d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
19510e25031STed Kremenek   DefinedOrUnknownSVal symVal =
196a4ee064cSJordan Rose     svalBuilder.getConjuredSymbolVal(0, CNE, LCtx, CNE->getType(), blockCount);
19744320367SJordan Rose   ProgramStateRef State = Pred->getState();
19810e25031STed Kremenek 
199a4ee064cSJordan Rose   // Invalidate placement args.
200a4ee064cSJordan Rose   CXXAllocatorCall Call(CNE, State, LCtx);
201a4ee064cSJordan Rose   State = Call.invalidateRegions(blockCount);
202a4ee064cSJordan Rose 
20310e25031STed Kremenek   if (CNE->isArray()) {
20410e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
20510e25031STed Kremenek     // For now, just return a symbolicated region.
206a4ee064cSJordan Rose     const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion();
207a4ee064cSJordan Rose     QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
208a4ee064cSJordan Rose     const ElementRegion *EleReg =
209a4ee064cSJordan Rose       getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
21044320367SJordan Rose     State = State->BindExpr(CNE, Pred->getLocationContext(),
211632e3b7eSTed Kremenek                             loc::MemRegionVal(EleReg));
21244320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
21310e25031STed Kremenek     return;
21410e25031STed Kremenek   }
21510e25031STed Kremenek 
216a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
217a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
218a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
219a4ee064cSJordan Rose   // the constructor call anyway.)
22044320367SJordan Rose   FunctionDecl *FD = CNE->getOperatorNew();
22144320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
22244320367SJordan Rose     // Non-array placement new should always return the placement location.
22344320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
22444320367SJordan Rose     State = State->BindExpr(CNE, LCtx, PlacementLoc);
22544320367SJordan Rose   } else {
226a4ee064cSJordan Rose     State = State->BindExpr(CNE, LCtx, symVal);
22744320367SJordan Rose   }
22844320367SJordan Rose 
2295089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
2305089f3b3SJordan Rose   // initializer. Copy the value over.
2315089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
2325089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
2335089f3b3SJordan Rose       QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
2345089f3b3SJordan Rose       (void)ObjTy;
2355089f3b3SJordan Rose       assert(!ObjTy->isRecordType());
2365089f3b3SJordan Rose       SVal Location = State->getSVal(CNE, LCtx);
2375089f3b3SJordan Rose       if (isa<Loc>(Location))
2385089f3b3SJordan Rose         State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx));
2395089f3b3SJordan Rose     }
2405089f3b3SJordan Rose   }
2415089f3b3SJordan Rose 
24244320367SJordan Rose   Bldr.generateNode(CNE, Pred, State);
24310e25031STed Kremenek }
24410e25031STed Kremenek 
24510e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
24610e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
24764aea652SErik Verbruggen   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
24864aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
24964aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
25010e25031STed Kremenek }
25110e25031STed Kremenek 
2528fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
2538fdb59f9STed Kremenek                                    ExplodedNode *Pred,
2548fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
2558fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
256b1279b5cSTed Kremenek   if (!VD) {
257b1279b5cSTed Kremenek     Dst.Add(Pred);
258b1279b5cSTed Kremenek     return;
259b1279b5cSTed Kremenek   }
260b1279b5cSTed Kremenek 
2618fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
2628fdb59f9STed Kremenek   SVal V = svalBuilder.getConjuredSymbolVal(CS, LCtx, VD->getType(),
2638fdb59f9STed Kremenek                                  currentBuilderContext->getCurrentBlockCount());
2648fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
2658fdb59f9STed Kremenek   state = state->bindLoc(state->getLValue(VD, LCtx), V);
2668fdb59f9STed Kremenek 
2678fdb59f9STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
2688fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
2698fdb59f9STed Kremenek }
2708fdb59f9STed Kremenek 
27110e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
27210e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
273e4e5712cSAnna Zaks   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
274e594034fSAnna Zaks 
27510e25031STed Kremenek   // Get the this object region from StoreManager.
276632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
27710e25031STed Kremenek   const MemRegion *R =
27810e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
27910e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
280632e3b7eSTed Kremenek                                                     LCtx);
28110e25031STed Kremenek 
28249b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
28310e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
284632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
28510e25031STed Kremenek }
286