blob: acb007490ee19d986d60b095119617c27dab2e06 [file] [log] [blame]
Ted Kremenek5f764312011-08-20 05:59:58 +00001//===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
Zhongxing Xucb7464a2010-04-19 12:51:02 +00002//
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 Kyrtzidiseb48bd12011-03-01 01:16:03 +000014#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Jordy Rosed1e5a892011-09-02 08:02:59 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
Zhongxing Xucb7464a2010-04-19 12:51:02 +000018#include "clang/AST/DeclCXX.h"
19
20using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xucb7464a2010-04-19 12:51:02 +000022
Ted Kremenekc69c4382010-09-23 05:14:51 +000023namespace {
24class CallExprWLItem {
25public:
26 CallExpr::const_arg_iterator I;
27 ExplodedNode *N;
28
29 CallExprWLItem(const CallExpr::const_arg_iterator &i, ExplodedNode *n)
30 : I(i), N(n) {}
31};
32}
33
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000034void ExprEngine::evalArguments(ConstExprIterator AI, ConstExprIterator AE,
Zhongxing Xub17b1b32010-04-20 03:37:34 +000035 const FunctionProtoType *FnType,
Marcin Swiderski82c63bf2010-11-17 21:27:36 +000036 ExplodedNode *Pred, ExplodedNodeSet &Dst,
37 bool FstArgAsLValue) {
Ted Kremenekc69c4382010-09-23 05:14:51 +000038
39
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 SmallVector<CallExprWLItem, 20> WorkList;
Zhongxing Xub17b1b32010-04-20 03:37:34 +000041 WorkList.reserve(AE - AI);
42 WorkList.push_back(CallExprWLItem(AI, Pred));
43
44 while (!WorkList.empty()) {
45 CallExprWLItem Item = WorkList.back();
46 WorkList.pop_back();
47
48 if (Item.I == AE) {
49 Dst.insert(Item.N);
50 continue;
51 }
52
Ted Kremenekc69c4382010-09-23 05:14:51 +000053 // Evaluate the argument.
Zhongxing Xub17b1b32010-04-20 03:37:34 +000054 ExplodedNodeSet Tmp;
Marcin Swiderski82c63bf2010-11-17 21:27:36 +000055 if (FstArgAsLValue) {
56 FstArgAsLValue = false;
Marcin Swiderski82c63bf2010-11-17 21:27:36 +000057 }
Ted Kremenekc69c4382010-09-23 05:14:51 +000058
Ted Kremenek892697d2010-12-16 07:46:53 +000059 Visit(*Item.I, Item.N, Tmp);
Zhongxing Xub17b1b32010-04-20 03:37:34 +000060 ++(Item.I);
61 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI != NE; ++NI)
62 WorkList.push_back(CallExprWLItem(Item.I, *NI));
63 }
64}
65
Ted Kremenekb2771592011-03-30 17:41:19 +000066void ExprEngine::evalCallee(const CallExpr *callExpr,
67 const ExplodedNodeSet &src,
68 ExplodedNodeSet &dest) {
69
70 const Expr *callee = 0;
71
72 switch (callExpr->getStmtClass()) {
73 case Stmt::CXXMemberCallExprClass: {
74 // Evaluate the implicit object argument that is the recipient of the
75 // call.
76 callee = cast<CXXMemberCallExpr>(callExpr)->getImplicitObjectArgument();
77
78 // FIXME: handle member pointers.
79 if (!callee)
80 return;
81
82 break;
83 }
84 default: {
85 callee = callExpr->getCallee()->IgnoreParens();
86 break;
87 }
88 }
89
90 for (ExplodedNodeSet::iterator i = src.begin(), e = src.end(); i != e; ++i)
91 Visit(callee, *i, dest);
92}
93
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000094const CXXThisRegion *ExprEngine::getCXXThisRegion(const CXXRecordDecl *D,
Zhongxing Xucb7464a2010-04-19 12:51:02 +000095 const StackFrameContext *SFC) {
John McCallf4c73712011-01-19 06:33:43 +000096 const Type *T = D->getTypeForDecl();
John McCallb168cbf2010-11-16 09:18:38 +000097 QualType PT = getContext().getPointerType(QualType(T, 0));
Ted Kremenekc8413fd2010-12-02 07:49:45 +000098 return svalBuilder.getRegionManager().getCXXThisRegion(PT, SFC);
Zhongxing Xucb7464a2010-04-19 12:51:02 +000099}
100
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000101const CXXThisRegion *ExprEngine::getCXXThisRegion(const CXXMethodDecl *decl,
Zhongxing Xu32303022010-11-24 13:48:50 +0000102 const StackFrameContext *frameCtx) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000103 return svalBuilder.getRegionManager().
Zhongxing Xu32303022010-11-24 13:48:50 +0000104 getCXXThisRegion(decl->getThisType(getContext()), frameCtx);
105}
106
Ted Kremenekeea72a92011-07-28 23:07:36 +0000107void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
108 ExplodedNode *Pred,
109 ExplodedNodeSet &Dst) {
Ted Kremenek6a835dd2011-10-02 00:54:48 +0000110 const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
111 const ProgramState *state = Pred->getState();
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000112
Ted Kremenek6a835dd2011-10-02 00:54:48 +0000113 // Bind the temporary object to the value of the expression. Then bind
114 // the expression to the location of the object.
115 SVal V = state->getSVal(tempExpr);
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000116
Ted Kremenek6a835dd2011-10-02 00:54:48 +0000117 const MemRegion *R =
118 svalBuilder.getRegionManager().getCXXTempObjectRegion(ME,
119 Pred->getLocationContext());
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000120
Ted Kremenek6a835dd2011-10-02 00:54:48 +0000121 state = state->bindLoc(loc::MemRegionVal(R), V);
122 MakeNode(Dst, ME, Pred, state->BindExpr(ME, loc::MemRegionVal(R)));
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000123}
124
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000125void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *E,
Ted Kremenek5fe98722011-04-08 22:42:35 +0000126 const MemRegion *Dest,
127 ExplodedNode *Pred,
128 ExplodedNodeSet &destNodes) {
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000129
130 const CXXConstructorDecl *CD = E->getConstructor();
131 assert(CD);
Ted Kremenek5fe98722011-04-08 22:42:35 +0000132
133#if 0
Sean Hunt10620eb2011-05-06 20:44:56 +0000134 if (!(CD->doesThisDeclarationHaveABody() && AMgr.shouldInlineCall()))
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000135 // FIXME: invalidate the object.
136 return;
Ted Kremenek5fe98722011-04-08 22:42:35 +0000137#endif
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000138
139 // Evaluate other arguments.
Ted Kremenek9c149532010-12-01 21:57:22 +0000140 ExplodedNodeSet argsEvaluated;
Zhongxing Xu107ccd12010-04-20 05:40:40 +0000141 const FunctionProtoType *FnType = CD->getType()->getAs<FunctionProtoType>();
Ted Kremenek9c149532010-12-01 21:57:22 +0000142 evalArguments(E->arg_begin(), E->arg_end(), FnType, Pred, argsEvaluated);
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000143
Ted Kremenek5fe98722011-04-08 22:42:35 +0000144#if 0
145 // Is the constructor elidable?
146 if (E->isElidable()) {
147 VisitAggExpr(E->getArg(0), destNodes, Pred, Dst);
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000148 // FIXME: this is here to force propagation if VisitAggExpr doesn't
Ted Kremenek5fe98722011-04-08 22:42:35 +0000149 if (destNodes.empty())
150 destNodes.Add(Pred);
151 return;
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000152 }
Ted Kremenek5fe98722011-04-08 22:42:35 +0000153#endif
154
155 // Perform the previsit of the constructor.
156 ExplodedNodeSet destPreVisit;
157 getCheckerManager().runCheckersForPreStmt(destPreVisit, argsEvaluated, E,
158 *this);
159
160 // Evaluate the constructor. Currently we don't now allow checker-specific
161 // implementations of specific constructors (as we do with ordinary
162 // function calls. We can re-evaluate this in the future.
163
164#if 0
165 // Inlining currently isn't fully implemented.
166
167 if (AMgr.shouldInlineCall()) {
168 if (!Dest)
169 Dest =
170 svalBuilder.getRegionManager().getCXXTempObjectRegion(E,
171 Pred->getLocationContext());
172
173 // The callee stack frame context used to create the 'this'
174 // parameter region.
175 const StackFrameContext *SFC =
176 AMgr.getStackFrame(CD, Pred->getLocationContext(),
177 E, Builder->getBlock(), Builder->getIndex());
178
179 // Create the 'this' region.
180 const CXXThisRegion *ThisR =
181 getCXXThisRegion(E->getConstructor()->getParent(), SFC);
182
183 CallEnter Loc(E, SFC, Pred->getLocationContext());
184
185
186 for (ExplodedNodeSet::iterator NI = argsEvaluated.begin(),
187 NE = argsEvaluated.end(); NI != NE; ++NI) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000188 const ProgramState *state = (*NI)->getState();
Ted Kremenek5fe98722011-04-08 22:42:35 +0000189 // Setup 'this' region, so that the ctor is evaluated on the object pointed
190 // by 'Dest'.
191 state = state->bindLoc(loc::MemRegionVal(ThisR), loc::MemRegionVal(Dest));
192 if (ExplodedNode *N = Builder->generateNode(Loc, state, *NI))
193 destNodes.Add(N);
194 }
195 }
196#endif
197
198 // Default semantics: invalidate all regions passed as arguments.
Ted Kremenek5fe98722011-04-08 22:42:35 +0000199 ExplodedNodeSet destCall;
200
201 for (ExplodedNodeSet::iterator
202 i = destPreVisit.begin(), e = destPreVisit.end();
203 i != e; ++i)
204 {
205 ExplodedNode *Pred = *i;
Jordy Rosee38dd952011-08-28 05:16:28 +0000206 const LocationContext *LC = Pred->getLocationContext();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000207 const ProgramState *state = Pred->getState();
Ted Kremenek5fe98722011-04-08 22:42:35 +0000208
Jordy Rosee38dd952011-08-28 05:16:28 +0000209 state = invalidateArguments(state, CallOrObjCMessage(E, state), LC);
Ted Kremenek5fe98722011-04-08 22:42:35 +0000210 Builder->MakeNode(destCall, E, Pred, state);
211 }
212
213 // Do the post visit.
214 getCheckerManager().runCheckersForPostStmt(destNodes, destCall, E, *this);
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000215}
216
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000217void ExprEngine::VisitCXXDestructor(const CXXDestructorDecl *DD,
Zhongxing Xub13453b2010-11-20 06:53:12 +0000218 const MemRegion *Dest,
219 const Stmt *S,
220 ExplodedNode *Pred,
221 ExplodedNodeSet &Dst) {
Sean Hunt10620eb2011-05-06 20:44:56 +0000222 if (!(DD->doesThisDeclarationHaveABody() && AMgr.shouldInlineCall()))
Zhongxing Xub13453b2010-11-20 06:53:12 +0000223 return;
224 // Create the context for 'this' region.
225 const StackFrameContext *SFC = AMgr.getStackFrame(DD,
226 Pred->getLocationContext(),
Ted Kremenek892697d2010-12-16 07:46:53 +0000227 S, Builder->getBlock(),
Zhongxing Xub13453b2010-11-20 06:53:12 +0000228 Builder->getIndex());
229
230 const CXXThisRegion *ThisR = getCXXThisRegion(DD->getParent(), SFC);
231
Zhongxing Xu19b78d92010-11-24 08:53:20 +0000232 CallEnter PP(S, SFC, Pred->getLocationContext());
Zhongxing Xub13453b2010-11-20 06:53:12 +0000233
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000234 const ProgramState *state = Pred->getState();
Zhongxing Xub13453b2010-11-20 06:53:12 +0000235 state = state->bindLoc(loc::MemRegionVal(ThisR), loc::MemRegionVal(Dest));
236 ExplodedNode *N = Builder->generateNode(PP, state, Pred);
237 if (N)
238 Dst.Add(N);
239}
240
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000241void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000242 ExplodedNodeSet &Dst) {
Ted Kremenek41c5f492011-03-31 04:04:48 +0000243
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000244 unsigned blockCount = Builder->getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000245 DefinedOrUnknownSVal symVal =
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000246 svalBuilder.getConjuredSymbolVal(NULL, CNE, CNE->getType(), blockCount);
Ted Kremenek41c5f492011-03-31 04:04:48 +0000247 const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion();
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000248 QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000249 const ElementRegion *EleReg =
Ted Kremenek41c5f492011-03-31 04:04:48 +0000250 getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
251
252 if (CNE->isArray()) {
253 // FIXME: allocating an array requires simulating the constructors.
254 // For now, just return a symbolicated region.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000255 const ProgramState *state = Pred->getState();
Ted Kremenek41c5f492011-03-31 04:04:48 +0000256 state = state->BindExpr(CNE, loc::MemRegionVal(EleReg));
257 MakeNode(Dst, CNE, Pred, state);
258 return;
259 }
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000260
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000261 // Evaluate constructor arguments.
262 const FunctionProtoType *FnType = NULL;
263 const CXXConstructorDecl *CD = CNE->getConstructor();
264 if (CD)
265 FnType = CD->getType()->getAs<FunctionProtoType>();
Ted Kremenek9c149532010-12-01 21:57:22 +0000266 ExplodedNodeSet argsEvaluated;
267 evalArguments(CNE->constructor_arg_begin(), CNE->constructor_arg_end(),
268 FnType, Pred, argsEvaluated);
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000269
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000270 // Initialize the object region and bind the 'new' expression.
Ted Kremenek9c149532010-12-01 21:57:22 +0000271 for (ExplodedNodeSet::iterator I = argsEvaluated.begin(),
272 E = argsEvaluated.end(); I != E; ++I) {
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000273
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000274 const ProgramState *state = (*I)->getState();
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000275
276 // Accumulate list of regions that are invalidated.
277 // FIXME: Eventually we should unify the logic for constructor
278 // processing in one place.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 SmallVector<const MemRegion*, 10> regionsToInvalidate;
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000280 for (CXXNewExpr::const_arg_iterator
281 ai = CNE->constructor_arg_begin(), ae = CNE->constructor_arg_end();
282 ai != ae; ++ai)
283 {
284 SVal val = state->getSVal(*ai);
285 if (const MemRegion *region = val.getAsRegion())
286 regionsToInvalidate.push_back(region);
287 }
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000288
289 if (ObjTy->isRecordType()) {
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000290 regionsToInvalidate.push_back(EleReg);
291 // Invalidate the regions.
Jordy Rose537716a2011-08-27 22:51:26 +0000292 state = state->invalidateRegions(regionsToInvalidate,
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000293 CNE, blockCount, 0,
294 /* invalidateGlobals = */ true);
295
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000296 } else {
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000297 // Invalidate the regions.
Jordy Rose537716a2011-08-27 22:51:26 +0000298 state = state->invalidateRegions(regionsToInvalidate,
Ted Kremenek9d5d3082011-04-12 05:12:39 +0000299 CNE, blockCount, 0,
300 /* invalidateGlobals = */ true);
301
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000302 if (CNE->hasInitializer()) {
303 SVal V = state->getSVal(*CNE->constructor_arg_begin());
304 state = state->bindLoc(loc::MemRegionVal(EleReg), V);
305 } else {
306 // Explicitly set to undefined, because currently we retrieve symbolic
307 // value from symbolic region.
308 state = state->bindLoc(loc::MemRegionVal(EleReg), UndefinedVal());
309 }
310 }
311 state = state->BindExpr(CNE, loc::MemRegionVal(EleReg));
Zhongxing Xu978b9352010-04-21 02:20:10 +0000312 MakeNode(Dst, CNE, *I, state);
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000313 }
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000314}
315
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000316void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000317 ExplodedNode *Pred,ExplodedNodeSet &Dst) {
Zhongxing Xu6b851382010-04-21 02:17:31 +0000318 // Should do more checking.
Ted Kremenek9c149532010-12-01 21:57:22 +0000319 ExplodedNodeSet Argevaluated;
320 Visit(CDE->getArgument(), Pred, Argevaluated);
321 for (ExplodedNodeSet::iterator I = Argevaluated.begin(),
322 E = Argevaluated.end(); I != E; ++I) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000323 const ProgramState *state = (*I)->getState();
Zhongxing Xu6b851382010-04-21 02:17:31 +0000324 MakeNode(Dst, CDE, *I, state);
325 }
326}
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000327
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000328void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
Zhongxing Xu6b851382010-04-21 02:17:31 +0000329 ExplodedNodeSet &Dst) {
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000330 // Get the this object region from StoreManager.
331 const MemRegion *R =
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000332 svalBuilder.getRegionManager().getCXXThisRegion(
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000333 getContext().getCanonicalType(TE->getType()),
334 Pred->getLocationContext());
335
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000336 const ProgramState *state = Pred->getState();
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000337 SVal V = state->getSVal(loc::MemRegionVal(R));
338 MakeNode(Dst, TE, Pred, state->BindExpr(TE, V));
339}