Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 1 | //===- GRCXXExprEngine.cpp - C++ expr evaluation engine ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the C++ expression evaluation engine. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | eb48bd1 | 2011-03-01 01:16:03 +0000 | [diff] [blame^] | 14 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 16 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
| 18 | |
| 19 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 20 | using namespace ento; |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | c69c438 | 2010-09-23 05:14:51 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | class CallExprWLItem { |
| 24 | public: |
| 25 | CallExpr::const_arg_iterator I; |
| 26 | ExplodedNode *N; |
| 27 | |
| 28 | CallExprWLItem(const CallExpr::const_arg_iterator &i, ExplodedNode *n) |
| 29 | : I(i), N(n) {} |
| 30 | }; |
| 31 | } |
| 32 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 33 | void ExprEngine::evalArguments(ConstExprIterator AI, ConstExprIterator AE, |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 34 | const FunctionProtoType *FnType, |
Marcin Swiderski | 82c63bf | 2010-11-17 21:27:36 +0000 | [diff] [blame] | 35 | ExplodedNode *Pred, ExplodedNodeSet &Dst, |
| 36 | bool FstArgAsLValue) { |
Ted Kremenek | c69c438 | 2010-09-23 05:14:51 +0000 | [diff] [blame] | 37 | |
| 38 | |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 39 | llvm::SmallVector<CallExprWLItem, 20> WorkList; |
| 40 | WorkList.reserve(AE - AI); |
| 41 | WorkList.push_back(CallExprWLItem(AI, Pred)); |
| 42 | |
| 43 | while (!WorkList.empty()) { |
| 44 | CallExprWLItem Item = WorkList.back(); |
| 45 | WorkList.pop_back(); |
| 46 | |
| 47 | if (Item.I == AE) { |
| 48 | Dst.insert(Item.N); |
| 49 | continue; |
| 50 | } |
| 51 | |
Ted Kremenek | c69c438 | 2010-09-23 05:14:51 +0000 | [diff] [blame] | 52 | // Evaluate the argument. |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 53 | ExplodedNodeSet Tmp; |
Marcin Swiderski | 82c63bf | 2010-11-17 21:27:36 +0000 | [diff] [blame] | 54 | if (FstArgAsLValue) { |
| 55 | FstArgAsLValue = false; |
Marcin Swiderski | 82c63bf | 2010-11-17 21:27:36 +0000 | [diff] [blame] | 56 | } |
Ted Kremenek | c69c438 | 2010-09-23 05:14:51 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 58 | Visit(*Item.I, Item.N, Tmp); |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 59 | ++(Item.I); |
| 60 | for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI != NE; ++NI) |
| 61 | WorkList.push_back(CallExprWLItem(Item.I, *NI)); |
| 62 | } |
| 63 | } |
| 64 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 65 | const CXXThisRegion *ExprEngine::getCXXThisRegion(const CXXRecordDecl *D, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 66 | const StackFrameContext *SFC) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 67 | const Type *T = D->getTypeForDecl(); |
John McCall | b168cbf | 2010-11-16 09:18:38 +0000 | [diff] [blame] | 68 | QualType PT = getContext().getPointerType(QualType(T, 0)); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 69 | return svalBuilder.getRegionManager().getCXXThisRegion(PT, SFC); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 72 | const CXXThisRegion *ExprEngine::getCXXThisRegion(const CXXMethodDecl *decl, |
Zhongxing Xu | 3230302 | 2010-11-24 13:48:50 +0000 | [diff] [blame] | 73 | const StackFrameContext *frameCtx) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 74 | return svalBuilder.getRegionManager(). |
Zhongxing Xu | 3230302 | 2010-11-24 13:48:50 +0000 | [diff] [blame] | 75 | getCXXThisRegion(decl->getThisType(getContext()), frameCtx); |
| 76 | } |
| 77 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 78 | void ExprEngine::CreateCXXTemporaryObject(const Expr *Ex, ExplodedNode *Pred, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 79 | ExplodedNodeSet &Dst) { |
| 80 | ExplodedNodeSet Tmp; |
| 81 | Visit(Ex, Pred, Tmp); |
| 82 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) { |
| 83 | const GRState *state = GetState(*I); |
| 84 | |
| 85 | // Bind the temporary object to the value of the expression. Then bind |
| 86 | // the expression to the location of the object. |
| 87 | SVal V = state->getSVal(Ex); |
| 88 | |
| 89 | const MemRegion *R = |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 90 | svalBuilder.getRegionManager().getCXXTempObjectRegion(Ex, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 91 | Pred->getLocationContext()); |
| 92 | |
| 93 | state = state->bindLoc(loc::MemRegionVal(R), V); |
| 94 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, loc::MemRegionVal(R))); |
| 95 | } |
| 96 | } |
| 97 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 98 | void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *E, |
Zhongxing Xu | 7ce351d | 2010-11-01 09:09:44 +0000 | [diff] [blame] | 99 | const MemRegion *Dest, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 100 | ExplodedNode *Pred, |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 101 | ExplodedNodeSet &Dst) { |
Zhongxing Xu | 7ce351d | 2010-11-01 09:09:44 +0000 | [diff] [blame] | 102 | if (!Dest) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 103 | Dest = svalBuilder.getRegionManager().getCXXTempObjectRegion(E, |
Zhongxing Xu | 7ce351d | 2010-11-01 09:09:44 +0000 | [diff] [blame] | 104 | Pred->getLocationContext()); |
| 105 | |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 106 | if (E->isElidable()) { |
| 107 | VisitAggExpr(E->getArg(0), Dest, Pred, Dst); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 112 | assert(CD); |
| 113 | |
Zhongxing Xu | 7b99d12 | 2010-05-06 02:59:29 +0000 | [diff] [blame] | 114 | if (!(CD->isThisDeclarationADefinition() && AMgr.shouldInlineCall())) |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 115 | // FIXME: invalidate the object. |
| 116 | return; |
| 117 | |
| 118 | |
| 119 | // Evaluate other arguments. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 120 | ExplodedNodeSet argsEvaluated; |
Zhongxing Xu | 107ccd1 | 2010-04-20 05:40:40 +0000 | [diff] [blame] | 121 | const FunctionProtoType *FnType = CD->getType()->getAs<FunctionProtoType>(); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 122 | evalArguments(E->arg_begin(), E->arg_end(), FnType, Pred, argsEvaluated); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 123 | // The callee stack frame context used to create the 'this' parameter region. |
| 124 | const StackFrameContext *SFC = AMgr.getStackFrame(CD, |
| 125 | Pred->getLocationContext(), |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 126 | E, Builder->getBlock(), |
| 127 | Builder->getIndex()); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 128 | |
Zhongxing Xu | 9dc84c9 | 2010-11-16 07:52:17 +0000 | [diff] [blame] | 129 | const CXXThisRegion *ThisR =getCXXThisRegion(E->getConstructor()->getParent(), |
| 130 | SFC); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 131 | |
Zhongxing Xu | 19b78d9 | 2010-11-24 08:53:20 +0000 | [diff] [blame] | 132 | CallEnter Loc(E, SFC, Pred->getLocationContext()); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 133 | for (ExplodedNodeSet::iterator NI = argsEvaluated.begin(), |
| 134 | NE = argsEvaluated.end(); NI != NE; ++NI) { |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 135 | const GRState *state = GetState(*NI); |
Zhongxing Xu | 3bf185b | 2010-08-24 04:26:55 +0000 | [diff] [blame] | 136 | // Setup 'this' region, so that the ctor is evaluated on the object pointed |
| 137 | // by 'Dest'. |
Zhongxing Xu | 7ce351d | 2010-11-01 09:09:44 +0000 | [diff] [blame] | 138 | state = state->bindLoc(loc::MemRegionVal(ThisR), loc::MemRegionVal(Dest)); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 139 | ExplodedNode *N = Builder->generateNode(Loc, state, Pred); |
| 140 | if (N) |
| 141 | Dst.Add(N); |
| 142 | } |
| 143 | } |
| 144 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 145 | void ExprEngine::VisitCXXDestructor(const CXXDestructorDecl *DD, |
Zhongxing Xu | b13453b | 2010-11-20 06:53:12 +0000 | [diff] [blame] | 146 | const MemRegion *Dest, |
| 147 | const Stmt *S, |
| 148 | ExplodedNode *Pred, |
| 149 | ExplodedNodeSet &Dst) { |
| 150 | if (!(DD->isThisDeclarationADefinition() && AMgr.shouldInlineCall())) |
| 151 | return; |
| 152 | // Create the context for 'this' region. |
| 153 | const StackFrameContext *SFC = AMgr.getStackFrame(DD, |
| 154 | Pred->getLocationContext(), |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 155 | S, Builder->getBlock(), |
Zhongxing Xu | b13453b | 2010-11-20 06:53:12 +0000 | [diff] [blame] | 156 | Builder->getIndex()); |
| 157 | |
| 158 | const CXXThisRegion *ThisR = getCXXThisRegion(DD->getParent(), SFC); |
| 159 | |
Zhongxing Xu | 19b78d9 | 2010-11-24 08:53:20 +0000 | [diff] [blame] | 160 | CallEnter PP(S, SFC, Pred->getLocationContext()); |
Zhongxing Xu | b13453b | 2010-11-20 06:53:12 +0000 | [diff] [blame] | 161 | |
| 162 | const GRState *state = Pred->getState(); |
| 163 | state = state->bindLoc(loc::MemRegionVal(ThisR), loc::MemRegionVal(Dest)); |
| 164 | ExplodedNode *N = Builder->generateNode(PP, state, Pred); |
| 165 | if (N) |
| 166 | Dst.Add(N); |
| 167 | } |
| 168 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 169 | void ExprEngine::VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 170 | ExplodedNode *Pred, |
| 171 | ExplodedNodeSet &Dst) { |
| 172 | // Get the method type. |
| 173 | const FunctionProtoType *FnType = |
| 174 | MCE->getCallee()->getType()->getAs<FunctionProtoType>(); |
| 175 | assert(FnType && "Method type not available"); |
| 176 | |
| 177 | // Evaluate explicit arguments with a worklist. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 178 | ExplodedNodeSet argsEvaluated; |
| 179 | evalArguments(MCE->arg_begin(), MCE->arg_end(), FnType, Pred, argsEvaluated); |
Zhongxing Xu | 107ccd1 | 2010-04-20 05:40:40 +0000 | [diff] [blame] | 180 | |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 181 | // Evaluate the implicit object argument. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 182 | ExplodedNodeSet AllargsEvaluated; |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 183 | const MemberExpr *ME = dyn_cast<MemberExpr>(MCE->getCallee()->IgnoreParens()); |
| 184 | if (!ME) |
| 185 | return; |
| 186 | Expr *ObjArgExpr = ME->getBase(); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 187 | for (ExplodedNodeSet::iterator I = argsEvaluated.begin(), |
| 188 | E = argsEvaluated.end(); I != E; ++I) { |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 189 | Visit(ObjArgExpr, *I, AllargsEvaluated); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ted Kremenek | a54e824 | 2010-09-30 01:06:29 +0000 | [diff] [blame] | 192 | // Now evaluate the call itself. |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 193 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 194 | assert(MD && "not a CXXMethodDecl?"); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 195 | evalMethodCall(MCE, MD, ObjArgExpr, Pred, AllargsEvaluated, Dst); |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 198 | void ExprEngine::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *C, |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 199 | ExplodedNode *Pred, |
| 200 | ExplodedNodeSet &Dst) { |
| 201 | const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(C->getCalleeDecl()); |
| 202 | if (!MD) { |
| 203 | // If the operator doesn't represent a method call treat as regural call. |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 204 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Determine the type of function we're calling (if available). |
| 209 | const FunctionProtoType *Proto = NULL; |
| 210 | QualType FnType = C->getCallee()->IgnoreParens()->getType(); |
| 211 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) |
| 212 | Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>(); |
| 213 | |
| 214 | // Evaluate arguments treating the first one (object method is called on) |
| 215 | // as alvalue. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 216 | ExplodedNodeSet argsEvaluated; |
| 217 | evalArguments(C->arg_begin(), C->arg_end(), Proto, Pred, argsEvaluated, true); |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 218 | |
| 219 | // Now evaluate the call itself. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 220 | evalMethodCall(C, MD, C->getArg(0), Pred, argsEvaluated, Dst); |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 223 | void ExprEngine::evalMethodCall(const CallExpr *MCE, const CXXMethodDecl *MD, |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 224 | const Expr *ThisExpr, ExplodedNode *Pred, |
| 225 | ExplodedNodeSet &Src, ExplodedNodeSet &Dst) { |
| 226 | // Allow checkers to pre-visit the member call. |
| 227 | ExplodedNodeSet PreVisitChecks; |
Argyrios Kyrtzidis | eb48bd1 | 2011-03-01 01:16:03 +0000 | [diff] [blame^] | 228 | getCheckerManager().runCheckersForPreStmt(PreVisitChecks, Src, MCE, *this); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | a54e824 | 2010-09-30 01:06:29 +0000 | [diff] [blame] | 230 | if (!(MD->isThisDeclarationADefinition() && AMgr.shouldInlineCall())) { |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 231 | // FIXME: conservative method call evaluation. |
Argyrios Kyrtzidis | eb48bd1 | 2011-03-01 01:16:03 +0000 | [diff] [blame^] | 232 | getCheckerManager().runCheckersForPostStmt(Dst, PreVisitChecks, MCE, *this); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 233 | return; |
Ted Kremenek | a54e824 | 2010-09-30 01:06:29 +0000 | [diff] [blame] | 234 | } |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 235 | |
| 236 | const StackFrameContext *SFC = AMgr.getStackFrame(MD, |
| 237 | Pred->getLocationContext(), |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 238 | MCE, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 239 | Builder->getBlock(), |
| 240 | Builder->getIndex()); |
Zhongxing Xu | 3230302 | 2010-11-24 13:48:50 +0000 | [diff] [blame] | 241 | const CXXThisRegion *ThisR = getCXXThisRegion(MD, SFC); |
Zhongxing Xu | 19b78d9 | 2010-11-24 08:53:20 +0000 | [diff] [blame] | 242 | CallEnter Loc(MCE, SFC, Pred->getLocationContext()); |
Ted Kremenek | a54e824 | 2010-09-30 01:06:29 +0000 | [diff] [blame] | 243 | for (ExplodedNodeSet::iterator I = PreVisitChecks.begin(), |
| 244 | E = PreVisitChecks.end(); I != E; ++I) { |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 245 | // Set up 'this' region. |
| 246 | const GRState *state = GetState(*I); |
Marcin Swiderski | 6a02b60 | 2010-11-18 06:29:23 +0000 | [diff] [blame] | 247 | state = state->bindLoc(loc::MemRegionVal(ThisR), state->getSVal(ThisExpr)); |
| 248 | Dst.Add(Builder->generateNode(Loc, state, *I)); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 252 | void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 253 | ExplodedNodeSet &Dst) { |
| 254 | if (CNE->isArray()) { |
| 255 | // FIXME: allocating an array has not been handled. |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | unsigned Count = Builder->getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 260 | DefinedOrUnknownSVal symVal = |
| 261 | svalBuilder.getConjuredSymbolVal(NULL, CNE, CNE->getType(), Count); |
| 262 | const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion(); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 263 | |
| 264 | QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); |
| 265 | |
| 266 | const ElementRegion *EleReg = |
| 267 | getStoreManager().GetElementZeroRegion(NewReg, ObjTy); |
| 268 | |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 269 | // Evaluate constructor arguments. |
| 270 | const FunctionProtoType *FnType = NULL; |
| 271 | const CXXConstructorDecl *CD = CNE->getConstructor(); |
| 272 | if (CD) |
| 273 | FnType = CD->getType()->getAs<FunctionProtoType>(); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 274 | ExplodedNodeSet argsEvaluated; |
| 275 | evalArguments(CNE->constructor_arg_begin(), CNE->constructor_arg_end(), |
| 276 | FnType, Pred, argsEvaluated); |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 277 | |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 278 | // Initialize the object region and bind the 'new' expression. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 279 | for (ExplodedNodeSet::iterator I = argsEvaluated.begin(), |
| 280 | E = argsEvaluated.end(); I != E; ++I) { |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 281 | const GRState *state = GetState(*I); |
| 282 | |
| 283 | if (ObjTy->isRecordType()) { |
Ted Kremenek | 2534528 | 2011-02-11 19:48:15 +0000 | [diff] [blame] | 284 | state = state->invalidateRegion(EleReg, CNE, Count); |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 285 | } else { |
| 286 | if (CNE->hasInitializer()) { |
| 287 | SVal V = state->getSVal(*CNE->constructor_arg_begin()); |
| 288 | state = state->bindLoc(loc::MemRegionVal(EleReg), V); |
| 289 | } else { |
| 290 | // Explicitly set to undefined, because currently we retrieve symbolic |
| 291 | // value from symbolic region. |
| 292 | state = state->bindLoc(loc::MemRegionVal(EleReg), UndefinedVal()); |
| 293 | } |
| 294 | } |
| 295 | state = state->BindExpr(CNE, loc::MemRegionVal(EleReg)); |
Zhongxing Xu | 978b935 | 2010-04-21 02:20:10 +0000 | [diff] [blame] | 296 | MakeNode(Dst, CNE, *I, state); |
Zhongxing Xu | b17b1b3 | 2010-04-20 03:37:34 +0000 | [diff] [blame] | 297 | } |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 300 | void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 301 | ExplodedNode *Pred,ExplodedNodeSet &Dst) { |
Zhongxing Xu | 6b85138 | 2010-04-21 02:17:31 +0000 | [diff] [blame] | 302 | // Should do more checking. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 303 | ExplodedNodeSet Argevaluated; |
| 304 | Visit(CDE->getArgument(), Pred, Argevaluated); |
| 305 | for (ExplodedNodeSet::iterator I = Argevaluated.begin(), |
| 306 | E = Argevaluated.end(); I != E; ++I) { |
Zhongxing Xu | 6b85138 | 2010-04-21 02:17:31 +0000 | [diff] [blame] | 307 | const GRState *state = GetState(*I); |
| 308 | MakeNode(Dst, CDE, *I, state); |
| 309 | } |
| 310 | } |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 311 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 312 | void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, |
Zhongxing Xu | 6b85138 | 2010-04-21 02:17:31 +0000 | [diff] [blame] | 313 | ExplodedNodeSet &Dst) { |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 314 | // Get the this object region from StoreManager. |
| 315 | const MemRegion *R = |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 316 | svalBuilder.getRegionManager().getCXXThisRegion( |
Zhongxing Xu | cb7464a | 2010-04-19 12:51:02 +0000 | [diff] [blame] | 317 | getContext().getCanonicalType(TE->getType()), |
| 318 | Pred->getLocationContext()); |
| 319 | |
| 320 | const GRState *state = GetState(Pred); |
| 321 | SVal V = state->getSVal(loc::MemRegionVal(R)); |
| 322 | MakeNode(Dst, TE, Pred, state->BindExpr(TE, V)); |
| 323 | } |