blob: d9cd3de87e52da3e0ad156604472e4d70265e6ec [file] [log] [blame]
Zhongxing Xucb7464a2010-04-19 12:51:02 +00001//===- 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
14#include "clang/Checker/PathSensitive/AnalysisManager.h"
15#include "clang/Checker/PathSensitive/GRExprEngine.h"
16#include "clang/AST/DeclCXX.h"
17
18using namespace clang;
19
Zhongxing Xu03509ae2010-07-20 06:22:24 +000020void GRExprEngine::EvalArguments(ConstExprIterator AI, ConstExprIterator AE,
Zhongxing Xub17b1b32010-04-20 03:37:34 +000021 const FunctionProtoType *FnType,
22 ExplodedNode *Pred, ExplodedNodeSet &Dst) {
23 llvm::SmallVector<CallExprWLItem, 20> WorkList;
24 WorkList.reserve(AE - AI);
25 WorkList.push_back(CallExprWLItem(AI, Pred));
26
27 while (!WorkList.empty()) {
28 CallExprWLItem Item = WorkList.back();
29 WorkList.pop_back();
30
31 if (Item.I == AE) {
32 Dst.insert(Item.N);
33 continue;
34 }
35
36 ExplodedNodeSet Tmp;
37 const unsigned ParamIdx = Item.I - AI;
38 bool VisitAsLvalue = FnType? FnType->getArgType(ParamIdx)->isReferenceType()
39 : false;
40 if (VisitAsLvalue)
41 VisitLValue(*Item.I, Item.N, Tmp);
42 else
43 Visit(*Item.I, Item.N, Tmp);
44
45 ++(Item.I);
46 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI != NE; ++NI)
47 WorkList.push_back(CallExprWLItem(Item.I, *NI));
48 }
49}
50
Zhongxing Xucb7464a2010-04-19 12:51:02 +000051const CXXThisRegion *GRExprEngine::getCXXThisRegion(const CXXMethodDecl *D,
52 const StackFrameContext *SFC) {
53 Type *T = D->getParent()->getTypeForDecl();
54 QualType PT = getContext().getPointerType(QualType(T,0));
55 return ValMgr.getRegionManager().getCXXThisRegion(PT, SFC);
56}
57
Zhongxing Xu03509ae2010-07-20 06:22:24 +000058void GRExprEngine::CreateCXXTemporaryObject(const Expr *Ex, ExplodedNode *Pred,
Zhongxing Xucb7464a2010-04-19 12:51:02 +000059 ExplodedNodeSet &Dst) {
60 ExplodedNodeSet Tmp;
61 Visit(Ex, Pred, Tmp);
62 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
63 const GRState *state = GetState(*I);
64
65 // Bind the temporary object to the value of the expression. Then bind
66 // the expression to the location of the object.
67 SVal V = state->getSVal(Ex);
68
69 const MemRegion *R =
70 ValMgr.getRegionManager().getCXXObjectRegion(Ex,
71 Pred->getLocationContext());
72
73 state = state->bindLoc(loc::MemRegionVal(R), V);
74 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, loc::MemRegionVal(R)));
75 }
76}
77
78void GRExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *E, SVal Dest,
79 ExplodedNode *Pred,
80 ExplodedNodeSet &Dst) {
81 if (E->isElidable()) {
82 VisitAggExpr(E->getArg(0), Dest, Pred, Dst);
83 return;
84 }
85
86 const CXXConstructorDecl *CD = E->getConstructor();
87 assert(CD);
88
Zhongxing Xu7b99d122010-05-06 02:59:29 +000089 if (!(CD->isThisDeclarationADefinition() && AMgr.shouldInlineCall()))
Zhongxing Xucb7464a2010-04-19 12:51:02 +000090 // FIXME: invalidate the object.
91 return;
92
93
94 // Evaluate other arguments.
Zhongxing Xucb7464a2010-04-19 12:51:02 +000095 ExplodedNodeSet ArgsEvaluated;
Zhongxing Xu107ccd12010-04-20 05:40:40 +000096 const FunctionProtoType *FnType = CD->getType()->getAs<FunctionProtoType>();
Zhongxing Xu03509ae2010-07-20 06:22:24 +000097 EvalArguments(E->arg_begin(), E->arg_end(), FnType, Pred, ArgsEvaluated);
Zhongxing Xucb7464a2010-04-19 12:51:02 +000098 // The callee stack frame context used to create the 'this' parameter region.
99 const StackFrameContext *SFC = AMgr.getStackFrame(CD,
100 Pred->getLocationContext(),
101 E, Builder->getBlock(), Builder->getIndex());
102
103 const CXXThisRegion *ThisR = getCXXThisRegion(E->getConstructor(), SFC);
104
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000105 CallEnter Loc(E, SFC->getAnalysisContext(), Pred->getLocationContext());
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000106 for (ExplodedNodeSet::iterator NI = ArgsEvaluated.begin(),
107 NE = ArgsEvaluated.end(); NI != NE; ++NI) {
108 const GRState *state = GetState(*NI);
109 // Setup 'this' region.
110 state = state->bindLoc(loc::MemRegionVal(ThisR), Dest);
111 ExplodedNode *N = Builder->generateNode(Loc, state, Pred);
112 if (N)
113 Dst.Add(N);
114 }
115}
116
117void GRExprEngine::VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE,
118 ExplodedNode *Pred,
119 ExplodedNodeSet &Dst) {
120 // Get the method type.
121 const FunctionProtoType *FnType =
122 MCE->getCallee()->getType()->getAs<FunctionProtoType>();
123 assert(FnType && "Method type not available");
124
125 // Evaluate explicit arguments with a worklist.
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000126 ExplodedNodeSet ArgsEvaluated;
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000127 EvalArguments(MCE->arg_begin(), MCE->arg_end(), FnType, Pred, ArgsEvaluated);
Zhongxing Xu107ccd12010-04-20 05:40:40 +0000128
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000129 // Evaluate the implicit object argument.
130 ExplodedNodeSet AllArgsEvaluated;
131 const MemberExpr *ME = dyn_cast<MemberExpr>(MCE->getCallee()->IgnoreParens());
132 if (!ME)
133 return;
134 Expr *ObjArgExpr = ME->getBase();
135 for (ExplodedNodeSet::iterator I = ArgsEvaluated.begin(),
136 E = ArgsEvaluated.end(); I != E; ++I) {
137 if (ME->isArrow())
138 Visit(ObjArgExpr, *I, AllArgsEvaluated);
139 else
140 VisitLValue(ObjArgExpr, *I, AllArgsEvaluated);
141 }
142
143 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
144 assert(MD && "not a CXXMethodDecl?");
145
Zhongxing Xu7b99d122010-05-06 02:59:29 +0000146 if (!(MD->isThisDeclarationADefinition() && AMgr.shouldInlineCall()))
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000147 // FIXME: conservative method call evaluation.
148 return;
149
150 const StackFrameContext *SFC = AMgr.getStackFrame(MD,
151 Pred->getLocationContext(),
152 MCE,
153 Builder->getBlock(),
154 Builder->getIndex());
155 const CXXThisRegion *ThisR = getCXXThisRegion(MD, SFC);
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000156 CallEnter Loc(MCE, SFC->getAnalysisContext(), Pred->getLocationContext());
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000157 for (ExplodedNodeSet::iterator I = AllArgsEvaluated.begin(),
158 E = AllArgsEvaluated.end(); I != E; ++I) {
159 // Set up 'this' region.
160 const GRState *state = GetState(*I);
161 state = state->bindLoc(loc::MemRegionVal(ThisR),state->getSVal(ObjArgExpr));
162 ExplodedNode *N = Builder->generateNode(Loc, state, *I);
163 if (N)
164 Dst.Add(N);
165 }
166}
167
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000168void GRExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000169 ExplodedNodeSet &Dst) {
170 if (CNE->isArray()) {
171 // FIXME: allocating an array has not been handled.
172 return;
173 }
174
175 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000176 DefinedOrUnknownSVal SymVal = getValueManager().getConjuredSymbolVal(NULL,CNE,
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000177 CNE->getType(), Count);
178 const MemRegion *NewReg = cast<loc::MemRegionVal>(SymVal).getRegion();
179
180 QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
181
182 const ElementRegion *EleReg =
183 getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
184
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000185 // Evaluate constructor arguments.
186 const FunctionProtoType *FnType = NULL;
187 const CXXConstructorDecl *CD = CNE->getConstructor();
188 if (CD)
189 FnType = CD->getType()->getAs<FunctionProtoType>();
190 ExplodedNodeSet ArgsEvaluated;
191 EvalArguments(CNE->constructor_arg_begin(), CNE->constructor_arg_end(),
192 FnType, Pred, ArgsEvaluated);
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000193
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000194 // Initialize the object region and bind the 'new' expression.
195 for (ExplodedNodeSet::iterator I = ArgsEvaluated.begin(),
196 E = ArgsEvaluated.end(); I != E; ++I) {
197 const GRState *state = GetState(*I);
198
199 if (ObjTy->isRecordType()) {
200 Store store = state->getStore();
201 StoreManager::InvalidatedSymbols IS;
202 store = getStoreManager().InvalidateRegion(store, EleReg, CNE, Count, &IS);
203 state = state->makeWithStore(store);
204 } else {
205 if (CNE->hasInitializer()) {
206 SVal V = state->getSVal(*CNE->constructor_arg_begin());
207 state = state->bindLoc(loc::MemRegionVal(EleReg), V);
208 } else {
209 // Explicitly set to undefined, because currently we retrieve symbolic
210 // value from symbolic region.
211 state = state->bindLoc(loc::MemRegionVal(EleReg), UndefinedVal());
212 }
213 }
214 state = state->BindExpr(CNE, loc::MemRegionVal(EleReg));
Zhongxing Xu978b9352010-04-21 02:20:10 +0000215 MakeNode(Dst, CNE, *I, state);
Zhongxing Xub17b1b32010-04-20 03:37:34 +0000216 }
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000217}
218
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000219void GRExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
220 ExplodedNode *Pred,ExplodedNodeSet &Dst) {
Zhongxing Xu6b851382010-04-21 02:17:31 +0000221 // Should do more checking.
222 ExplodedNodeSet ArgEvaluated;
223 Visit(CDE->getArgument(), Pred, ArgEvaluated);
224 for (ExplodedNodeSet::iterator I = ArgEvaluated.begin(),
225 E = ArgEvaluated.end(); I != E; ++I) {
226 const GRState *state = GetState(*I);
227 MakeNode(Dst, CDE, *I, state);
228 }
229}
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000230
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000231void GRExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
Zhongxing Xu6b851382010-04-21 02:17:31 +0000232 ExplodedNodeSet &Dst) {
Zhongxing Xucb7464a2010-04-19 12:51:02 +0000233 // Get the this object region from StoreManager.
234 const MemRegion *R =
235 ValMgr.getRegionManager().getCXXThisRegion(
236 getContext().getCanonicalType(TE->getType()),
237 Pred->getLocationContext());
238
239 const GRState *state = GetState(Pred);
240 SVal V = state->getSVal(loc::MemRegionVal(R));
241 MakeNode(Dst, TE, Pred, state->BindExpr(TE, V));
242}