blob: cc39618095f07c79e580c6f1194a6959509ee3a6 [file] [log] [blame]
Ted Kremenek64de2072008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremeneka0be8262008-01-31 02:35:41 +00002//
Ted Kremenek6f4a9ef2008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekde8d62b2008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek64de2072008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekde8d62b2008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Zhongxing Xuc6123a12009-11-24 08:24:26 +000016#include "GRExprEngineInternalChecks.h"
Ted Kremenek64de2072008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke68c0fc2009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek49513cc2009-07-22 21:43:51 +000019#include "clang/Analysis/PathSensitive/Checker.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000020#include "clang/AST/ParentMap.h"
21#include "clang/AST/StmtObjC.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek6947a792008-03-07 20:57:30 +000024#include "clang/Basic/SourceManager.h"
Ted Kremenek91076ca2009-03-11 02:41:36 +000025#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000027#include "llvm/ADT/ImmutableList.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000028#include "llvm/ADT/StringSwitch.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000029
Ted Kremenekc0258412008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
Ted Kremenekc0258412008-02-27 06:07:00 +000032#endif
33
Ted Kremenekbd8957b2008-02-14 22:16:04 +000034using namespace clang;
35using llvm::dyn_cast;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000036using llvm::dyn_cast_or_null;
Ted Kremenekbd8957b2008-02-14 22:16:04 +000037using llvm::cast;
38using llvm::APSInt;
Ted Kremenek0a8d3762008-01-23 19:59:44 +000039
Ted Kremenek667cacb2008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
Ted Kremenekd0fe8042009-11-25 21:51:20 +000041// Utility functions.
42//===----------------------------------------------------------------------===//
43
44static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
45 IdentifierInfo* II = &Ctx.Idents.get(name);
46 return Ctx.Selectors.getSelector(0, &II);
47}
48
Ted Kremeneke19711d2009-12-22 22:13:46 +000049
Ted Kremenekaf1bdd72009-12-18 20:13:39 +000050static bool CalleeReturnsReference(const CallExpr *CE) {
51 const Expr *Callee = CE->getCallee();
52 QualType T = Callee->getType();
53
Ted Kremenekaf1bdd72009-12-18 20:13:39 +000054 if (const PointerType *PT = T->getAs<PointerType>()) {
55 const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>();
56 T = FT->getResultType();
57 }
58 else {
59 const BlockPointerType *BT = T->getAs<BlockPointerType>();
60 T = BT->getPointeeType()->getAs<FunctionType>()->getResultType();
Ted Kremeneke19711d2009-12-22 22:13:46 +000061 }
Ted Kremenekaf1bdd72009-12-18 20:13:39 +000062 return T->isReferenceType();
63}
64
Ted Kremeneke19711d2009-12-22 22:13:46 +000065static bool ReceiverReturnsReference(const ObjCMessageExpr *ME) {
66 const ObjCMethodDecl *MD = ME->getMethodDecl();
67 if (!MD)
68 return false;
69 return MD->getResultType()->isReferenceType();
70}
71
Ted Kremenekd0fe8042009-11-25 21:51:20 +000072//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +000073// Batch auditor. DEPRECATED.
Ted Kremenek667cacb2008-04-15 23:06:53 +000074//===----------------------------------------------------------------------===//
75
Ted Kremenekc50e1a12008-07-11 18:37:32 +000076namespace {
77
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000078class MappedBatchAuditor : public GRSimpleAPICheck {
Ted Kremenekc50e1a12008-07-11 18:37:32 +000079 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
80 typedef llvm::DenseMap<void*,Checks> MapTy;
Mike Stump11289f42009-09-09 15:08:12 +000081
Ted Kremenekc50e1a12008-07-11 18:37:32 +000082 MapTy M;
83 Checks::Factory F;
Ted Kremenek4967c892009-03-30 17:53:05 +000084 Checks AllStmts;
Ted Kremenekc50e1a12008-07-11 18:37:32 +000085
86public:
Ted Kremenek4967c892009-03-30 17:53:05 +000087 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
88 F(Alloc), AllStmts(F.GetEmptyList()) {}
Mike Stump11289f42009-09-09 15:08:12 +000089
Ted Kremenekc50e1a12008-07-11 18:37:32 +000090 virtual ~MappedBatchAuditor() {
91 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
Mike Stump11289f42009-09-09 15:08:12 +000092
Ted Kremenekc50e1a12008-07-11 18:37:32 +000093 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
94 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
95
96 GRSimpleAPICheck* check = *I;
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremenekc50e1a12008-07-11 18:37:32 +000098 if (AlreadyVisited.count(check))
99 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000100
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000101 AlreadyVisited.insert(check);
102 delete check;
103 }
104 }
105
Ted Kremenek4967c892009-03-30 17:53:05 +0000106 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000107 assert (A && "Check cannot be null.");
108 void* key = reinterpret_cast<void*>((uintptr_t) C);
109 MapTy::iterator I = M.find(key);
110 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek4967c892009-03-30 17:53:05 +0000113 void AddCheck(GRSimpleAPICheck *A) {
114 assert (A && "Check cannot be null.");
Mike Stump11289f42009-09-09 15:08:12 +0000115 AllStmts = F.Concat(A, AllStmts);
Ted Kremenek4967c892009-03-30 17:53:05 +0000116 }
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000117
Zhongxing Xu107f7592009-08-06 12:48:26 +0000118 virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) {
Ted Kremenek4967c892009-03-30 17:53:05 +0000119 // First handle the auditors that accept all statements.
120 bool isSink = false;
121 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
122 isSink |= (*I)->Audit(N, VMgr);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremenek4967c892009-03-30 17:53:05 +0000124 // Next handle the auditors that accept only specific statements.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +0000125 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000126 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
127 MapTy::iterator MI = M.find(key);
Mike Stump11289f42009-09-09 15:08:12 +0000128 if (MI != M.end()) {
Ted Kremenek4967c892009-03-30 17:53:05 +0000129 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
130 isSink |= (*I)->Audit(N, VMgr);
131 }
Mike Stump11289f42009-09-09 15:08:12 +0000132
133 return isSink;
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000134 }
135};
136
137} // end anonymous namespace
138
139//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +0000140// Checker worklist routines.
141//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000142
Zhongxing Xuaf353292009-12-02 05:49:12 +0000143void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000144 ExplodedNodeSet &Src, bool isPrevisit) {
Mike Stump11289f42009-09-09 15:08:12 +0000145
Ted Kremenek49513cc2009-07-22 21:43:51 +0000146 if (Checkers.empty()) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000147 Dst.insert(Src);
Zhongxing Xuaf353292009-12-02 05:49:12 +0000148 return;
Ted Kremenek49513cc2009-07-22 21:43:51 +0000149 }
Mike Stump11289f42009-09-09 15:08:12 +0000150
Zhongxing Xu107f7592009-08-06 12:48:26 +0000151 ExplodedNodeSet Tmp;
152 ExplodedNodeSet *PrevSet = &Src;
Mike Stump11289f42009-09-09 15:08:12 +0000153
Zhongxing Xuaf353292009-12-02 05:49:12 +0000154 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E;++I){
Ted Kremenek32c32892009-12-09 02:45:41 +0000155 ExplodedNodeSet *CurrSet = 0;
156 if (I+1 == E)
157 CurrSet = &Dst;
158 else {
159 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
160 CurrSet->clear();
161 }
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000162 void *tag = I->first;
163 Checker *checker = I->second;
Ted Kremenekacdc8172009-11-25 21:40:22 +0000164
Zhongxing Xu107f7592009-08-06 12:48:26 +0000165 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Zhongxing Xuaf353292009-12-02 05:49:12 +0000166 NI != NE; ++NI)
167 checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, tag, isPrevisit);
Ted Kremenek49513cc2009-07-22 21:43:51 +0000168 PrevSet = CurrSet;
169 }
170
171 // Don't autotransition. The CheckerContext objects should do this
172 // automatically.
Zhongxing Xuaf353292009-12-02 05:49:12 +0000173}
174
175void GRExprEngine::CheckerEvalNilReceiver(const ObjCMessageExpr *ME,
176 ExplodedNodeSet &Dst,
177 const GRState *state,
178 ExplodedNode *Pred) {
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000179 bool Evaluated = false;
180 ExplodedNodeSet DstTmp;
181
Zhongxing Xuaf353292009-12-02 05:49:12 +0000182 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
183 void *tag = I->first;
184 Checker *checker = I->second;
185
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000186 if (checker->GR_EvalNilReceiver(DstTmp, *Builder, *this, ME, Pred, state,
187 tag)) {
188 Evaluated = true;
Zhongxing Xuaf353292009-12-02 05:49:12 +0000189 break;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000190 } else
191 // The checker didn't evaluate the expr. Restore the Dst.
192 DstTmp.clear();
Zhongxing Xuaf353292009-12-02 05:49:12 +0000193 }
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000194
195 if (Evaluated)
196 Dst.insert(DstTmp);
197 else
198 Dst.insert(Pred);
Ted Kremenek49513cc2009-07-22 21:43:51 +0000199}
200
Zhongxing Xu175447f2009-12-07 09:17:35 +0000201// CheckerEvalCall returns true if one of the checkers processed the node.
202// This may return void when all call evaluation logic goes to some checker
203// in the future.
204bool GRExprEngine::CheckerEvalCall(const CallExpr *CE,
205 ExplodedNodeSet &Dst,
206 ExplodedNode *Pred) {
207 bool Evaluated = false;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000208 ExplodedNodeSet DstTmp;
Zhongxing Xu175447f2009-12-07 09:17:35 +0000209
210 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
211 void *tag = I->first;
212 Checker *checker = I->second;
213
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000214 if (checker->GR_EvalCallExpr(DstTmp, *Builder, *this, CE, Pred, tag)) {
Zhongxing Xu175447f2009-12-07 09:17:35 +0000215 Evaluated = true;
216 break;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000217 } else
218 // The checker didn't evaluate the expr. Restore the DstTmp set.
219 DstTmp.clear();
Zhongxing Xu175447f2009-12-07 09:17:35 +0000220 }
221
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000222 if (Evaluated)
223 Dst.insert(DstTmp);
224 else
225 Dst.insert(Pred);
226
Zhongxing Xu175447f2009-12-07 09:17:35 +0000227 return Evaluated;
228}
229
Ted Kremenekef910042009-11-04 04:24:16 +0000230// FIXME: This is largely copy-paste from CheckerVisit(). Need to
231// unify.
Ted Kremenek209e31b2009-11-05 00:42:23 +0000232void GRExprEngine::CheckerVisitBind(const Stmt *AssignE, const Stmt *StoreE,
233 ExplodedNodeSet &Dst,
Ted Kremenekef910042009-11-04 04:24:16 +0000234 ExplodedNodeSet &Src,
235 SVal location, SVal val, bool isPrevisit) {
236
237 if (Checkers.empty()) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000238 Dst.insert(Src);
Ted Kremenekef910042009-11-04 04:24:16 +0000239 return;
240 }
241
242 ExplodedNodeSet Tmp;
243 ExplodedNodeSet *PrevSet = &Src;
244
245 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
246 {
Ted Kremenek32c32892009-12-09 02:45:41 +0000247 ExplodedNodeSet *CurrSet = 0;
248 if (I+1 == E)
249 CurrSet = &Dst;
250 else {
251 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
252 CurrSet->clear();
253 }
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000254
Ted Kremenekef910042009-11-04 04:24:16 +0000255 void *tag = I->first;
256 Checker *checker = I->second;
257
258 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
259 NI != NE; ++NI)
Ted Kremenek209e31b2009-11-05 00:42:23 +0000260 checker->GR_VisitBind(*CurrSet, *Builder, *this, AssignE, StoreE,
261 *NI, tag, location, val, isPrevisit);
Ted Kremenekef910042009-11-04 04:24:16 +0000262
263 // Update which NodeSet is the current one.
264 PrevSet = CurrSet;
265 }
266
267 // Don't autotransition. The CheckerContext objects should do this
268 // automatically.
269}
Ted Kremenek49513cc2009-07-22 21:43:51 +0000270//===----------------------------------------------------------------------===//
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000271// Engine construction and deletion.
272//===----------------------------------------------------------------------===//
273
Ted Kremenekd0fe8042009-11-25 21:51:20 +0000274static void RegisterInternalChecks(GRExprEngine &Eng) {
275 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
276 // are different than what probably many checks will do since they don't
277 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
278 // analyzing a function. Generation of BugReport objects is done via a call
279 // to 'FlushReports' from BugReporter.
280 // The following checks do not need to have their associated BugTypes
281 // explicitly registered with the BugReporter. If they issue any BugReports,
282 // their associated BugType will get registered with the BugReporter
283 // automatically. Note that the check itself is owned by the GRExprEngine
284 // object.
285 RegisterAttrNonNullChecker(Eng);
286 RegisterCallAndMessageChecker(Eng);
287 RegisterDereferenceChecker(Eng);
288 RegisterVLASizeChecker(Eng);
289 RegisterDivZeroChecker(Eng);
290 RegisterReturnStackAddressChecker(Eng);
291 RegisterReturnUndefChecker(Eng);
292 RegisterUndefinedArraySubscriptChecker(Eng);
293 RegisterUndefinedAssignmentChecker(Eng);
294 RegisterUndefBranchChecker(Eng);
295 RegisterUndefResultChecker(Eng);
Zhongxing Xu175447f2009-12-07 09:17:35 +0000296
297 // This is not a checker yet.
298 RegisterNoReturnFunctionChecker(Eng);
Zhongxing Xufe2f9012009-12-08 09:07:59 +0000299 RegisterBuiltinFunctionChecker(Eng);
Zhongxing Xu1042bf42009-12-09 12:23:28 +0000300 RegisterOSAtomicChecker(Eng);
Ted Kremenek7f824732008-05-01 18:33:28 +0000301}
302
Zhongxing Xu342950e2009-08-25 06:51:30 +0000303GRExprEngine::GRExprEngine(AnalysisManager &mgr)
Zhongxing Xue1190f72009-08-15 03:17:38 +0000304 : AMgr(mgr),
Mike Stump11289f42009-09-09 15:08:12 +0000305 CoreEngine(mgr.getASTContext(), *this),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000306 G(CoreEngine.getGraph()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000307 Builder(NULL),
Mike Stump11289f42009-09-09 15:08:12 +0000308 StateMgr(G.getContext(), mgr.getStoreManagerCreator(),
Zhongxing Xu342950e2009-08-25 06:51:30 +0000309 mgr.getConstraintManagerCreator(), G.getAllocator()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000310 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekf8cb51c2009-04-09 16:46:55 +0000311 ValMgr(StateMgr.getValueManager()),
Ted Kremenekf267a152009-07-16 01:32:00 +0000312 SVator(ValMgr.getSValuator()),
Ted Kremenek7f824732008-05-01 18:33:28 +0000313 CurrentStmt(NULL),
Zhongxing Xu4ee570a2008-12-22 08:30:52 +0000314 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Mike Stump11289f42009-09-09 15:08:12 +0000315 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekefb50032009-11-25 21:45:48 +0000316 BR(mgr, *this)
317{
318 // Register internal checks.
Ted Kremenekd0fe8042009-11-25 21:51:20 +0000319 RegisterInternalChecks(*this);
Ted Kremenekefb50032009-11-25 21:45:48 +0000320}
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000321
Mike Stump11289f42009-09-09 15:08:12 +0000322GRExprEngine::~GRExprEngine() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000323 BR.FlushReports();
Ted Kremenek7f824732008-05-01 18:33:28 +0000324 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek6f2a7052009-10-30 17:47:32 +0000325 for (CheckersOrdered::iterator I=Checkers.begin(), E=Checkers.end(); I!=E;++I)
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000326 delete I->second;
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000327}
328
Ted Kremenek667cacb2008-04-15 23:06:53 +0000329//===----------------------------------------------------------------------===//
330// Utility methods.
331//===----------------------------------------------------------------------===//
332
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000333void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek9c32a1e2008-07-17 23:15:45 +0000334 StateMgr.TF = tf;
Ted Kremenek0fbbb082009-11-03 23:30:34 +0000335 tf->RegisterChecks(*this);
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000336 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000337}
338
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000339void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
340 if (!BatchAuditor)
341 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
Mike Stump11289f42009-09-09 15:08:12 +0000342
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000343 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000344}
345
Ted Kremenek4967c892009-03-30 17:53:05 +0000346void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
347 if (!BatchAuditor)
348 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
349
350 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
351}
352
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000353const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) {
354 const GRState *state = StateMgr.getInitialState(InitLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000356 // Preconditions.
357
Ted Kremenek50546632009-04-10 00:59:50 +0000358 // FIXME: It would be nice if we had a more general mechanism to add
359 // such preconditions. Some day.
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000360 do {
361 const Decl *D = InitLoc->getDecl();
362 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
363 // Precondition: the first argument of 'main' is an integer guaranteed
364 // to be > 0.
365 const IdentifierInfo *II = FD->getIdentifier();
366 if (!II || !(II->getName() == "main" && FD->getNumParams() > 0))
367 break;
Ted Kremenekf9906842009-06-18 22:57:13 +0000368
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000369 const ParmVarDecl *PD = FD->getParamDecl(0);
370 QualType T = PD->getType();
371 if (!T->isIntegerType())
372 break;
373
374 const MemRegion *R = state->getRegion(PD, InitLoc);
375 if (!R)
376 break;
377
378 SVal V = state->getSVal(loc::MemRegionVal(R));
379 SVal Constraint_untested = EvalBinOp(state, BinaryOperator::GT, V,
380 ValMgr.makeZeroVal(T),
381 getContext().IntTy);
382
383 DefinedOrUnknownSVal *Constraint =
384 dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested);
385
386 if (!Constraint)
387 break;
388
389 if (const GRState *newState = state->Assume(*Constraint, true))
390 state = newState;
391
392 break;
393 }
394
395 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
396 // Precondition: 'self' is always non-null upon entry to an Objective-C
397 // method.
398 const ImplicitParamDecl *SelfD = MD->getSelfDecl();
399 const MemRegion *R = state->getRegion(SelfD, InitLoc);
400 SVal V = state->getSVal(loc::MemRegionVal(R));
401
402 if (const Loc *LV = dyn_cast<Loc>(&V)) {
403 // Assume that the pointer value in 'self' is non-null.
404 state = state->Assume(*LV, true);
405 assert(state && "'self' cannot be null");
Ted Kremenek2e2b2582009-12-17 01:20:43 +0000406 }
Ted Kremenek50546632009-04-10 00:59:50 +0000407 }
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000408 } while (0);
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000409
Ted Kremenek50546632009-04-10 00:59:50 +0000410 return state;
Ted Kremenek723fe3f2008-02-04 21:59:01 +0000411}
412
Ted Kremenek667cacb2008-04-15 23:06:53 +0000413//===----------------------------------------------------------------------===//
414// Top-level transfer function logic (Dispatcher).
415//===----------------------------------------------------------------------===//
416
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000417void GRExprEngine::ProcessStmt(CFGElement CE, GRStmtNodeBuilder& builder) {
418 CurrentStmt = CE.getStmt();
Ted Kremenek91076ca2009-03-11 02:41:36 +0000419 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000420 CurrentStmt->getLocStart(),
Ted Kremenek91076ca2009-03-11 02:41:36 +0000421 "Error evaluating statement");
Mike Stump11289f42009-09-09 15:08:12 +0000422
Ted Kremenek667cacb2008-04-15 23:06:53 +0000423 Builder = &builder;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000424 EntryNode = builder.getLastNode();
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek667cacb2008-04-15 23:06:53 +0000426 // Set up our simple checks.
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000427 if (BatchAuditor)
428 Builder->setAuditor(BatchAuditor.get());
Mike Stump11289f42009-09-09 15:08:12 +0000429
430 // Create the cleaned state.
Ted Kremenek814c4162009-12-14 22:15:06 +0000431 const ExplodedNode *BasePred = Builder->getBasePredecessor();
432 SymbolReaper SymReaper(BasePred->getLiveVariables(), SymMgr,
433 BasePred->getLocationContext()->getCurrentStackFrame());
Zhongxing Xu3ca89b92009-08-27 06:55:26 +0000434 CleanedState = AMgr.shouldPurgeDead()
435 ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
436 : EntryNode->getState();
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000437
Ted Kremenek3812b762008-04-24 18:31:42 +0000438 // Process any special transfer function for dead symbols.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000439 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000441 if (!SymReaper.hasDeadSymbols())
Ted Kremenekae8014c2008-04-24 23:35:58 +0000442 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000443 else {
444 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000445 SaveOr OldHasGen(Builder->HasGeneratedNode);
446
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000447 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
448 Builder->PurgingDeadSymbols = true;
Mike Stump11289f42009-09-09 15:08:12 +0000449
Zhongxing Xua4276b02009-11-13 06:53:04 +0000450 // FIXME: This should soon be removed.
451 ExplodedNodeSet Tmp2;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000452 getTF().EvalDeadSymbols(Tmp2, *this, *Builder, EntryNode, CurrentStmt,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000453 CleanedState, SymReaper);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000454
Zhongxing Xua4276b02009-11-13 06:53:04 +0000455 if (Checkers.empty())
Zhongxing Xucf86de42009-12-14 02:13:39 +0000456 Tmp.insert(Tmp2);
Zhongxing Xua4276b02009-11-13 06:53:04 +0000457 else {
458 ExplodedNodeSet Tmp3;
459 ExplodedNodeSet *SrcSet = &Tmp2;
460 for (CheckersOrdered::iterator I = Checkers.begin(), E = Checkers.end();
461 I != E; ++I) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000462 ExplodedNodeSet *DstSet = 0;
463 if (I+1 == E)
464 DstSet = &Tmp;
465 else {
466 DstSet = (SrcSet == &Tmp2) ? &Tmp3 : &Tmp2;
467 DstSet->clear();
468 }
469
Zhongxing Xua4276b02009-11-13 06:53:04 +0000470 void *tag = I->first;
471 Checker *checker = I->second;
472 for (ExplodedNodeSet::iterator NI = SrcSet->begin(), NE = SrcSet->end();
473 NI != NE; ++NI)
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000474 checker->GR_EvalDeadSymbols(*DstSet, *Builder, *this, CurrentStmt,
475 *NI, SymReaper, tag);
Zhongxing Xua4276b02009-11-13 06:53:04 +0000476 SrcSet = DstSet;
477 }
478 }
479
Ted Kremenekae8014c2008-04-24 23:35:58 +0000480 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
481 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenekae8014c2008-04-24 23:35:58 +0000484 bool HasAutoGenerated = false;
485
Zhongxing Xu107f7592009-08-06 12:48:26 +0000486 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekae8014c2008-04-24 23:35:58 +0000487
Zhongxing Xu107f7592009-08-06 12:48:26 +0000488 ExplodedNodeSet Dst;
Mike Stump11289f42009-09-09 15:08:12 +0000489
490 // Set the cleaned state.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000491 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
Mike Stump11289f42009-09-09 15:08:12 +0000492
493 // Visit the statement.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000494 if (CE.asLValue())
495 VisitLValue(cast<Expr>(CurrentStmt), *I, Dst);
496 else
497 Visit(CurrentStmt, *I, Dst);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000498
499 // Do we need to auto-generate a node? We only need to do this to generate
500 // a node with a "cleaned" state; GRCoreEngine will actually handle
Mike Stump11289f42009-09-09 15:08:12 +0000501 // auto-transitions for other cases.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000502 if (Dst.size() == 1 && *Dst.begin() == EntryNode
503 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
504 HasAutoGenerated = true;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000505 builder.generateNode(CurrentStmt, GetState(EntryNode), *I);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000506 }
Ted Kremenek3812b762008-04-24 18:31:42 +0000507 }
Mike Stump11289f42009-09-09 15:08:12 +0000508
Ted Kremenek667cacb2008-04-15 23:06:53 +0000509 // NULL out these variables to cleanup.
Ted Kremenek667cacb2008-04-15 23:06:53 +0000510 CleanedState = NULL;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000511 EntryNode = NULL;
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000512
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000513 CurrentStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Ted Kremenekae8014c2008-04-24 23:35:58 +0000515 Builder = NULL;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000516}
517
Mike Stump11289f42009-09-09 15:08:12 +0000518void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek91076ca2009-03-11 02:41:36 +0000519 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
520 S->getLocStart(),
521 "Error evaluating statement");
522
Ted Kremenek667cacb2008-04-15 23:06:53 +0000523 // FIXME: add metadata to the CFG so that we can disable
524 // this check when we KNOW that there is no block-level subexpression.
525 // The motivation is that this check requires a hashtable lookup.
Mike Stump11289f42009-09-09 15:08:12 +0000526
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000527 if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000528 Dst.Add(Pred);
529 return;
530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Ted Kremenek667cacb2008-04-15 23:06:53 +0000532 switch (S->getStmtClass()) {
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000533 // C++ stuff we don't support yet.
534 case Stmt::CXXMemberCallExprClass:
535 case Stmt::CXXNamedCastExprClass:
536 case Stmt::CXXStaticCastExprClass:
537 case Stmt::CXXDynamicCastExprClass:
538 case Stmt::CXXReinterpretCastExprClass:
539 case Stmt::CXXConstCastExprClass:
540 case Stmt::CXXFunctionalCastExprClass:
541 case Stmt::CXXTypeidExprClass:
542 case Stmt::CXXBoolLiteralExprClass:
543 case Stmt::CXXNullPtrLiteralExprClass:
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000544 case Stmt::CXXThrowExprClass:
545 case Stmt::CXXDefaultArgExprClass:
546 case Stmt::CXXZeroInitValueExprClass:
547 case Stmt::CXXNewExprClass:
548 case Stmt::CXXDeleteExprClass:
549 case Stmt::CXXPseudoDestructorExprClass:
550 case Stmt::UnresolvedLookupExprClass:
551 case Stmt::UnaryTypeTraitExprClass:
552 case Stmt::DependentScopeDeclRefExprClass:
553 case Stmt::CXXConstructExprClass:
554 case Stmt::CXXBindTemporaryExprClass:
555 case Stmt::CXXExprWithTemporariesClass:
556 case Stmt::CXXTemporaryObjectExprClass:
557 case Stmt::CXXUnresolvedConstructExprClass:
558 case Stmt::CXXDependentScopeMemberExprClass:
559 case Stmt::UnresolvedMemberExprClass:
560 case Stmt::CXXCatchStmtClass:
561 case Stmt::CXXTryStmtClass: {
562 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
563 Builder->BuildSinks = true;
564 MakeNode(Dst, S, Pred, GetState(Pred));
565 break;
566 }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Ted Kremenek667cacb2008-04-15 23:06:53 +0000568 default:
569 // Cases we intentionally have "default" handle:
570 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Mike Stump11289f42009-09-09 15:08:12 +0000571
Ted Kremenek667cacb2008-04-15 23:06:53 +0000572 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
573 break;
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000575 case Stmt::ArraySubscriptExprClass:
576 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
577 break;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek667cacb2008-04-15 23:06:53 +0000579 case Stmt::AsmStmtClass:
580 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
581 break;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000582
583 case Stmt::BlockDeclRefExprClass:
584 VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), Pred, Dst, false);
585 break;
Mike Stump11289f42009-09-09 15:08:12 +0000586
Ted Kremenekcfe223f2009-11-25 01:33:13 +0000587 case Stmt::BlockExprClass:
588 VisitBlockExpr(cast<BlockExpr>(S), Pred, Dst);
589 break;
590
Ted Kremenek667cacb2008-04-15 23:06:53 +0000591 case Stmt::BinaryOperatorClass: {
592 BinaryOperator* B = cast<BinaryOperator>(S);
Mike Stump11289f42009-09-09 15:08:12 +0000593
Ted Kremenek667cacb2008-04-15 23:06:53 +0000594 if (B->isLogicalOp()) {
595 VisitLogicalExpr(B, Pred, Dst);
596 break;
597 }
598 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000599 const GRState* state = GetState(Pred);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000600 MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenek667cacb2008-04-15 23:06:53 +0000601 break;
602 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000603
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000604 if (AMgr.shouldEagerlyAssume() &&
605 (B->isRelationalOp() || B->isEqualityOp())) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000606 ExplodedNodeSet Tmp;
Zhongxing Xub9eda672009-10-30 07:19:39 +0000607 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp, false);
Mike Stump11289f42009-09-09 15:08:12 +0000608 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000609 }
610 else
Zhongxing Xub9eda672009-10-30 07:19:39 +0000611 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false);
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000612
Ted Kremenek667cacb2008-04-15 23:06:53 +0000613 break;
614 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000615
Douglas Gregor993603d2008-11-14 16:09:21 +0000616 case Stmt::CallExprClass:
617 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000618 CallExpr* C = cast<CallExpr>(S);
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000619 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst, false);
Ted Kremenek537f6382008-11-14 19:47:18 +0000620 break;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000621 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000622
Ted Kremenek667cacb2008-04-15 23:06:53 +0000623 // FIXME: ChooseExpr is really a constant. We need to fix
624 // the CFG do not model them as explicit control-flow.
Mike Stump11289f42009-09-09 15:08:12 +0000625
Ted Kremenek667cacb2008-04-15 23:06:53 +0000626 case Stmt::ChooseExprClass: { // __builtin_choose_expr
627 ChooseExpr* C = cast<ChooseExpr>(S);
628 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
629 break;
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Ted Kremenek667cacb2008-04-15 23:06:53 +0000632 case Stmt::CompoundAssignOperatorClass:
Zhongxing Xub9eda672009-10-30 07:19:39 +0000633 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000634 break;
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000635
636 case Stmt::CompoundLiteralExprClass:
637 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
638 break;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Ted Kremenek667cacb2008-04-15 23:06:53 +0000640 case Stmt::ConditionalOperatorClass: { // '?' operator
641 ConditionalOperator* C = cast<ConditionalOperator>(S);
642 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
643 break;
644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000646 case Stmt::CXXThisExprClass:
647 VisitCXXThisExpr(cast<CXXThisExpr>(S), Pred, Dst);
648 break;
649
Ted Kremenek667cacb2008-04-15 23:06:53 +0000650 case Stmt::DeclRefExprClass:
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000651 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000652 break;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenek667cacb2008-04-15 23:06:53 +0000654 case Stmt::DeclStmtClass:
655 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
656 break;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000658 case Stmt::ImplicitCastExprClass:
Douglas Gregorf19b2312008-10-28 15:36:24 +0000659 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000660 CastExpr* C = cast<CastExpr>(S);
Ted Kremenek22cc1a82009-12-23 00:26:16 +0000661 VisitCast(C, C->getSubExpr(), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000662 break;
663 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +0000664
665 case Stmt::InitListExprClass:
666 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
667 break;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000669 case Stmt::MemberExprClass:
Ted Kremenek38213f92008-04-21 23:43:38 +0000670 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
671 break;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000673 case Stmt::ObjCIvarRefExprClass:
674 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
675 break;
Ted Kremenek17810802008-11-12 19:24:17 +0000676
677 case Stmt::ObjCForCollectionStmtClass:
678 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
679 break;
Mike Stump11289f42009-09-09 15:08:12 +0000680
Ted Kremeneke19711d2009-12-22 22:13:46 +0000681 case Stmt::ObjCMessageExprClass:
682 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000683 break;
Mike Stump11289f42009-09-09 15:08:12 +0000684
Ted Kremenek1857ff42008-12-09 20:18:58 +0000685 case Stmt::ObjCAtThrowStmtClass: {
686 // FIXME: This is not complete. We basically treat @throw as
687 // an abort.
688 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
689 Builder->BuildSinks = true;
690 MakeNode(Dst, S, Pred, GetState(Pred));
691 break;
692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Ted Kremenek667cacb2008-04-15 23:06:53 +0000694 case Stmt::ParenExprClass:
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000695 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000696 break;
Mike Stump11289f42009-09-09 15:08:12 +0000697
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000698 case Stmt::ReturnStmtClass:
699 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
700 break;
Mike Stump11289f42009-09-09 15:08:12 +0000701
Sebastian Redl6f282892008-11-11 17:56:53 +0000702 case Stmt::SizeOfAlignOfExprClass:
703 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000704 break;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Ted Kremenek667cacb2008-04-15 23:06:53 +0000706 case Stmt::StmtExprClass: {
707 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000708
709 if (SE->getSubStmt()->body_empty()) {
710 // Empty statement expression.
711 assert(SE->getType() == getContext().VoidTy
712 && "Empty statement expression must have void type.");
713 Dst.Add(Pred);
714 break;
715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000717 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
718 const GRState* state = GetState(Pred);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000719 MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000720 }
Ted Kremenek667cacb2008-04-15 23:06:53 +0000721 else
722 Dst.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Ted Kremenek667cacb2008-04-15 23:06:53 +0000724 break;
725 }
Zhongxing Xud2fa1e02008-11-30 05:49:49 +0000726
727 case Stmt::StringLiteralClass:
728 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
729 break;
Mike Stump11289f42009-09-09 15:08:12 +0000730
Ted Kremenek891642e2009-03-18 23:49:26 +0000731 case Stmt::UnaryOperatorClass: {
732 UnaryOperator *U = cast<UnaryOperator>(S);
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000733 if (AMgr.shouldEagerlyAssume()&&(U->getOpcode() == UnaryOperator::LNot)) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000734 ExplodedNodeSet Tmp;
Ted Kremenek891642e2009-03-18 23:49:26 +0000735 VisitUnaryOperator(U, Pred, Tmp, false);
736 EvalEagerlyAssume(Dst, Tmp, U);
737 }
738 else
739 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000740 break;
Ted Kremenek891642e2009-03-18 23:49:26 +0000741 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000742 }
743}
744
Mike Stump11289f42009-09-09 15:08:12 +0000745void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000746 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000747
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000748 Ex = Ex->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +0000749
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000750 if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)){
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000751 Dst.Add(Pred);
752 return;
753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000755 switch (Ex->getStmtClass()) {
Ted Kremenek22cc1a82009-12-23 00:26:16 +0000756 // C++ stuff we don't support yet.
Ted Kremenek9a05f202009-12-23 03:14:23 +0000757 case Stmt::CXXExprWithTemporariesClass:
Ted Kremenekbb7a8262009-12-23 01:25:13 +0000758 case Stmt::CXXMemberCallExprClass:
759 case Stmt::CXXZeroInitValueExprClass: {
Ted Kremenek22cc1a82009-12-23 00:26:16 +0000760 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
761 Builder->BuildSinks = true;
762 MakeNode(Dst, Ex, Pred, GetState(Pred));
763 break;
764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000766 case Stmt::ArraySubscriptExprClass:
767 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
768 return;
Mike Stump11289f42009-09-09 15:08:12 +0000769
Ted Kremenekf907cee2009-12-17 07:38:34 +0000770 case Stmt::BinaryOperatorClass:
771 case Stmt::CompoundAssignOperatorClass:
772 VisitBinaryOperator(cast<BinaryOperator>(Ex), Pred, Dst, true);
773 return;
774
Ted Kremenek04af9f22009-12-07 22:05:27 +0000775 case Stmt::BlockDeclRefExprClass:
776 VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(Ex), Pred, Dst, true);
777 return;
Ted Kremenekf907cee2009-12-17 07:38:34 +0000778
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000779 case Stmt::CallExprClass:
780 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke19711d2009-12-22 22:13:46 +0000781 CallExpr *C = cast<CallExpr>(Ex);
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000782 assert(CalleeReturnsReference(C));
783 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst, true);
784 break;
785 }
786
Ted Kremenekf907cee2009-12-17 07:38:34 +0000787 case Stmt::CompoundLiteralExprClass:
788 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
789 return;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000790
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000791 case Stmt::DeclRefExprClass:
792 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
793 return;
Ted Kremenekf907cee2009-12-17 07:38:34 +0000794
Ted Kremenek22cc1a82009-12-23 00:26:16 +0000795 case Stmt::ImplicitCastExprClass:
796 case Stmt::CStyleCastExprClass: {
797 CastExpr *C = cast<CastExpr>(Ex);
798 QualType T = Ex->getType();
799 VisitCast(C, C->getSubExpr(), Pred, Dst, true);
800 break;
801 }
802
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000803 case Stmt::MemberExprClass:
804 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
805 return;
Mike Stump11289f42009-09-09 15:08:12 +0000806
Ted Kremenekf907cee2009-12-17 07:38:34 +0000807 case Stmt::ObjCIvarRefExprClass:
808 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
Ted Kremenekbf263682008-10-27 21:54:31 +0000809 return;
Ted Kremeneke19711d2009-12-22 22:13:46 +0000810
811 case Stmt::ObjCMessageExprClass: {
812 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(Ex);
813 assert(ReceiverReturnsReference(ME));
814 VisitObjCMessageExpr(ME, Pred, Dst, true);
815 return;
816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Ted Kremenek58700462008-10-17 17:24:14 +0000818 case Stmt::ObjCPropertyRefExprClass:
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000819 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Ted Kremenek58700462008-10-17 17:24:14 +0000820 // FIXME: Property assignments are lvalues, but not really "locations".
821 // e.g.: self.x = something;
822 // Here the "self.x" really can translate to a method call (setter) when
823 // the assignment is made. Moreover, the entire assignment expression
824 // evaluate to whatever "something" is, not calling the "getter" for
825 // the property (which would make sense since it can have side effects).
826 // We'll probably treat this as a location, but not one that we can
827 // take the address of. Perhaps we need a new SVal class for cases
828 // like thsis?
829 // Note that we have a similar problem for bitfields, since they don't
830 // have "locations" in the sense that we can take their address.
831 Dst.Add(Pred);
Ted Kremenek8f5dc292008-10-18 04:08:49 +0000832 return;
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000833
834 case Stmt::StringLiteralClass: {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000835 const GRState* state = GetState(Pred);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000836 SVal V = state->getLValue(cast<StringLiteral>(Ex));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000837 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000838 return;
839 }
Mike Stump11289f42009-09-09 15:08:12 +0000840
Ted Kremenekf907cee2009-12-17 07:38:34 +0000841 case Stmt::UnaryOperatorClass:
842 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
Zhongxing Xub9eda672009-10-30 07:19:39 +0000843 return;
Ted Kremenekf907cee2009-12-17 07:38:34 +0000844
Ted Kremenek850422e2008-10-18 04:15:35 +0000845 default:
846 // Arbitrary subexpressions can return aggregate temporaries that
847 // can be used in a lvalue context. We need to enhance our support
848 // of such temporaries in both the environment and the store, so right
849 // now we just do a regular visit.
Douglas Gregorddb24852009-01-30 17:31:00 +0000850 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremeneke69a1fa2008-10-25 20:09:21 +0000851 "Other kinds of expressions with non-aggregate/union types do"
852 " not have lvalues.");
Mike Stump11289f42009-09-09 15:08:12 +0000853
Ted Kremenek850422e2008-10-18 04:15:35 +0000854 Visit(Ex, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000855 }
856}
857
858//===----------------------------------------------------------------------===//
859// Block entrance. (Update counters).
860//===----------------------------------------------------------------------===//
861
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000862bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenek667cacb2008-04-15 23:06:53 +0000863 GRBlockCounter BC) {
Mike Stump11289f42009-09-09 15:08:12 +0000864
Ted Kremenek667cacb2008-04-15 23:06:53 +0000865 return BC.getNumVisited(B->getBlockID()) < 3;
866}
867
868//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +0000869// Generic node creation.
870//===----------------------------------------------------------------------===//
871
Zhongxing Xu107f7592009-08-06 12:48:26 +0000872ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
873 ExplodedNode* Pred, const GRState* St,
874 ProgramPoint::Kind K, const void *tag) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000875 assert (Builder && "GRStmtNodeBuilder not present.");
876 SaveAndRestore<const void*> OldTag(Builder->Tag);
877 Builder->Tag = tag;
878 return Builder->MakeNode(Dst, S, Pred, St, K);
879}
880
881//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +0000882// Branch processing.
883//===----------------------------------------------------------------------===//
884
Ted Kremenek17d541d2009-02-13 01:45:31 +0000885const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000886 Stmt* Terminator,
887 bool branchTaken) {
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000889 switch (Terminator->getStmtClass()) {
890 default:
Ted Kremenek17d541d2009-02-13 01:45:31 +0000891 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000892
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000893 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump11289f42009-09-09 15:08:12 +0000894
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000895 BinaryOperator* B = cast<BinaryOperator>(Terminator);
896 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000898 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +0000899
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000900 // For &&, if we take the true branch, then the value of the whole
901 // expression is that of the RHS expression.
902 //
903 // For ||, if we take the false branch, then the value of the whole
904 // expression is that of the RHS expression.
Mike Stump11289f42009-09-09 15:08:12 +0000905
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000906 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
Mike Stump11289f42009-09-09 15:08:12 +0000907 (Op == BinaryOperator::LOr && !branchTaken)
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000908 ? B->getRHS() : B->getLHS();
Mike Stump11289f42009-09-09 15:08:12 +0000909
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000910 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000913 case Stmt::ConditionalOperatorClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000914
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000915 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000917 // For ?, if branchTaken == true then the value is either the LHS or
918 // the condition itself. (GNU extension).
Mike Stump11289f42009-09-09 15:08:12 +0000919
920 Expr* Ex;
921
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000922 if (branchTaken)
Mike Stump11289f42009-09-09 15:08:12 +0000923 Ex = C->getLHS() ? C->getLHS() : C->getCond();
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000924 else
925 Ex = C->getRHS();
Mike Stump11289f42009-09-09 15:08:12 +0000926
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000927 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000928 }
Mike Stump11289f42009-09-09 15:08:12 +0000929
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000930 case Stmt::ChooseExprClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000931
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000932 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000933
934 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000935 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000936 }
937 }
938}
939
Ted Kremenek22358bd2009-03-13 16:32:54 +0000940/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
941/// to try to recover some path-sensitivity for casts of symbolic
942/// integers that promote their values (which are currently not tracked well).
943/// This function returns the SVal bound to Condition->IgnoreCasts if all the
944// cast(s) did was sign-extend the original value.
945static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
946 Stmt* Condition, ASTContext& Ctx) {
947
948 Expr *Ex = dyn_cast<Expr>(Condition);
949 if (!Ex)
950 return UnknownVal();
951
952 uint64_t bits = 0;
953 bool bitsInit = false;
Mike Stump11289f42009-09-09 15:08:12 +0000954
Ted Kremenek22358bd2009-03-13 16:32:54 +0000955 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
956 QualType T = CE->getType();
957
958 if (!T->isIntegerType())
959 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000960
Ted Kremenek22358bd2009-03-13 16:32:54 +0000961 uint64_t newBits = Ctx.getTypeSize(T);
962 if (!bitsInit || newBits < bits) {
963 bitsInit = true;
964 bits = newBits;
965 }
Mike Stump11289f42009-09-09 15:08:12 +0000966
Ted Kremenek22358bd2009-03-13 16:32:54 +0000967 Ex = CE->getSubExpr();
968 }
969
970 // We reached a non-cast. Is it a symbolic value?
971 QualType T = Ex->getType();
972
973 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
974 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000975
Ted Kremenek095f1a92009-06-18 23:58:37 +0000976 return state->getSVal(Ex);
Ted Kremenek22358bd2009-03-13 16:32:54 +0000977}
978
Ted Kremenek17810802008-11-12 19:24:17 +0000979void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000980 GRBranchNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000981
Ted Kremenek8db4b112008-02-15 22:29:00 +0000982 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump11289f42009-09-09 15:08:12 +0000983 if (!Condition) {
Ted Kremenek8db4b112008-02-15 22:29:00 +0000984 builder.markInfeasible(false);
Ted Kremenek8db4b112008-02-15 22:29:00 +0000985 return;
986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Ted Kremenek32c41ec2009-03-11 03:54:24 +0000988 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
989 Condition->getLocStart(),
990 "Error evaluating branch");
Ted Kremenek907a7112009-08-27 01:39:13 +0000991
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000992 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
993 void *tag = I->first;
994 Checker *checker = I->second;
995 checker->VisitBranchCondition(builder, *this, Condition, tag);
996 }
997
998 // If the branch condition is undefined, return;
999 if (!builder.isFeasible(true) && !builder.isFeasible(false))
1000 return;
1001
Mike Stump11289f42009-09-09 15:08:12 +00001002 const GRState* PrevState = builder.getState();
Ted Kremenek7020eae2009-09-11 22:07:28 +00001003 SVal X = PrevState->getSVal(Condition);
Mike Stump11289f42009-09-09 15:08:12 +00001004
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001005 if (X.isUnknown()) {
1006 // Give it a chance to recover from unknown.
1007 if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
1008 if (Ex->getType()->isIntegerType()) {
1009 // Try to recover some path-sensitivity. Right now casts of symbolic
1010 // integers that promote their values are currently not tracked well.
1011 // If 'Condition' is such an expression, try and recover the
1012 // underlying value and use that instead.
1013 SVal recovered = RecoverCastedSymbol(getStateManager(),
1014 builder.getState(), Condition,
1015 getContext());
1016
1017 if (!recovered.isUnknown()) {
1018 X = recovered;
1019 }
Ted Kremenek22358bd2009-03-13 16:32:54 +00001020 }
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001021 }
1022 // If the condition is still unknown, give up.
1023 if (X.isUnknown()) {
1024 builder.generateNode(MarkBranch(PrevState, Term, true), true);
1025 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremeneka50d9852008-01-30 23:03:39 +00001026 return;
Mike Stump11289f42009-09-09 15:08:12 +00001027 }
Ted Kremeneka50d9852008-01-30 23:03:39 +00001028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001030 DefinedSVal V = cast<DefinedSVal>(X);
1031
Ted Kremenek17f4dbd2008-02-29 20:27:50 +00001032 // Process the true branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001033 if (builder.isFeasible(true)) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001034 if (const GRState *state = PrevState->Assume(V, true))
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001035 builder.generateNode(MarkBranch(state, Term, true), true);
1036 else
1037 builder.markInfeasible(true);
1038 }
Mike Stump11289f42009-09-09 15:08:12 +00001039
1040 // Process the false branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001041 if (builder.isFeasible(false)) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001042 if (const GRState *state = PrevState->Assume(V, false))
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001043 builder.generateNode(MarkBranch(state, Term, false), false);
1044 else
1045 builder.markInfeasible(false);
1046 }
Ted Kremenek7ff18932008-01-29 23:32:35 +00001047}
1048
Ted Kremenekf6c62f32008-02-13 17:41:41 +00001049/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7022efb2008-02-13 00:24:44 +00001050/// nodes by processing the 'effects' of a computed goto jump.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001051void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
Ted Kremenek7022efb2008-02-13 00:24:44 +00001052
Mike Stump11289f42009-09-09 15:08:12 +00001053 const GRState *state = builder.getState();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001054 SVal V = state->getSVal(builder.getTarget());
Mike Stump11289f42009-09-09 15:08:12 +00001055
Ted Kremenek7022efb2008-02-13 00:24:44 +00001056 // Three possibilities:
1057 //
1058 // (1) We know the computed label.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001059 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek7022efb2008-02-13 00:24:44 +00001060 // (3) We have no clue about the label. Dispatch to all targets.
1061 //
Mike Stump11289f42009-09-09 15:08:12 +00001062
Zhongxing Xu107f7592009-08-06 12:48:26 +00001063 typedef GRIndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek7022efb2008-02-13 00:24:44 +00001064
Zhongxing Xu27f17422008-10-17 05:57:07 +00001065 if (isa<loc::GotoLabel>(V)) {
1066 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00001067
Ted Kremenek7022efb2008-02-13 00:24:44 +00001068 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek2bba9012008-02-13 17:27:37 +00001069 if (I.getLabel() == L) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001070 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001071 return;
1072 }
1073 }
Mike Stump11289f42009-09-09 15:08:12 +00001074
Ted Kremenek7022efb2008-02-13 00:24:44 +00001075 assert (false && "No block with label.");
1076 return;
1077 }
1078
Zhongxing Xu27f17422008-10-17 05:57:07 +00001079 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek7022efb2008-02-13 00:24:44 +00001080 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu9e200792009-11-24 07:06:39 +00001081 //ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
1082 // FIXME: add checker visit.
1083 // UndefBranches.insert(N);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001084 return;
1085 }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Ted Kremenek7022efb2008-02-13 00:24:44 +00001087 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenek9c03f682009-04-23 17:49:43 +00001088 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump11289f42009-09-09 15:08:12 +00001089
Ted Kremenek7022efb2008-02-13 00:24:44 +00001090 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek17d541d2009-02-13 01:45:31 +00001091 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001092}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001093
Ted Kremenek667cacb2008-04-15 23:06:53 +00001094
1095void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001096 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001097
Zhongxing Xu6df9f542009-12-16 11:27:52 +00001098 assert(Ex == CurrentStmt &&
1099 Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump11289f42009-09-09 15:08:12 +00001100
Ted Kremenek17d541d2009-02-13 01:45:31 +00001101 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +00001102 SVal X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001103
Ted Kremenek667cacb2008-04-15 23:06:53 +00001104 assert (X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001105
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001106 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump11289f42009-09-09 15:08:12 +00001107 assert(SE);
Ted Kremenek907a7112009-08-27 01:39:13 +00001108 X = state->getSVal(SE);
Mike Stump11289f42009-09-09 15:08:12 +00001109
Ted Kremenek667cacb2008-04-15 23:06:53 +00001110 // Make sure that we invalidate the previous binding.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001111 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremenek667cacb2008-04-15 23:06:53 +00001112}
1113
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001114/// ProcessEndPath - Called by GRCoreEngine. Used to generate end-of-path
1115/// nodes when the control reaches the end of a function.
1116void GRExprEngine::ProcessEndPath(GREndPathNodeBuilder& builder) {
1117 getTF().EvalEndPath(*this, builder);
1118 StateMgr.EndPath(builder.getState());
Zhongxing Xu4668c7e2009-11-17 07:54:15 +00001119 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E;++I){
1120 void *tag = I->first;
1121 Checker *checker = I->second;
1122 checker->EvalEndPath(builder, tag, *this);
1123 }
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001124}
1125
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001126/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
1127/// nodes by processing the 'effects' of a switch statement.
Mike Stump11289f42009-09-09 15:08:12 +00001128void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
1129 typedef GRSwitchNodeBuilder::iterator iterator;
1130 const GRState* state = builder.getState();
Ted Kremenek346169f2008-02-18 22:57:02 +00001131 Expr* CondE = builder.getCondition();
Ted Kremenek7020eae2009-09-11 22:07:28 +00001132 SVal CondV_untested = state->getSVal(CondE);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001133
Ted Kremenek7020eae2009-09-11 22:07:28 +00001134 if (CondV_untested.isUndef()) {
Zhongxing Xu9e200792009-11-24 07:06:39 +00001135 //ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
1136 // FIXME: add checker
1137 //UndefBranches.insert(N);
1138
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001139 return;
1140 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001141 DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
Ted Kremenek346169f2008-02-18 22:57:02 +00001142
Ted Kremenek7020eae2009-09-11 22:07:28 +00001143 const GRState *DefaultSt = state;
Ted Kremenekf9906842009-06-18 22:57:13 +00001144 bool defaultIsFeasible = false;
Mike Stump11289f42009-09-09 15:08:12 +00001145
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001146 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001147 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001148
1149 // Evaluate the LHS of the case value.
1150 Expr::EvalResult V1;
Mike Stump11289f42009-09-09 15:08:12 +00001151 bool b = Case->getLHS()->Evaluate(V1, getContext());
1152
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001153 // Sanity checks. These go away in Release builds.
Mike Stump11289f42009-09-09 15:08:12 +00001154 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001155 && "Case condition must evaluate to an integer constant.");
Mike Stump11289f42009-09-09 15:08:12 +00001156 b = b; // silence unused variable warning
1157 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001158 getContext().getTypeSize(CondE->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001159
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001160 // Get the RHS of the case, if it exists.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001161 Expr::EvalResult V2;
Mike Stump11289f42009-09-09 15:08:12 +00001162
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001163 if (Expr* E = Case->getRHS()) {
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001164 b = E->Evaluate(V2, getContext());
Mike Stump11289f42009-09-09 15:08:12 +00001165 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001166 && "Case condition must evaluate to an integer constant.");
1167 b = b; // silence unused variable warning
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001168 }
Ted Kremenek9eae4032008-03-17 22:17:56 +00001169 else
1170 V2 = V1;
Mike Stump11289f42009-09-09 15:08:12 +00001171
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001172 // FIXME: Eventually we should replace the logic below with a range
1173 // comparison, rather than concretize the values within the range.
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001174 // This should be easy once we have "ranges" for NonLVals.
Mike Stump11289f42009-09-09 15:08:12 +00001175
Ted Kremenek9eae4032008-03-17 22:17:56 +00001176 do {
Mike Stump11289f42009-09-09 15:08:12 +00001177 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek7020eae2009-09-11 22:07:28 +00001178 DefinedOrUnknownSVal Res = SVator.EvalEQ(DefaultSt, CondV, CaseVal);
1179
Mike Stump11289f42009-09-09 15:08:12 +00001180 // Now "assume" that the case matches.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001181 if (const GRState* stateNew = state->Assume(Res, true)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001182 builder.generateCaseStmtNode(I, stateNew);
Mike Stump11289f42009-09-09 15:08:12 +00001183
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001184 // If CondV evaluates to a constant, then we know that this
1185 // is the *only* case that we can take, so stop evaluating the
1186 // others.
Zhongxing Xu27f17422008-10-17 05:57:07 +00001187 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001188 return;
1189 }
Mike Stump11289f42009-09-09 15:08:12 +00001190
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001191 // Now "assume" that the case doesn't match. Add this state
1192 // to the default state (if it is feasible).
Ted Kremenek7020eae2009-09-11 22:07:28 +00001193 if (const GRState *stateNew = DefaultSt->Assume(Res, false)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001194 defaultIsFeasible = true;
1195 DefaultSt = stateNew;
Ted Kremenekd2419a02008-04-23 05:03:18 +00001196 }
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001197
Ted Kremenek9eae4032008-03-17 22:17:56 +00001198 // Concretize the next value in the range.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001199 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek9eae4032008-03-17 22:17:56 +00001200 break;
Mike Stump11289f42009-09-09 15:08:12 +00001201
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001202 ++V1.Val.getInt();
1203 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump11289f42009-09-09 15:08:12 +00001204
Ted Kremenek9eae4032008-03-17 22:17:56 +00001205 } while (true);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001206 }
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001208 // If we reach here, than we know that the default branch is
Mike Stump11289f42009-09-09 15:08:12 +00001209 // possible.
Ted Kremenekf9906842009-06-18 22:57:13 +00001210 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001211}
1212
Ted Kremenek667cacb2008-04-15 23:06:53 +00001213//===----------------------------------------------------------------------===//
1214// Transfer functions: logical operations ('&&', '||').
1215//===----------------------------------------------------------------------===//
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001216
Zhongxing Xu107f7592009-08-06 12:48:26 +00001217void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
1218 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001219
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001220 assert(B->getOpcode() == BinaryOperator::LAnd ||
1221 B->getOpcode() == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Zhongxing Xu6df9f542009-12-16 11:27:52 +00001223 assert(B==CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump11289f42009-09-09 15:08:12 +00001224
Ted Kremenek17d541d2009-02-13 01:45:31 +00001225 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +00001226 SVal X = state->getSVal(B);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001227 assert(X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001228
Ted Kremenek7020eae2009-09-11 22:07:28 +00001229 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001230 assert(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001231
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001232 if (Ex == B->getRHS()) {
Ted Kremenek907a7112009-08-27 01:39:13 +00001233 X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001234
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001235 // Handle undefined values.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001236 if (X.isUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001237 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenekbc543902008-02-26 19:40:44 +00001238 return;
1239 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001240
1241 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
Mike Stump11289f42009-09-09 15:08:12 +00001242
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001243 // We took the RHS. Because the value of the '&&' or '||' expression must
1244 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
1245 // or 1. Alternatively, we could take a lazy approach, and calculate this
1246 // value later when necessary. We don't have the machinery in place for
1247 // this right now, and since most logical expressions are used for branches,
Mike Stump11289f42009-09-09 15:08:12 +00001248 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001249 if (const GRState *newState = state->Assume(XD, true))
Mike Stump11289f42009-09-09 15:08:12 +00001250 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001251 newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Mike Stump11289f42009-09-09 15:08:12 +00001252
Ted Kremenek7020eae2009-09-11 22:07:28 +00001253 if (const GRState *newState = state->Assume(XD, false))
Mike Stump11289f42009-09-09 15:08:12 +00001254 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001255 newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001256 }
1257 else {
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001258 // We took the LHS expression. Depending on whether we are '&&' or
1259 // '||' we know what the value of the expression is via properties of
1260 // the short-circuiting.
Mike Stump11289f42009-09-09 15:08:12 +00001261 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001262 B->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001263 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001264 }
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001265}
Mike Stump11289f42009-09-09 15:08:12 +00001266
Ted Kremenek667cacb2008-04-15 23:06:53 +00001267//===----------------------------------------------------------------------===//
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001268// Transfer functions: Loads and stores.
Ted Kremenek667cacb2008-04-15 23:06:53 +00001269//===----------------------------------------------------------------------===//
Ted Kremenekde8d62b2008-01-15 23:55:06 +00001270
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001271void GRExprEngine::VisitBlockExpr(BlockExpr *BE, ExplodedNode *Pred,
1272 ExplodedNodeSet &Dst) {
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001273
1274 ExplodedNodeSet Tmp;
1275
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001276 CanQualType T = getContext().getCanonicalType(BE->getType());
Ted Kremenekb63ad7a2009-11-25 23:53:07 +00001277 SVal V = ValMgr.getBlockPointer(BE->getBlockDecl(), T,
1278 Pred->getLocationContext());
1279
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001280 MakeNode(Tmp, BE, Pred, GetState(Pred)->BindExpr(BE, V),
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001281 ProgramPoint::PostLValueKind);
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001282
1283 // Post-visit the BlockExpr.
1284 CheckerVisit(BE, Dst, Tmp, false);
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001285}
1286
Mike Stump11289f42009-09-09 15:08:12 +00001287void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
Ted Kremenek14536f62009-08-21 22:28:32 +00001288 ExplodedNodeSet &Dst, bool asLValue) {
Ted Kremenek04af9f22009-12-07 22:05:27 +00001289 VisitCommonDeclRefExpr(Ex, Ex->getDecl(), Pred, Dst, asLValue);
1290}
1291
1292void GRExprEngine::VisitBlockDeclRefExpr(BlockDeclRefExpr *Ex,
1293 ExplodedNode *Pred,
1294 ExplodedNodeSet &Dst, bool asLValue) {
1295 VisitCommonDeclRefExpr(Ex, Ex->getDecl(), Pred, Dst, asLValue);
1296}
1297
1298void GRExprEngine::VisitCommonDeclRefExpr(Expr *Ex, const NamedDecl *D,
1299 ExplodedNode *Pred,
1300 ExplodedNodeSet &Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001301
Ted Kremenek7020eae2009-09-11 22:07:28 +00001302 const GRState *state = GetState(Pred);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001303
1304 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1305
Ted Kremenek14536f62009-08-21 22:28:32 +00001306 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xu252fe5c2008-10-17 02:20:14 +00001307
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001308 if (asLValue) {
1309 // For references, the 'lvalue' is the pointer address stored in the
1310 // reference region.
1311 if (VD->getType()->isReferenceType()) {
1312 if (const MemRegion *R = V.getAsRegion())
1313 V = state->getSVal(R);
1314 else
1315 V = UnknownVal();
1316 }
1317
1318 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V,
1319 ProgramPoint::PostLValueKind));
1320 }
Zhongxing Xu232c7922008-10-16 06:09:51 +00001321 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001322 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001323
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001324 return;
Zhongxing Xu232c7922008-10-16 06:09:51 +00001325 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
1326 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
1327
Zhongxing Xud09b5202009-06-23 06:13:19 +00001328 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001329 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu232c7922008-10-16 06:09:51 +00001330 return;
1331
1332 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek1624a472009-09-23 01:30:01 +00001333 // This code is valid regardless of the value of 'isLValue'.
Zhongxing Xuac129432009-04-20 05:24:46 +00001334 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001335 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001336 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001337 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Zhongxing Xu232c7922008-10-16 06:09:51 +00001340 assert (false &&
1341 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek88da1de2008-02-07 04:16:04 +00001342}
1343
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001344/// VisitArraySubscriptExpr - Transfer function for array accesses
Mike Stump11289f42009-09-09 15:08:12 +00001345void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001346 ExplodedNode* Pred,
1347 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump11289f42009-09-09 15:08:12 +00001348
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001349 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek10246e82008-04-29 23:24:44 +00001350 Expr* Idx = A->getIdx()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001351 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001352
Ted Kremenekcce27f52009-02-24 02:23:11 +00001353 if (Base->getType()->isVectorType()) {
1354 // For vector types get its lvalue.
1355 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1356 // In fact, I think this is just a hack. We need to get the right
1357 // semantics.
1358 VisitLValue(Base, Pred, Tmp);
1359 }
Mike Stump11289f42009-09-09 15:08:12 +00001360 else
Ted Kremenekcce27f52009-02-24 02:23:11 +00001361 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Mike Stump11289f42009-09-09 15:08:12 +00001362
Zhongxing Xu107f7592009-08-06 12:48:26 +00001363 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1364 ExplodedNodeSet Tmp2;
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001365 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Mike Stump11289f42009-09-09 15:08:12 +00001366
Zhongxing Xub1667122009-11-11 13:42:54 +00001367 ExplodedNodeSet Tmp3;
1368 CheckerVisit(A, Tmp3, Tmp2, true);
1369
1370 for (ExplodedNodeSet::iterator I2=Tmp3.begin(),E2=Tmp3.end();I2!=E2; ++I2) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001371 const GRState* state = GetState(*I2);
Zhongxing Xu7d6387b2009-10-14 03:33:08 +00001372 SVal V = state->getLValue(A->getType(), state->getSVal(Idx),
1373 state->getSVal(Base));
Ted Kremenek10246e82008-04-29 23:24:44 +00001374
Zhongxing Xu232c7922008-10-16 06:09:51 +00001375 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001376 MakeNode(Dst, A, *I2, state->BindExpr(A, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001377 ProgramPoint::PostLValueKind);
Ted Kremenek10246e82008-04-29 23:24:44 +00001378 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001379 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek10246e82008-04-29 23:24:44 +00001380 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001381 }
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001382}
1383
Ted Kremenek38213f92008-04-21 23:43:38 +00001384/// VisitMemberExpr - Transfer function for member expressions.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001385void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
1386 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001387
Ted Kremenek38213f92008-04-21 23:43:38 +00001388 Expr* Base = M->getBase()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001389 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001390
1391 if (M->isArrow())
Ted Kremenekfef1f302008-10-18 03:28:48 +00001392 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1393 else
1394 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
Mike Stump11289f42009-09-09 15:08:12 +00001395
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001396 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1397 if (!Field) // FIXME: skipping member expressions for non-fields
1398 return;
1399
Zhongxing Xu107f7592009-08-06 12:48:26 +00001400 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001401 const GRState* state = GetState(*I);
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001402 // FIXME: Should we insert some assumption logic in here to determine
1403 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001404 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xub9eda672009-10-30 07:19:39 +00001405 SVal L = state->getLValue(Field, state->getSVal(Base));
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001406
Zhongxing Xub9eda672009-10-30 07:19:39 +00001407 if (asLValue)
1408 MakeNode(Dst, M, *I, state->BindExpr(M, L), ProgramPoint::PostLValueKind);
1409 else
1410 EvalLoad(Dst, M, *I, state, L);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001411 }
Ted Kremenek38213f92008-04-21 23:43:38 +00001412}
1413
Ted Kremenek17d541d2009-02-13 01:45:31 +00001414/// EvalBind - Handle the semantics of binding a value to a specific location.
1415/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001416void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Stmt *AssignE,
1417 Stmt* StoreE, ExplodedNode* Pred,
Ted Kremenekb006b822009-11-04 00:09:15 +00001418 const GRState* state, SVal location, SVal Val,
1419 bool atDeclInit) {
Ted Kremenekef910042009-11-04 04:24:16 +00001420
1421
1422 // Do a previsit of the bind.
1423 ExplodedNodeSet CheckedSet, Src;
1424 Src.Add(Pred);
Ted Kremenek209e31b2009-11-05 00:42:23 +00001425 CheckerVisitBind(AssignE, StoreE, CheckedSet, Src, location, Val, true);
Ted Kremenekef910042009-11-04 04:24:16 +00001426
1427 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
1428 I!=E; ++I) {
1429
1430 if (Pred != *I)
1431 state = GetState(*I);
1432
1433 const GRState* newState = 0;
Ted Kremenek17d541d2009-02-13 01:45:31 +00001434
Ted Kremenekef910042009-11-04 04:24:16 +00001435 if (atDeclInit) {
1436 const VarRegion *VR =
1437 cast<VarRegion>(cast<loc::MemRegionVal>(location).getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001438
Ted Kremenekef910042009-11-04 04:24:16 +00001439 newState = state->bindDecl(VR, Val);
Ted Kremenekb006b822009-11-04 00:09:15 +00001440 }
1441 else {
Ted Kremenekef910042009-11-04 04:24:16 +00001442 if (location.isUnknown()) {
1443 // We know that the new state will be the same as the old state since
1444 // the location of the binding is "unknown". Consequently, there
1445 // is no reason to just create a new node.
1446 newState = state;
1447 }
1448 else {
1449 // We are binding to a value other than 'unknown'. Perform the binding
1450 // using the StoreManager.
1451 newState = state->bindLoc(cast<Loc>(location), Val);
1452 }
Ted Kremenekb006b822009-11-04 00:09:15 +00001453 }
Ted Kremenekef910042009-11-04 04:24:16 +00001454
1455 // The next thing to do is check if the GRTransferFuncs object wants to
1456 // update the state based on the new binding. If the GRTransferFunc object
1457 // doesn't do anything, just auto-propagate the current state.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001458 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, newState, StoreE,
Ted Kremenekef910042009-11-04 04:24:16 +00001459 newState != state);
1460
1461 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001462 }
Ted Kremenek17d541d2009-02-13 01:45:31 +00001463}
1464
1465/// EvalStore - Handle the semantics of a store via an assignment.
1466/// @param Dst The node set to store generated state nodes
1467/// @param Ex The expression representing the location of the store
1468/// @param state The current simulation state
1469/// @param location The location to store the value
1470/// @param Val The value to be stored
Ted Kremenek209e31b2009-11-05 00:42:23 +00001471void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr *AssignE,
1472 Expr* StoreE,
1473 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001474 const GRState* state, SVal location, SVal Val,
1475 const void *tag) {
Mike Stump11289f42009-09-09 15:08:12 +00001476
Ted Kremenek209e31b2009-11-05 00:42:23 +00001477 assert(Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump11289f42009-09-09 15:08:12 +00001478
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001479 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001480 ExplodedNodeSet Tmp;
1481 EvalLocation(Tmp, StoreE, Pred, state, location, tag, false);
Mike Stump11289f42009-09-09 15:08:12 +00001482
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001483 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001484 return;
Ted Kremenekc072b822008-04-18 20:35:30 +00001485
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001486 assert(!location.isUndef());
Ted Kremenek17d541d2009-02-13 01:45:31 +00001487
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001488 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind,
1489 ProgramPoint::PostStoreKind);
1490 SaveAndRestore<const void*> OldTag(Builder->Tag, tag);
1491
Mike Stump11289f42009-09-09 15:08:12 +00001492 // Proceed with the store.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001493 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
1494 EvalBind(Dst, AssignE, StoreE, *NI, GetState(*NI), location, Val);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001495}
1496
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001497void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr *Ex, ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001498 const GRState* state, SVal location,
Zhongxing Xu731f4622009-11-16 04:49:44 +00001499 const void *tag, QualType LoadTy) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001500
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001501 // Are we loading from a region? This actually results in two loads; one
1502 // to fetch the address of the referenced value and one to fetch the
1503 // referenced value.
1504 if (const TypedRegion *TR =
1505 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
1506
1507 QualType ValTy = TR->getValueType(getContext());
1508 if (const ReferenceType *RT = ValTy->getAs<ReferenceType>()) {
1509 static int loadReferenceTag = 0;
1510 ExplodedNodeSet Tmp;
1511 EvalLoadCommon(Tmp, Ex, Pred, state, location, &loadReferenceTag,
1512 getContext().getPointerType(RT->getPointeeType()));
1513
1514 // Perform the load from the referenced value.
1515 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end() ; I!=E; ++I) {
1516 state = GetState(*I);
1517 location = state->getSVal(Ex);
1518 EvalLoadCommon(Dst, Ex, *I, state, location, tag, LoadTy);
1519 }
1520 return;
1521 }
1522 }
1523
1524 EvalLoadCommon(Dst, Ex, Pred, state, location, tag, LoadTy);
1525}
1526
1527void GRExprEngine::EvalLoadCommon(ExplodedNodeSet& Dst, Expr *Ex,
1528 ExplodedNode* Pred,
1529 const GRState* state, SVal location,
1530 const void *tag, QualType LoadTy) {
1531
Mike Stump11289f42009-09-09 15:08:12 +00001532 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001533 ExplodedNodeSet Tmp;
1534 EvalLocation(Tmp, Ex, Pred, state, location, tag, true);
Mike Stump11289f42009-09-09 15:08:12 +00001535
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001536 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001537 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001538
1539 assert(!location.isUndef());
1540
1541 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1542 SaveAndRestore<const void*> OldTag(Builder->Tag);
Mike Stump11289f42009-09-09 15:08:12 +00001543
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001544 // Proceed with the load.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001545 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1546 state = GetState(*NI);
1547 if (location.isUnknown()) {
1548 // This is important. We must nuke the old binding.
1549 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, UnknownVal()),
1550 ProgramPoint::PostLoadKind, tag);
1551 }
1552 else {
Zhongxing Xu731f4622009-11-16 04:49:44 +00001553 SVal V = state->getSVal(cast<Loc>(location), LoadTy.isNull() ?
1554 Ex->getType() : LoadTy);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001555 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, V), ProgramPoint::PostLoadKind,
1556 tag);
1557 }
Zhongxing Xu33178a02008-11-28 08:34:30 +00001558 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001559}
1560
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001561void GRExprEngine::EvalLocation(ExplodedNodeSet &Dst, Stmt *S,
1562 ExplodedNode* Pred,
1563 const GRState* state, SVal location,
1564 const void *tag, bool isLoad) {
Zhongxing Xuab0ae212009-11-20 03:50:46 +00001565 // Early checks for performance reason.
1566 if (location.isUnknown() || Checkers.empty()) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001567 Dst.Add(Pred);
1568 return;
Ted Kremenekfac290d2009-11-02 23:19:29 +00001569 }
1570
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001571 ExplodedNodeSet Src, Tmp;
1572 Src.Add(Pred);
1573 ExplodedNodeSet *PrevSet = &Src;
1574
1575 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
1576 {
Ted Kremenek32c32892009-12-09 02:45:41 +00001577 ExplodedNodeSet *CurrSet = 0;
1578 if (I+1 == E)
1579 CurrSet = &Dst;
1580 else {
1581 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
1582 CurrSet->clear();
1583 }
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001584
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001585 void *tag = I->first;
1586 Checker *checker = I->second;
1587
1588 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenekf5735152009-11-23 22:22:01 +00001589 NI != NE; ++NI) {
1590 // Use the 'state' argument only when the predecessor node is the
1591 // same as Pred. This allows us to catch updates to the state.
1592 checker->GR_VisitLocation(*CurrSet, *Builder, *this, S, *NI,
1593 *NI == Pred ? state : GetState(*NI),
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001594 location, tag, isLoad);
Ted Kremenekf5735152009-11-23 22:22:01 +00001595 }
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001596
1597 // Update which NodeSet is the current one.
1598 PrevSet = CurrSet;
1599 }
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001600}
1601
Ted Kremenek667cacb2008-04-15 23:06:53 +00001602//===----------------------------------------------------------------------===//
1603// Transfer function: Function calls.
1604//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +00001605
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001606namespace {
1607class CallExprWLItem {
1608public:
1609 CallExpr::arg_iterator I;
1610 ExplodedNode *N;
1611
1612 CallExprWLItem(const CallExpr::arg_iterator &i, ExplodedNode *n)
1613 : I(i), N(n) {}
1614};
1615} // end anonymous namespace
1616
Zhongxing Xu107f7592009-08-06 12:48:26 +00001617void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001618 CallExpr::arg_iterator AI,
1619 CallExpr::arg_iterator AE,
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001620 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001621
Douglas Gregor6b754842008-10-28 00:22:11 +00001622 // Determine the type of function we're calling (if available).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001623 const FunctionProtoType *Proto = NULL;
Douglas Gregor6b754842008-10-28 00:22:11 +00001624 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001625 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
John McCall9dd450b2009-09-21 23:43:11 +00001626 Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>();
Douglas Gregor6b754842008-10-28 00:22:11 +00001627
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001628 // Create a worklist to process the arguments.
1629 llvm::SmallVector<CallExprWLItem, 20> WorkList;
1630 WorkList.reserve(AE - AI);
1631 WorkList.push_back(CallExprWLItem(AI, Pred));
1632
1633 ExplodedNodeSet ArgsEvaluated;
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001634
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001635 while (!WorkList.empty()) {
1636 CallExprWLItem Item = WorkList.back();
1637 WorkList.pop_back();
1638
1639 if (Item.I == AE) {
1640 ArgsEvaluated.insert(Item.N);
1641 continue;
1642 }
1643
1644 // Evaluate the argument.
1645 ExplodedNodeSet Tmp;
1646 const unsigned ParamIdx = Item.I - AI;
1647
Douglas Gregor6b754842008-10-28 00:22:11 +00001648 bool VisitAsLvalue = false;
1649 if (Proto && ParamIdx < Proto->getNumArgs())
1650 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001651
Douglas Gregor6b754842008-10-28 00:22:11 +00001652 if (VisitAsLvalue)
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001653 VisitLValue(*Item.I, Item.N, Tmp);
Douglas Gregor6b754842008-10-28 00:22:11 +00001654 else
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001655 Visit(*Item.I, Item.N, Tmp);
1656
1657 // Enqueue evaluating the next argument on the worklist.
1658 ++(Item.I);
Mike Stump11289f42009-09-09 15:08:12 +00001659
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001660 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
1661 WorkList.push_back(CallExprWLItem(Item.I, *NI));
Ted Kremeneke0188e62008-02-19 01:44:53 +00001662 }
1663
Ted Kremenek48af0e02009-12-17 20:10:17 +00001664 // Now process the call itself.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001665 ExplodedNodeSet DstTmp;
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001666 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001667
1668 for (ExplodedNodeSet::iterator NI=ArgsEvaluated.begin(),
Ted Kremenek48af0e02009-12-17 20:10:17 +00001669 NE=ArgsEvaluated.end(); NI != NE; ++NI) {
1670 // Evaluate the callee.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001671 ExplodedNodeSet DstTmp2;
Ted Kremenek48af0e02009-12-17 20:10:17 +00001672 Visit(Callee, *NI, DstTmp2);
Ted Kremenek49513cc2009-07-22 21:43:51 +00001673 // Perform the previsit of the CallExpr, storing the results in DstTmp.
1674 CheckerVisit(CE, DstTmp, DstTmp2, true);
1675 }
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001676
1677 // Finally, evaluate the function call. We try each of the checkers
1678 // to see if the can evaluate the function call.
Ted Kremenek32c32892009-12-09 02:45:41 +00001679 ExplodedNodeSet DstTmp3;
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001680
Ted Kremenek32c32892009-12-09 02:45:41 +00001681
Mike Stump11289f42009-09-09 15:08:12 +00001682 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001683 DI != DE; ++DI) {
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001684
Ted Kremenek17d541d2009-02-13 01:45:31 +00001685 const GRState* state = GetState(*DI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001686 SVal L = state->getSVal(Callee);
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001687
Ted Kremenek8efd6b4e2008-03-03 16:47:31 +00001688 // FIXME: Add support for symbolic function calls (calls involving
1689 // function pointer values that are symbolic).
Ted Kremenek70342362008-03-05 21:15:02 +00001690 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek32c32892009-12-09 02:45:41 +00001691 ExplodedNodeSet DstChecker;
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001692
Zhongxing Xu175447f2009-12-07 09:17:35 +00001693 // If the callee is processed by a checker, skip the rest logic.
1694 if (CheckerEvalCall(CE, DstChecker, *DI))
Zhongxing Xud1dee7e2009-12-09 05:48:53 +00001695 DstTmp3.insert(DstChecker);
Zhongxing Xu175447f2009-12-07 09:17:35 +00001696 else {
Ted Kremenek32c32892009-12-09 02:45:41 +00001697 for (ExplodedNodeSet::iterator DI_Checker = DstChecker.begin(),
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001698 DE_Checker = DstChecker.end();
1699 DI_Checker != DE_Checker; ++DI_Checker) {
1700
1701 // Dispatch to the plug-in transfer function.
Ted Kremenek32c32892009-12-09 02:45:41 +00001702 unsigned OldSize = DstTmp3.size();
1703 SaveOr OldHasGen(Builder->HasGeneratedNode);
1704 Pred = *DI_Checker;
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001705
Ted Kremenek32c32892009-12-09 02:45:41 +00001706 // Dispatch to transfer function logic to handle the call itself.
1707 // FIXME: Allow us to chain together transfer functions.
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001708 assert(Builder && "GRStmtNodeBuilder must be defined.");
Zhongxing Xu1042bf42009-12-09 12:23:28 +00001709 getTF().EvalCall(DstTmp3, *this, *Builder, CE, L, Pred);
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001710
Ted Kremenek32c32892009-12-09 02:45:41 +00001711 // Handle the case where no nodes where generated. Auto-generate that
1712 // contains the updated state if we aren't generating sinks.
1713 if (!Builder->BuildSinks && DstTmp3.size() == OldSize &&
1714 !Builder->HasGeneratedNode)
1715 MakeNode(DstTmp3, CE, Pred, state);
1716 }
Zhongxing Xu175447f2009-12-07 09:17:35 +00001717 }
Ted Kremeneke0188e62008-02-19 01:44:53 +00001718 }
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001719
1720 // Finally, perform the post-condition check of the CallExpr and store
1721 // the created nodes in 'Dst'.
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001722
1723 if (!(!asLValue && CalleeReturnsReference(CE))) {
1724 CheckerVisit(CE, Dst, DstTmp3, false);
1725 return;
1726 }
1727
1728 // Handle the case where the called function returns a reference but
1729 // we expect an rvalue. For such cases, convert the reference to
1730 // an rvalue.
1731 // FIXME: This conversion doesn't actually happen unless the result
1732 // of CallExpr is consumed by another expression.
1733 ExplodedNodeSet DstTmp4;
1734 CheckerVisit(CE, DstTmp4, DstTmp3, false);
1735 QualType LoadTy = CE->getType();
1736
1737 static int *ConvertToRvalueTag = 0;
1738 for (ExplodedNodeSet::iterator NI = DstTmp4.begin(), NE = DstTmp4.end();
1739 NI!=NE; ++NI) {
1740 const GRState *state = GetState(*NI);
1741 EvalLoad(Dst, CE, *NI, state, state->getSVal(CE),
1742 &ConvertToRvalueTag, LoadTy);
1743 }
Ted Kremeneke0188e62008-02-19 01:44:53 +00001744}
1745
Ted Kremenek667cacb2008-04-15 23:06:53 +00001746//===----------------------------------------------------------------------===//
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001747// Transfer function: Objective-C ivar references.
1748//===----------------------------------------------------------------------===//
1749
Ted Kremenek111a6bd2009-02-28 20:50:43 +00001750static std::pair<const void*,const void*> EagerlyAssumeTag
1751 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1752
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001753void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
1754 Expr *Ex) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001755 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1756 ExplodedNode *Pred = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001757
Ted Kremenekff290ca2009-02-25 23:32:10 +00001758 // Test if the previous node was as the same expression. This can happen
1759 // when the expression fails to evaluate to anything meaningful and
1760 // (as an optimization) we don't generate a node.
Mike Stump11289f42009-09-09 15:08:12 +00001761 ProgramPoint P = Pred->getLocation();
Ted Kremenekff290ca2009-02-25 23:32:10 +00001762 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump11289f42009-09-09 15:08:12 +00001763 Dst.Add(Pred);
Ted Kremenekff290ca2009-02-25 23:32:10 +00001764 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001765 }
Ted Kremenekff290ca2009-02-25 23:32:10 +00001766
Mike Stump11289f42009-09-09 15:08:12 +00001767 const GRState* state = Pred->getState();
1768 SVal V = state->getSVal(Ex);
Ted Kremenek7020eae2009-09-11 22:07:28 +00001769 if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) {
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001770 // First assume that the condition is true.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001771 if (const GRState *stateTrue = state->Assume(*SEV, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00001772 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001773 ValMgr.makeIntVal(1U, Ex->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001774 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xue1190f72009-08-15 03:17:38 +00001775 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001776 stateTrue, Pred));
1777 }
Mike Stump11289f42009-09-09 15:08:12 +00001778
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001779 // Next, assume that the condition is false.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001780 if (const GRState *stateFalse = state->Assume(*SEV, false)) {
Mike Stump11289f42009-09-09 15:08:12 +00001781 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001782 ValMgr.makeIntVal(0U, Ex->getType()));
Zhongxing Xue1190f72009-08-15 03:17:38 +00001783 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
1784 Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001785 stateFalse, Pred));
1786 }
1787 }
1788 else
1789 Dst.Add(Pred);
1790 }
1791}
1792
1793//===----------------------------------------------------------------------===//
1794// Transfer function: Objective-C ivar references.
1795//===----------------------------------------------------------------------===//
1796
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001797void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, ExplodedNode* Pred,
1798 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001799
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001800 Expr* Base = cast<Expr>(Ex->getBase());
Zhongxing Xu107f7592009-08-06 12:48:26 +00001801 ExplodedNodeSet Tmp;
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001802 Visit(Base, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001803
Zhongxing Xu107f7592009-08-06 12:48:26 +00001804 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001805 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001806 SVal BaseVal = state->getSVal(Base);
1807 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Mike Stump11289f42009-09-09 15:08:12 +00001808
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001809 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001810 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001811 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001812 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001813 }
1814}
1815
1816//===----------------------------------------------------------------------===//
Ted Kremenek17810802008-11-12 19:24:17 +00001817// Transfer function: Objective-C fast enumeration 'for' statements.
1818//===----------------------------------------------------------------------===//
1819
1820void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001821 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001822
Ted Kremenek17810802008-11-12 19:24:17 +00001823 // ObjCForCollectionStmts are processed in two places. This method
1824 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1825 // statements within a basic block. This transfer function does two things:
1826 //
1827 // (1) binds the next container value to 'element'. This creates a new
1828 // node in the ExplodedGraph.
1829 //
1830 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1831 // whether or not the container has any more elements. This value
1832 // will be tested in ProcessBranch. We need to explicitly bind
1833 // this value because a container can contain nil elements.
Mike Stump11289f42009-09-09 15:08:12 +00001834 //
Ted Kremenek17810802008-11-12 19:24:17 +00001835 // FIXME: Eventually this logic should actually do dispatches to
1836 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1837 // This will require simulating a temporary NSFastEnumerationState, either
1838 // through an SVal or through the use of MemRegions. This value can
1839 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1840 // terminates we reclaim the temporary (it goes out of scope) and we
1841 // we can test if the SVal is 0 or if the MemRegion is null (depending
1842 // on what approach we take).
1843 //
1844 // For now: simulate (1) by assigning either a symbol or nil if the
1845 // container is empty. Thus this transfer function will by default
1846 // result in state splitting.
Mike Stump11289f42009-09-09 15:08:12 +00001847
Ted Kremenek537f6382008-11-14 19:47:18 +00001848 Stmt* elem = S->getElement();
1849 SVal ElementV;
Mike Stump11289f42009-09-09 15:08:12 +00001850
Ted Kremenek17810802008-11-12 19:24:17 +00001851 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner529efc72009-03-28 06:33:19 +00001852 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek17810802008-11-12 19:24:17 +00001853 assert (ElemD->getInit() == 0);
Ted Kremenek14536f62009-08-21 22:28:32 +00001854 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek537f6382008-11-14 19:47:18 +00001855 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1856 return;
Ted Kremenek17810802008-11-12 19:24:17 +00001857 }
Ted Kremenek537f6382008-11-14 19:47:18 +00001858
Zhongxing Xu107f7592009-08-06 12:48:26 +00001859 ExplodedNodeSet Tmp;
Ted Kremenek537f6382008-11-14 19:47:18 +00001860 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001861
Zhongxing Xu107f7592009-08-06 12:48:26 +00001862 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001863 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001864 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek537f6382008-11-14 19:47:18 +00001865 }
1866}
1867
1868void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001869 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek537f6382008-11-14 19:47:18 +00001870 SVal ElementV) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001871
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001872 // Check if the location we are writing back to is a null pointer.
Ted Kremenek537f6382008-11-14 19:47:18 +00001873 Stmt* elem = S->getElement();
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001874 ExplodedNodeSet Tmp;
1875 EvalLocation(Tmp, elem, Pred, GetState(Pred), ElementV, NULL, false);
1876
1877 if (Tmp.empty())
Ted Kremenek537f6382008-11-14 19:47:18 +00001878 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001879
1880 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1881 Pred = *NI;
1882 const GRState *state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001883
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001884 // Handle the case where the container still has elements.
1885 SVal TrueV = ValMgr.makeTruthVal(1);
1886 const GRState *hasElems = state->BindExpr(S, TrueV);
Ted Kremenek537f6382008-11-14 19:47:18 +00001887
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001888 // Handle the case where the container has no elements.
1889 SVal FalseV = ValMgr.makeTruthVal(0);
1890 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump11289f42009-09-09 15:08:12 +00001891
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001892 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1893 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1894 // FIXME: The proper thing to do is to really iterate over the
1895 // container. We will do this with dispatch logic to the store.
1896 // For now, just 'conjure' up a symbolic value.
1897 QualType T = R->getValueType(getContext());
1898 assert(Loc::IsLocType(T));
1899 unsigned Count = Builder->getCurrentBlockCount();
1900 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1901 SVal V = ValMgr.makeLoc(Sym);
1902 hasElems = hasElems->bindLoc(ElementV, V);
Mike Stump11289f42009-09-09 15:08:12 +00001903
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001904 // Bind the location to 'nil' on the false branch.
1905 SVal nilV = ValMgr.makeIntVal(0, T);
1906 noElems = noElems->bindLoc(ElementV, nilV);
1907 }
Ted Kremenekdf317922008-11-12 21:12:46 +00001908
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001909 // Create the new nodes.
1910 MakeNode(Dst, S, Pred, hasElems);
1911 MakeNode(Dst, S, Pred, noElems);
1912 }
Ted Kremenek17810802008-11-12 19:24:17 +00001913}
1914
1915//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001916// Transfer function: Objective-C message expressions.
1917//===----------------------------------------------------------------------===//
1918
Zhongxing Xu107f7592009-08-06 12:48:26 +00001919void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
Ted Kremeneke19711d2009-12-22 22:13:46 +00001920 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump11289f42009-09-09 15:08:12 +00001921
Ted Kremenek667cacb2008-04-15 23:06:53 +00001922 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
Ted Kremeneke19711d2009-12-22 22:13:46 +00001923 Pred, Dst, asLValue);
Mike Stump11289f42009-09-09 15:08:12 +00001924}
Ted Kremenek667cacb2008-04-15 23:06:53 +00001925
1926void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Ted Kremeneke19711d2009-12-22 22:13:46 +00001927 ObjCMessageExpr::arg_iterator AI,
1928 ObjCMessageExpr::arg_iterator AE,
1929 ExplodedNode* Pred,
1930 ExplodedNodeSet& Dst,
1931 bool asLValue) {
Ted Kremenek667cacb2008-04-15 23:06:53 +00001932 if (AI == AE) {
Mike Stump11289f42009-09-09 15:08:12 +00001933
Ted Kremenek667cacb2008-04-15 23:06:53 +00001934 // Process the receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001935
Ted Kremenek667cacb2008-04-15 23:06:53 +00001936 if (Expr* Receiver = ME->getReceiver()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001937 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001938 Visit(Receiver, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001939
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001940 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE;
1941 ++NI)
Ted Kremeneke19711d2009-12-22 22:13:46 +00001942 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst, asLValue);
Mike Stump11289f42009-09-09 15:08:12 +00001943
Ted Kremenek667cacb2008-04-15 23:06:53 +00001944 return;
1945 }
Mike Stump11289f42009-09-09 15:08:12 +00001946
Ted Kremeneke19711d2009-12-22 22:13:46 +00001947 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst, asLValue);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001948 return;
1949 }
Mike Stump11289f42009-09-09 15:08:12 +00001950
Zhongxing Xu107f7592009-08-06 12:48:26 +00001951 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001952 Visit(*AI, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001953
Ted Kremenek667cacb2008-04-15 23:06:53 +00001954 ++AI;
Mike Stump11289f42009-09-09 15:08:12 +00001955
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001956 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremeneke19711d2009-12-22 22:13:46 +00001957 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst, asLValue);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001958}
1959
1960void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001961 ExplodedNode* Pred,
Ted Kremeneke19711d2009-12-22 22:13:46 +00001962 ExplodedNodeSet& Dst,
1963 bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001964
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001965 // Handle previsits checks.
1966 ExplodedNodeSet Src, DstTmp;
Ted Kremenek005e8a02009-11-24 21:41:28 +00001967 Src.Add(Pred);
1968
Zhongxing Xuaf353292009-12-02 05:49:12 +00001969 CheckerVisit(ME, DstTmp, Src, true);
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001970
Ted Kremeneke19711d2009-12-22 22:13:46 +00001971 ExplodedNodeSet PostVisitSrc;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001972
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001973 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001974 DI!=DE; ++DI) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00001975
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001976 Pred = *DI;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001977 bool RaisesException = false;
Ted Kremeneke19711d2009-12-22 22:13:46 +00001978
1979 unsigned OldSize = PostVisitSrc.size();
1980 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1981 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001982
Zhongxing Xuaf353292009-12-02 05:49:12 +00001983 if (const Expr *Receiver = ME->getReceiver()) {
1984 const GRState *state = Pred->getState();
1985
1986 // Bifurcate the state into nil and non-nil ones.
1987 DefinedOrUnknownSVal receiverVal =
1988 cast<DefinedOrUnknownSVal>(state->getSVal(Receiver));
1989
1990 const GRState *notNilState, *nilState;
1991 llvm::tie(notNilState, nilState) = state->Assume(receiverVal);
1992
1993 // There are three cases: can be nil or non-nil, must be nil, must be
1994 // non-nil. We handle must be nil, and merge the rest two into non-nil.
1995 if (nilState && !notNilState) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00001996 CheckerEvalNilReceiver(ME, PostVisitSrc, nilState, Pred);
1997 continue;
Zhongxing Xuaf353292009-12-02 05:49:12 +00001998 }
1999
2000 assert(notNilState);
2001
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002002 // Check if the "raise" message was sent.
2003 if (ME->getSelector() == RaiseSel)
2004 RaisesException = true;
Zhongxing Xuaf353292009-12-02 05:49:12 +00002005
2006 // Check if we raise an exception. For now treat these as sinks.
2007 // Eventually we will want to handle exceptions properly.
Zhongxing Xuaf353292009-12-02 05:49:12 +00002008 if (RaisesException)
2009 Builder->BuildSinks = true;
2010
2011 // Dispatch to plug-in transfer function.
Ted Kremeneke19711d2009-12-22 22:13:46 +00002012 EvalObjCMessageExpr(PostVisitSrc, ME, Pred, notNilState);
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002013 }
2014 else {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002015 IdentifierInfo* ClsName = ME->getClassName();
2016 Selector S = ME->getSelector();
2017
2018 // Check for special instance methods.
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002019 if (!NSExceptionII) {
2020 ASTContext& Ctx = getContext();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002021 NSExceptionII = &Ctx.Idents.get("NSException");
2022 }
2023
2024 if (ClsName == NSExceptionII) {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002025 enum { NUM_RAISE_SELECTORS = 2 };
2026
2027 // Lazily create a cache of the selectors.
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002028 if (!NSExceptionInstanceRaiseSelectors) {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002029 ASTContext& Ctx = getContext();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002030 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002031 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
2032 unsigned idx = 0;
2033
2034 // raise:format:
2035 II.push_back(&Ctx.Idents.get("raise"));
2036 II.push_back(&Ctx.Idents.get("format"));
2037 NSExceptionInstanceRaiseSelectors[idx++] =
2038 Ctx.Selectors.getSelector(II.size(), &II[0]);
2039
2040 // raise:format::arguments:
2041 II.push_back(&Ctx.Idents.get("arguments"));
2042 NSExceptionInstanceRaiseSelectors[idx++] =
2043 Ctx.Selectors.getSelector(II.size(), &II[0]);
2044 }
2045
2046 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2047 if (S == NSExceptionInstanceRaiseSelectors[i]) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00002048 RaisesException = true;
2049 break;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002050 }
2051 }
Zhongxing Xuaf353292009-12-02 05:49:12 +00002052
2053 // Check if we raise an exception. For now treat these as sinks.
2054 // Eventually we will want to handle exceptions properly.
Zhongxing Xuaf353292009-12-02 05:49:12 +00002055 if (RaisesException)
2056 Builder->BuildSinks = true;
2057
2058 // Dispatch to plug-in transfer function.
Ted Kremeneke19711d2009-12-22 22:13:46 +00002059 EvalObjCMessageExpr(PostVisitSrc, ME, Pred, Builder->GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002060 }
Ted Kremeneke19711d2009-12-22 22:13:46 +00002061
2062 // Handle the case where no nodes where generated. Auto-generate that
2063 // contains the updated state if we aren't generating sinks.
2064 if (!Builder->BuildSinks && PostVisitSrc.size() == OldSize &&
2065 !Builder->HasGeneratedNode)
2066 MakeNode(PostVisitSrc, ME, Pred, GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002067 }
Mike Stump11289f42009-09-09 15:08:12 +00002068
Ted Kremeneke19711d2009-12-22 22:13:46 +00002069 // Finally, perform the post-condition check of the ObjCMessageExpr and store
2070 // the created nodes in 'Dst'.
2071 if (!(!asLValue && ReceiverReturnsReference(ME))) {
2072 CheckerVisit(ME, Dst, PostVisitSrc, false);
2073 return;
2074 }
2075
2076 // Handle the case where the message expression returns a reference but
2077 // we expect an rvalue. For such cases, convert the reference to
2078 // an rvalue.
2079 // FIXME: This conversion doesn't actually happen unless the result
2080 // of ObjCMessageExpr is consumed by another expression.
2081 ExplodedNodeSet DstRValueConvert;
2082 CheckerVisit(ME, DstRValueConvert, PostVisitSrc, false);
2083 QualType LoadTy = ME->getType();
2084
2085 static int *ConvertToRvalueTag = 0;
2086 for (ExplodedNodeSet::iterator NI = DstRValueConvert.begin(),
2087 NE = DstRValueConvert.end();
2088 NI!=NE; ++NI) {
2089 const GRState *state = GetState(*NI);
2090 EvalLoad(Dst, ME, *NI, state, state->getSVal(ME),
2091 &ConvertToRvalueTag, LoadTy);
2092 }
Ted Kremenek667cacb2008-04-15 23:06:53 +00002093}
2094
2095//===----------------------------------------------------------------------===//
2096// Transfer functions: Miscellaneous statements.
2097//===----------------------------------------------------------------------===//
2098
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002099void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002100 ExplodedNodeSet& Dst, bool asLValue){
Zhongxing Xu107f7592009-08-06 12:48:26 +00002101 ExplodedNodeSet S1;
Ted Kremenek9fd25312008-02-19 18:52:54 +00002102 QualType T = CastE->getType();
Zhongxing Xudab76fd2008-10-21 06:54:23 +00002103 QualType ExTy = Ex->getType();
Zhongxing Xuc2721522008-10-22 08:02:16 +00002104
Zhongxing Xu4de1c852008-10-31 07:26:14 +00002105 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregore200adc2008-10-27 19:41:14 +00002106 T = ExCast->getTypeAsWritten();
2107
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002108 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType() ||
2109 asLValue)
Zhongxing Xu232c7922008-10-16 06:09:51 +00002110 VisitLValue(Ex, Pred, S1);
Ted Kremenek9f3f8272008-03-04 22:16:08 +00002111 else
2112 Visit(Ex, Pred, S1);
Mike Stump11289f42009-09-09 15:08:12 +00002113
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002114 ExplodedNodeSet S2;
2115 CheckerVisit(CastE, S2, S1, true);
2116
Ted Kremenekeccf3e52008-04-22 21:10:18 +00002117 // Check for casting to "void".
Mike Stump11289f42009-09-09 15:08:12 +00002118 if (T->isVoidType()) {
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002119 assert(!asLValue);
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002120 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I)
2121 Dst.Add(*I);
Ted Kremenek33d82852008-01-24 02:02:54 +00002122 return;
2123 }
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002124
2125 // If we are evaluating the cast in an lvalue context, we implicitly want
2126 // the cast to evaluate to a location.
2127 if (asLValue) {
2128 ASTContext &Ctx = getContext();
2129 T = Ctx.getPointerType(Ctx.getCanonicalType(T));
Ted Kremenek343b5122009-12-23 01:19:20 +00002130 ExTy = Ctx.getPointerType(Ctx.getCanonicalType(ExTy));
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002131 }
Ted Kremenekac7c7242009-07-21 21:03:30 +00002132
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002133 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2134 ExplodedNode* N = *I;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002135 const GRState* state = GetState(N);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002136 SVal V = state->getSVal(Ex);
Ted Kremenekac7c7242009-07-21 21:03:30 +00002137 const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002138 state = Res.getState()->BindExpr(CastE, Res.getSVal());
Ted Kremenekac7c7242009-07-21 21:03:30 +00002139 MakeNode(Dst, CastE, N, state);
Ted Kremenek33d82852008-01-24 02:02:54 +00002140 }
Ted Kremenek05352742008-01-24 20:55:43 +00002141}
2142
Ted Kremenekbf263682008-10-27 21:54:31 +00002143void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Mike Stump11289f42009-09-09 15:08:12 +00002144 ExplodedNode* Pred,
2145 ExplodedNodeSet& Dst,
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002146 bool asLValue) {
Ted Kremenekbf263682008-10-27 21:54:31 +00002147 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002148 ExplodedNodeSet Tmp;
Ted Kremenekbf263682008-10-27 21:54:31 +00002149 Visit(ILE, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002150
Zhongxing Xu107f7592009-08-06 12:48:26 +00002151 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002152 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002153 SVal ILV = state->getSVal(ILE);
Ted Kremenek04af9f22009-12-07 22:05:27 +00002154 const LocationContext *LC = (*I)->getLocationContext();
2155 state = state->bindCompoundLiteral(CL, LC, ILV);
Ted Kremenekbf263682008-10-27 21:54:31 +00002156
Ted Kremenek04af9f22009-12-07 22:05:27 +00002157 if (asLValue) {
2158 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL, LC)));
2159 }
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002160 else
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002161 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenekbf263682008-10-27 21:54:31 +00002162 }
2163}
2164
Ted Kremenek14536f62009-08-21 22:28:32 +00002165void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +00002166 ExplodedNodeSet& Dst) {
Ted Kremenek3b427152008-04-22 22:25:27 +00002167
Mike Stump11289f42009-09-09 15:08:12 +00002168 // The CFG has one DeclStmt per Decl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002169 Decl* D = *DS->decl_begin();
Mike Stump11289f42009-09-09 15:08:12 +00002170
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002171 if (!D || !isa<VarDecl>(D))
Ted Kremenek3b427152008-04-22 22:25:27 +00002172 return;
Mike Stump11289f42009-09-09 15:08:12 +00002173
2174 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek17810802008-11-12 19:24:17 +00002175 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek3b427152008-04-22 22:25:27 +00002176
2177 // FIXME: static variables may have an initializer, but the second
2178 // time a function is called those values may not be current.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002179 ExplodedNodeSet Tmp;
Ted Kremenek3b427152008-04-22 22:25:27 +00002180
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002181 if (InitEx) {
2182 if (VD->getType()->isReferenceType())
2183 VisitLValue(InitEx, Pred, Tmp);
2184 else
2185 Visit(InitEx, Pred, Tmp);
2186 }
Ted Kremenekfc311292009-07-17 23:48:26 +00002187 else
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002188 Tmp.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002189
Ted Kremenekae3361d2009-11-07 03:56:57 +00002190 ExplodedNodeSet Tmp2;
2191 CheckerVisit(DS, Tmp2, Tmp, true);
2192
2193 for (ExplodedNodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002194 ExplodedNode *N = *I;
Ted Kremenekae3361d2009-11-07 03:56:57 +00002195 const GRState *state = GetState(N);
Zhongxing Xu27fee832009-11-03 12:13:38 +00002196
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00002197 // Decls without InitExpr are not initialized explicitly.
Zhongxing Xu27fee832009-11-03 12:13:38 +00002198 const LocationContext *LC = N->getLocationContext();
Ted Kremenek14536f62009-08-21 22:28:32 +00002199
Ted Kremenek17810802008-11-12 19:24:17 +00002200 if (InitEx) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002201 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek17810802008-11-12 19:24:17 +00002202 QualType T = VD->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002203
Ted Kremenek17810802008-11-12 19:24:17 +00002204 // Recover some path-sensitivity if a scalar value evaluated to
2205 // UnknownVal.
Mike Stump11289f42009-09-09 15:08:12 +00002206 if (InitVal.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002207 !getConstraintManager().canReasonAbout(InitVal)) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002208 InitVal = ValMgr.getConjuredSymbolVal(NULL, InitEx,
2209 Builder->getCurrentBlockCount());
Mike Stump11289f42009-09-09 15:08:12 +00002210 }
Ted Kremenekb006b822009-11-04 00:09:15 +00002211
Ted Kremenek209e31b2009-11-05 00:42:23 +00002212 EvalBind(Dst, DS, DS, *I, state,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002213 loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true);
Mike Stump11289f42009-09-09 15:08:12 +00002214 }
Ted Kremenek13363532009-02-14 01:54:57 +00002215 else {
Ted Kremenekb006b822009-11-04 00:09:15 +00002216 state = state->bindDeclWithNoInit(state->getRegion(VD, LC));
Ted Kremenek13363532009-02-14 01:54:57 +00002217 MakeNode(Dst, DS, *I, state);
Ted Kremenek8f7afdd2008-12-08 22:47:34 +00002218 }
Ted Kremenek3b427152008-04-22 22:25:27 +00002219 }
Ted Kremenek05352742008-01-24 20:55:43 +00002220}
Ted Kremenek33d82852008-01-24 02:02:54 +00002221
Ted Kremenekf68bf632008-10-30 17:47:32 +00002222namespace {
2223 // This class is used by VisitInitListExpr as an item in a worklist
2224 // for processing the values contained in an InitListExpr.
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002225class InitListWLItem {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002226public:
2227 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu107f7592009-08-06 12:48:26 +00002228 ExplodedNode* N;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002229 InitListExpr::reverse_iterator Itr;
Mike Stump11289f42009-09-09 15:08:12 +00002230
Zhongxing Xu107f7592009-08-06 12:48:26 +00002231 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
2232 InitListExpr::reverse_iterator itr)
Ted Kremenekf68bf632008-10-30 17:47:32 +00002233 : Vals(vals), N(n), Itr(itr) {}
2234};
2235}
2236
2237
Mike Stump11289f42009-09-09 15:08:12 +00002238void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002239 ExplodedNodeSet& Dst) {
Ted Kremenek828e6df2008-10-30 23:14:36 +00002240
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002241 const GRState* state = GetState(Pred);
Ted Kremenek45698bf2008-11-13 05:05:34 +00002242 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002243 unsigned NumInitElements = E->getNumInits();
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002244
Ted Kremeneka41d9dd2009-07-28 20:46:55 +00002245 if (T->isArrayType() || T->isStructureType() ||
2246 T->isUnionType() || T->isVectorType()) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002247
Ted Kremenek828e6df2008-10-30 23:14:36 +00002248 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump11289f42009-09-09 15:08:12 +00002249
Ted Kremenek828e6df2008-10-30 23:14:36 +00002250 // Handle base case where the initializer has no elements.
2251 // e.g: static int* myArray[] = {};
2252 if (NumInitElements == 0) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002253 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002254 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremenek828e6df2008-10-30 23:14:36 +00002255 return;
Mike Stump11289f42009-09-09 15:08:12 +00002256 }
2257
Ted Kremenek828e6df2008-10-30 23:14:36 +00002258 // Create a worklist to process the initializers.
2259 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump11289f42009-09-09 15:08:12 +00002260 WorkList.reserve(NumInitElements);
2261 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002262 InitListExpr::reverse_iterator ItrEnd = E->rend();
Ted Kremenek30030012009-09-22 21:19:14 +00002263 assert(!(E->rbegin() == E->rend()));
Mike Stump11289f42009-09-09 15:08:12 +00002264
Ted Kremenek828e6df2008-10-30 23:14:36 +00002265 // Process the worklist until it is empty.
Ted Kremenekf68bf632008-10-30 17:47:32 +00002266 while (!WorkList.empty()) {
2267 InitListWLItem X = WorkList.back();
2268 WorkList.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +00002269
Zhongxing Xu107f7592009-08-06 12:48:26 +00002270 ExplodedNodeSet Tmp;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002271 Visit(*X.Itr, X.N, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002272
Ted Kremenekf68bf632008-10-30 17:47:32 +00002273 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002274
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002275 for (ExplodedNodeSet::iterator NI=Tmp.begin(),NE=Tmp.end();NI!=NE;++NI) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002276 // Get the last initializer value.
2277 state = GetState(*NI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002278 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump11289f42009-09-09 15:08:12 +00002279
Ted Kremenekf68bf632008-10-30 17:47:32 +00002280 // Construct the new list of values by prepending the new value to
2281 // the already constructed list.
2282 llvm::ImmutableList<SVal> NewVals =
2283 getBasicVals().consVals(InitV, X.Vals);
Mike Stump11289f42009-09-09 15:08:12 +00002284
Ted Kremenekf68bf632008-10-30 17:47:32 +00002285 if (NewItr == ItrEnd) {
Zhongxing Xu121a53a2008-10-31 03:01:26 +00002286 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002287 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002288
Ted Kremenekf68bf632008-10-30 17:47:32 +00002289 // Make final state and node.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002290 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002291 }
2292 else {
2293 // Still some initializer values to go. Push them onto the worklist.
2294 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2295 }
2296 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002297 }
Mike Stump11289f42009-09-09 15:08:12 +00002298
Ted Kremenek28f41ba2008-10-30 18:34:31 +00002299 return;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002300 }
2301
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002302 if (Loc::IsLocType(T) || T->isIntegerType()) {
2303 assert (E->getNumInits() == 1);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002304 ExplodedNodeSet Tmp;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002305 Expr* Init = E->getInit(0);
2306 Visit(Init, Pred, Tmp);
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002307 for (ExplodedNodeSet::iterator I=Tmp.begin(), EI=Tmp.end(); I != EI; ++I) {
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002308 state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002309 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002310 }
2311 return;
2312 }
2313
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002314 assert(0 && "unprocessed InitListExpr type");
2315}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00002316
Sebastian Redl6f282892008-11-11 17:56:53 +00002317/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2318void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002319 ExplodedNode* Pred,
2320 ExplodedNodeSet& Dst) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002321 QualType T = Ex->getTypeOfArgument();
Mike Stump11289f42009-09-09 15:08:12 +00002322 uint64_t amt;
2323
Ted Kremenekae5b7862008-03-15 03:13:20 +00002324 if (Ex->isSizeOf()) {
Mike Stump11289f42009-09-09 15:08:12 +00002325 if (T == getContext().VoidTy) {
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002326 // sizeof(void) == 1 byte.
2327 amt = 1;
2328 }
2329 else if (!T.getTypePtr()->isConstantSizeType()) {
2330 // FIXME: Add support for VLAs.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002331 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002332 }
2333 else if (T->isObjCInterfaceType()) {
2334 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2335 // the compiler has laid out its representation. Just report Unknown
Mike Stump11289f42009-09-09 15:08:12 +00002336 // for these.
Ted Kremenek99057462008-04-30 21:31:12 +00002337 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002338 }
2339 else {
2340 // All other cases.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002341 amt = getContext().getTypeSize(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002342 }
Ted Kremenekae5b7862008-03-15 03:13:20 +00002343 }
2344 else // Get alignment of the type.
Ted Kremenek88ba7502008-03-15 03:13:55 +00002345 amt = getContext().getTypeAlign(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002346
Ted Kremenek181f7232008-03-21 21:30:14 +00002347 MakeNode(Dst, Ex, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002348 GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenek002bf742008-02-12 19:49:57 +00002349}
2350
Ted Kremenekb597bb92008-02-20 04:02:35 +00002351
Zhongxing Xu107f7592009-08-06 12:48:26 +00002352void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
2353 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002354
Ted Kremenekb597bb92008-02-20 04:02:35 +00002355 switch (U->getOpcode()) {
Mike Stump11289f42009-09-09 15:08:12 +00002356
Ted Kremenekb597bb92008-02-20 04:02:35 +00002357 default:
Ted Kremenekb597bb92008-02-20 04:02:35 +00002358 break;
Mike Stump11289f42009-09-09 15:08:12 +00002359
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002360 case UnaryOperator::Deref: {
Mike Stump11289f42009-09-09 15:08:12 +00002361
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002362 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002363 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002364 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002365
Zhongxing Xu107f7592009-08-06 12:48:26 +00002366 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002367
Ted Kremenek17d541d2009-02-13 01:45:31 +00002368 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002369 SVal location = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002370
Zhongxing Xu232c7922008-10-16 06:09:51 +00002371 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002372 MakeNode(Dst, U, *I, state->BindExpr(U, location),
Ted Kremeneka6e08322009-05-07 18:27:16 +00002373 ProgramPoint::PostLValueKind);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002374 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00002375 EvalLoad(Dst, U, *I, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002376 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002377
2378 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002379 }
Mike Stump11289f42009-09-09 15:08:12 +00002380
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002381 case UnaryOperator::Real: {
Mike Stump11289f42009-09-09 15:08:12 +00002382
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002383 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002384 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002385 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002386
Zhongxing Xu107f7592009-08-06 12:48:26 +00002387 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002388
Zhongxing Xu27f17422008-10-17 05:57:07 +00002389 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002390 if (Ex->getType()->isAnyComplexType()) {
2391 // Just report "Unknown."
2392 Dst.Add(*I);
2393 continue;
2394 }
Mike Stump11289f42009-09-09 15:08:12 +00002395
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002396 // For all other types, UnaryOperator::Real is an identity operation.
2397 assert (U->getType() == Ex->getType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002398 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002399 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump11289f42009-09-09 15:08:12 +00002400 }
2401
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002402 return;
2403 }
Mike Stump11289f42009-09-09 15:08:12 +00002404
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002405 case UnaryOperator::Imag: {
Mike Stump11289f42009-09-09 15:08:12 +00002406
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002407 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002408 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002409 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002410
Zhongxing Xu107f7592009-08-06 12:48:26 +00002411 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu27f17422008-10-17 05:57:07 +00002412 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002413 if (Ex->getType()->isAnyComplexType()) {
2414 // Just report "Unknown."
2415 Dst.Add(*I);
2416 continue;
2417 }
Mike Stump11289f42009-09-09 15:08:12 +00002418
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002419 // For all other types, UnaryOperator::Float returns 0.
2420 assert (Ex->getType()->isIntegerType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002421 const GRState* state = GetState(*I);
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002422 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002423 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002424 }
Mike Stump11289f42009-09-09 15:08:12 +00002425
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002426 return;
2427 }
Mike Stump11289f42009-09-09 15:08:12 +00002428
Ted Kremenek27347132009-09-15 00:40:32 +00002429 case UnaryOperator::OffsetOf: {
Ted Kremenekb6622942009-09-15 04:19:09 +00002430 Expr::EvalResult Res;
2431 if (U->Evaluate(Res, getContext()) && Res.Val.isInt()) {
2432 const APSInt &IV = Res.Val.getInt();
2433 assert(IV.getBitWidth() == getContext().getTypeSize(U->getType()));
2434 assert(U->getType()->isIntegerType());
2435 assert(IV.isSigned() == U->getType()->isSignedIntegerType());
2436 SVal X = ValMgr.makeIntVal(IV);
2437 MakeNode(Dst, U, Pred, GetState(Pred)->BindExpr(U, X));
2438 return;
2439 }
2440 // FIXME: Handle the case where __builtin_offsetof is not a constant.
2441 Dst.Add(Pred);
Ted Kremenekca67cab2008-04-30 21:45:55 +00002442 return;
Ted Kremenek27347132009-09-15 00:40:32 +00002443 }
Mike Stump11289f42009-09-09 15:08:12 +00002444
Zhongxing Xu232c7922008-10-16 06:09:51 +00002445 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002446 case UnaryOperator::Extension: {
Mike Stump11289f42009-09-09 15:08:12 +00002447
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002448 // Unary "+" is a no-op, similar to a parentheses. We still have places
2449 // where it may be a block-level expression, so we need to
2450 // generate an extra node that just propagates the value of the
2451 // subexpression.
2452
2453 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002454 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002455 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002456
2457 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002458 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002459 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002460 }
Mike Stump11289f42009-09-09 15:08:12 +00002461
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002462 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002463 }
Mike Stump11289f42009-09-09 15:08:12 +00002464
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002465 case UnaryOperator::AddrOf: {
Mike Stump11289f42009-09-09 15:08:12 +00002466
Zhongxing Xu232c7922008-10-16 06:09:51 +00002467 assert(!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002468 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002469 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002470 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002471
2472 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002473 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002474 SVal V = state->getSVal(Ex);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002475 state = state->BindExpr(U, V);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002476 MakeNode(Dst, U, *I, state);
Ted Kremenek7f8ebb72008-02-21 19:15:37 +00002477 }
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002478
Mike Stump11289f42009-09-09 15:08:12 +00002479 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002480 }
Mike Stump11289f42009-09-09 15:08:12 +00002481
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002482 case UnaryOperator::LNot:
2483 case UnaryOperator::Minus:
2484 case UnaryOperator::Not: {
Mike Stump11289f42009-09-09 15:08:12 +00002485
Zhongxing Xu232c7922008-10-16 06:09:51 +00002486 assert (!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002487 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002488 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002489 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002490
2491 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002492 const GRState* state = GetState(*I);
Mike Stump11289f42009-09-09 15:08:12 +00002493
Ted Kremenek76bccf62008-09-30 05:32:44 +00002494 // Get the value of the subexpression.
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002495 SVal V = state->getSVal(Ex);
Ted Kremenek76bccf62008-09-30 05:32:44 +00002496
Ted Kremenek1ca33462008-11-15 00:20:05 +00002497 if (V.isUnknownOrUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002498 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremenek1ca33462008-11-15 00:20:05 +00002499 continue;
2500 }
Mike Stump11289f42009-09-09 15:08:12 +00002501
Ted Kremenek44137142008-11-15 04:01:56 +00002502// QualType DstT = getContext().getCanonicalType(U->getType());
2503// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002504//
Ted Kremenek44137142008-11-15 04:01:56 +00002505// if (DstT != SrcT) // Perform promotions.
Mike Stump11289f42009-09-09 15:08:12 +00002506// V = EvalCast(V, DstT);
2507//
Ted Kremenek44137142008-11-15 04:01:56 +00002508// if (V.isUnknownOrUndef()) {
2509// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2510// continue;
2511// }
Mike Stump11289f42009-09-09 15:08:12 +00002512
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002513 switch (U->getOpcode()) {
2514 default:
2515 assert(false && "Invalid Opcode.");
2516 break;
Mike Stump11289f42009-09-09 15:08:12 +00002517
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002518 case UnaryOperator::Not:
Ted Kremenekd331d092008-10-01 00:21:14 +00002519 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002520 state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002521 break;
2522
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002523 case UnaryOperator::Minus:
Ted Kremenekd331d092008-10-01 00:21:14 +00002524 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002525 state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002526 break;
2527
2528 case UnaryOperator::LNot:
2529
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002530 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2531 //
2532 // Note: technically we do "E == 0", but this is the same in the
2533 // transfer functions as "0 == E".
Ted Kremenek1642bda2009-06-26 00:05:51 +00002534 SVal Result;
Mike Stump11289f42009-09-09 15:08:12 +00002535
Zhongxing Xu27f17422008-10-17 05:57:07 +00002536 if (isa<Loc>(V)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002537 Loc X = ValMgr.makeNull();
Ted Kremenek1642bda2009-06-26 00:05:51 +00002538 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2539 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002540 }
2541 else {
Ted Kremenek44137142008-11-15 04:01:56 +00002542 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenek8ec57712009-10-06 01:39:48 +00002543 Result = EvalBinOp(state, BinaryOperator::EQ, cast<NonLoc>(V), X,
Ted Kremenek1642bda2009-06-26 00:05:51 +00002544 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002545 }
Mike Stump11289f42009-09-09 15:08:12 +00002546
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002547 state = state->BindExpr(U, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002548
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002549 break;
2550 }
Mike Stump11289f42009-09-09 15:08:12 +00002551
Ted Kremenek17d541d2009-02-13 01:45:31 +00002552 MakeNode(Dst, U, *I, state);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002553 }
Mike Stump11289f42009-09-09 15:08:12 +00002554
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002555 return;
2556 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002557 }
2558
2559 // Handle ++ and -- (both pre- and post-increment).
2560
2561 assert (U->isIncrementDecrementOp());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002562 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002563 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu232c7922008-10-16 06:09:51 +00002564 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002565
Zhongxing Xu107f7592009-08-06 12:48:26 +00002566 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002567
Ted Kremenek17d541d2009-02-13 01:45:31 +00002568 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002569 SVal V1 = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002570
2571 // Perform a load.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002572 ExplodedNodeSet Tmp2;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002573 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002574
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002575 for (ExplodedNodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end();I2!=E2;++I2) {
Mike Stump11289f42009-09-09 15:08:12 +00002576
Ted Kremenek17d541d2009-02-13 01:45:31 +00002577 state = GetState(*I2);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002578 SVal V2_untested = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002579
2580 // Propagate unknown and undefined values.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002581 if (V2_untested.isUnknownOrUndef()) {
2582 MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested));
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002583 continue;
Ted Kremenek7020eae2009-09-11 22:07:28 +00002584 }
2585 DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
Mike Stump11289f42009-09-09 15:08:12 +00002586
2587 // Handle all other values.
Ted Kremeneke81734b2008-02-15 22:09:30 +00002588 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2589 : BinaryOperator::Sub;
Ted Kremenek32c41ec2009-03-11 03:54:24 +00002590
Zhongxing Xufe971652009-08-05 02:51:59 +00002591 // If the UnaryOperator has non-location type, use its type to create the
2592 // constant value. If the UnaryOperator has location type, create the
2593 // constant with int type and pointer width.
2594 SVal RHS;
2595
2596 if (U->getType()->isAnyPointerType())
2597 RHS = ValMgr.makeIntValWithPtrWidth(1, false);
2598 else
2599 RHS = ValMgr.makeIntVal(1, U->getType());
2600
Mike Stump11289f42009-09-09 15:08:12 +00002601 SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
2602
Ted Kremenek6b315332009-03-20 20:10:45 +00002603 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002604 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek7020eae2009-09-11 22:07:28 +00002605 DefinedOrUnknownSVal SymVal =
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002606 ValMgr.getConjuredSymbolVal(NULL, Ex,
2607 Builder->getCurrentBlockCount());
Ted Kremenek7020eae2009-09-11 22:07:28 +00002608 Result = SymVal;
Mike Stump11289f42009-09-09 15:08:12 +00002609
Ted Kremenek35f875c2009-04-21 22:38:05 +00002610 // If the value is a location, ++/-- should always preserve
Ted Kremenek1642bda2009-06-26 00:05:51 +00002611 // non-nullness. Check if the original value was non-null, and if so
Mike Stump11289f42009-09-09 15:08:12 +00002612 // propagate that constraint.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002613 if (Loc::IsLocType(U->getType())) {
Ted Kremenek7020eae2009-09-11 22:07:28 +00002614 DefinedOrUnknownSVal Constraint =
2615 SVator.EvalEQ(state, V2, ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002616
Ted Kremenek7020eae2009-09-11 22:07:28 +00002617 if (!state->Assume(Constraint, true)) {
Ted Kremenek35f875c2009-04-21 22:38:05 +00002618 // It isn't feasible for the original value to be null.
2619 // Propagate this constraint.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002620 Constraint = SVator.EvalEQ(state, SymVal,
2621 ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002622
Ted Kremenek7020eae2009-09-11 22:07:28 +00002623
2624 state = state->Assume(Constraint, false);
Ted Kremenekf9906842009-06-18 22:57:13 +00002625 assert(state);
Mike Stump11289f42009-09-09 15:08:12 +00002626 }
2627 }
Ted Kremenek35f875c2009-04-21 22:38:05 +00002628 }
Mike Stump11289f42009-09-09 15:08:12 +00002629
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002630 state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002631
Mike Stump11289f42009-09-09 15:08:12 +00002632 // Perform the store.
Ted Kremenek209e31b2009-11-05 00:42:23 +00002633 EvalStore(Dst, NULL, U, *I2, state, V1, Result);
Ted Kremenek43523e02008-02-07 01:08:27 +00002634 }
Ted Kremenek38213f92008-04-21 23:43:38 +00002635 }
Ted Kremenek43523e02008-02-07 01:08:27 +00002636}
2637
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002638
2639void GRExprEngine::VisitCXXThisExpr(CXXThisExpr *TE, ExplodedNode *Pred,
2640 ExplodedNodeSet & Dst) {
2641 // Get the this object region from StoreManager.
2642 Loc V = getStoreManager().getThisObject(TE->getType()->getPointeeType());
2643 MakeNode(Dst, TE, Pred, GetState(Pred)->BindExpr(TE, V));
2644}
2645
2646void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred,
2647 ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002648 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00002649}
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002650
2651void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2652 AsmStmt::outputs_iterator I,
2653 AsmStmt::outputs_iterator E,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002654 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002655 if (I == E) {
2656 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2657 return;
2658 }
Mike Stump11289f42009-09-09 15:08:12 +00002659
Zhongxing Xu107f7592009-08-06 12:48:26 +00002660 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002661 VisitLValue(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002662
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002663 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002664
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002665 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002666 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2667}
2668
2669void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2670 AsmStmt::inputs_iterator I,
2671 AsmStmt::inputs_iterator E,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002672 ExplodedNode* Pred,
2673 ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002674 if (I == E) {
Mike Stump11289f42009-09-09 15:08:12 +00002675
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002676 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu27f17422008-10-17 05:57:07 +00002677 // should evaluate to Locs. Nuke all of their values.
Mike Stump11289f42009-09-09 15:08:12 +00002678
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002679 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2680 // which interprets the inline asm and stores proper results in the
2681 // outputs.
Mike Stump11289f42009-09-09 15:08:12 +00002682
Ted Kremenek17d541d2009-02-13 01:45:31 +00002683 const GRState* state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002684
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002685 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2686 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump11289f42009-09-09 15:08:12 +00002687
2688 SVal X = state->getSVal(*OI);
Zhongxing Xu27f17422008-10-17 05:57:07 +00002689 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump11289f42009-09-09 15:08:12 +00002690
Zhongxing Xu27f17422008-10-17 05:57:07 +00002691 if (isa<Loc>(X))
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002692 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002693 }
Mike Stump11289f42009-09-09 15:08:12 +00002694
Ted Kremenek17d541d2009-02-13 01:45:31 +00002695 MakeNode(Dst, A, Pred, state);
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002696 return;
2697 }
Mike Stump11289f42009-09-09 15:08:12 +00002698
Zhongxing Xu107f7592009-08-06 12:48:26 +00002699 ExplodedNodeSet Tmp;
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002700 Visit(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002701
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002702 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002703
Ted Kremenek17a02962009-09-03 03:02:58 +00002704 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002705 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2706}
2707
Ted Kremenekbee01e52009-11-06 02:24:13 +00002708void GRExprEngine::VisitReturnStmt(ReturnStmt *RS, ExplodedNode *Pred,
2709 ExplodedNodeSet &Dst) {
2710
2711 ExplodedNodeSet Src;
2712 if (Expr *RetE = RS->getRetValue()) {
2713 Visit(RetE, Pred, Src);
Ted Kremenekf6467742008-03-31 15:02:58 +00002714 }
Ted Kremenekbee01e52009-11-06 02:24:13 +00002715 else {
2716 Src.Add(Pred);
2717 }
2718
2719 ExplodedNodeSet CheckedSet;
2720 CheckerVisit(RS, CheckedSet, Src, true);
2721
2722 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
2723 I != E; ++I) {
Ted Kremenek9c375152008-04-16 23:05:51 +00002724
Ted Kremenekbee01e52009-11-06 02:24:13 +00002725 assert(Builder && "GRStmtNodeBuilder must be defined.");
2726
2727 Pred = *I;
2728 unsigned size = Dst.size();
2729
2730 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2731 SaveOr OldHasGen(Builder->HasGeneratedNode);
2732
2733 getTF().EvalReturn(Dst, *this, *Builder, RS, Pred);
2734
2735 // Handle the case where no nodes where generated.
2736 if (!Builder->BuildSinks && Dst.size() == size &&
2737 !Builder->HasGeneratedNode)
2738 MakeNode(Dst, RS, Pred, GetState(Pred));
Ted Kremenek0b63f962008-11-21 00:27:44 +00002739 }
Ted Kremenekf6467742008-03-31 15:02:58 +00002740}
Ted Kremenek64100da2008-03-25 00:34:37 +00002741
Ted Kremenek667cacb2008-04-15 23:06:53 +00002742//===----------------------------------------------------------------------===//
2743// Transfer functions: Binary operators.
2744//===----------------------------------------------------------------------===//
2745
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002746void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002747 ExplodedNode* Pred,
Zhongxing Xub9eda672009-10-30 07:19:39 +00002748 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002749
Zhongxing Xu107f7592009-08-06 12:48:26 +00002750 ExplodedNodeSet Tmp1;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002751 Expr* LHS = B->getLHS()->IgnoreParens();
2752 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002753
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002754 // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
2755 if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
Mike Stump11289f42009-09-09 15:08:12 +00002756 Visit(RHS, Pred, Dst);
Ted Kremenek69d78b92008-12-06 02:39:30 +00002757 return;
2758 }
Mike Stump11289f42009-09-09 15:08:12 +00002759
Ted Kremenek43523e02008-02-07 01:08:27 +00002760 if (B->isAssignmentOp())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002761 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek43523e02008-02-07 01:08:27 +00002762 else
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002763 Visit(LHS, Pred, Tmp1);
Ted Kremenekfb553542008-01-16 00:53:15 +00002764
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002765 ExplodedNodeSet Tmp3;
2766
Ted Kremenek17a02962009-09-03 03:02:58 +00002767 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002768 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002769 ExplodedNodeSet Tmp2;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002770 Visit(RHS, *I1, Tmp2);
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002771
2772 ExplodedNodeSet CheckedSet;
2773 CheckerVisit(B, CheckedSet, Tmp2, true);
Mike Stump11289f42009-09-09 15:08:12 +00002774
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002775 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump11289f42009-09-09 15:08:12 +00002776
2777 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002778 I2 != E2; ++I2) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002779
Ted Kremenek7020eae2009-09-11 22:07:28 +00002780 const GRState *state = GetState(*I2);
2781 const GRState *OldSt = state;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002782 SVal RightV = state->getSVal(RHS);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002783
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002784 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00002785
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002786 if (Op == BinaryOperator::Assign) {
2787 // EXPERIMENTAL: "Conjured" symbols.
2788 // FIXME: Handle structs.
2789 QualType T = RHS->getType();
2790
2791 if ((RightV.isUnknown()||!getConstraintManager().canReasonAbout(RightV))
2792 && (Loc::IsLocType(T) || (T->isScalarType()&&T->isIntegerType()))) {
2793 unsigned Count = Builder->getCurrentBlockCount();
2794 RightV = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), Count);
2795 }
Zhongxing Xub9eda672009-10-30 07:19:39 +00002796
Ted Kremenek5c2040b12009-10-30 22:01:29 +00002797 SVal ExprVal = asLValue ? LeftV : RightV;
Zhongxing Xub9eda672009-10-30 07:19:39 +00002798
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002799 // Simulate the effects of a "store": bind the value of the RHS
2800 // to the L-Value represented by the LHS.
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002801 EvalStore(Tmp3, B, LHS, *I2, state->BindExpr(B, ExprVal), LeftV,RightV);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002802 continue;
2803 }
2804
2805 if (!B->isAssignmentOp()) {
2806 // Process non-assignments except commas or short-circuited
2807 // logical expressions (LAnd and LOr).
2808 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
2809
2810 if (Result.isUnknown()) {
2811 if (OldSt != state) {
2812 // Generate a new node if we have already created a new state.
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002813 MakeNode(Tmp3, B, *I2, state);
Ted Kremeneke2f6d6c2008-03-12 21:45:47 +00002814 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002815 else
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002816 Tmp3.Add(*I2);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002817
Ted Kremenek2044a512008-04-16 18:21:25 +00002818 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002819 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002820
2821 state = state->BindExpr(B, Result);
2822
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002823 MakeNode(Tmp3, B, *I2, state);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002824 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002825 }
Mike Stump11289f42009-09-09 15:08:12 +00002826
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002827 assert (B->isCompoundAssignmentOp());
2828
Ted Kremenek5d7662c2009-02-07 00:52:24 +00002829 switch (Op) {
2830 default:
2831 assert(0 && "Invalid opcode for compound assignment.");
2832 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2833 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2834 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2835 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2836 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2837 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2838 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2839 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2840 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2841 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek54d399a2008-10-27 23:02:39 +00002842 }
Mike Stump11289f42009-09-09 15:08:12 +00002843
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002844 // Perform a load (the LHS). This performs the checks for
2845 // null dereferences, and so on.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002846 ExplodedNodeSet Tmp3;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002847 SVal location = state->getSVal(LHS);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002848 EvalLoad(Tmp3, LHS, *I2, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002849
2850 for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002851 ++I3) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002852 state = GetState(*I3);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002853 SVal V = state->getSVal(LHS);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002854
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002855 // Get the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002856 QualType CTy =
2857 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek44137142008-11-15 04:01:56 +00002858 CTy = getContext().getCanonicalType(CTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002859
Ted Kremenekac7c7242009-07-21 21:03:30 +00002860 QualType CLHSTy =
2861 cast<CompoundAssignOperator>(B)->getComputationLHSType();
2862 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002863
Ted Kremenek44137142008-11-15 04:01:56 +00002864 QualType LTy = getContext().getCanonicalType(LHS->getType());
2865 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002866
2867 // Promote LHS.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002868 llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002869
Mike Stump11289f42009-09-09 15:08:12 +00002870 // Compute the result of the operation.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002871 SVal Result;
2872 llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
2873 RightV, CTy),
2874 state, B->getType(), CTy);
Mike Stump11289f42009-09-09 15:08:12 +00002875
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002876 // EXPERIMENTAL: "Conjured" symbols.
2877 // FIXME: Handle structs.
Mike Stump11289f42009-09-09 15:08:12 +00002878
Ted Kremenek44137142008-11-15 04:01:56 +00002879 SVal LHSVal;
Mike Stump11289f42009-09-09 15:08:12 +00002880
2881 if ((Result.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002882 !getConstraintManager().canReasonAbout(Result))
Mike Stump11289f42009-09-09 15:08:12 +00002883 && (Loc::IsLocType(CTy)
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002884 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Mike Stump11289f42009-09-09 15:08:12 +00002885
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002886 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002887
Ted Kremenek44137142008-11-15 04:01:56 +00002888 // The symbolic value is actually for the type of the left-hand side
2889 // expression, not the computation type, as this is the value the
2890 // LValue on the LHS will bind to.
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002891 LHSVal = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002892
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00002893 // However, we need to convert the symbol to the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002894 llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002895 }
Ted Kremenek44137142008-11-15 04:01:56 +00002896 else {
2897 // The left-hand side may bind to a different value then the
2898 // computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002899 llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
Ted Kremenek44137142008-11-15 04:01:56 +00002900 }
Mike Stump11289f42009-09-09 15:08:12 +00002901
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002902 EvalStore(Tmp3, B, LHS, *I3, state->BindExpr(B, Result),
Zhongxing Xu342950e2009-08-25 06:51:30 +00002903 location, LHSVal);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002904 }
Ted Kremenekfb553542008-01-16 00:53:15 +00002905 }
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002906 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002907
2908 CheckerVisit(B, Dst, Tmp3, false);
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002909}
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002910
2911//===----------------------------------------------------------------------===//
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002912// Checker registration/lookup.
2913//===----------------------------------------------------------------------===//
2914
2915Checker *GRExprEngine::lookupChecker(void *tag) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +00002916 CheckerMap::const_iterator I = CheckerM.find(tag);
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002917 return (I == CheckerM.end()) ? NULL : Checkers[I->second].second;
2918}
2919
2920//===----------------------------------------------------------------------===//
Ted Kremenekd3122cb2008-02-14 22:36:46 +00002921// Visualization.
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002922//===----------------------------------------------------------------------===//
2923
Ted Kremenekac886cb2008-01-16 21:46:15 +00002924#ifndef NDEBUG
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002925static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek6947a792008-03-07 20:57:30 +00002926static SourceManager* GraphPrintSourceManager;
Ted Kremenek2531fce2008-01-30 23:24:39 +00002927
Ted Kremenekac886cb2008-01-16 21:46:15 +00002928namespace llvm {
2929template<>
Douglas Gregor693ba202009-11-30 16:08:24 +00002930struct DOTGraphTraits<ExplodedNode*> :
Ted Kremenekac886cb2008-01-16 21:46:15 +00002931 public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002932
2933 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2934
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +00002935 // FIXME: Since we do not cache error nodes in GRExprEngine now, this does not
2936 // work.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002937 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump11289f42009-09-09 15:08:12 +00002938
Ted Kremenek7cf82382009-11-11 20:16:36 +00002939#if 0
2940 // FIXME: Replace with a general scheme to tell if the node is
2941 // an error node.
Ted Kremenek5b70a222008-02-14 22:54:53 +00002942 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek0f7130a2008-02-19 00:22:37 +00002943 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek93d1fed2008-02-28 09:25:22 +00002944 GraphPrintCheckerState->isUndefDeref(N) ||
2945 GraphPrintCheckerState->isUndefStore(N) ||
2946 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek6f5fca72008-02-29 23:14:48 +00002947 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek51e87ea2008-02-29 23:53:11 +00002948 GraphPrintCheckerState->isBadCall(N) ||
2949 GraphPrintCheckerState->isUndefArg(N))
Ted Kremenek5b70a222008-02-14 22:54:53 +00002950 return "color=\"red\",style=\"filled\"";
Mike Stump11289f42009-09-09 15:08:12 +00002951
Ted Kremeneke0c79382008-02-28 20:32:03 +00002952 if (GraphPrintCheckerState->isNoReturnCall(N))
2953 return "color=\"blue\",style=\"filled\"";
Zhongxing Xu9e200792009-11-24 07:06:39 +00002954#endif
Ted Kremenek5b70a222008-02-14 22:54:53 +00002955 return "";
2956 }
Mike Stump11289f42009-09-09 15:08:12 +00002957
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002958 static std::string getNodeLabel(const ExplodedNode* N, void*){
Mike Stump11289f42009-09-09 15:08:12 +00002959
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002960 std::string sbuf;
2961 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek930191c2008-01-23 22:30:44 +00002962
2963 // Program Location.
Ted Kremenekac886cb2008-01-16 21:46:15 +00002964 ProgramPoint Loc = N->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002965
Ted Kremenekac886cb2008-01-16 21:46:15 +00002966 switch (Loc.getKind()) {
2967 case ProgramPoint::BlockEntranceKind:
Mike Stump11289f42009-09-09 15:08:12 +00002968 Out << "Block Entrance: B"
Ted Kremenekac886cb2008-01-16 21:46:15 +00002969 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2970 break;
Mike Stump11289f42009-09-09 15:08:12 +00002971
Ted Kremenekac886cb2008-01-16 21:46:15 +00002972 case ProgramPoint::BlockExitKind:
2973 assert (false);
2974 break;
Mike Stump11289f42009-09-09 15:08:12 +00002975
Ted Kremenekac886cb2008-01-16 21:46:15 +00002976 default: {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002977 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
2978 const Stmt* S = L->getStmt();
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002979 SourceLocation SLoc = S->getLocStart();
2980
Mike Stump11289f42009-09-09 15:08:12 +00002981 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattnerc61089a2009-06-30 01:26:17 +00002982 LangOptions LO; // FIXME.
2983 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00002984
2985 if (SLoc.isFileID()) {
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002986 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00002987 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2988 << " col="
2989 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2990 << "\\l";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002991 }
Mike Stump11289f42009-09-09 15:08:12 +00002992
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002993 if (isa<PreStmt>(Loc))
Mike Stump11289f42009-09-09 15:08:12 +00002994 Out << "\\lPreStmt\\l;";
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002995 else if (isa<PostLoad>(Loc))
Ted Kremeneka6e08322009-05-07 18:27:16 +00002996 Out << "\\lPostLoad\\l;";
2997 else if (isa<PostStore>(Loc))
2998 Out << "\\lPostStore\\l";
2999 else if (isa<PostLValue>(Loc))
3000 Out << "\\lPostLValue\\l";
Mike Stump11289f42009-09-09 15:08:12 +00003001
Ted Kremenek7cf82382009-11-11 20:16:36 +00003002#if 0
3003 // FIXME: Replace with a general scheme to determine
3004 // the name of the check.
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003005 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3006 Out << "\\|Implicit-Null Dereference.\\l";
3007 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3008 Out << "\\|Explicit-Null Dereference.\\l";
3009 else if (GraphPrintCheckerState->isUndefDeref(N))
3010 Out << "\\|Dereference of undefialied value.\\l";
3011 else if (GraphPrintCheckerState->isUndefStore(N))
3012 Out << "\\|Store to Undefined Loc.";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003013 else if (GraphPrintCheckerState->isUndefResult(N))
3014 Out << "\\|Result of operation is undefined.";
3015 else if (GraphPrintCheckerState->isNoReturnCall(N))
3016 Out << "\\|Call to function marked \"noreturn\".";
3017 else if (GraphPrintCheckerState->isBadCall(N))
3018 Out << "\\|Call to NULL/Undefined.";
3019 else if (GraphPrintCheckerState->isUndefArg(N))
3020 Out << "\\|Argument in call is undefined";
Ted Kremenek7cf82382009-11-11 20:16:36 +00003021#endif
Mike Stump11289f42009-09-09 15:08:12 +00003022
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003023 break;
3024 }
3025
Ted Kremenekac886cb2008-01-16 21:46:15 +00003026 const BlockEdge& E = cast<BlockEdge>(Loc);
3027 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3028 << E.getDst()->getBlockID() << ')';
Mike Stump11289f42009-09-09 15:08:12 +00003029
Ted Kremeneka50d9852008-01-30 23:03:39 +00003030 if (Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump11289f42009-09-09 15:08:12 +00003031
Ted Kremenek6947a792008-03-07 20:57:30 +00003032 SourceLocation SLoc = T->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00003033
Ted Kremeneka50d9852008-01-30 23:03:39 +00003034 Out << "\\|Terminator: ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003035 LangOptions LO; // FIXME.
3036 E.getSrc()->printTerminator(Out, LO);
Mike Stump11289f42009-09-09 15:08:12 +00003037
Ted Kremenek03ab1562008-03-09 03:30:59 +00003038 if (SLoc.isFileID()) {
3039 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00003040 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3041 << " col="
3042 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek03ab1562008-03-09 03:30:59 +00003043 }
Mike Stump11289f42009-09-09 15:08:12 +00003044
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003045 if (isa<SwitchStmt>(T)) {
3046 Stmt* Label = E.getDst()->getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00003047
3048 if (Label) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003049 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3050 Out << "\\lcase ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003051 LangOptions LO; // FIXME.
3052 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00003053
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003054 if (Stmt* RHS = C->getRHS()) {
3055 Out << " .. ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003056 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003057 }
Mike Stump11289f42009-09-09 15:08:12 +00003058
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003059 Out << ":";
3060 }
3061 else {
3062 assert (isa<DefaultStmt>(Label));
3063 Out << "\\ldefault:";
3064 }
3065 }
Mike Stump11289f42009-09-09 15:08:12 +00003066 else
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003067 Out << "\\l(implicit) default:";
3068 }
3069 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremeneka50d9852008-01-30 23:03:39 +00003070 // FIXME
3071 }
3072 else {
3073 Out << "\\lCondition: ";
3074 if (*E.getSrc()->succ_begin() == E.getDst())
3075 Out << "true";
3076 else
Mike Stump11289f42009-09-09 15:08:12 +00003077 Out << "false";
Ted Kremeneka50d9852008-01-30 23:03:39 +00003078 }
Mike Stump11289f42009-09-09 15:08:12 +00003079
Ted Kremeneka50d9852008-01-30 23:03:39 +00003080 Out << "\\l";
3081 }
Mike Stump11289f42009-09-09 15:08:12 +00003082
Ted Kremenek7cf82382009-11-11 20:16:36 +00003083#if 0
3084 // FIXME: Replace with a general scheme to determine
3085 // the name of the check.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00003086 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3087 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek2531fce2008-01-30 23:24:39 +00003088 }
Ted Kremenek7cf82382009-11-11 20:16:36 +00003089#endif
Ted Kremenekac886cb2008-01-16 21:46:15 +00003090 }
3091 }
Mike Stump11289f42009-09-09 15:08:12 +00003092
Ted Kremenek22c62c12008-02-28 10:21:43 +00003093 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenekb54312d2008-02-08 21:10:02 +00003094
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003095 const GRState *state = N->getState();
3096 state->printDOT(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003097
Ted Kremenek930191c2008-01-23 22:30:44 +00003098 Out << "\\l";
Ted Kremenekac886cb2008-01-16 21:46:15 +00003099 return Out.str();
3100 }
3101};
Mike Stump11289f42009-09-09 15:08:12 +00003102} // end llvm namespace
Ted Kremenekac886cb2008-01-16 21:46:15 +00003103#endif
3104
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003105#ifndef NDEBUG
Ted Kremenek576b76a2008-03-12 17:18:20 +00003106template <typename ITERATOR>
Zhongxing Xu107f7592009-08-06 12:48:26 +00003107ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek576b76a2008-03-12 17:18:20 +00003108
Zhongxing Xu107f7592009-08-06 12:48:26 +00003109template <> ExplodedNode*
3110GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3111 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek576b76a2008-03-12 17:18:20 +00003112 return I->first;
3113}
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003114#endif
3115
3116void GRExprEngine::ViewGraph(bool trim) {
Mike Stump11289f42009-09-09 15:08:12 +00003117#ifndef NDEBUG
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003118 if (trim) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00003119 std::vector<ExplodedNode*> Src;
Ted Kremenek95175052009-03-11 01:41:22 +00003120
3121 // Flush any outstanding reports to make sure we cover all the nodes.
3122 // This does not cause them to get displayed.
3123 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3124 const_cast<BugType*>(*I)->FlushReports(BR);
3125
3126 // Iterate through the reports and get their nodes.
3127 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
Ted Kremenek17a02962009-09-03 03:02:58 +00003128 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
Mike Stump11289f42009-09-09 15:08:12 +00003129 I2!=E2; ++I2) {
Ted Kremenek95175052009-03-11 01:41:22 +00003130 const BugReportEquivClass& EQ = *I2;
3131 const BugReport &R = **EQ.begin();
Zhongxing Xu107f7592009-08-06 12:48:26 +00003132 ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
Ted Kremenek95175052009-03-11 01:41:22 +00003133 if (N) Src.push_back(N);
3134 }
3135 }
Mike Stump11289f42009-09-09 15:08:12 +00003136
Ted Kremenek576b76a2008-03-12 17:18:20 +00003137 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003138 }
Ted Kremeneka7178c72008-03-11 18:25:33 +00003139 else {
3140 GraphPrintCheckerState = this;
3141 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek16306102008-08-13 21:24:49 +00003142
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003143 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Mike Stump11289f42009-09-09 15:08:12 +00003144
Ted Kremeneka7178c72008-03-11 18:25:33 +00003145 GraphPrintCheckerState = NULL;
3146 GraphPrintSourceManager = NULL;
3147 }
3148#endif
3149}
3150
Zhongxing Xu107f7592009-08-06 12:48:26 +00003151void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremeneka7178c72008-03-11 18:25:33 +00003152#ifndef NDEBUG
3153 GraphPrintCheckerState = this;
3154 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00003155
Zhongxing Xu107f7592009-08-06 12:48:26 +00003156 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneka7178c72008-03-11 18:25:33 +00003157
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003158 if (!TrimmedG.get())
Benjamin Kramer89b422c2009-08-23 12:08:50 +00003159 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003160 else
Mike Stump11289f42009-09-09 15:08:12 +00003161 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
3162
Ted Kremenek2531fce2008-01-30 23:24:39 +00003163 GraphPrintCheckerState = NULL;
Ted Kremenek6947a792008-03-07 20:57:30 +00003164 GraphPrintSourceManager = NULL;
Ted Kremenekd3122cb2008-02-14 22:36:46 +00003165#endif
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003166}