blob: 4d431e56390506a6d0331848518a899f896c3248 [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 Kremenek667cacb2008-04-15 23:06:53 +0000661 VisitCast(C, C->getSubExpr(), Pred, Dst);
662 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()) {
Mike Stump11289f42009-09-09 15:08:12 +0000756
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000757 case Stmt::ArraySubscriptExprClass:
758 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
759 return;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Ted Kremenekf907cee2009-12-17 07:38:34 +0000761 case Stmt::BinaryOperatorClass:
762 case Stmt::CompoundAssignOperatorClass:
763 VisitBinaryOperator(cast<BinaryOperator>(Ex), Pred, Dst, true);
764 return;
765
Ted Kremenek04af9f22009-12-07 22:05:27 +0000766 case Stmt::BlockDeclRefExprClass:
767 VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(Ex), Pred, Dst, true);
768 return;
Ted Kremenekf907cee2009-12-17 07:38:34 +0000769
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000770 case Stmt::CallExprClass:
771 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke19711d2009-12-22 22:13:46 +0000772 CallExpr *C = cast<CallExpr>(Ex);
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000773 assert(CalleeReturnsReference(C));
774 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst, true);
775 break;
776 }
777
Ted Kremenekf907cee2009-12-17 07:38:34 +0000778 case Stmt::CompoundLiteralExprClass:
779 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
780 return;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000781
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000782 case Stmt::DeclRefExprClass:
783 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
784 return;
Ted Kremenekf907cee2009-12-17 07:38:34 +0000785
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000786 case Stmt::MemberExprClass:
787 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
788 return;
Mike Stump11289f42009-09-09 15:08:12 +0000789
Ted Kremenekf907cee2009-12-17 07:38:34 +0000790 case Stmt::ObjCIvarRefExprClass:
791 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
Ted Kremenekbf263682008-10-27 21:54:31 +0000792 return;
Ted Kremeneke19711d2009-12-22 22:13:46 +0000793
794 case Stmt::ObjCMessageExprClass: {
795 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(Ex);
796 assert(ReceiverReturnsReference(ME));
797 VisitObjCMessageExpr(ME, Pred, Dst, true);
798 return;
799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek58700462008-10-17 17:24:14 +0000801 case Stmt::ObjCPropertyRefExprClass:
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000802 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Ted Kremenek58700462008-10-17 17:24:14 +0000803 // FIXME: Property assignments are lvalues, but not really "locations".
804 // e.g.: self.x = something;
805 // Here the "self.x" really can translate to a method call (setter) when
806 // the assignment is made. Moreover, the entire assignment expression
807 // evaluate to whatever "something" is, not calling the "getter" for
808 // the property (which would make sense since it can have side effects).
809 // We'll probably treat this as a location, but not one that we can
810 // take the address of. Perhaps we need a new SVal class for cases
811 // like thsis?
812 // Note that we have a similar problem for bitfields, since they don't
813 // have "locations" in the sense that we can take their address.
814 Dst.Add(Pred);
Ted Kremenek8f5dc292008-10-18 04:08:49 +0000815 return;
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000816
817 case Stmt::StringLiteralClass: {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000818 const GRState* state = GetState(Pred);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000819 SVal V = state->getLValue(cast<StringLiteral>(Ex));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000820 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000821 return;
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Ted Kremenekf907cee2009-12-17 07:38:34 +0000824 case Stmt::UnaryOperatorClass:
825 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
Zhongxing Xub9eda672009-10-30 07:19:39 +0000826 return;
Ted Kremenekf907cee2009-12-17 07:38:34 +0000827
Ted Kremenek850422e2008-10-18 04:15:35 +0000828 default:
829 // Arbitrary subexpressions can return aggregate temporaries that
830 // can be used in a lvalue context. We need to enhance our support
831 // of such temporaries in both the environment and the store, so right
832 // now we just do a regular visit.
Douglas Gregorddb24852009-01-30 17:31:00 +0000833 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremeneke69a1fa2008-10-25 20:09:21 +0000834 "Other kinds of expressions with non-aggregate/union types do"
835 " not have lvalues.");
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenek850422e2008-10-18 04:15:35 +0000837 Visit(Ex, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000838 }
839}
840
841//===----------------------------------------------------------------------===//
842// Block entrance. (Update counters).
843//===----------------------------------------------------------------------===//
844
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000845bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenek667cacb2008-04-15 23:06:53 +0000846 GRBlockCounter BC) {
Mike Stump11289f42009-09-09 15:08:12 +0000847
Ted Kremenek667cacb2008-04-15 23:06:53 +0000848 return BC.getNumVisited(B->getBlockID()) < 3;
849}
850
851//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +0000852// Generic node creation.
853//===----------------------------------------------------------------------===//
854
Zhongxing Xu107f7592009-08-06 12:48:26 +0000855ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
856 ExplodedNode* Pred, const GRState* St,
857 ProgramPoint::Kind K, const void *tag) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000858 assert (Builder && "GRStmtNodeBuilder not present.");
859 SaveAndRestore<const void*> OldTag(Builder->Tag);
860 Builder->Tag = tag;
861 return Builder->MakeNode(Dst, S, Pred, St, K);
862}
863
864//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +0000865// Branch processing.
866//===----------------------------------------------------------------------===//
867
Ted Kremenek17d541d2009-02-13 01:45:31 +0000868const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000869 Stmt* Terminator,
870 bool branchTaken) {
Mike Stump11289f42009-09-09 15:08:12 +0000871
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000872 switch (Terminator->getStmtClass()) {
873 default:
Ted Kremenek17d541d2009-02-13 01:45:31 +0000874 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000875
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000876 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump11289f42009-09-09 15:08:12 +0000877
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000878 BinaryOperator* B = cast<BinaryOperator>(Terminator);
879 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +0000880
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000881 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +0000882
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000883 // For &&, if we take the true branch, then the value of the whole
884 // expression is that of the RHS expression.
885 //
886 // For ||, if we take the false branch, then the value of the whole
887 // expression is that of the RHS expression.
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000889 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
Mike Stump11289f42009-09-09 15:08:12 +0000890 (Op == BinaryOperator::LOr && !branchTaken)
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000891 ? B->getRHS() : B->getLHS();
Mike Stump11289f42009-09-09 15:08:12 +0000892
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000893 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000894 }
Mike Stump11289f42009-09-09 15:08:12 +0000895
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000896 case Stmt::ConditionalOperatorClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000898 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000899
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000900 // For ?, if branchTaken == true then the value is either the LHS or
901 // the condition itself. (GNU extension).
Mike Stump11289f42009-09-09 15:08:12 +0000902
903 Expr* Ex;
904
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000905 if (branchTaken)
Mike Stump11289f42009-09-09 15:08:12 +0000906 Ex = C->getLHS() ? C->getLHS() : C->getCond();
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000907 else
908 Ex = C->getRHS();
Mike Stump11289f42009-09-09 15:08:12 +0000909
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000910 return state->BindExpr(C, 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::ChooseExprClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000914
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000915 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000916
917 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000918 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000919 }
920 }
921}
922
Ted Kremenek22358bd2009-03-13 16:32:54 +0000923/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
924/// to try to recover some path-sensitivity for casts of symbolic
925/// integers that promote their values (which are currently not tracked well).
926/// This function returns the SVal bound to Condition->IgnoreCasts if all the
927// cast(s) did was sign-extend the original value.
928static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
929 Stmt* Condition, ASTContext& Ctx) {
930
931 Expr *Ex = dyn_cast<Expr>(Condition);
932 if (!Ex)
933 return UnknownVal();
934
935 uint64_t bits = 0;
936 bool bitsInit = false;
Mike Stump11289f42009-09-09 15:08:12 +0000937
Ted Kremenek22358bd2009-03-13 16:32:54 +0000938 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
939 QualType T = CE->getType();
940
941 if (!T->isIntegerType())
942 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000943
Ted Kremenek22358bd2009-03-13 16:32:54 +0000944 uint64_t newBits = Ctx.getTypeSize(T);
945 if (!bitsInit || newBits < bits) {
946 bitsInit = true;
947 bits = newBits;
948 }
Mike Stump11289f42009-09-09 15:08:12 +0000949
Ted Kremenek22358bd2009-03-13 16:32:54 +0000950 Ex = CE->getSubExpr();
951 }
952
953 // We reached a non-cast. Is it a symbolic value?
954 QualType T = Ex->getType();
955
956 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
957 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000958
Ted Kremenek095f1a92009-06-18 23:58:37 +0000959 return state->getSVal(Ex);
Ted Kremenek22358bd2009-03-13 16:32:54 +0000960}
961
Ted Kremenek17810802008-11-12 19:24:17 +0000962void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000963 GRBranchNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000964
Ted Kremenek8db4b112008-02-15 22:29:00 +0000965 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump11289f42009-09-09 15:08:12 +0000966 if (!Condition) {
Ted Kremenek8db4b112008-02-15 22:29:00 +0000967 builder.markInfeasible(false);
Ted Kremenek8db4b112008-02-15 22:29:00 +0000968 return;
969 }
Mike Stump11289f42009-09-09 15:08:12 +0000970
Ted Kremenek32c41ec2009-03-11 03:54:24 +0000971 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
972 Condition->getLocStart(),
973 "Error evaluating branch");
Ted Kremenek907a7112009-08-27 01:39:13 +0000974
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000975 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
976 void *tag = I->first;
977 Checker *checker = I->second;
978 checker->VisitBranchCondition(builder, *this, Condition, tag);
979 }
980
981 // If the branch condition is undefined, return;
982 if (!builder.isFeasible(true) && !builder.isFeasible(false))
983 return;
984
Mike Stump11289f42009-09-09 15:08:12 +0000985 const GRState* PrevState = builder.getState();
Ted Kremenek7020eae2009-09-11 22:07:28 +0000986 SVal X = PrevState->getSVal(Condition);
Mike Stump11289f42009-09-09 15:08:12 +0000987
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000988 if (X.isUnknown()) {
989 // Give it a chance to recover from unknown.
990 if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
991 if (Ex->getType()->isIntegerType()) {
992 // Try to recover some path-sensitivity. Right now casts of symbolic
993 // integers that promote their values are currently not tracked well.
994 // If 'Condition' is such an expression, try and recover the
995 // underlying value and use that instead.
996 SVal recovered = RecoverCastedSymbol(getStateManager(),
997 builder.getState(), Condition,
998 getContext());
999
1000 if (!recovered.isUnknown()) {
1001 X = recovered;
1002 }
Ted Kremenek22358bd2009-03-13 16:32:54 +00001003 }
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001004 }
1005 // If the condition is still unknown, give up.
1006 if (X.isUnknown()) {
1007 builder.generateNode(MarkBranch(PrevState, Term, true), true);
1008 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremeneka50d9852008-01-30 23:03:39 +00001009 return;
Mike Stump11289f42009-09-09 15:08:12 +00001010 }
Ted Kremeneka50d9852008-01-30 23:03:39 +00001011 }
Mike Stump11289f42009-09-09 15:08:12 +00001012
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001013 DefinedSVal V = cast<DefinedSVal>(X);
1014
Ted Kremenek17f4dbd2008-02-29 20:27:50 +00001015 // Process the true branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001016 if (builder.isFeasible(true)) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001017 if (const GRState *state = PrevState->Assume(V, true))
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001018 builder.generateNode(MarkBranch(state, Term, true), true);
1019 else
1020 builder.markInfeasible(true);
1021 }
Mike Stump11289f42009-09-09 15:08:12 +00001022
1023 // Process the false branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001024 if (builder.isFeasible(false)) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001025 if (const GRState *state = PrevState->Assume(V, false))
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001026 builder.generateNode(MarkBranch(state, Term, false), false);
1027 else
1028 builder.markInfeasible(false);
1029 }
Ted Kremenek7ff18932008-01-29 23:32:35 +00001030}
1031
Ted Kremenekf6c62f32008-02-13 17:41:41 +00001032/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7022efb2008-02-13 00:24:44 +00001033/// nodes by processing the 'effects' of a computed goto jump.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001034void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
Ted Kremenek7022efb2008-02-13 00:24:44 +00001035
Mike Stump11289f42009-09-09 15:08:12 +00001036 const GRState *state = builder.getState();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001037 SVal V = state->getSVal(builder.getTarget());
Mike Stump11289f42009-09-09 15:08:12 +00001038
Ted Kremenek7022efb2008-02-13 00:24:44 +00001039 // Three possibilities:
1040 //
1041 // (1) We know the computed label.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001042 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek7022efb2008-02-13 00:24:44 +00001043 // (3) We have no clue about the label. Dispatch to all targets.
1044 //
Mike Stump11289f42009-09-09 15:08:12 +00001045
Zhongxing Xu107f7592009-08-06 12:48:26 +00001046 typedef GRIndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek7022efb2008-02-13 00:24:44 +00001047
Zhongxing Xu27f17422008-10-17 05:57:07 +00001048 if (isa<loc::GotoLabel>(V)) {
1049 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00001050
Ted Kremenek7022efb2008-02-13 00:24:44 +00001051 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek2bba9012008-02-13 17:27:37 +00001052 if (I.getLabel() == L) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001053 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001054 return;
1055 }
1056 }
Mike Stump11289f42009-09-09 15:08:12 +00001057
Ted Kremenek7022efb2008-02-13 00:24:44 +00001058 assert (false && "No block with label.");
1059 return;
1060 }
1061
Zhongxing Xu27f17422008-10-17 05:57:07 +00001062 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek7022efb2008-02-13 00:24:44 +00001063 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu9e200792009-11-24 07:06:39 +00001064 //ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
1065 // FIXME: add checker visit.
1066 // UndefBranches.insert(N);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001067 return;
1068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
Ted Kremenek7022efb2008-02-13 00:24:44 +00001070 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenek9c03f682009-04-23 17:49:43 +00001071 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump11289f42009-09-09 15:08:12 +00001072
Ted Kremenek7022efb2008-02-13 00:24:44 +00001073 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek17d541d2009-02-13 01:45:31 +00001074 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001075}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001076
Ted Kremenek667cacb2008-04-15 23:06:53 +00001077
1078void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001079 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001080
Zhongxing Xu6df9f542009-12-16 11:27:52 +00001081 assert(Ex == CurrentStmt &&
1082 Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump11289f42009-09-09 15:08:12 +00001083
Ted Kremenek17d541d2009-02-13 01:45:31 +00001084 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +00001085 SVal X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001086
Ted Kremenek667cacb2008-04-15 23:06:53 +00001087 assert (X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001088
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001089 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump11289f42009-09-09 15:08:12 +00001090 assert(SE);
Ted Kremenek907a7112009-08-27 01:39:13 +00001091 X = state->getSVal(SE);
Mike Stump11289f42009-09-09 15:08:12 +00001092
Ted Kremenek667cacb2008-04-15 23:06:53 +00001093 // Make sure that we invalidate the previous binding.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001094 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremenek667cacb2008-04-15 23:06:53 +00001095}
1096
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001097/// ProcessEndPath - Called by GRCoreEngine. Used to generate end-of-path
1098/// nodes when the control reaches the end of a function.
1099void GRExprEngine::ProcessEndPath(GREndPathNodeBuilder& builder) {
1100 getTF().EvalEndPath(*this, builder);
1101 StateMgr.EndPath(builder.getState());
Zhongxing Xu4668c7e2009-11-17 07:54:15 +00001102 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E;++I){
1103 void *tag = I->first;
1104 Checker *checker = I->second;
1105 checker->EvalEndPath(builder, tag, *this);
1106 }
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001107}
1108
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001109/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
1110/// nodes by processing the 'effects' of a switch statement.
Mike Stump11289f42009-09-09 15:08:12 +00001111void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
1112 typedef GRSwitchNodeBuilder::iterator iterator;
1113 const GRState* state = builder.getState();
Ted Kremenek346169f2008-02-18 22:57:02 +00001114 Expr* CondE = builder.getCondition();
Ted Kremenek7020eae2009-09-11 22:07:28 +00001115 SVal CondV_untested = state->getSVal(CondE);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001116
Ted Kremenek7020eae2009-09-11 22:07:28 +00001117 if (CondV_untested.isUndef()) {
Zhongxing Xu9e200792009-11-24 07:06:39 +00001118 //ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
1119 // FIXME: add checker
1120 //UndefBranches.insert(N);
1121
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001122 return;
1123 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001124 DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
Ted Kremenek346169f2008-02-18 22:57:02 +00001125
Ted Kremenek7020eae2009-09-11 22:07:28 +00001126 const GRState *DefaultSt = state;
Ted Kremenekf9906842009-06-18 22:57:13 +00001127 bool defaultIsFeasible = false;
Mike Stump11289f42009-09-09 15:08:12 +00001128
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001129 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001130 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001131
1132 // Evaluate the LHS of the case value.
1133 Expr::EvalResult V1;
Mike Stump11289f42009-09-09 15:08:12 +00001134 bool b = Case->getLHS()->Evaluate(V1, getContext());
1135
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001136 // Sanity checks. These go away in Release builds.
Mike Stump11289f42009-09-09 15:08:12 +00001137 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001138 && "Case condition must evaluate to an integer constant.");
Mike Stump11289f42009-09-09 15:08:12 +00001139 b = b; // silence unused variable warning
1140 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001141 getContext().getTypeSize(CondE->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001142
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001143 // Get the RHS of the case, if it exists.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001144 Expr::EvalResult V2;
Mike Stump11289f42009-09-09 15:08:12 +00001145
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001146 if (Expr* E = Case->getRHS()) {
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001147 b = E->Evaluate(V2, getContext());
Mike Stump11289f42009-09-09 15:08:12 +00001148 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001149 && "Case condition must evaluate to an integer constant.");
1150 b = b; // silence unused variable warning
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001151 }
Ted Kremenek9eae4032008-03-17 22:17:56 +00001152 else
1153 V2 = V1;
Mike Stump11289f42009-09-09 15:08:12 +00001154
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001155 // FIXME: Eventually we should replace the logic below with a range
1156 // comparison, rather than concretize the values within the range.
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001157 // This should be easy once we have "ranges" for NonLVals.
Mike Stump11289f42009-09-09 15:08:12 +00001158
Ted Kremenek9eae4032008-03-17 22:17:56 +00001159 do {
Mike Stump11289f42009-09-09 15:08:12 +00001160 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek7020eae2009-09-11 22:07:28 +00001161 DefinedOrUnknownSVal Res = SVator.EvalEQ(DefaultSt, CondV, CaseVal);
1162
Mike Stump11289f42009-09-09 15:08:12 +00001163 // Now "assume" that the case matches.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001164 if (const GRState* stateNew = state->Assume(Res, true)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001165 builder.generateCaseStmtNode(I, stateNew);
Mike Stump11289f42009-09-09 15:08:12 +00001166
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001167 // If CondV evaluates to a constant, then we know that this
1168 // is the *only* case that we can take, so stop evaluating the
1169 // others.
Zhongxing Xu27f17422008-10-17 05:57:07 +00001170 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001171 return;
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001174 // Now "assume" that the case doesn't match. Add this state
1175 // to the default state (if it is feasible).
Ted Kremenek7020eae2009-09-11 22:07:28 +00001176 if (const GRState *stateNew = DefaultSt->Assume(Res, false)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001177 defaultIsFeasible = true;
1178 DefaultSt = stateNew;
Ted Kremenekd2419a02008-04-23 05:03:18 +00001179 }
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001180
Ted Kremenek9eae4032008-03-17 22:17:56 +00001181 // Concretize the next value in the range.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001182 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek9eae4032008-03-17 22:17:56 +00001183 break;
Mike Stump11289f42009-09-09 15:08:12 +00001184
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001185 ++V1.Val.getInt();
1186 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump11289f42009-09-09 15:08:12 +00001187
Ted Kremenek9eae4032008-03-17 22:17:56 +00001188 } while (true);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001189 }
Mike Stump11289f42009-09-09 15:08:12 +00001190
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001191 // If we reach here, than we know that the default branch is
Mike Stump11289f42009-09-09 15:08:12 +00001192 // possible.
Ted Kremenekf9906842009-06-18 22:57:13 +00001193 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001194}
1195
Ted Kremenek667cacb2008-04-15 23:06:53 +00001196//===----------------------------------------------------------------------===//
1197// Transfer functions: logical operations ('&&', '||').
1198//===----------------------------------------------------------------------===//
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001199
Zhongxing Xu107f7592009-08-06 12:48:26 +00001200void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
1201 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001203 assert(B->getOpcode() == BinaryOperator::LAnd ||
1204 B->getOpcode() == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +00001205
Zhongxing Xu6df9f542009-12-16 11:27:52 +00001206 assert(B==CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenek17d541d2009-02-13 01:45:31 +00001208 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +00001209 SVal X = state->getSVal(B);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001210 assert(X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001211
Ted Kremenek7020eae2009-09-11 22:07:28 +00001212 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001213 assert(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001214
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001215 if (Ex == B->getRHS()) {
Ted Kremenek907a7112009-08-27 01:39:13 +00001216 X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001217
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001218 // Handle undefined values.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001219 if (X.isUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001220 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenekbc543902008-02-26 19:40:44 +00001221 return;
1222 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001223
1224 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001226 // We took the RHS. Because the value of the '&&' or '||' expression must
1227 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
1228 // or 1. Alternatively, we could take a lazy approach, and calculate this
1229 // value later when necessary. We don't have the machinery in place for
1230 // this right now, and since most logical expressions are used for branches,
Mike Stump11289f42009-09-09 15:08:12 +00001231 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001232 if (const GRState *newState = state->Assume(XD, true))
Mike Stump11289f42009-09-09 15:08:12 +00001233 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001234 newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Mike Stump11289f42009-09-09 15:08:12 +00001235
Ted Kremenek7020eae2009-09-11 22:07:28 +00001236 if (const GRState *newState = state->Assume(XD, false))
Mike Stump11289f42009-09-09 15:08:12 +00001237 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001238 newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001239 }
1240 else {
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001241 // We took the LHS expression. Depending on whether we are '&&' or
1242 // '||' we know what the value of the expression is via properties of
1243 // the short-circuiting.
Mike Stump11289f42009-09-09 15:08:12 +00001244 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001245 B->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001246 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001247 }
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001248}
Mike Stump11289f42009-09-09 15:08:12 +00001249
Ted Kremenek667cacb2008-04-15 23:06:53 +00001250//===----------------------------------------------------------------------===//
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001251// Transfer functions: Loads and stores.
Ted Kremenek667cacb2008-04-15 23:06:53 +00001252//===----------------------------------------------------------------------===//
Ted Kremenekde8d62b2008-01-15 23:55:06 +00001253
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001254void GRExprEngine::VisitBlockExpr(BlockExpr *BE, ExplodedNode *Pred,
1255 ExplodedNodeSet &Dst) {
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001256
1257 ExplodedNodeSet Tmp;
1258
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001259 CanQualType T = getContext().getCanonicalType(BE->getType());
Ted Kremenekb63ad7a2009-11-25 23:53:07 +00001260 SVal V = ValMgr.getBlockPointer(BE->getBlockDecl(), T,
1261 Pred->getLocationContext());
1262
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001263 MakeNode(Tmp, BE, Pred, GetState(Pred)->BindExpr(BE, V),
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001264 ProgramPoint::PostLValueKind);
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001265
1266 // Post-visit the BlockExpr.
1267 CheckerVisit(BE, Dst, Tmp, false);
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001268}
1269
Mike Stump11289f42009-09-09 15:08:12 +00001270void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
Ted Kremenek14536f62009-08-21 22:28:32 +00001271 ExplodedNodeSet &Dst, bool asLValue) {
Ted Kremenek04af9f22009-12-07 22:05:27 +00001272 VisitCommonDeclRefExpr(Ex, Ex->getDecl(), Pred, Dst, asLValue);
1273}
1274
1275void GRExprEngine::VisitBlockDeclRefExpr(BlockDeclRefExpr *Ex,
1276 ExplodedNode *Pred,
1277 ExplodedNodeSet &Dst, bool asLValue) {
1278 VisitCommonDeclRefExpr(Ex, Ex->getDecl(), Pred, Dst, asLValue);
1279}
1280
1281void GRExprEngine::VisitCommonDeclRefExpr(Expr *Ex, const NamedDecl *D,
1282 ExplodedNode *Pred,
1283 ExplodedNodeSet &Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001284
Ted Kremenek7020eae2009-09-11 22:07:28 +00001285 const GRState *state = GetState(Pred);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001286
1287 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1288
Ted Kremenek14536f62009-08-21 22:28:32 +00001289 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xu252fe5c2008-10-17 02:20:14 +00001290
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001291 if (asLValue) {
1292 // For references, the 'lvalue' is the pointer address stored in the
1293 // reference region.
1294 if (VD->getType()->isReferenceType()) {
1295 if (const MemRegion *R = V.getAsRegion())
1296 V = state->getSVal(R);
1297 else
1298 V = UnknownVal();
1299 }
1300
1301 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V,
1302 ProgramPoint::PostLValueKind));
1303 }
Zhongxing Xu232c7922008-10-16 06:09:51 +00001304 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001305 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001306
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001307 return;
Zhongxing Xu232c7922008-10-16 06:09:51 +00001308 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
1309 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
1310
Zhongxing Xud09b5202009-06-23 06:13:19 +00001311 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001312 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu232c7922008-10-16 06:09:51 +00001313 return;
1314
1315 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek1624a472009-09-23 01:30:01 +00001316 // This code is valid regardless of the value of 'isLValue'.
Zhongxing Xuac129432009-04-20 05:24:46 +00001317 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001318 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001319 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001320 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001321 }
Mike Stump11289f42009-09-09 15:08:12 +00001322
Zhongxing Xu232c7922008-10-16 06:09:51 +00001323 assert (false &&
1324 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek88da1de2008-02-07 04:16:04 +00001325}
1326
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001327/// VisitArraySubscriptExpr - Transfer function for array accesses
Mike Stump11289f42009-09-09 15:08:12 +00001328void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001329 ExplodedNode* Pred,
1330 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump11289f42009-09-09 15:08:12 +00001331
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001332 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek10246e82008-04-29 23:24:44 +00001333 Expr* Idx = A->getIdx()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001334 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001335
Ted Kremenekcce27f52009-02-24 02:23:11 +00001336 if (Base->getType()->isVectorType()) {
1337 // For vector types get its lvalue.
1338 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1339 // In fact, I think this is just a hack. We need to get the right
1340 // semantics.
1341 VisitLValue(Base, Pred, Tmp);
1342 }
Mike Stump11289f42009-09-09 15:08:12 +00001343 else
Ted Kremenekcce27f52009-02-24 02:23:11 +00001344 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Mike Stump11289f42009-09-09 15:08:12 +00001345
Zhongxing Xu107f7592009-08-06 12:48:26 +00001346 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1347 ExplodedNodeSet Tmp2;
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001348 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Mike Stump11289f42009-09-09 15:08:12 +00001349
Zhongxing Xub1667122009-11-11 13:42:54 +00001350 ExplodedNodeSet Tmp3;
1351 CheckerVisit(A, Tmp3, Tmp2, true);
1352
1353 for (ExplodedNodeSet::iterator I2=Tmp3.begin(),E2=Tmp3.end();I2!=E2; ++I2) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001354 const GRState* state = GetState(*I2);
Zhongxing Xu7d6387b2009-10-14 03:33:08 +00001355 SVal V = state->getLValue(A->getType(), state->getSVal(Idx),
1356 state->getSVal(Base));
Ted Kremenek10246e82008-04-29 23:24:44 +00001357
Zhongxing Xu232c7922008-10-16 06:09:51 +00001358 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001359 MakeNode(Dst, A, *I2, state->BindExpr(A, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001360 ProgramPoint::PostLValueKind);
Ted Kremenek10246e82008-04-29 23:24:44 +00001361 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001362 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek10246e82008-04-29 23:24:44 +00001363 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001364 }
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001365}
1366
Ted Kremenek38213f92008-04-21 23:43:38 +00001367/// VisitMemberExpr - Transfer function for member expressions.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001368void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
1369 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001370
Ted Kremenek38213f92008-04-21 23:43:38 +00001371 Expr* Base = M->getBase()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001372 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001373
1374 if (M->isArrow())
Ted Kremenekfef1f302008-10-18 03:28:48 +00001375 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1376 else
1377 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001379 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1380 if (!Field) // FIXME: skipping member expressions for non-fields
1381 return;
1382
Zhongxing Xu107f7592009-08-06 12:48:26 +00001383 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001384 const GRState* state = GetState(*I);
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001385 // FIXME: Should we insert some assumption logic in here to determine
1386 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001387 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xub9eda672009-10-30 07:19:39 +00001388 SVal L = state->getLValue(Field, state->getSVal(Base));
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001389
Zhongxing Xub9eda672009-10-30 07:19:39 +00001390 if (asLValue)
1391 MakeNode(Dst, M, *I, state->BindExpr(M, L), ProgramPoint::PostLValueKind);
1392 else
1393 EvalLoad(Dst, M, *I, state, L);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001394 }
Ted Kremenek38213f92008-04-21 23:43:38 +00001395}
1396
Ted Kremenek17d541d2009-02-13 01:45:31 +00001397/// EvalBind - Handle the semantics of binding a value to a specific location.
1398/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001399void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Stmt *AssignE,
1400 Stmt* StoreE, ExplodedNode* Pred,
Ted Kremenekb006b822009-11-04 00:09:15 +00001401 const GRState* state, SVal location, SVal Val,
1402 bool atDeclInit) {
Ted Kremenekef910042009-11-04 04:24:16 +00001403
1404
1405 // Do a previsit of the bind.
1406 ExplodedNodeSet CheckedSet, Src;
1407 Src.Add(Pred);
Ted Kremenek209e31b2009-11-05 00:42:23 +00001408 CheckerVisitBind(AssignE, StoreE, CheckedSet, Src, location, Val, true);
Ted Kremenekef910042009-11-04 04:24:16 +00001409
1410 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
1411 I!=E; ++I) {
1412
1413 if (Pred != *I)
1414 state = GetState(*I);
1415
1416 const GRState* newState = 0;
Ted Kremenek17d541d2009-02-13 01:45:31 +00001417
Ted Kremenekef910042009-11-04 04:24:16 +00001418 if (atDeclInit) {
1419 const VarRegion *VR =
1420 cast<VarRegion>(cast<loc::MemRegionVal>(location).getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001421
Ted Kremenekef910042009-11-04 04:24:16 +00001422 newState = state->bindDecl(VR, Val);
Ted Kremenekb006b822009-11-04 00:09:15 +00001423 }
1424 else {
Ted Kremenekef910042009-11-04 04:24:16 +00001425 if (location.isUnknown()) {
1426 // We know that the new state will be the same as the old state since
1427 // the location of the binding is "unknown". Consequently, there
1428 // is no reason to just create a new node.
1429 newState = state;
1430 }
1431 else {
1432 // We are binding to a value other than 'unknown'. Perform the binding
1433 // using the StoreManager.
1434 newState = state->bindLoc(cast<Loc>(location), Val);
1435 }
Ted Kremenekb006b822009-11-04 00:09:15 +00001436 }
Ted Kremenekef910042009-11-04 04:24:16 +00001437
1438 // The next thing to do is check if the GRTransferFuncs object wants to
1439 // update the state based on the new binding. If the GRTransferFunc object
1440 // doesn't do anything, just auto-propagate the current state.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001441 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, newState, StoreE,
Ted Kremenekef910042009-11-04 04:24:16 +00001442 newState != state);
1443
1444 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001445 }
Ted Kremenek17d541d2009-02-13 01:45:31 +00001446}
1447
1448/// EvalStore - Handle the semantics of a store via an assignment.
1449/// @param Dst The node set to store generated state nodes
1450/// @param Ex The expression representing the location of the store
1451/// @param state The current simulation state
1452/// @param location The location to store the value
1453/// @param Val The value to be stored
Ted Kremenek209e31b2009-11-05 00:42:23 +00001454void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr *AssignE,
1455 Expr* StoreE,
1456 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001457 const GRState* state, SVal location, SVal Val,
1458 const void *tag) {
Mike Stump11289f42009-09-09 15:08:12 +00001459
Ted Kremenek209e31b2009-11-05 00:42:23 +00001460 assert(Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump11289f42009-09-09 15:08:12 +00001461
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001462 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001463 ExplodedNodeSet Tmp;
1464 EvalLocation(Tmp, StoreE, Pred, state, location, tag, false);
Mike Stump11289f42009-09-09 15:08:12 +00001465
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001466 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001467 return;
Ted Kremenekc072b822008-04-18 20:35:30 +00001468
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001469 assert(!location.isUndef());
Ted Kremenek17d541d2009-02-13 01:45:31 +00001470
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001471 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind,
1472 ProgramPoint::PostStoreKind);
1473 SaveAndRestore<const void*> OldTag(Builder->Tag, tag);
1474
Mike Stump11289f42009-09-09 15:08:12 +00001475 // Proceed with the store.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001476 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
1477 EvalBind(Dst, AssignE, StoreE, *NI, GetState(*NI), location, Val);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001478}
1479
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001480void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr *Ex, ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001481 const GRState* state, SVal location,
Zhongxing Xu731f4622009-11-16 04:49:44 +00001482 const void *tag, QualType LoadTy) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001483
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001484 // Are we loading from a region? This actually results in two loads; one
1485 // to fetch the address of the referenced value and one to fetch the
1486 // referenced value.
1487 if (const TypedRegion *TR =
1488 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
1489
1490 QualType ValTy = TR->getValueType(getContext());
1491 if (const ReferenceType *RT = ValTy->getAs<ReferenceType>()) {
1492 static int loadReferenceTag = 0;
1493 ExplodedNodeSet Tmp;
1494 EvalLoadCommon(Tmp, Ex, Pred, state, location, &loadReferenceTag,
1495 getContext().getPointerType(RT->getPointeeType()));
1496
1497 // Perform the load from the referenced value.
1498 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end() ; I!=E; ++I) {
1499 state = GetState(*I);
1500 location = state->getSVal(Ex);
1501 EvalLoadCommon(Dst, Ex, *I, state, location, tag, LoadTy);
1502 }
1503 return;
1504 }
1505 }
1506
1507 EvalLoadCommon(Dst, Ex, Pred, state, location, tag, LoadTy);
1508}
1509
1510void GRExprEngine::EvalLoadCommon(ExplodedNodeSet& Dst, Expr *Ex,
1511 ExplodedNode* Pred,
1512 const GRState* state, SVal location,
1513 const void *tag, QualType LoadTy) {
1514
Mike Stump11289f42009-09-09 15:08:12 +00001515 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001516 ExplodedNodeSet Tmp;
1517 EvalLocation(Tmp, Ex, Pred, state, location, tag, true);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001519 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001520 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001521
1522 assert(!location.isUndef());
1523
1524 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1525 SaveAndRestore<const void*> OldTag(Builder->Tag);
Mike Stump11289f42009-09-09 15:08:12 +00001526
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001527 // Proceed with the load.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001528 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1529 state = GetState(*NI);
1530 if (location.isUnknown()) {
1531 // This is important. We must nuke the old binding.
1532 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, UnknownVal()),
1533 ProgramPoint::PostLoadKind, tag);
1534 }
1535 else {
Zhongxing Xu731f4622009-11-16 04:49:44 +00001536 SVal V = state->getSVal(cast<Loc>(location), LoadTy.isNull() ?
1537 Ex->getType() : LoadTy);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001538 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, V), ProgramPoint::PostLoadKind,
1539 tag);
1540 }
Zhongxing Xu33178a02008-11-28 08:34:30 +00001541 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001542}
1543
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001544void GRExprEngine::EvalLocation(ExplodedNodeSet &Dst, Stmt *S,
1545 ExplodedNode* Pred,
1546 const GRState* state, SVal location,
1547 const void *tag, bool isLoad) {
Zhongxing Xuab0ae212009-11-20 03:50:46 +00001548 // Early checks for performance reason.
1549 if (location.isUnknown() || Checkers.empty()) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001550 Dst.Add(Pred);
1551 return;
Ted Kremenekfac290d2009-11-02 23:19:29 +00001552 }
1553
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001554 ExplodedNodeSet Src, Tmp;
1555 Src.Add(Pred);
1556 ExplodedNodeSet *PrevSet = &Src;
1557
1558 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
1559 {
Ted Kremenek32c32892009-12-09 02:45:41 +00001560 ExplodedNodeSet *CurrSet = 0;
1561 if (I+1 == E)
1562 CurrSet = &Dst;
1563 else {
1564 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
1565 CurrSet->clear();
1566 }
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001567
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001568 void *tag = I->first;
1569 Checker *checker = I->second;
1570
1571 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenekf5735152009-11-23 22:22:01 +00001572 NI != NE; ++NI) {
1573 // Use the 'state' argument only when the predecessor node is the
1574 // same as Pred. This allows us to catch updates to the state.
1575 checker->GR_VisitLocation(*CurrSet, *Builder, *this, S, *NI,
1576 *NI == Pred ? state : GetState(*NI),
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001577 location, tag, isLoad);
Ted Kremenekf5735152009-11-23 22:22:01 +00001578 }
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001579
1580 // Update which NodeSet is the current one.
1581 PrevSet = CurrSet;
1582 }
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001583}
1584
Ted Kremenek667cacb2008-04-15 23:06:53 +00001585//===----------------------------------------------------------------------===//
1586// Transfer function: Function calls.
1587//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +00001588
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001589namespace {
1590class CallExprWLItem {
1591public:
1592 CallExpr::arg_iterator I;
1593 ExplodedNode *N;
1594
1595 CallExprWLItem(const CallExpr::arg_iterator &i, ExplodedNode *n)
1596 : I(i), N(n) {}
1597};
1598} // end anonymous namespace
1599
Zhongxing Xu107f7592009-08-06 12:48:26 +00001600void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001601 CallExpr::arg_iterator AI,
1602 CallExpr::arg_iterator AE,
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001603 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001604
Douglas Gregor6b754842008-10-28 00:22:11 +00001605 // Determine the type of function we're calling (if available).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001606 const FunctionProtoType *Proto = NULL;
Douglas Gregor6b754842008-10-28 00:22:11 +00001607 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001608 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
John McCall9dd450b2009-09-21 23:43:11 +00001609 Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>();
Douglas Gregor6b754842008-10-28 00:22:11 +00001610
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001611 // Create a worklist to process the arguments.
1612 llvm::SmallVector<CallExprWLItem, 20> WorkList;
1613 WorkList.reserve(AE - AI);
1614 WorkList.push_back(CallExprWLItem(AI, Pred));
1615
1616 ExplodedNodeSet ArgsEvaluated;
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001617
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001618 while (!WorkList.empty()) {
1619 CallExprWLItem Item = WorkList.back();
1620 WorkList.pop_back();
1621
1622 if (Item.I == AE) {
1623 ArgsEvaluated.insert(Item.N);
1624 continue;
1625 }
1626
1627 // Evaluate the argument.
1628 ExplodedNodeSet Tmp;
1629 const unsigned ParamIdx = Item.I - AI;
1630
Douglas Gregor6b754842008-10-28 00:22:11 +00001631 bool VisitAsLvalue = false;
1632 if (Proto && ParamIdx < Proto->getNumArgs())
1633 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001634
Douglas Gregor6b754842008-10-28 00:22:11 +00001635 if (VisitAsLvalue)
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001636 VisitLValue(*Item.I, Item.N, Tmp);
Douglas Gregor6b754842008-10-28 00:22:11 +00001637 else
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001638 Visit(*Item.I, Item.N, Tmp);
1639
1640 // Enqueue evaluating the next argument on the worklist.
1641 ++(Item.I);
Mike Stump11289f42009-09-09 15:08:12 +00001642
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001643 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
1644 WorkList.push_back(CallExprWLItem(Item.I, *NI));
Ted Kremeneke0188e62008-02-19 01:44:53 +00001645 }
1646
Ted Kremenek48af0e02009-12-17 20:10:17 +00001647 // Now process the call itself.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001648 ExplodedNodeSet DstTmp;
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001649 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001650
1651 for (ExplodedNodeSet::iterator NI=ArgsEvaluated.begin(),
Ted Kremenek48af0e02009-12-17 20:10:17 +00001652 NE=ArgsEvaluated.end(); NI != NE; ++NI) {
1653 // Evaluate the callee.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001654 ExplodedNodeSet DstTmp2;
Ted Kremenek48af0e02009-12-17 20:10:17 +00001655 Visit(Callee, *NI, DstTmp2);
Ted Kremenek49513cc2009-07-22 21:43:51 +00001656 // Perform the previsit of the CallExpr, storing the results in DstTmp.
1657 CheckerVisit(CE, DstTmp, DstTmp2, true);
1658 }
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001659
1660 // Finally, evaluate the function call. We try each of the checkers
1661 // to see if the can evaluate the function call.
Ted Kremenek32c32892009-12-09 02:45:41 +00001662 ExplodedNodeSet DstTmp3;
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001663
Ted Kremenek32c32892009-12-09 02:45:41 +00001664
Mike Stump11289f42009-09-09 15:08:12 +00001665 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001666 DI != DE; ++DI) {
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001667
Ted Kremenek17d541d2009-02-13 01:45:31 +00001668 const GRState* state = GetState(*DI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001669 SVal L = state->getSVal(Callee);
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001670
Ted Kremenek8efd6b4e2008-03-03 16:47:31 +00001671 // FIXME: Add support for symbolic function calls (calls involving
1672 // function pointer values that are symbolic).
Ted Kremenek70342362008-03-05 21:15:02 +00001673 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek32c32892009-12-09 02:45:41 +00001674 ExplodedNodeSet DstChecker;
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001675
Zhongxing Xu175447f2009-12-07 09:17:35 +00001676 // If the callee is processed by a checker, skip the rest logic.
1677 if (CheckerEvalCall(CE, DstChecker, *DI))
Zhongxing Xud1dee7e2009-12-09 05:48:53 +00001678 DstTmp3.insert(DstChecker);
Zhongxing Xu175447f2009-12-07 09:17:35 +00001679 else {
Ted Kremenek32c32892009-12-09 02:45:41 +00001680 for (ExplodedNodeSet::iterator DI_Checker = DstChecker.begin(),
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001681 DE_Checker = DstChecker.end();
1682 DI_Checker != DE_Checker; ++DI_Checker) {
1683
1684 // Dispatch to the plug-in transfer function.
Ted Kremenek32c32892009-12-09 02:45:41 +00001685 unsigned OldSize = DstTmp3.size();
1686 SaveOr OldHasGen(Builder->HasGeneratedNode);
1687 Pred = *DI_Checker;
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001688
Ted Kremenek32c32892009-12-09 02:45:41 +00001689 // Dispatch to transfer function logic to handle the call itself.
1690 // FIXME: Allow us to chain together transfer functions.
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001691 assert(Builder && "GRStmtNodeBuilder must be defined.");
Zhongxing Xu1042bf42009-12-09 12:23:28 +00001692 getTF().EvalCall(DstTmp3, *this, *Builder, CE, L, Pred);
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001693
Ted Kremenek32c32892009-12-09 02:45:41 +00001694 // Handle the case where no nodes where generated. Auto-generate that
1695 // contains the updated state if we aren't generating sinks.
1696 if (!Builder->BuildSinks && DstTmp3.size() == OldSize &&
1697 !Builder->HasGeneratedNode)
1698 MakeNode(DstTmp3, CE, Pred, state);
1699 }
Zhongxing Xu175447f2009-12-07 09:17:35 +00001700 }
Ted Kremeneke0188e62008-02-19 01:44:53 +00001701 }
Ted Kremenek94cc33f2009-12-17 20:06:29 +00001702
1703 // Finally, perform the post-condition check of the CallExpr and store
1704 // the created nodes in 'Dst'.
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00001705
1706 if (!(!asLValue && CalleeReturnsReference(CE))) {
1707 CheckerVisit(CE, Dst, DstTmp3, false);
1708 return;
1709 }
1710
1711 // Handle the case where the called function returns a reference but
1712 // we expect an rvalue. For such cases, convert the reference to
1713 // an rvalue.
1714 // FIXME: This conversion doesn't actually happen unless the result
1715 // of CallExpr is consumed by another expression.
1716 ExplodedNodeSet DstTmp4;
1717 CheckerVisit(CE, DstTmp4, DstTmp3, false);
1718 QualType LoadTy = CE->getType();
1719
1720 static int *ConvertToRvalueTag = 0;
1721 for (ExplodedNodeSet::iterator NI = DstTmp4.begin(), NE = DstTmp4.end();
1722 NI!=NE; ++NI) {
1723 const GRState *state = GetState(*NI);
1724 EvalLoad(Dst, CE, *NI, state, state->getSVal(CE),
1725 &ConvertToRvalueTag, LoadTy);
1726 }
Ted Kremeneke0188e62008-02-19 01:44:53 +00001727}
1728
Ted Kremenek667cacb2008-04-15 23:06:53 +00001729//===----------------------------------------------------------------------===//
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001730// Transfer function: Objective-C ivar references.
1731//===----------------------------------------------------------------------===//
1732
Ted Kremenek111a6bd2009-02-28 20:50:43 +00001733static std::pair<const void*,const void*> EagerlyAssumeTag
1734 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1735
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001736void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
1737 Expr *Ex) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001738 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1739 ExplodedNode *Pred = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001740
Ted Kremenekff290ca2009-02-25 23:32:10 +00001741 // Test if the previous node was as the same expression. This can happen
1742 // when the expression fails to evaluate to anything meaningful and
1743 // (as an optimization) we don't generate a node.
Mike Stump11289f42009-09-09 15:08:12 +00001744 ProgramPoint P = Pred->getLocation();
Ted Kremenekff290ca2009-02-25 23:32:10 +00001745 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump11289f42009-09-09 15:08:12 +00001746 Dst.Add(Pred);
Ted Kremenekff290ca2009-02-25 23:32:10 +00001747 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001748 }
Ted Kremenekff290ca2009-02-25 23:32:10 +00001749
Mike Stump11289f42009-09-09 15:08:12 +00001750 const GRState* state = Pred->getState();
1751 SVal V = state->getSVal(Ex);
Ted Kremenek7020eae2009-09-11 22:07:28 +00001752 if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) {
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001753 // First assume that the condition is true.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001754 if (const GRState *stateTrue = state->Assume(*SEV, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00001755 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001756 ValMgr.makeIntVal(1U, Ex->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001757 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xue1190f72009-08-15 03:17:38 +00001758 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001759 stateTrue, Pred));
1760 }
Mike Stump11289f42009-09-09 15:08:12 +00001761
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001762 // Next, assume that the condition is false.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001763 if (const GRState *stateFalse = state->Assume(*SEV, false)) {
Mike Stump11289f42009-09-09 15:08:12 +00001764 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001765 ValMgr.makeIntVal(0U, Ex->getType()));
Zhongxing Xue1190f72009-08-15 03:17:38 +00001766 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
1767 Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001768 stateFalse, Pred));
1769 }
1770 }
1771 else
1772 Dst.Add(Pred);
1773 }
1774}
1775
1776//===----------------------------------------------------------------------===//
1777// Transfer function: Objective-C ivar references.
1778//===----------------------------------------------------------------------===//
1779
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001780void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, ExplodedNode* Pred,
1781 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001782
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001783 Expr* Base = cast<Expr>(Ex->getBase());
Zhongxing Xu107f7592009-08-06 12:48:26 +00001784 ExplodedNodeSet Tmp;
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001785 Visit(Base, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Zhongxing Xu107f7592009-08-06 12:48:26 +00001787 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001788 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001789 SVal BaseVal = state->getSVal(Base);
1790 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Mike Stump11289f42009-09-09 15:08:12 +00001791
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001792 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001793 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001794 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001795 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001796 }
1797}
1798
1799//===----------------------------------------------------------------------===//
Ted Kremenek17810802008-11-12 19:24:17 +00001800// Transfer function: Objective-C fast enumeration 'for' statements.
1801//===----------------------------------------------------------------------===//
1802
1803void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001804 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001805
Ted Kremenek17810802008-11-12 19:24:17 +00001806 // ObjCForCollectionStmts are processed in two places. This method
1807 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1808 // statements within a basic block. This transfer function does two things:
1809 //
1810 // (1) binds the next container value to 'element'. This creates a new
1811 // node in the ExplodedGraph.
1812 //
1813 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1814 // whether or not the container has any more elements. This value
1815 // will be tested in ProcessBranch. We need to explicitly bind
1816 // this value because a container can contain nil elements.
Mike Stump11289f42009-09-09 15:08:12 +00001817 //
Ted Kremenek17810802008-11-12 19:24:17 +00001818 // FIXME: Eventually this logic should actually do dispatches to
1819 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1820 // This will require simulating a temporary NSFastEnumerationState, either
1821 // through an SVal or through the use of MemRegions. This value can
1822 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1823 // terminates we reclaim the temporary (it goes out of scope) and we
1824 // we can test if the SVal is 0 or if the MemRegion is null (depending
1825 // on what approach we take).
1826 //
1827 // For now: simulate (1) by assigning either a symbol or nil if the
1828 // container is empty. Thus this transfer function will by default
1829 // result in state splitting.
Mike Stump11289f42009-09-09 15:08:12 +00001830
Ted Kremenek537f6382008-11-14 19:47:18 +00001831 Stmt* elem = S->getElement();
1832 SVal ElementV;
Mike Stump11289f42009-09-09 15:08:12 +00001833
Ted Kremenek17810802008-11-12 19:24:17 +00001834 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner529efc72009-03-28 06:33:19 +00001835 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek17810802008-11-12 19:24:17 +00001836 assert (ElemD->getInit() == 0);
Ted Kremenek14536f62009-08-21 22:28:32 +00001837 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek537f6382008-11-14 19:47:18 +00001838 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1839 return;
Ted Kremenek17810802008-11-12 19:24:17 +00001840 }
Ted Kremenek537f6382008-11-14 19:47:18 +00001841
Zhongxing Xu107f7592009-08-06 12:48:26 +00001842 ExplodedNodeSet Tmp;
Ted Kremenek537f6382008-11-14 19:47:18 +00001843 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001844
Zhongxing Xu107f7592009-08-06 12:48:26 +00001845 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001846 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001847 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek537f6382008-11-14 19:47:18 +00001848 }
1849}
1850
1851void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001852 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek537f6382008-11-14 19:47:18 +00001853 SVal ElementV) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001854
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001855 // Check if the location we are writing back to is a null pointer.
Ted Kremenek537f6382008-11-14 19:47:18 +00001856 Stmt* elem = S->getElement();
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001857 ExplodedNodeSet Tmp;
1858 EvalLocation(Tmp, elem, Pred, GetState(Pred), ElementV, NULL, false);
1859
1860 if (Tmp.empty())
Ted Kremenek537f6382008-11-14 19:47:18 +00001861 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001862
1863 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1864 Pred = *NI;
1865 const GRState *state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001866
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001867 // Handle the case where the container still has elements.
1868 SVal TrueV = ValMgr.makeTruthVal(1);
1869 const GRState *hasElems = state->BindExpr(S, TrueV);
Ted Kremenek537f6382008-11-14 19:47:18 +00001870
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001871 // Handle the case where the container has no elements.
1872 SVal FalseV = ValMgr.makeTruthVal(0);
1873 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump11289f42009-09-09 15:08:12 +00001874
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001875 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1876 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1877 // FIXME: The proper thing to do is to really iterate over the
1878 // container. We will do this with dispatch logic to the store.
1879 // For now, just 'conjure' up a symbolic value.
1880 QualType T = R->getValueType(getContext());
1881 assert(Loc::IsLocType(T));
1882 unsigned Count = Builder->getCurrentBlockCount();
1883 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1884 SVal V = ValMgr.makeLoc(Sym);
1885 hasElems = hasElems->bindLoc(ElementV, V);
Mike Stump11289f42009-09-09 15:08:12 +00001886
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001887 // Bind the location to 'nil' on the false branch.
1888 SVal nilV = ValMgr.makeIntVal(0, T);
1889 noElems = noElems->bindLoc(ElementV, nilV);
1890 }
Ted Kremenekdf317922008-11-12 21:12:46 +00001891
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001892 // Create the new nodes.
1893 MakeNode(Dst, S, Pred, hasElems);
1894 MakeNode(Dst, S, Pred, noElems);
1895 }
Ted Kremenek17810802008-11-12 19:24:17 +00001896}
1897
1898//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001899// Transfer function: Objective-C message expressions.
1900//===----------------------------------------------------------------------===//
1901
Zhongxing Xu107f7592009-08-06 12:48:26 +00001902void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
Ted Kremeneke19711d2009-12-22 22:13:46 +00001903 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump11289f42009-09-09 15:08:12 +00001904
Ted Kremenek667cacb2008-04-15 23:06:53 +00001905 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
Ted Kremeneke19711d2009-12-22 22:13:46 +00001906 Pred, Dst, asLValue);
Mike Stump11289f42009-09-09 15:08:12 +00001907}
Ted Kremenek667cacb2008-04-15 23:06:53 +00001908
1909void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Ted Kremeneke19711d2009-12-22 22:13:46 +00001910 ObjCMessageExpr::arg_iterator AI,
1911 ObjCMessageExpr::arg_iterator AE,
1912 ExplodedNode* Pred,
1913 ExplodedNodeSet& Dst,
1914 bool asLValue) {
Ted Kremenek667cacb2008-04-15 23:06:53 +00001915 if (AI == AE) {
Mike Stump11289f42009-09-09 15:08:12 +00001916
Ted Kremenek667cacb2008-04-15 23:06:53 +00001917 // Process the receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001918
Ted Kremenek667cacb2008-04-15 23:06:53 +00001919 if (Expr* Receiver = ME->getReceiver()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001920 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001921 Visit(Receiver, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001922
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001923 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE;
1924 ++NI)
Ted Kremeneke19711d2009-12-22 22:13:46 +00001925 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst, asLValue);
Mike Stump11289f42009-09-09 15:08:12 +00001926
Ted Kremenek667cacb2008-04-15 23:06:53 +00001927 return;
1928 }
Mike Stump11289f42009-09-09 15:08:12 +00001929
Ted Kremeneke19711d2009-12-22 22:13:46 +00001930 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst, asLValue);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001931 return;
1932 }
Mike Stump11289f42009-09-09 15:08:12 +00001933
Zhongxing Xu107f7592009-08-06 12:48:26 +00001934 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001935 Visit(*AI, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001936
Ted Kremenek667cacb2008-04-15 23:06:53 +00001937 ++AI;
Mike Stump11289f42009-09-09 15:08:12 +00001938
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001939 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremeneke19711d2009-12-22 22:13:46 +00001940 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst, asLValue);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001941}
1942
1943void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001944 ExplodedNode* Pred,
Ted Kremeneke19711d2009-12-22 22:13:46 +00001945 ExplodedNodeSet& Dst,
1946 bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001947
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001948 // Handle previsits checks.
1949 ExplodedNodeSet Src, DstTmp;
Ted Kremenek005e8a02009-11-24 21:41:28 +00001950 Src.Add(Pred);
1951
Zhongxing Xuaf353292009-12-02 05:49:12 +00001952 CheckerVisit(ME, DstTmp, Src, true);
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001953
Ted Kremeneke19711d2009-12-22 22:13:46 +00001954 ExplodedNodeSet PostVisitSrc;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001955
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001956 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001957 DI!=DE; ++DI) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00001958
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001959 Pred = *DI;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001960 bool RaisesException = false;
Ted Kremeneke19711d2009-12-22 22:13:46 +00001961
1962 unsigned OldSize = PostVisitSrc.size();
1963 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1964 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001965
Zhongxing Xuaf353292009-12-02 05:49:12 +00001966 if (const Expr *Receiver = ME->getReceiver()) {
1967 const GRState *state = Pred->getState();
1968
1969 // Bifurcate the state into nil and non-nil ones.
1970 DefinedOrUnknownSVal receiverVal =
1971 cast<DefinedOrUnknownSVal>(state->getSVal(Receiver));
1972
1973 const GRState *notNilState, *nilState;
1974 llvm::tie(notNilState, nilState) = state->Assume(receiverVal);
1975
1976 // There are three cases: can be nil or non-nil, must be nil, must be
1977 // non-nil. We handle must be nil, and merge the rest two into non-nil.
1978 if (nilState && !notNilState) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00001979 CheckerEvalNilReceiver(ME, PostVisitSrc, nilState, Pred);
1980 continue;
Zhongxing Xuaf353292009-12-02 05:49:12 +00001981 }
1982
1983 assert(notNilState);
1984
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001985 // Check if the "raise" message was sent.
1986 if (ME->getSelector() == RaiseSel)
1987 RaisesException = true;
Zhongxing Xuaf353292009-12-02 05:49:12 +00001988
1989 // Check if we raise an exception. For now treat these as sinks.
1990 // Eventually we will want to handle exceptions properly.
Zhongxing Xuaf353292009-12-02 05:49:12 +00001991 if (RaisesException)
1992 Builder->BuildSinks = true;
1993
1994 // Dispatch to plug-in transfer function.
Ted Kremeneke19711d2009-12-22 22:13:46 +00001995 EvalObjCMessageExpr(PostVisitSrc, ME, Pred, notNilState);
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001996 }
1997 else {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001998 IdentifierInfo* ClsName = ME->getClassName();
1999 Selector S = ME->getSelector();
2000
2001 // Check for special instance methods.
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002002 if (!NSExceptionII) {
2003 ASTContext& Ctx = getContext();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002004 NSExceptionII = &Ctx.Idents.get("NSException");
2005 }
2006
2007 if (ClsName == NSExceptionII) {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002008 enum { NUM_RAISE_SELECTORS = 2 };
2009
2010 // Lazily create a cache of the selectors.
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002011 if (!NSExceptionInstanceRaiseSelectors) {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002012 ASTContext& Ctx = getContext();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002013 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002014 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
2015 unsigned idx = 0;
2016
2017 // raise:format:
2018 II.push_back(&Ctx.Idents.get("raise"));
2019 II.push_back(&Ctx.Idents.get("format"));
2020 NSExceptionInstanceRaiseSelectors[idx++] =
2021 Ctx.Selectors.getSelector(II.size(), &II[0]);
2022
2023 // raise:format::arguments:
2024 II.push_back(&Ctx.Idents.get("arguments"));
2025 NSExceptionInstanceRaiseSelectors[idx++] =
2026 Ctx.Selectors.getSelector(II.size(), &II[0]);
2027 }
2028
2029 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2030 if (S == NSExceptionInstanceRaiseSelectors[i]) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00002031 RaisesException = true;
2032 break;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002033 }
2034 }
Zhongxing Xuaf353292009-12-02 05:49:12 +00002035
2036 // Check if we raise an exception. For now treat these as sinks.
2037 // Eventually we will want to handle exceptions properly.
Zhongxing Xuaf353292009-12-02 05:49:12 +00002038 if (RaisesException)
2039 Builder->BuildSinks = true;
2040
2041 // Dispatch to plug-in transfer function.
Ted Kremeneke19711d2009-12-22 22:13:46 +00002042 EvalObjCMessageExpr(PostVisitSrc, ME, Pred, Builder->GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002043 }
Ted Kremeneke19711d2009-12-22 22:13:46 +00002044
2045 // Handle the case where no nodes where generated. Auto-generate that
2046 // contains the updated state if we aren't generating sinks.
2047 if (!Builder->BuildSinks && PostVisitSrc.size() == OldSize &&
2048 !Builder->HasGeneratedNode)
2049 MakeNode(PostVisitSrc, ME, Pred, GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002050 }
Mike Stump11289f42009-09-09 15:08:12 +00002051
Ted Kremeneke19711d2009-12-22 22:13:46 +00002052 // Finally, perform the post-condition check of the ObjCMessageExpr and store
2053 // the created nodes in 'Dst'.
2054 if (!(!asLValue && ReceiverReturnsReference(ME))) {
2055 CheckerVisit(ME, Dst, PostVisitSrc, false);
2056 return;
2057 }
2058
2059 // Handle the case where the message expression returns a reference but
2060 // we expect an rvalue. For such cases, convert the reference to
2061 // an rvalue.
2062 // FIXME: This conversion doesn't actually happen unless the result
2063 // of ObjCMessageExpr is consumed by another expression.
2064 ExplodedNodeSet DstRValueConvert;
2065 CheckerVisit(ME, DstRValueConvert, PostVisitSrc, false);
2066 QualType LoadTy = ME->getType();
2067
2068 static int *ConvertToRvalueTag = 0;
2069 for (ExplodedNodeSet::iterator NI = DstRValueConvert.begin(),
2070 NE = DstRValueConvert.end();
2071 NI!=NE; ++NI) {
2072 const GRState *state = GetState(*NI);
2073 EvalLoad(Dst, ME, *NI, state, state->getSVal(ME),
2074 &ConvertToRvalueTag, LoadTy);
2075 }
Ted Kremenek667cacb2008-04-15 23:06:53 +00002076}
2077
2078//===----------------------------------------------------------------------===//
2079// Transfer functions: Miscellaneous statements.
2080//===----------------------------------------------------------------------===//
2081
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002082void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred,
2083 ExplodedNodeSet& Dst){
Zhongxing Xu107f7592009-08-06 12:48:26 +00002084 ExplodedNodeSet S1;
Ted Kremenek9fd25312008-02-19 18:52:54 +00002085 QualType T = CastE->getType();
Zhongxing Xudab76fd2008-10-21 06:54:23 +00002086 QualType ExTy = Ex->getType();
Zhongxing Xuc2721522008-10-22 08:02:16 +00002087
Zhongxing Xu4de1c852008-10-31 07:26:14 +00002088 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregore200adc2008-10-27 19:41:14 +00002089 T = ExCast->getTypeAsWritten();
2090
Zhongxing Xuc2721522008-10-22 08:02:16 +00002091 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002092 VisitLValue(Ex, Pred, S1);
Ted Kremenek9f3f8272008-03-04 22:16:08 +00002093 else
2094 Visit(Ex, Pred, S1);
Mike Stump11289f42009-09-09 15:08:12 +00002095
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002096 ExplodedNodeSet S2;
2097 CheckerVisit(CastE, S2, S1, true);
2098
Ted Kremenekeccf3e52008-04-22 21:10:18 +00002099 // Check for casting to "void".
Mike Stump11289f42009-09-09 15:08:12 +00002100 if (T->isVoidType()) {
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002101 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I)
2102 Dst.Add(*I);
Ted Kremenek33d82852008-01-24 02:02:54 +00002103 return;
2104 }
Ted Kremenekac7c7242009-07-21 21:03:30 +00002105
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002106 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2107 ExplodedNode* N = *I;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002108 const GRState* state = GetState(N);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002109 SVal V = state->getSVal(Ex);
Ted Kremenekac7c7242009-07-21 21:03:30 +00002110 const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002111 state = Res.getState()->BindExpr(CastE, Res.getSVal());
Ted Kremenekac7c7242009-07-21 21:03:30 +00002112 MakeNode(Dst, CastE, N, state);
Ted Kremenek33d82852008-01-24 02:02:54 +00002113 }
Ted Kremenek05352742008-01-24 20:55:43 +00002114}
2115
Ted Kremenekbf263682008-10-27 21:54:31 +00002116void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Mike Stump11289f42009-09-09 15:08:12 +00002117 ExplodedNode* Pred,
2118 ExplodedNodeSet& Dst,
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002119 bool asLValue) {
Ted Kremenekbf263682008-10-27 21:54:31 +00002120 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002121 ExplodedNodeSet Tmp;
Ted Kremenekbf263682008-10-27 21:54:31 +00002122 Visit(ILE, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002123
Zhongxing Xu107f7592009-08-06 12:48:26 +00002124 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002125 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002126 SVal ILV = state->getSVal(ILE);
Ted Kremenek04af9f22009-12-07 22:05:27 +00002127 const LocationContext *LC = (*I)->getLocationContext();
2128 state = state->bindCompoundLiteral(CL, LC, ILV);
Ted Kremenekbf263682008-10-27 21:54:31 +00002129
Ted Kremenek04af9f22009-12-07 22:05:27 +00002130 if (asLValue) {
2131 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL, LC)));
2132 }
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002133 else
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002134 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenekbf263682008-10-27 21:54:31 +00002135 }
2136}
2137
Ted Kremenek14536f62009-08-21 22:28:32 +00002138void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +00002139 ExplodedNodeSet& Dst) {
Ted Kremenek3b427152008-04-22 22:25:27 +00002140
Mike Stump11289f42009-09-09 15:08:12 +00002141 // The CFG has one DeclStmt per Decl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002142 Decl* D = *DS->decl_begin();
Mike Stump11289f42009-09-09 15:08:12 +00002143
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002144 if (!D || !isa<VarDecl>(D))
Ted Kremenek3b427152008-04-22 22:25:27 +00002145 return;
Mike Stump11289f42009-09-09 15:08:12 +00002146
2147 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek17810802008-11-12 19:24:17 +00002148 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek3b427152008-04-22 22:25:27 +00002149
2150 // FIXME: static variables may have an initializer, but the second
2151 // time a function is called those values may not be current.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002152 ExplodedNodeSet Tmp;
Ted Kremenek3b427152008-04-22 22:25:27 +00002153
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002154 if (InitEx) {
2155 if (VD->getType()->isReferenceType())
2156 VisitLValue(InitEx, Pred, Tmp);
2157 else
2158 Visit(InitEx, Pred, Tmp);
2159 }
Ted Kremenekfc311292009-07-17 23:48:26 +00002160 else
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002161 Tmp.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002162
Ted Kremenekae3361d2009-11-07 03:56:57 +00002163 ExplodedNodeSet Tmp2;
2164 CheckerVisit(DS, Tmp2, Tmp, true);
2165
2166 for (ExplodedNodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002167 ExplodedNode *N = *I;
Ted Kremenekae3361d2009-11-07 03:56:57 +00002168 const GRState *state = GetState(N);
Zhongxing Xu27fee832009-11-03 12:13:38 +00002169
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00002170 // Decls without InitExpr are not initialized explicitly.
Zhongxing Xu27fee832009-11-03 12:13:38 +00002171 const LocationContext *LC = N->getLocationContext();
Ted Kremenek14536f62009-08-21 22:28:32 +00002172
Ted Kremenek17810802008-11-12 19:24:17 +00002173 if (InitEx) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002174 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek17810802008-11-12 19:24:17 +00002175 QualType T = VD->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002176
Ted Kremenek17810802008-11-12 19:24:17 +00002177 // Recover some path-sensitivity if a scalar value evaluated to
2178 // UnknownVal.
Mike Stump11289f42009-09-09 15:08:12 +00002179 if (InitVal.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002180 !getConstraintManager().canReasonAbout(InitVal)) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002181 InitVal = ValMgr.getConjuredSymbolVal(NULL, InitEx,
2182 Builder->getCurrentBlockCount());
Mike Stump11289f42009-09-09 15:08:12 +00002183 }
Ted Kremenekb006b822009-11-04 00:09:15 +00002184
Ted Kremenek209e31b2009-11-05 00:42:23 +00002185 EvalBind(Dst, DS, DS, *I, state,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002186 loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true);
Mike Stump11289f42009-09-09 15:08:12 +00002187 }
Ted Kremenek13363532009-02-14 01:54:57 +00002188 else {
Ted Kremenekb006b822009-11-04 00:09:15 +00002189 state = state->bindDeclWithNoInit(state->getRegion(VD, LC));
Ted Kremenek13363532009-02-14 01:54:57 +00002190 MakeNode(Dst, DS, *I, state);
Ted Kremenek8f7afdd2008-12-08 22:47:34 +00002191 }
Ted Kremenek3b427152008-04-22 22:25:27 +00002192 }
Ted Kremenek05352742008-01-24 20:55:43 +00002193}
Ted Kremenek33d82852008-01-24 02:02:54 +00002194
Ted Kremenekf68bf632008-10-30 17:47:32 +00002195namespace {
2196 // This class is used by VisitInitListExpr as an item in a worklist
2197 // for processing the values contained in an InitListExpr.
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002198class InitListWLItem {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002199public:
2200 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu107f7592009-08-06 12:48:26 +00002201 ExplodedNode* N;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002202 InitListExpr::reverse_iterator Itr;
Mike Stump11289f42009-09-09 15:08:12 +00002203
Zhongxing Xu107f7592009-08-06 12:48:26 +00002204 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
2205 InitListExpr::reverse_iterator itr)
Ted Kremenekf68bf632008-10-30 17:47:32 +00002206 : Vals(vals), N(n), Itr(itr) {}
2207};
2208}
2209
2210
Mike Stump11289f42009-09-09 15:08:12 +00002211void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002212 ExplodedNodeSet& Dst) {
Ted Kremenek828e6df2008-10-30 23:14:36 +00002213
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002214 const GRState* state = GetState(Pred);
Ted Kremenek45698bf2008-11-13 05:05:34 +00002215 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002216 unsigned NumInitElements = E->getNumInits();
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002217
Ted Kremeneka41d9dd2009-07-28 20:46:55 +00002218 if (T->isArrayType() || T->isStructureType() ||
2219 T->isUnionType() || T->isVectorType()) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002220
Ted Kremenek828e6df2008-10-30 23:14:36 +00002221 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump11289f42009-09-09 15:08:12 +00002222
Ted Kremenek828e6df2008-10-30 23:14:36 +00002223 // Handle base case where the initializer has no elements.
2224 // e.g: static int* myArray[] = {};
2225 if (NumInitElements == 0) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002226 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002227 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremenek828e6df2008-10-30 23:14:36 +00002228 return;
Mike Stump11289f42009-09-09 15:08:12 +00002229 }
2230
Ted Kremenek828e6df2008-10-30 23:14:36 +00002231 // Create a worklist to process the initializers.
2232 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump11289f42009-09-09 15:08:12 +00002233 WorkList.reserve(NumInitElements);
2234 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002235 InitListExpr::reverse_iterator ItrEnd = E->rend();
Ted Kremenek30030012009-09-22 21:19:14 +00002236 assert(!(E->rbegin() == E->rend()));
Mike Stump11289f42009-09-09 15:08:12 +00002237
Ted Kremenek828e6df2008-10-30 23:14:36 +00002238 // Process the worklist until it is empty.
Ted Kremenekf68bf632008-10-30 17:47:32 +00002239 while (!WorkList.empty()) {
2240 InitListWLItem X = WorkList.back();
2241 WorkList.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +00002242
Zhongxing Xu107f7592009-08-06 12:48:26 +00002243 ExplodedNodeSet Tmp;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002244 Visit(*X.Itr, X.N, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002245
Ted Kremenekf68bf632008-10-30 17:47:32 +00002246 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002247
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002248 for (ExplodedNodeSet::iterator NI=Tmp.begin(),NE=Tmp.end();NI!=NE;++NI) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002249 // Get the last initializer value.
2250 state = GetState(*NI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002251 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump11289f42009-09-09 15:08:12 +00002252
Ted Kremenekf68bf632008-10-30 17:47:32 +00002253 // Construct the new list of values by prepending the new value to
2254 // the already constructed list.
2255 llvm::ImmutableList<SVal> NewVals =
2256 getBasicVals().consVals(InitV, X.Vals);
Mike Stump11289f42009-09-09 15:08:12 +00002257
Ted Kremenekf68bf632008-10-30 17:47:32 +00002258 if (NewItr == ItrEnd) {
Zhongxing Xu121a53a2008-10-31 03:01:26 +00002259 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002260 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002261
Ted Kremenekf68bf632008-10-30 17:47:32 +00002262 // Make final state and node.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002263 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002264 }
2265 else {
2266 // Still some initializer values to go. Push them onto the worklist.
2267 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2268 }
2269 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002270 }
Mike Stump11289f42009-09-09 15:08:12 +00002271
Ted Kremenek28f41ba2008-10-30 18:34:31 +00002272 return;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002273 }
2274
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002275 if (Loc::IsLocType(T) || T->isIntegerType()) {
2276 assert (E->getNumInits() == 1);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002277 ExplodedNodeSet Tmp;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002278 Expr* Init = E->getInit(0);
2279 Visit(Init, Pred, Tmp);
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002280 for (ExplodedNodeSet::iterator I=Tmp.begin(), EI=Tmp.end(); I != EI; ++I) {
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002281 state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002282 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002283 }
2284 return;
2285 }
2286
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002287 assert(0 && "unprocessed InitListExpr type");
2288}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00002289
Sebastian Redl6f282892008-11-11 17:56:53 +00002290/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2291void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002292 ExplodedNode* Pred,
2293 ExplodedNodeSet& Dst) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002294 QualType T = Ex->getTypeOfArgument();
Mike Stump11289f42009-09-09 15:08:12 +00002295 uint64_t amt;
2296
Ted Kremenekae5b7862008-03-15 03:13:20 +00002297 if (Ex->isSizeOf()) {
Mike Stump11289f42009-09-09 15:08:12 +00002298 if (T == getContext().VoidTy) {
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002299 // sizeof(void) == 1 byte.
2300 amt = 1;
2301 }
2302 else if (!T.getTypePtr()->isConstantSizeType()) {
2303 // FIXME: Add support for VLAs.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002304 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002305 }
2306 else if (T->isObjCInterfaceType()) {
2307 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2308 // the compiler has laid out its representation. Just report Unknown
Mike Stump11289f42009-09-09 15:08:12 +00002309 // for these.
Ted Kremenek99057462008-04-30 21:31:12 +00002310 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002311 }
2312 else {
2313 // All other cases.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002314 amt = getContext().getTypeSize(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002315 }
Ted Kremenekae5b7862008-03-15 03:13:20 +00002316 }
2317 else // Get alignment of the type.
Ted Kremenek88ba7502008-03-15 03:13:55 +00002318 amt = getContext().getTypeAlign(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002319
Ted Kremenek181f7232008-03-21 21:30:14 +00002320 MakeNode(Dst, Ex, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002321 GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenek002bf742008-02-12 19:49:57 +00002322}
2323
Ted Kremenekb597bb92008-02-20 04:02:35 +00002324
Zhongxing Xu107f7592009-08-06 12:48:26 +00002325void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
2326 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002327
Ted Kremenekb597bb92008-02-20 04:02:35 +00002328 switch (U->getOpcode()) {
Mike Stump11289f42009-09-09 15:08:12 +00002329
Ted Kremenekb597bb92008-02-20 04:02:35 +00002330 default:
Ted Kremenekb597bb92008-02-20 04:02:35 +00002331 break;
Mike Stump11289f42009-09-09 15:08:12 +00002332
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002333 case UnaryOperator::Deref: {
Mike Stump11289f42009-09-09 15:08:12 +00002334
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002335 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002336 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002337 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002338
Zhongxing Xu107f7592009-08-06 12:48:26 +00002339 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002340
Ted Kremenek17d541d2009-02-13 01:45:31 +00002341 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002342 SVal location = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002343
Zhongxing Xu232c7922008-10-16 06:09:51 +00002344 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002345 MakeNode(Dst, U, *I, state->BindExpr(U, location),
Ted Kremeneka6e08322009-05-07 18:27:16 +00002346 ProgramPoint::PostLValueKind);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002347 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00002348 EvalLoad(Dst, U, *I, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002349 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002350
2351 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002352 }
Mike Stump11289f42009-09-09 15:08:12 +00002353
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002354 case UnaryOperator::Real: {
Mike Stump11289f42009-09-09 15:08:12 +00002355
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002356 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002357 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002358 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002359
Zhongxing Xu107f7592009-08-06 12:48:26 +00002360 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002361
Zhongxing Xu27f17422008-10-17 05:57:07 +00002362 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002363 if (Ex->getType()->isAnyComplexType()) {
2364 // Just report "Unknown."
2365 Dst.Add(*I);
2366 continue;
2367 }
Mike Stump11289f42009-09-09 15:08:12 +00002368
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002369 // For all other types, UnaryOperator::Real is an identity operation.
2370 assert (U->getType() == Ex->getType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002371 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002372 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump11289f42009-09-09 15:08:12 +00002373 }
2374
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002375 return;
2376 }
Mike Stump11289f42009-09-09 15:08:12 +00002377
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002378 case UnaryOperator::Imag: {
Mike Stump11289f42009-09-09 15:08:12 +00002379
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002380 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002381 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002382 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002383
Zhongxing Xu107f7592009-08-06 12:48:26 +00002384 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu27f17422008-10-17 05:57:07 +00002385 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002386 if (Ex->getType()->isAnyComplexType()) {
2387 // Just report "Unknown."
2388 Dst.Add(*I);
2389 continue;
2390 }
Mike Stump11289f42009-09-09 15:08:12 +00002391
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002392 // For all other types, UnaryOperator::Float returns 0.
2393 assert (Ex->getType()->isIntegerType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002394 const GRState* state = GetState(*I);
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002395 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002396 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002397 }
Mike Stump11289f42009-09-09 15:08:12 +00002398
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002399 return;
2400 }
Mike Stump11289f42009-09-09 15:08:12 +00002401
Ted Kremenek27347132009-09-15 00:40:32 +00002402 case UnaryOperator::OffsetOf: {
Ted Kremenekb6622942009-09-15 04:19:09 +00002403 Expr::EvalResult Res;
2404 if (U->Evaluate(Res, getContext()) && Res.Val.isInt()) {
2405 const APSInt &IV = Res.Val.getInt();
2406 assert(IV.getBitWidth() == getContext().getTypeSize(U->getType()));
2407 assert(U->getType()->isIntegerType());
2408 assert(IV.isSigned() == U->getType()->isSignedIntegerType());
2409 SVal X = ValMgr.makeIntVal(IV);
2410 MakeNode(Dst, U, Pred, GetState(Pred)->BindExpr(U, X));
2411 return;
2412 }
2413 // FIXME: Handle the case where __builtin_offsetof is not a constant.
2414 Dst.Add(Pred);
Ted Kremenekca67cab2008-04-30 21:45:55 +00002415 return;
Ted Kremenek27347132009-09-15 00:40:32 +00002416 }
Mike Stump11289f42009-09-09 15:08:12 +00002417
Zhongxing Xu232c7922008-10-16 06:09:51 +00002418 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002419 case UnaryOperator::Extension: {
Mike Stump11289f42009-09-09 15:08:12 +00002420
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002421 // Unary "+" is a no-op, similar to a parentheses. We still have places
2422 // where it may be a block-level expression, so we need to
2423 // generate an extra node that just propagates the value of the
2424 // subexpression.
2425
2426 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002427 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002428 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002429
2430 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002431 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002432 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002433 }
Mike Stump11289f42009-09-09 15:08:12 +00002434
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002435 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002436 }
Mike Stump11289f42009-09-09 15:08:12 +00002437
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002438 case UnaryOperator::AddrOf: {
Mike Stump11289f42009-09-09 15:08:12 +00002439
Zhongxing Xu232c7922008-10-16 06:09:51 +00002440 assert(!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002441 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002442 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002443 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002444
2445 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002446 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002447 SVal V = state->getSVal(Ex);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002448 state = state->BindExpr(U, V);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002449 MakeNode(Dst, U, *I, state);
Ted Kremenek7f8ebb72008-02-21 19:15:37 +00002450 }
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002451
Mike Stump11289f42009-09-09 15:08:12 +00002452 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002453 }
Mike Stump11289f42009-09-09 15:08:12 +00002454
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002455 case UnaryOperator::LNot:
2456 case UnaryOperator::Minus:
2457 case UnaryOperator::Not: {
Mike Stump11289f42009-09-09 15:08:12 +00002458
Zhongxing Xu232c7922008-10-16 06:09:51 +00002459 assert (!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002460 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002461 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002462 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002463
2464 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002465 const GRState* state = GetState(*I);
Mike Stump11289f42009-09-09 15:08:12 +00002466
Ted Kremenek76bccf62008-09-30 05:32:44 +00002467 // Get the value of the subexpression.
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002468 SVal V = state->getSVal(Ex);
Ted Kremenek76bccf62008-09-30 05:32:44 +00002469
Ted Kremenek1ca33462008-11-15 00:20:05 +00002470 if (V.isUnknownOrUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002471 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremenek1ca33462008-11-15 00:20:05 +00002472 continue;
2473 }
Mike Stump11289f42009-09-09 15:08:12 +00002474
Ted Kremenek44137142008-11-15 04:01:56 +00002475// QualType DstT = getContext().getCanonicalType(U->getType());
2476// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002477//
Ted Kremenek44137142008-11-15 04:01:56 +00002478// if (DstT != SrcT) // Perform promotions.
Mike Stump11289f42009-09-09 15:08:12 +00002479// V = EvalCast(V, DstT);
2480//
Ted Kremenek44137142008-11-15 04:01:56 +00002481// if (V.isUnknownOrUndef()) {
2482// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2483// continue;
2484// }
Mike Stump11289f42009-09-09 15:08:12 +00002485
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002486 switch (U->getOpcode()) {
2487 default:
2488 assert(false && "Invalid Opcode.");
2489 break;
Mike Stump11289f42009-09-09 15:08:12 +00002490
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002491 case UnaryOperator::Not:
Ted Kremenekd331d092008-10-01 00:21:14 +00002492 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002493 state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002494 break;
2495
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002496 case UnaryOperator::Minus:
Ted Kremenekd331d092008-10-01 00:21:14 +00002497 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002498 state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002499 break;
2500
2501 case UnaryOperator::LNot:
2502
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002503 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2504 //
2505 // Note: technically we do "E == 0", but this is the same in the
2506 // transfer functions as "0 == E".
Ted Kremenek1642bda2009-06-26 00:05:51 +00002507 SVal Result;
Mike Stump11289f42009-09-09 15:08:12 +00002508
Zhongxing Xu27f17422008-10-17 05:57:07 +00002509 if (isa<Loc>(V)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002510 Loc X = ValMgr.makeNull();
Ted Kremenek1642bda2009-06-26 00:05:51 +00002511 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2512 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002513 }
2514 else {
Ted Kremenek44137142008-11-15 04:01:56 +00002515 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenek8ec57712009-10-06 01:39:48 +00002516 Result = EvalBinOp(state, BinaryOperator::EQ, cast<NonLoc>(V), X,
Ted Kremenek1642bda2009-06-26 00:05:51 +00002517 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002518 }
Mike Stump11289f42009-09-09 15:08:12 +00002519
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002520 state = state->BindExpr(U, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002521
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002522 break;
2523 }
Mike Stump11289f42009-09-09 15:08:12 +00002524
Ted Kremenek17d541d2009-02-13 01:45:31 +00002525 MakeNode(Dst, U, *I, state);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002526 }
Mike Stump11289f42009-09-09 15:08:12 +00002527
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002528 return;
2529 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002530 }
2531
2532 // Handle ++ and -- (both pre- and post-increment).
2533
2534 assert (U->isIncrementDecrementOp());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002535 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002536 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu232c7922008-10-16 06:09:51 +00002537 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002538
Zhongxing Xu107f7592009-08-06 12:48:26 +00002539 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002540
Ted Kremenek17d541d2009-02-13 01:45:31 +00002541 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002542 SVal V1 = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002543
2544 // Perform a load.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002545 ExplodedNodeSet Tmp2;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002546 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002547
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002548 for (ExplodedNodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end();I2!=E2;++I2) {
Mike Stump11289f42009-09-09 15:08:12 +00002549
Ted Kremenek17d541d2009-02-13 01:45:31 +00002550 state = GetState(*I2);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002551 SVal V2_untested = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002552
2553 // Propagate unknown and undefined values.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002554 if (V2_untested.isUnknownOrUndef()) {
2555 MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested));
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002556 continue;
Ted Kremenek7020eae2009-09-11 22:07:28 +00002557 }
2558 DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
Mike Stump11289f42009-09-09 15:08:12 +00002559
2560 // Handle all other values.
Ted Kremeneke81734b2008-02-15 22:09:30 +00002561 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2562 : BinaryOperator::Sub;
Ted Kremenek32c41ec2009-03-11 03:54:24 +00002563
Zhongxing Xufe971652009-08-05 02:51:59 +00002564 // If the UnaryOperator has non-location type, use its type to create the
2565 // constant value. If the UnaryOperator has location type, create the
2566 // constant with int type and pointer width.
2567 SVal RHS;
2568
2569 if (U->getType()->isAnyPointerType())
2570 RHS = ValMgr.makeIntValWithPtrWidth(1, false);
2571 else
2572 RHS = ValMgr.makeIntVal(1, U->getType());
2573
Mike Stump11289f42009-09-09 15:08:12 +00002574 SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
2575
Ted Kremenek6b315332009-03-20 20:10:45 +00002576 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002577 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek7020eae2009-09-11 22:07:28 +00002578 DefinedOrUnknownSVal SymVal =
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002579 ValMgr.getConjuredSymbolVal(NULL, Ex,
2580 Builder->getCurrentBlockCount());
Ted Kremenek7020eae2009-09-11 22:07:28 +00002581 Result = SymVal;
Mike Stump11289f42009-09-09 15:08:12 +00002582
Ted Kremenek35f875c2009-04-21 22:38:05 +00002583 // If the value is a location, ++/-- should always preserve
Ted Kremenek1642bda2009-06-26 00:05:51 +00002584 // non-nullness. Check if the original value was non-null, and if so
Mike Stump11289f42009-09-09 15:08:12 +00002585 // propagate that constraint.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002586 if (Loc::IsLocType(U->getType())) {
Ted Kremenek7020eae2009-09-11 22:07:28 +00002587 DefinedOrUnknownSVal Constraint =
2588 SVator.EvalEQ(state, V2, ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002589
Ted Kremenek7020eae2009-09-11 22:07:28 +00002590 if (!state->Assume(Constraint, true)) {
Ted Kremenek35f875c2009-04-21 22:38:05 +00002591 // It isn't feasible for the original value to be null.
2592 // Propagate this constraint.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002593 Constraint = SVator.EvalEQ(state, SymVal,
2594 ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002595
Ted Kremenek7020eae2009-09-11 22:07:28 +00002596
2597 state = state->Assume(Constraint, false);
Ted Kremenekf9906842009-06-18 22:57:13 +00002598 assert(state);
Mike Stump11289f42009-09-09 15:08:12 +00002599 }
2600 }
Ted Kremenek35f875c2009-04-21 22:38:05 +00002601 }
Mike Stump11289f42009-09-09 15:08:12 +00002602
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002603 state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002604
Mike Stump11289f42009-09-09 15:08:12 +00002605 // Perform the store.
Ted Kremenek209e31b2009-11-05 00:42:23 +00002606 EvalStore(Dst, NULL, U, *I2, state, V1, Result);
Ted Kremenek43523e02008-02-07 01:08:27 +00002607 }
Ted Kremenek38213f92008-04-21 23:43:38 +00002608 }
Ted Kremenek43523e02008-02-07 01:08:27 +00002609}
2610
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002611
2612void GRExprEngine::VisitCXXThisExpr(CXXThisExpr *TE, ExplodedNode *Pred,
2613 ExplodedNodeSet & Dst) {
2614 // Get the this object region from StoreManager.
2615 Loc V = getStoreManager().getThisObject(TE->getType()->getPointeeType());
2616 MakeNode(Dst, TE, Pred, GetState(Pred)->BindExpr(TE, V));
2617}
2618
2619void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred,
2620 ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002621 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00002622}
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002623
2624void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2625 AsmStmt::outputs_iterator I,
2626 AsmStmt::outputs_iterator E,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002627 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002628 if (I == E) {
2629 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2630 return;
2631 }
Mike Stump11289f42009-09-09 15:08:12 +00002632
Zhongxing Xu107f7592009-08-06 12:48:26 +00002633 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002634 VisitLValue(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002635
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002636 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002637
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002638 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002639 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2640}
2641
2642void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2643 AsmStmt::inputs_iterator I,
2644 AsmStmt::inputs_iterator E,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002645 ExplodedNode* Pred,
2646 ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002647 if (I == E) {
Mike Stump11289f42009-09-09 15:08:12 +00002648
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002649 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu27f17422008-10-17 05:57:07 +00002650 // should evaluate to Locs. Nuke all of their values.
Mike Stump11289f42009-09-09 15:08:12 +00002651
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002652 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2653 // which interprets the inline asm and stores proper results in the
2654 // outputs.
Mike Stump11289f42009-09-09 15:08:12 +00002655
Ted Kremenek17d541d2009-02-13 01:45:31 +00002656 const GRState* state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002657
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002658 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2659 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump11289f42009-09-09 15:08:12 +00002660
2661 SVal X = state->getSVal(*OI);
Zhongxing Xu27f17422008-10-17 05:57:07 +00002662 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump11289f42009-09-09 15:08:12 +00002663
Zhongxing Xu27f17422008-10-17 05:57:07 +00002664 if (isa<Loc>(X))
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002665 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002666 }
Mike Stump11289f42009-09-09 15:08:12 +00002667
Ted Kremenek17d541d2009-02-13 01:45:31 +00002668 MakeNode(Dst, A, Pred, state);
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002669 return;
2670 }
Mike Stump11289f42009-09-09 15:08:12 +00002671
Zhongxing Xu107f7592009-08-06 12:48:26 +00002672 ExplodedNodeSet Tmp;
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002673 Visit(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002674
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002675 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002676
Ted Kremenek17a02962009-09-03 03:02:58 +00002677 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002678 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2679}
2680
Ted Kremenekbee01e52009-11-06 02:24:13 +00002681void GRExprEngine::VisitReturnStmt(ReturnStmt *RS, ExplodedNode *Pred,
2682 ExplodedNodeSet &Dst) {
2683
2684 ExplodedNodeSet Src;
2685 if (Expr *RetE = RS->getRetValue()) {
2686 Visit(RetE, Pred, Src);
Ted Kremenekf6467742008-03-31 15:02:58 +00002687 }
Ted Kremenekbee01e52009-11-06 02:24:13 +00002688 else {
2689 Src.Add(Pred);
2690 }
2691
2692 ExplodedNodeSet CheckedSet;
2693 CheckerVisit(RS, CheckedSet, Src, true);
2694
2695 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
2696 I != E; ++I) {
Ted Kremenek9c375152008-04-16 23:05:51 +00002697
Ted Kremenekbee01e52009-11-06 02:24:13 +00002698 assert(Builder && "GRStmtNodeBuilder must be defined.");
2699
2700 Pred = *I;
2701 unsigned size = Dst.size();
2702
2703 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2704 SaveOr OldHasGen(Builder->HasGeneratedNode);
2705
2706 getTF().EvalReturn(Dst, *this, *Builder, RS, Pred);
2707
2708 // Handle the case where no nodes where generated.
2709 if (!Builder->BuildSinks && Dst.size() == size &&
2710 !Builder->HasGeneratedNode)
2711 MakeNode(Dst, RS, Pred, GetState(Pred));
Ted Kremenek0b63f962008-11-21 00:27:44 +00002712 }
Ted Kremenekf6467742008-03-31 15:02:58 +00002713}
Ted Kremenek64100da2008-03-25 00:34:37 +00002714
Ted Kremenek667cacb2008-04-15 23:06:53 +00002715//===----------------------------------------------------------------------===//
2716// Transfer functions: Binary operators.
2717//===----------------------------------------------------------------------===//
2718
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002719void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002720 ExplodedNode* Pred,
Zhongxing Xub9eda672009-10-30 07:19:39 +00002721 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002722
Zhongxing Xu107f7592009-08-06 12:48:26 +00002723 ExplodedNodeSet Tmp1;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002724 Expr* LHS = B->getLHS()->IgnoreParens();
2725 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002726
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002727 // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
2728 if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
Mike Stump11289f42009-09-09 15:08:12 +00002729 Visit(RHS, Pred, Dst);
Ted Kremenek69d78b92008-12-06 02:39:30 +00002730 return;
2731 }
Mike Stump11289f42009-09-09 15:08:12 +00002732
Ted Kremenek43523e02008-02-07 01:08:27 +00002733 if (B->isAssignmentOp())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002734 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek43523e02008-02-07 01:08:27 +00002735 else
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002736 Visit(LHS, Pred, Tmp1);
Ted Kremenekfb553542008-01-16 00:53:15 +00002737
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002738 ExplodedNodeSet Tmp3;
2739
Ted Kremenek17a02962009-09-03 03:02:58 +00002740 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002741 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002742 ExplodedNodeSet Tmp2;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002743 Visit(RHS, *I1, Tmp2);
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002744
2745 ExplodedNodeSet CheckedSet;
2746 CheckerVisit(B, CheckedSet, Tmp2, true);
Mike Stump11289f42009-09-09 15:08:12 +00002747
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002748 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump11289f42009-09-09 15:08:12 +00002749
2750 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002751 I2 != E2; ++I2) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002752
Ted Kremenek7020eae2009-09-11 22:07:28 +00002753 const GRState *state = GetState(*I2);
2754 const GRState *OldSt = state;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002755 SVal RightV = state->getSVal(RHS);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002756
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002757 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00002758
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002759 if (Op == BinaryOperator::Assign) {
2760 // EXPERIMENTAL: "Conjured" symbols.
2761 // FIXME: Handle structs.
2762 QualType T = RHS->getType();
2763
2764 if ((RightV.isUnknown()||!getConstraintManager().canReasonAbout(RightV))
2765 && (Loc::IsLocType(T) || (T->isScalarType()&&T->isIntegerType()))) {
2766 unsigned Count = Builder->getCurrentBlockCount();
2767 RightV = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), Count);
2768 }
Zhongxing Xub9eda672009-10-30 07:19:39 +00002769
Ted Kremenek5c2040b12009-10-30 22:01:29 +00002770 SVal ExprVal = asLValue ? LeftV : RightV;
Zhongxing Xub9eda672009-10-30 07:19:39 +00002771
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002772 // Simulate the effects of a "store": bind the value of the RHS
2773 // to the L-Value represented by the LHS.
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002774 EvalStore(Tmp3, B, LHS, *I2, state->BindExpr(B, ExprVal), LeftV,RightV);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002775 continue;
2776 }
2777
2778 if (!B->isAssignmentOp()) {
2779 // Process non-assignments except commas or short-circuited
2780 // logical expressions (LAnd and LOr).
2781 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
2782
2783 if (Result.isUnknown()) {
2784 if (OldSt != state) {
2785 // Generate a new node if we have already created a new state.
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002786 MakeNode(Tmp3, B, *I2, state);
Ted Kremeneke2f6d6c2008-03-12 21:45:47 +00002787 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002788 else
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002789 Tmp3.Add(*I2);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002790
Ted Kremenek2044a512008-04-16 18:21:25 +00002791 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002792 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002793
2794 state = state->BindExpr(B, Result);
2795
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002796 MakeNode(Tmp3, B, *I2, state);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002797 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002798 }
Mike Stump11289f42009-09-09 15:08:12 +00002799
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002800 assert (B->isCompoundAssignmentOp());
2801
Ted Kremenek5d7662c2009-02-07 00:52:24 +00002802 switch (Op) {
2803 default:
2804 assert(0 && "Invalid opcode for compound assignment.");
2805 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2806 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2807 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2808 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2809 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2810 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2811 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2812 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2813 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2814 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek54d399a2008-10-27 23:02:39 +00002815 }
Mike Stump11289f42009-09-09 15:08:12 +00002816
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002817 // Perform a load (the LHS). This performs the checks for
2818 // null dereferences, and so on.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002819 ExplodedNodeSet Tmp3;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002820 SVal location = state->getSVal(LHS);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002821 EvalLoad(Tmp3, LHS, *I2, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002822
2823 for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002824 ++I3) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002825 state = GetState(*I3);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002826 SVal V = state->getSVal(LHS);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002827
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002828 // Get the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002829 QualType CTy =
2830 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek44137142008-11-15 04:01:56 +00002831 CTy = getContext().getCanonicalType(CTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002832
Ted Kremenekac7c7242009-07-21 21:03:30 +00002833 QualType CLHSTy =
2834 cast<CompoundAssignOperator>(B)->getComputationLHSType();
2835 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002836
Ted Kremenek44137142008-11-15 04:01:56 +00002837 QualType LTy = getContext().getCanonicalType(LHS->getType());
2838 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002839
2840 // Promote LHS.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002841 llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002842
Mike Stump11289f42009-09-09 15:08:12 +00002843 // Compute the result of the operation.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002844 SVal Result;
2845 llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
2846 RightV, CTy),
2847 state, B->getType(), CTy);
Mike Stump11289f42009-09-09 15:08:12 +00002848
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002849 // EXPERIMENTAL: "Conjured" symbols.
2850 // FIXME: Handle structs.
Mike Stump11289f42009-09-09 15:08:12 +00002851
Ted Kremenek44137142008-11-15 04:01:56 +00002852 SVal LHSVal;
Mike Stump11289f42009-09-09 15:08:12 +00002853
2854 if ((Result.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002855 !getConstraintManager().canReasonAbout(Result))
Mike Stump11289f42009-09-09 15:08:12 +00002856 && (Loc::IsLocType(CTy)
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002857 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Mike Stump11289f42009-09-09 15:08:12 +00002858
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002859 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002860
Ted Kremenek44137142008-11-15 04:01:56 +00002861 // The symbolic value is actually for the type of the left-hand side
2862 // expression, not the computation type, as this is the value the
2863 // LValue on the LHS will bind to.
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002864 LHSVal = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002865
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00002866 // However, we need to convert the symbol to the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002867 llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002868 }
Ted Kremenek44137142008-11-15 04:01:56 +00002869 else {
2870 // The left-hand side may bind to a different value then the
2871 // computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002872 llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
Ted Kremenek44137142008-11-15 04:01:56 +00002873 }
Mike Stump11289f42009-09-09 15:08:12 +00002874
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002875 EvalStore(Tmp3, B, LHS, *I3, state->BindExpr(B, Result),
Zhongxing Xu342950e2009-08-25 06:51:30 +00002876 location, LHSVal);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002877 }
Ted Kremenekfb553542008-01-16 00:53:15 +00002878 }
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002879 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002880
2881 CheckerVisit(B, Dst, Tmp3, false);
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002882}
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002883
2884//===----------------------------------------------------------------------===//
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002885// Checker registration/lookup.
2886//===----------------------------------------------------------------------===//
2887
2888Checker *GRExprEngine::lookupChecker(void *tag) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +00002889 CheckerMap::const_iterator I = CheckerM.find(tag);
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002890 return (I == CheckerM.end()) ? NULL : Checkers[I->second].second;
2891}
2892
2893//===----------------------------------------------------------------------===//
Ted Kremenekd3122cb2008-02-14 22:36:46 +00002894// Visualization.
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002895//===----------------------------------------------------------------------===//
2896
Ted Kremenekac886cb2008-01-16 21:46:15 +00002897#ifndef NDEBUG
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002898static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek6947a792008-03-07 20:57:30 +00002899static SourceManager* GraphPrintSourceManager;
Ted Kremenek2531fce2008-01-30 23:24:39 +00002900
Ted Kremenekac886cb2008-01-16 21:46:15 +00002901namespace llvm {
2902template<>
Douglas Gregor693ba202009-11-30 16:08:24 +00002903struct DOTGraphTraits<ExplodedNode*> :
Ted Kremenekac886cb2008-01-16 21:46:15 +00002904 public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002905
2906 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2907
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +00002908 // FIXME: Since we do not cache error nodes in GRExprEngine now, this does not
2909 // work.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002910 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump11289f42009-09-09 15:08:12 +00002911
Ted Kremenek7cf82382009-11-11 20:16:36 +00002912#if 0
2913 // FIXME: Replace with a general scheme to tell if the node is
2914 // an error node.
Ted Kremenek5b70a222008-02-14 22:54:53 +00002915 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek0f7130a2008-02-19 00:22:37 +00002916 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek93d1fed2008-02-28 09:25:22 +00002917 GraphPrintCheckerState->isUndefDeref(N) ||
2918 GraphPrintCheckerState->isUndefStore(N) ||
2919 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek6f5fca72008-02-29 23:14:48 +00002920 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek51e87ea2008-02-29 23:53:11 +00002921 GraphPrintCheckerState->isBadCall(N) ||
2922 GraphPrintCheckerState->isUndefArg(N))
Ted Kremenek5b70a222008-02-14 22:54:53 +00002923 return "color=\"red\",style=\"filled\"";
Mike Stump11289f42009-09-09 15:08:12 +00002924
Ted Kremeneke0c79382008-02-28 20:32:03 +00002925 if (GraphPrintCheckerState->isNoReturnCall(N))
2926 return "color=\"blue\",style=\"filled\"";
Zhongxing Xu9e200792009-11-24 07:06:39 +00002927#endif
Ted Kremenek5b70a222008-02-14 22:54:53 +00002928 return "";
2929 }
Mike Stump11289f42009-09-09 15:08:12 +00002930
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002931 static std::string getNodeLabel(const ExplodedNode* N, void*){
Mike Stump11289f42009-09-09 15:08:12 +00002932
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002933 std::string sbuf;
2934 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek930191c2008-01-23 22:30:44 +00002935
2936 // Program Location.
Ted Kremenekac886cb2008-01-16 21:46:15 +00002937 ProgramPoint Loc = N->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002938
Ted Kremenekac886cb2008-01-16 21:46:15 +00002939 switch (Loc.getKind()) {
2940 case ProgramPoint::BlockEntranceKind:
Mike Stump11289f42009-09-09 15:08:12 +00002941 Out << "Block Entrance: B"
Ted Kremenekac886cb2008-01-16 21:46:15 +00002942 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2943 break;
Mike Stump11289f42009-09-09 15:08:12 +00002944
Ted Kremenekac886cb2008-01-16 21:46:15 +00002945 case ProgramPoint::BlockExitKind:
2946 assert (false);
2947 break;
Mike Stump11289f42009-09-09 15:08:12 +00002948
Ted Kremenekac886cb2008-01-16 21:46:15 +00002949 default: {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002950 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
2951 const Stmt* S = L->getStmt();
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002952 SourceLocation SLoc = S->getLocStart();
2953
Mike Stump11289f42009-09-09 15:08:12 +00002954 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattnerc61089a2009-06-30 01:26:17 +00002955 LangOptions LO; // FIXME.
2956 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00002957
2958 if (SLoc.isFileID()) {
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002959 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00002960 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2961 << " col="
2962 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2963 << "\\l";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002964 }
Mike Stump11289f42009-09-09 15:08:12 +00002965
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002966 if (isa<PreStmt>(Loc))
Mike Stump11289f42009-09-09 15:08:12 +00002967 Out << "\\lPreStmt\\l;";
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002968 else if (isa<PostLoad>(Loc))
Ted Kremeneka6e08322009-05-07 18:27:16 +00002969 Out << "\\lPostLoad\\l;";
2970 else if (isa<PostStore>(Loc))
2971 Out << "\\lPostStore\\l";
2972 else if (isa<PostLValue>(Loc))
2973 Out << "\\lPostLValue\\l";
Mike Stump11289f42009-09-09 15:08:12 +00002974
Ted Kremenek7cf82382009-11-11 20:16:36 +00002975#if 0
2976 // FIXME: Replace with a general scheme to determine
2977 // the name of the check.
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002978 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2979 Out << "\\|Implicit-Null Dereference.\\l";
2980 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2981 Out << "\\|Explicit-Null Dereference.\\l";
2982 else if (GraphPrintCheckerState->isUndefDeref(N))
2983 Out << "\\|Dereference of undefialied value.\\l";
2984 else if (GraphPrintCheckerState->isUndefStore(N))
2985 Out << "\\|Store to Undefined Loc.";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002986 else if (GraphPrintCheckerState->isUndefResult(N))
2987 Out << "\\|Result of operation is undefined.";
2988 else if (GraphPrintCheckerState->isNoReturnCall(N))
2989 Out << "\\|Call to function marked \"noreturn\".";
2990 else if (GraphPrintCheckerState->isBadCall(N))
2991 Out << "\\|Call to NULL/Undefined.";
2992 else if (GraphPrintCheckerState->isUndefArg(N))
2993 Out << "\\|Argument in call is undefined";
Ted Kremenek7cf82382009-11-11 20:16:36 +00002994#endif
Mike Stump11289f42009-09-09 15:08:12 +00002995
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002996 break;
2997 }
2998
Ted Kremenekac886cb2008-01-16 21:46:15 +00002999 const BlockEdge& E = cast<BlockEdge>(Loc);
3000 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3001 << E.getDst()->getBlockID() << ')';
Mike Stump11289f42009-09-09 15:08:12 +00003002
Ted Kremeneka50d9852008-01-30 23:03:39 +00003003 if (Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump11289f42009-09-09 15:08:12 +00003004
Ted Kremenek6947a792008-03-07 20:57:30 +00003005 SourceLocation SLoc = T->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00003006
Ted Kremeneka50d9852008-01-30 23:03:39 +00003007 Out << "\\|Terminator: ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003008 LangOptions LO; // FIXME.
3009 E.getSrc()->printTerminator(Out, LO);
Mike Stump11289f42009-09-09 15:08:12 +00003010
Ted Kremenek03ab1562008-03-09 03:30:59 +00003011 if (SLoc.isFileID()) {
3012 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00003013 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3014 << " col="
3015 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek03ab1562008-03-09 03:30:59 +00003016 }
Mike Stump11289f42009-09-09 15:08:12 +00003017
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003018 if (isa<SwitchStmt>(T)) {
3019 Stmt* Label = E.getDst()->getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00003020
3021 if (Label) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003022 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3023 Out << "\\lcase ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003024 LangOptions LO; // FIXME.
3025 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00003026
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003027 if (Stmt* RHS = C->getRHS()) {
3028 Out << " .. ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003029 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003030 }
Mike Stump11289f42009-09-09 15:08:12 +00003031
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003032 Out << ":";
3033 }
3034 else {
3035 assert (isa<DefaultStmt>(Label));
3036 Out << "\\ldefault:";
3037 }
3038 }
Mike Stump11289f42009-09-09 15:08:12 +00003039 else
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003040 Out << "\\l(implicit) default:";
3041 }
3042 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremeneka50d9852008-01-30 23:03:39 +00003043 // FIXME
3044 }
3045 else {
3046 Out << "\\lCondition: ";
3047 if (*E.getSrc()->succ_begin() == E.getDst())
3048 Out << "true";
3049 else
Mike Stump11289f42009-09-09 15:08:12 +00003050 Out << "false";
Ted Kremeneka50d9852008-01-30 23:03:39 +00003051 }
Mike Stump11289f42009-09-09 15:08:12 +00003052
Ted Kremeneka50d9852008-01-30 23:03:39 +00003053 Out << "\\l";
3054 }
Mike Stump11289f42009-09-09 15:08:12 +00003055
Ted Kremenek7cf82382009-11-11 20:16:36 +00003056#if 0
3057 // FIXME: Replace with a general scheme to determine
3058 // the name of the check.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00003059 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3060 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek2531fce2008-01-30 23:24:39 +00003061 }
Ted Kremenek7cf82382009-11-11 20:16:36 +00003062#endif
Ted Kremenekac886cb2008-01-16 21:46:15 +00003063 }
3064 }
Mike Stump11289f42009-09-09 15:08:12 +00003065
Ted Kremenek22c62c12008-02-28 10:21:43 +00003066 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenekb54312d2008-02-08 21:10:02 +00003067
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003068 const GRState *state = N->getState();
3069 state->printDOT(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003070
Ted Kremenek930191c2008-01-23 22:30:44 +00003071 Out << "\\l";
Ted Kremenekac886cb2008-01-16 21:46:15 +00003072 return Out.str();
3073 }
3074};
Mike Stump11289f42009-09-09 15:08:12 +00003075} // end llvm namespace
Ted Kremenekac886cb2008-01-16 21:46:15 +00003076#endif
3077
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003078#ifndef NDEBUG
Ted Kremenek576b76a2008-03-12 17:18:20 +00003079template <typename ITERATOR>
Zhongxing Xu107f7592009-08-06 12:48:26 +00003080ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek576b76a2008-03-12 17:18:20 +00003081
Zhongxing Xu107f7592009-08-06 12:48:26 +00003082template <> ExplodedNode*
3083GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3084 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek576b76a2008-03-12 17:18:20 +00003085 return I->first;
3086}
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003087#endif
3088
3089void GRExprEngine::ViewGraph(bool trim) {
Mike Stump11289f42009-09-09 15:08:12 +00003090#ifndef NDEBUG
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003091 if (trim) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00003092 std::vector<ExplodedNode*> Src;
Ted Kremenek95175052009-03-11 01:41:22 +00003093
3094 // Flush any outstanding reports to make sure we cover all the nodes.
3095 // This does not cause them to get displayed.
3096 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3097 const_cast<BugType*>(*I)->FlushReports(BR);
3098
3099 // Iterate through the reports and get their nodes.
3100 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
Ted Kremenek17a02962009-09-03 03:02:58 +00003101 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
Mike Stump11289f42009-09-09 15:08:12 +00003102 I2!=E2; ++I2) {
Ted Kremenek95175052009-03-11 01:41:22 +00003103 const BugReportEquivClass& EQ = *I2;
3104 const BugReport &R = **EQ.begin();
Zhongxing Xu107f7592009-08-06 12:48:26 +00003105 ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
Ted Kremenek95175052009-03-11 01:41:22 +00003106 if (N) Src.push_back(N);
3107 }
3108 }
Mike Stump11289f42009-09-09 15:08:12 +00003109
Ted Kremenek576b76a2008-03-12 17:18:20 +00003110 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003111 }
Ted Kremeneka7178c72008-03-11 18:25:33 +00003112 else {
3113 GraphPrintCheckerState = this;
3114 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek16306102008-08-13 21:24:49 +00003115
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003116 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Mike Stump11289f42009-09-09 15:08:12 +00003117
Ted Kremeneka7178c72008-03-11 18:25:33 +00003118 GraphPrintCheckerState = NULL;
3119 GraphPrintSourceManager = NULL;
3120 }
3121#endif
3122}
3123
Zhongxing Xu107f7592009-08-06 12:48:26 +00003124void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremeneka7178c72008-03-11 18:25:33 +00003125#ifndef NDEBUG
3126 GraphPrintCheckerState = this;
3127 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00003128
Zhongxing Xu107f7592009-08-06 12:48:26 +00003129 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneka7178c72008-03-11 18:25:33 +00003130
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003131 if (!TrimmedG.get())
Benjamin Kramer89b422c2009-08-23 12:08:50 +00003132 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003133 else
Mike Stump11289f42009-09-09 15:08:12 +00003134 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
3135
Ted Kremenek2531fce2008-01-30 23:24:39 +00003136 GraphPrintCheckerState = NULL;
Ted Kremenek6947a792008-03-07 20:57:30 +00003137 GraphPrintSourceManager = NULL;
Ted Kremenekd3122cb2008-02-14 22:36:46 +00003138#endif
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003139}