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/PathSensitive/ExprEngine.h"
1510e25031STed Kremenek #include "clang/AST/DeclCXX.h"
168fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h"
1717a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h"
183a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h"
193a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
203a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
2110e25031STed Kremenek 
2210e25031STed Kremenek using namespace clang;
2310e25031STed Kremenek using namespace ento;
2410e25031STed Kremenek 
2510e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
2610e25031STed Kremenek                                           ExplodedNode *Pred,
2710e25031STed Kremenek                                           ExplodedNodeSet &Dst) {
28d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
292a14c695STed Kremenek   const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
3049b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
31632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
3210e25031STed Kremenek 
33861a1740SJordan Rose   state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME);
345772f82dSJordan Rose   Bldr.generateNode(ME, Pred, state);
3510e25031STed Kremenek }
3610e25031STed Kremenek 
373d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core
383d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine.
3988bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
403d7b7f52SJordan Rose                                     const CallEvent &Call) {
413d7b7f52SJordan Rose   SVal ThisVal;
423d7b7f52SJordan Rose   bool AlwaysReturnsLValue;
433d7b7f52SJordan Rose   if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
443d7b7f52SJordan Rose     assert(Ctor->getDecl()->isTrivial());
453d7b7f52SJordan Rose     assert(Ctor->getDecl()->isCopyOrMoveConstructor());
463d7b7f52SJordan Rose     ThisVal = Ctor->getCXXThisVal();
473d7b7f52SJordan Rose     AlwaysReturnsLValue = false;
483d7b7f52SJordan Rose   } else {
493d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
503d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
513d7b7f52SJordan Rose            OO_Equal);
523d7b7f52SJordan Rose     ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
533d7b7f52SJordan Rose     AlwaysReturnsLValue = true;
543d7b7f52SJordan Rose   }
5588bb563cSJordan Rose 
5688bb563cSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
5788bb563cSJordan Rose 
5888bb563cSJordan Rose   ExplodedNodeSet Dst;
5988bb563cSJordan Rose   Bldr.takeNodes(Pred);
6088bb563cSJordan Rose 
6188bb563cSJordan Rose   SVal V = Call.getArgSVal(0);
6288bb563cSJordan Rose 
633d7b7f52SJordan Rose   // If the value being copied is not unknown, load from its location to get
643d7b7f52SJordan Rose   // an aggregate rvalue.
6505785d16SDavid Blaikie   if (Optional<Loc> L = V.getAs<Loc>())
6688bb563cSJordan Rose     V = Pred->getState()->getSVal(*L);
673d7b7f52SJordan Rose   else
683d7b7f52SJordan Rose     assert(V.isUnknown());
6988bb563cSJordan Rose 
703d7b7f52SJordan Rose   const Expr *CallExpr = Call.getOriginExpr();
713d7b7f52SJordan Rose   evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
7288bb563cSJordan Rose 
733d7b7f52SJordan Rose   PostStmt PS(CallExpr, LCtx);
7488bb563cSJordan Rose   for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
7588bb563cSJordan Rose        I != E; ++I) {
7688bb563cSJordan Rose     ProgramStateRef State = (*I)->getState();
773d7b7f52SJordan Rose     if (AlwaysReturnsLValue)
783d7b7f52SJordan Rose       State = State->BindExpr(CallExpr, LCtx, ThisVal);
793d7b7f52SJordan Rose     else
8088bb563cSJordan Rose       State = bindReturnValue(Call, LCtx, State);
8188bb563cSJordan Rose     Bldr.generateNode(PS, State, *I);
8288bb563cSJordan Rose   }
8388bb563cSJordan Rose }
8488bb563cSJordan Rose 
85bc74eb1cSJordan Rose 
86bc74eb1cSJordan Rose /// Returns a region representing the first element of a (possibly
87bc74eb1cSJordan Rose /// multi-dimensional) array.
88bc74eb1cSJordan Rose ///
89bc74eb1cSJordan Rose /// On return, \p Ty will be set to the base type of the array.
90bc74eb1cSJordan Rose ///
91bc74eb1cSJordan Rose /// If the type is not an array type at all, the original value is returned.
92bc74eb1cSJordan Rose static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue,
93bc74eb1cSJordan Rose                                   QualType &Ty) {
94bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
95bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
96bc74eb1cSJordan Rose 
97bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
98bc74eb1cSJordan Rose     Ty = AT->getElementType();
99bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
100bc74eb1cSJordan Rose   }
101bc74eb1cSJordan Rose 
102bc74eb1cSJordan Rose   return LValue;
103bc74eb1cSJordan Rose }
104bc74eb1cSJordan Rose 
105f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
10610e25031STed Kremenek                                        ExplodedNode *Pred,
10710e25031STed Kremenek                                        ExplodedNodeSet &destNodes) {
108b970505dSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
10905375eb4SJordan Rose   ProgramStateRef State = Pred->getState();
11005375eb4SJordan Rose 
111b970505dSJordan Rose   const MemRegion *Target = 0;
112bc74eb1cSJordan Rose 
113bc74eb1cSJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
114bc74eb1cSJordan Rose   // For now, we just run the first constructor (which should still invalidate
115bc74eb1cSJordan Rose   // the entire array).
116b970505dSJordan Rose 
117b970505dSJordan Rose   switch (CE->getConstructionKind()) {
118b970505dSJordan Rose   case CXXConstructExpr::CK_Complete: {
11905375eb4SJordan Rose     // See if we're constructing an existing region by looking at the next
12005375eb4SJordan Rose     // element in the CFG.
121d94854a4STed Kremenek     const CFGBlock *B = currBldrCtx->getBlock();
122d94854a4STed Kremenek     if (currStmtIdx + 1 < B->size()) {
123d94854a4STed Kremenek       CFGElement Next = (*B)[currStmtIdx+1];
12405375eb4SJordan Rose 
12505375eb4SJordan Rose       // Is this a constructor for a local variable?
12600be69abSDavid Blaikie       if (Optional<CFGStmt> StmtElem = Next.getAs<CFGStmt>()) {
12700be69abSDavid Blaikie         if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) {
12820edae87SJordan Rose           if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) {
12920edae87SJordan Rose             if (Var->getInit()->IgnoreImplicit() == CE) {
130bc74eb1cSJordan Rose               SVal LValue = State->getLValue(Var, LCtx);
13120edae87SJordan Rose               QualType Ty = Var->getType();
132bc74eb1cSJordan Rose               LValue = makeZeroElementRegion(State, LValue, Ty);
133bc74eb1cSJordan Rose               Target = LValue.getAsRegion();
13420edae87SJordan Rose             }
13520edae87SJordan Rose           }
13620edae87SJordan Rose         }
13720edae87SJordan Rose       }
13805375eb4SJordan Rose 
13954529a34SJordan Rose       // Is this a constructor for a member?
14000be69abSDavid Blaikie       if (Optional<CFGInitializer> InitElem = Next.getAs<CFGInitializer>()) {
14100be69abSDavid Blaikie         const CXXCtorInitializer *Init = InitElem->getInitializer();
14254529a34SJordan Rose         assert(Init->isAnyMemberInitializer());
14354529a34SJordan Rose 
14454529a34SJordan Rose         const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
14554529a34SJordan Rose         Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
14654529a34SJordan Rose                                                   LCtx->getCurrentStackFrame());
14754529a34SJordan Rose         SVal ThisVal = State->getSVal(ThisPtr);
14854529a34SJordan Rose 
149bc74eb1cSJordan Rose         const ValueDecl *Field;
150bc74eb1cSJordan Rose         SVal FieldVal;
15154529a34SJordan Rose         if (Init->isIndirectMemberInitializer()) {
152bc74eb1cSJordan Rose           Field = Init->getIndirectMember();
153bc74eb1cSJordan Rose           FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
15454529a34SJordan Rose         } else {
155bc74eb1cSJordan Rose           Field = Init->getMember();
156bc74eb1cSJordan Rose           FieldVal = State->getLValue(Init->getMember(), ThisVal);
15754529a34SJordan Rose         }
158bc74eb1cSJordan Rose 
159bc74eb1cSJordan Rose         QualType Ty = Field->getType();
160bc74eb1cSJordan Rose         FieldVal = makeZeroElementRegion(State, FieldVal, Ty);
161bc74eb1cSJordan Rose         Target = FieldVal.getAsRegion();
16254529a34SJordan Rose       }
16354529a34SJordan Rose 
16405375eb4SJordan Rose       // FIXME: This will eventually need to handle new-expressions as well.
165b8e28654SJordan Rose       // Don't forget to update the pre-constructor initialization code below.
16605375eb4SJordan Rose     }
16705375eb4SJordan Rose 
168c9318304SJordan Rose     // If we couldn't find an existing region to construct into, assume we're
169c9318304SJordan Rose     // constructing a temporary.
170c9318304SJordan Rose     if (!Target) {
171c9318304SJordan Rose       MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
172c9318304SJordan Rose       Target = MRMgr.getCXXTempObjectRegion(CE, LCtx);
173c9318304SJordan Rose     }
17405375eb4SJordan Rose 
175b970505dSJordan Rose     break;
176b970505dSJordan Rose   }
177b3b976f0SJordan Rose   case CXXConstructExpr::CK_VirtualBase:
178b3b976f0SJordan Rose     // Make sure we are not calling virtual base class initializers twice.
179b3b976f0SJordan Rose     // Only the most-derived object should initialize virtual base classes.
180b3b976f0SJordan Rose     if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) {
181b3b976f0SJordan Rose       const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer);
182b3b976f0SJordan Rose       if (OuterCtor) {
183b3b976f0SJordan Rose         switch (OuterCtor->getConstructionKind()) {
184b970505dSJordan Rose         case CXXConstructExpr::CK_NonVirtualBase:
185b970505dSJordan Rose         case CXXConstructExpr::CK_VirtualBase:
186b3b976f0SJordan Rose           // Bail out!
187b3b976f0SJordan Rose           destNodes.Add(Pred);
188b3b976f0SJordan Rose           return;
189b3b976f0SJordan Rose         case CXXConstructExpr::CK_Complete:
190b3b976f0SJordan Rose         case CXXConstructExpr::CK_Delegating:
191b3b976f0SJordan Rose           break;
192b3b976f0SJordan Rose         }
193b3b976f0SJordan Rose       }
194b3b976f0SJordan Rose     }
195b3b976f0SJordan Rose     // FALLTHROUGH
196b3b976f0SJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
197b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
198b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
199b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
200b970505dSJordan Rose                                               LCtx->getCurrentStackFrame());
20105375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
202b970505dSJordan Rose 
203b970505dSJordan Rose     if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
204b970505dSJordan Rose       Target = ThisVal.getAsRegion();
205b970505dSJordan Rose     } else {
206b970505dSJordan Rose       // Cast to the base type.
207599f85abSJordan Rose       bool IsVirtual =
208599f85abSJordan Rose         (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
209599f85abSJordan Rose       SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
210599f85abSJordan Rose                                                          IsVirtual);
21120edae87SJordan Rose       Target = BaseVal.getAsRegion();
212b970505dSJordan Rose     }
213b970505dSJordan Rose     break;
214b970505dSJordan Rose   }
215b970505dSJordan Rose   }
216b970505dSJordan Rose 
217fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
218fcd016e5SJordan Rose   CallEventRef<CXXConstructorCall> Call =
219fcd016e5SJordan Rose     CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
22010e25031STed Kremenek 
221f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
222f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
223b8e28654SJordan Rose 
224b8e28654SJordan Rose   ExplodedNodeSet PreInitialized;
225b8e28654SJordan Rose   {
226b8e28654SJordan Rose     StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
227b8e28654SJordan Rose     if (CE->requiresZeroInitialization()) {
228b8e28654SJordan Rose       // Type of the zero doesn't matter.
229b8e28654SJordan Rose       SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy);
230b8e28654SJordan Rose 
231b8e28654SJordan Rose       for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
232b8e28654SJordan Rose                                      E = DstPreVisit.end();
233b8e28654SJordan Rose            I != E; ++I) {
234b8e28654SJordan Rose         ProgramStateRef State = (*I)->getState();
235b8e28654SJordan Rose         // FIXME: Once we properly handle constructors in new-expressions, we'll
236b8e28654SJordan Rose         // need to invalidate the region before setting a default value, to make
237b8e28654SJordan Rose         // sure there aren't any lingering bindings around. This probably needs
238b8e28654SJordan Rose         // to happen regardless of whether or not the object is zero-initialized
239b8e28654SJordan Rose         // to handle random fields of a placement-initialized object picking up
240b8e28654SJordan Rose         // old bindings. We might only want to do it when we need to, though.
241b8e28654SJordan Rose         // FIXME: This isn't actually correct for arrays -- we need to zero-
242b8e28654SJordan Rose         // initialize the entire array, not just the first element -- but our
243b8e28654SJordan Rose         // handling of arrays everywhere else is weak as well, so this shouldn't
244b8e28654SJordan Rose         // actually make things worse. Placement new makes this tricky as well,
245b8e28654SJordan Rose         // since it's then possible to be initializing one part of a multi-
246b8e28654SJordan Rose         // dimensional array.
247b8e28654SJordan Rose         State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal);
248b8e28654SJordan Rose         Bldr.generateNode(CE, *I, State, /*tag=*/0, ProgramPoint::PreStmtKind);
249b8e28654SJordan Rose       }
250b8e28654SJordan Rose     }
251b8e28654SJordan Rose   }
252b8e28654SJordan Rose 
253afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
254b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
255fcd016e5SJordan Rose                                             *Call, *this);
25610e25031STed Kremenek 
25788bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
25888bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
25988bb563cSJordan Rose 
260e83cb092SJordan Rose   bool IsArray = isa<ElementRegion>(Target);
261e83cb092SJordan Rose   if (CE->getConstructor()->isTrivial() &&
262e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
263e83cb092SJordan Rose       !IsArray) {
264e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
265e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
26688bb563cSJordan Rose          I != E; ++I)
26788bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
26888bb563cSJordan Rose 
26988bb563cSJordan Rose   } else {
270afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
271f3c12ac3SJordan Rose          I != E; ++I)
272fcd016e5SJordan Rose       defaultEvalCall(Bldr, *I, *Call);
27388bb563cSJordan Rose   }
27410e25031STed Kremenek 
275afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
27688bb563cSJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
277fcd016e5SJordan Rose                                              *Call, *this);
278afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
27910e25031STed Kremenek }
28010e25031STed Kremenek 
281b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
28210e25031STed Kremenek                                     const MemRegion *Dest,
28310e25031STed Kremenek                                     const Stmt *S,
2842bc9674bSJordan Rose                                     bool IsBaseDtor,
28510e25031STed Kremenek                                     ExplodedNode *Pred,
28610e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
287fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
288fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
289fcd016e5SJordan Rose 
29020edae87SJordan Rose   // FIXME: We need to run the same destructor on every element of the array.
29120edae87SJordan Rose   // This workaround will just run the first destructor (which will still
29220edae87SJordan Rose   // invalidate the entire array).
293bc74eb1cSJordan Rose   SVal DestVal = loc::MemRegionVal(Dest);
294bc74eb1cSJordan Rose   DestVal = makeZeroElementRegion(State, DestVal, ObjectType);
295bc74eb1cSJordan Rose   Dest = DestVal.getAsRegion();
29620edae87SJordan Rose 
297b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
298b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
299b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
300b970505dSJordan Rose 
301fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
302fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
3032bc9674bSJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
304142adc49STed Kremenek 
30517a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
30617a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
30717a8757aSJordan Rose                                 "Error evaluating destructor");
30817a8757aSJordan Rose 
3094ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
3104ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
311fcd016e5SJordan Rose                                             *Call, *this);
31210e25031STed Kremenek 
3134ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
314d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
3154ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
3164ee71b8aSJordan Rose        I != E; ++I)
317fcd016e5SJordan Rose     defaultEvalCall(Bldr, *I, *Call);
3184ee71b8aSJordan Rose 
3194ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
3204ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
321fcd016e5SJordan Rose                                              *Call, *this);
32210e25031STed Kremenek }
32310e25031STed Kremenek 
32410e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
32510e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
326a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
327a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
328a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
329a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
33010e25031STed Kremenek 
331d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
332d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
3338b662704SAnton Yartsev   DefinedOrUnknownSVal symVal = UnknownVal();
3348b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
33510e25031STed Kremenek 
3368b662704SAnton Yartsev   bool IsStandardGlobalOpNewFunction = false;
3378b662704SAnton Yartsev   if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) {
3388b662704SAnton Yartsev     if (FD->getNumParams() == 2) {
3398b662704SAnton Yartsev       QualType T = FD->getParamDecl(1)->getType();
3408b662704SAnton Yartsev       if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
3418b662704SAnton Yartsev         // NoThrow placement new behaves as a standard new.
3428b662704SAnton Yartsev         IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t");
3438b662704SAnton Yartsev     }
3448b662704SAnton Yartsev     else
3458b662704SAnton Yartsev       // Placement forms are considered non-standard.
3468b662704SAnton Yartsev       IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1);
3478b662704SAnton Yartsev   }
3488b662704SAnton Yartsev 
3498b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
3508b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
3518b662704SAnton Yartsev   // a custom global allocator.
3528b662704SAnton Yartsev   if (IsStandardGlobalOpNewFunction)
3538b662704SAnton Yartsev     symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
3548b662704SAnton Yartsev   else
3558b662704SAnton Yartsev     symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, CNE->getType(),
3568b662704SAnton Yartsev                                           blockCount);
3578b662704SAnton Yartsev 
3588b662704SAnton Yartsev   ProgramStateRef State = Pred->getState();
359fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
360fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
361fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
362fcd016e5SJordan Rose 
363a4ee064cSJordan Rose   // Invalidate placement args.
364fcd016e5SJordan Rose   // FIXME: Once we figure out how we want allocators to work,
365fcd016e5SJordan Rose   // we should be using the usual pre-/(default-)eval-/post-call checks here.
366fcd016e5SJordan Rose   State = Call->invalidateRegions(blockCount);
367b8cb8359SJordan Rose   if (!State)
368b8cb8359SJordan Rose     return;
369a4ee064cSJordan Rose 
370*fce1b03eSPavel Labath   // If this allocation function is not declared as non-throwing, failures
371*fce1b03eSPavel Labath   // /must/ be signalled by exceptions, and thus the return value will never be
372*fce1b03eSPavel Labath   // NULL. -fno-exceptions does not influence this semantics.
373*fce1b03eSPavel Labath   // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
374*fce1b03eSPavel Labath   // where new can return NULL. If we end up supporting that option, we can
375*fce1b03eSPavel Labath   // consider adding a check for it here.
3763957fd58SJordan Rose   // C++11 [basic.stc.dynamic.allocation]p3.
377*fce1b03eSPavel Labath   if (FD) {
3783957fd58SJordan Rose     QualType Ty = FD->getType();
3793957fd58SJordan Rose     if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>())
3803957fd58SJordan Rose       if (!ProtoType->isNothrow(getContext()))
3813957fd58SJordan Rose         State = State->assume(symVal, true);
3823957fd58SJordan Rose   }
3833957fd58SJordan Rose 
384b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
385b8cb8359SJordan Rose 
38610e25031STed Kremenek   if (CNE->isArray()) {
38710e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
38810e25031STed Kremenek     // For now, just return a symbolicated region.
3892fdacbc5SDavid Blaikie     const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion();
390a4ee064cSJordan Rose     QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
391a4ee064cSJordan Rose     const ElementRegion *EleReg =
392a4ee064cSJordan Rose       getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
39344320367SJordan Rose     State = State->BindExpr(CNE, Pred->getLocationContext(),
394632e3b7eSTed Kremenek                             loc::MemRegionVal(EleReg));
39544320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
39610e25031STed Kremenek     return;
39710e25031STed Kremenek   }
39810e25031STed Kremenek 
399a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
400a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
401a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
402a4ee064cSJordan Rose   // the constructor call anyway.)
403b8cb8359SJordan Rose   SVal Result = symVal;
40444320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
40544320367SJordan Rose     // Non-array placement new should always return the placement location.
40644320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
407b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
408aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
40944320367SJordan Rose   }
41044320367SJordan Rose 
411b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
412b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
4138f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
4148f6b4b04SJordan Rose   if (!NewN)
4158f6b4b04SJordan Rose     return;
4163503cb35SJordan Rose 
4175089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
4185089f3b3SJordan Rose   // initializer. Copy the value over.
4195089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
4205089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
4213503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
4228f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
423b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
424b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
4253503cb35SJordan Rose     }
4263503cb35SJordan Rose   }
42710e25031STed Kremenek }
42810e25031STed Kremenek 
42910e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
43010e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
431d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
43264aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
43364aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
43410e25031STed Kremenek }
43510e25031STed Kremenek 
4368fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
4378fdb59f9STed Kremenek                                    ExplodedNode *Pred,
4388fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
4398fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
440b1279b5cSTed Kremenek   if (!VD) {
441b1279b5cSTed Kremenek     Dst.Add(Pred);
442b1279b5cSTed Kremenek     return;
443b1279b5cSTed Kremenek   }
444b1279b5cSTed Kremenek 
4458fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
446d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
447d94854a4STed Kremenek                                         currBldrCtx->blockCount());
4488fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
4498fdb59f9STed Kremenek   state = state->bindLoc(state->getLValue(VD, LCtx), V);
4508fdb59f9STed Kremenek 
451d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
4528fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
4538fdb59f9STed Kremenek }
4548fdb59f9STed Kremenek 
45510e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
45610e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
457d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
458e594034fSAnna Zaks 
45910e25031STed Kremenek   // Get the this object region from StoreManager.
460632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
46110e25031STed Kremenek   const MemRegion *R =
46210e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
46310e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
464632e3b7eSTed Kremenek                                                     LCtx);
46510e25031STed Kremenek 
46649b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
46710e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
468632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
46910e25031STed Kremenek }
470