blob: 6dfbedc3bb95dbdf53522ba1dbacd6c58f3c95d6 [file] [log] [blame]
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001//=-- ExprEngine.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//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa700e972010-12-22 18:52:56 +000015
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngineBuilders.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/Checker.h"
Ken Dyck40775002010-01-11 17:06:35 +000022#include "clang/AST/CharUnits.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000023#include "clang/AST/ParentMap.h"
24#include "clang/AST/StmtObjC.h"
Zhongxing Xu0eb69032010-03-16 13:14:16 +000025#include "clang/AST/DeclCXX.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000027#include "clang/Basic/SourceManager.h"
Ted Kremenek6947a792008-03-07 20:57:30 +000028#include "clang/Basic/SourceManager.h"
Ted Kremenek91076ca2009-03-11 02:41:36 +000029#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000030#include "llvm/Support/raw_ostream.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000031#include "llvm/ADT/ImmutableList.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000032
Ted Kremenekc0258412008-02-27 06:07:00 +000033#ifndef NDEBUG
34#include "llvm/Support/GraphWriter.h"
Ted Kremenekc0258412008-02-27 06:07:00 +000035#endif
36
Ted Kremenekbd8957b2008-02-14 22:16:04 +000037using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000038using namespace ento;
Ted Kremenekbd8957b2008-02-14 22:16:04 +000039using llvm::dyn_cast;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000040using llvm::dyn_cast_or_null;
Ted Kremenekbd8957b2008-02-14 22:16:04 +000041using llvm::cast;
42using llvm::APSInt;
Ted Kremenek0a8d3762008-01-23 19:59:44 +000043
Zhongxing Xu5c075842010-02-26 15:43:34 +000044namespace {
45 // Trait class for recording returned expression in the state.
46 struct ReturnExpr {
47 static int TagInt;
48 typedef const Stmt *data_type;
49 };
50 int ReturnExpr::TagInt;
51}
52
Ted Kremenek667cacb2008-04-15 23:06:53 +000053//===----------------------------------------------------------------------===//
Ted Kremenekd0fe8042009-11-25 21:51:20 +000054// Utility functions.
55//===----------------------------------------------------------------------===//
56
57static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
58 IdentifierInfo* II = &Ctx.Idents.get(name);
59 return Ctx.Selectors.getSelector(0, &II);
60}
61
62//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +000063// Checker worklist routines.
64//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +000065
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000066void ExprEngine::CheckerVisit(const Stmt *S, ExplodedNodeSet &Dst,
Jordy Rosec36df4d2010-08-04 07:10:57 +000067 ExplodedNodeSet &Src, CallbackKind Kind) {
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenekfe97a1a2010-06-25 20:59:31 +000069 // Determine if we already have a cached 'CheckersOrdered' vector
Jordy Rosec36df4d2010-08-04 07:10:57 +000070 // specifically tailored for the provided <CallbackKind, Stmt kind>. This
Ted Kremenekfe97a1a2010-06-25 20:59:31 +000071 // can reduce the number of checkers actually called.
72 CheckersOrdered *CO = &Checkers;
73 llvm::OwningPtr<CheckersOrdered> NewCO;
Jordy Rosec36df4d2010-08-04 07:10:57 +000074
75 // The cache key is made up of the and the callback kind (pre- or post-visit)
76 // and the statement kind.
77 CallbackTag K = GetCallbackTag(Kind, S->getStmtClass());
Ted Kremenekfe97a1a2010-06-25 20:59:31 +000078
79 CheckersOrdered *& CO_Ref = COCache[K];
80
81 if (!CO_Ref) {
82 // If we have no previously cached CheckersOrdered vector for this
83 // statement kind, then create one.
84 NewCO.reset(new CheckersOrdered);
85 }
86 else {
87 // Use the already cached set.
88 CO = CO_Ref;
89 }
90
91 if (CO->empty()) {
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000092 // If there are no checkers, just delegate to the checker manager.
93 getCheckerManager().runCheckersForStmt(Kind == PreVisitStmtCallback,
94 Dst, Src, S, *this);
Zhongxing Xuaf353292009-12-02 05:49:12 +000095 return;
Ted Kremenek49513cc2009-07-22 21:43:51 +000096 }
Mike Stump11289f42009-09-09 15:08:12 +000097
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000098 ExplodedNodeSet CheckersV1Dst;
Zhongxing Xu107f7592009-08-06 12:48:26 +000099 ExplodedNodeSet Tmp;
100 ExplodedNodeSet *PrevSet = &Src;
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000101 unsigned checkersEvaluated = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Zhongxing Xu44207a92010-08-06 04:20:59 +0000103 for (CheckersOrdered::iterator I=CO->begin(), E=CO->end(); I!=E; ++I) {
104 // If all nodes are sunk, bail out early.
105 if (PrevSet->empty())
106 break;
Ted Kremenek32c32892009-12-09 02:45:41 +0000107 ExplodedNodeSet *CurrSet = 0;
108 if (I+1 == E)
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000109 CurrSet = &CheckersV1Dst;
Ted Kremenek32c32892009-12-09 02:45:41 +0000110 else {
111 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
112 CurrSet->clear();
Ted Kremenekd51217e2010-02-15 23:02:46 +0000113 }
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000114 void *tag = I->first;
115 Checker *checker = I->second;
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000116 bool respondsToCallback = true;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000117
Zhongxing Xu107f7592009-08-06 12:48:26 +0000118 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000119 NI != NE; ++NI) {
120
Jordy Rosec36df4d2010-08-04 07:10:57 +0000121 checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, tag,
122 Kind == PreVisitStmtCallback, respondsToCallback);
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000123
124 }
125
Ted Kremenek49513cc2009-07-22 21:43:51 +0000126 PrevSet = CurrSet;
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000127
128 if (NewCO.get()) {
129 ++checkersEvaluated;
130 if (respondsToCallback)
131 NewCO->push_back(*I);
132 }
Ted Kremenek49513cc2009-07-22 21:43:51 +0000133 }
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000134
135 // If we built NewCO, check if we called all the checkers. This is important
136 // so that we know that we accurately determined the entire set of checkers
Ted Kremenek9c222192010-08-05 15:03:30 +0000137 // that responds to this callback. Note that 'checkersEvaluated' might
138 // not be the same as Checkers.size() if one of the Checkers generates
139 // a sink node.
140 if (NewCO.get() && checkersEvaluated == Checkers.size())
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000141 CO_Ref = NewCO.take();
Ted Kremenek49513cc2009-07-22 21:43:51 +0000142
143 // Don't autotransition. The CheckerContext objects should do this
144 // automatically.
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000145
146 getCheckerManager().runCheckersForStmt(Kind == PreVisitStmtCallback,
147 Dst, CheckersV1Dst, S, *this);
Zhongxing Xuaf353292009-12-02 05:49:12 +0000148}
149
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000150void ExprEngine::CheckerVisitObjCMessage(const ObjCMessage &msg,
151 ExplodedNodeSet &Dst,
152 ExplodedNodeSet &Src,
153 bool isPrevisit) {
154
155 if (Checkers.empty()) {
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000156 getCheckerManager().runCheckersForObjCMessage(isPrevisit, Dst, Src, msg,
157 *this);
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000158 return;
159 }
160
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000161 ExplodedNodeSet CheckersV1Dst;
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000162 ExplodedNodeSet Tmp;
163 ExplodedNodeSet *PrevSet = &Src;
164
165 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
166 {
167 ExplodedNodeSet *CurrSet = 0;
168 if (I+1 == E)
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000169 CurrSet = &CheckersV1Dst;
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000170 else {
171 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
172 CurrSet->clear();
173 }
174
175 void *tag = I->first;
176 Checker *checker = I->second;
177
178 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
179 NI != NE; ++NI)
180 checker->GR_visitObjCMessage(*CurrSet, *Builder, *this, msg,
181 *NI, tag, isPrevisit);
182
183 // Update which NodeSet is the current one.
184 PrevSet = CurrSet;
185 }
186
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000187 getCheckerManager().runCheckersForObjCMessage(isPrevisit, Dst, CheckersV1Dst,
188 msg, *this);
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000189}
190
191void ExprEngine::CheckerEvalNilReceiver(const ObjCMessage &msg,
192 ExplodedNodeSet &Dst,
193 const GRState *state,
194 ExplodedNode *Pred) {
Ted Kremenekdc891422010-12-01 21:57:22 +0000195 bool evaluated = false;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000196 ExplodedNodeSet DstTmp;
197
Zhongxing Xuaf353292009-12-02 05:49:12 +0000198 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
199 void *tag = I->first;
200 Checker *checker = I->second;
201
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000202 if (checker->GR_evalNilReceiver(DstTmp, *Builder, *this, msg, Pred, state,
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000203 tag)) {
Ted Kremenekdc891422010-12-01 21:57:22 +0000204 evaluated = true;
Zhongxing Xuaf353292009-12-02 05:49:12 +0000205 break;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000206 } else
207 // The checker didn't evaluate the expr. Restore the Dst.
208 DstTmp.clear();
Zhongxing Xuaf353292009-12-02 05:49:12 +0000209 }
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000210
Ted Kremenekdc891422010-12-01 21:57:22 +0000211 if (evaluated)
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000212 Dst.insert(DstTmp);
213 else
214 Dst.insert(Pred);
Ted Kremenek49513cc2009-07-22 21:43:51 +0000215}
216
Zhongxing Xu175447f2009-12-07 09:17:35 +0000217// CheckerEvalCall returns true if one of the checkers processed the node.
218// This may return void when all call evaluation logic goes to some checker
219// in the future.
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000220bool ExprEngine::CheckerEvalCall(const CallExpr *CE,
Ted Kremenekd51217e2010-02-15 23:02:46 +0000221 ExplodedNodeSet &Dst,
Zhongxing Xu175447f2009-12-07 09:17:35 +0000222 ExplodedNode *Pred) {
Ted Kremenekdc891422010-12-01 21:57:22 +0000223 bool evaluated = false;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000224 ExplodedNodeSet DstTmp;
Zhongxing Xu175447f2009-12-07 09:17:35 +0000225
226 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
227 void *tag = I->first;
228 Checker *checker = I->second;
229
Ted Kremenekdc891422010-12-01 21:57:22 +0000230 if (checker->GR_evalCallExpr(DstTmp, *Builder, *this, CE, Pred, tag)) {
231 evaluated = true;
Zhongxing Xu175447f2009-12-07 09:17:35 +0000232 break;
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000233 } else
234 // The checker didn't evaluate the expr. Restore the DstTmp set.
235 DstTmp.clear();
Zhongxing Xu175447f2009-12-07 09:17:35 +0000236 }
237
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000238 if (evaluated) {
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000239 Dst.insert(DstTmp);
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000240 return evaluated;
241 }
Zhongxing Xu8cca37f2009-12-09 12:16:07 +0000242
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000243 class DefaultEval : public GraphExpander {
244 bool &Evaluated;
245 public:
246 DefaultEval(bool &evaluated) : Evaluated(evaluated) { }
247 virtual void expandGraph(ExplodedNodeSet &Dst, ExplodedNode *Pred) {
248 Evaluated = false;
249 Dst.insert(Pred);
250 }
251 };
252
253 evaluated = true;
254 DefaultEval defaultEval(evaluated);
255 getCheckerManager().runCheckersForEvalCall(Dst, Pred, CE, *this,
256 &defaultEval);
Ted Kremenekdc891422010-12-01 21:57:22 +0000257 return evaluated;
Zhongxing Xu175447f2009-12-07 09:17:35 +0000258}
259
Ted Kremenekd51217e2010-02-15 23:02:46 +0000260// FIXME: This is largely copy-paste from CheckerVisit(). Need to
Ted Kremenekef910042009-11-04 04:24:16 +0000261// unify.
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000262void ExprEngine::CheckerVisitBind(const Stmt *StoreE, ExplodedNodeSet &Dst,
Ted Kremenek07343c02010-09-02 00:56:20 +0000263 ExplodedNodeSet &Src, SVal location,
264 SVal val, bool isPrevisit) {
Ted Kremenekd51217e2010-02-15 23:02:46 +0000265
Ted Kremenekef910042009-11-04 04:24:16 +0000266 if (Checkers.empty()) {
Argyrios Kyrtzidis183f0fb2011-02-28 01:26:35 +0000267 getCheckerManager().runCheckersForBind(Dst, Src, location, val, StoreE,
268 *this);
Ted Kremenekef910042009-11-04 04:24:16 +0000269 return;
270 }
Ted Kremenekd51217e2010-02-15 23:02:46 +0000271
Argyrios Kyrtzidis183f0fb2011-02-28 01:26:35 +0000272 ExplodedNodeSet CheckerV1Tmp;
Ted Kremenekef910042009-11-04 04:24:16 +0000273 ExplodedNodeSet Tmp;
274 ExplodedNodeSet *PrevSet = &Src;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000275
Ted Kremenekef910042009-11-04 04:24:16 +0000276 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
277 {
Ted Kremenek32c32892009-12-09 02:45:41 +0000278 ExplodedNodeSet *CurrSet = 0;
279 if (I+1 == E)
Argyrios Kyrtzidis183f0fb2011-02-28 01:26:35 +0000280 CurrSet = &CheckerV1Tmp;
Ted Kremenek32c32892009-12-09 02:45:41 +0000281 else {
282 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
283 CurrSet->clear();
284 }
Ted Kremenekaf1bdd72009-12-18 20:13:39 +0000285
Ted Kremenekef910042009-11-04 04:24:16 +0000286 void *tag = I->first;
287 Checker *checker = I->second;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000288
Ted Kremenekef910042009-11-04 04:24:16 +0000289 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
290 NI != NE; ++NI)
Ted Kremenek07343c02010-09-02 00:56:20 +0000291 checker->GR_VisitBind(*CurrSet, *Builder, *this, StoreE,
Ted Kremenek209e31b2009-11-05 00:42:23 +0000292 *NI, tag, location, val, isPrevisit);
Ted Kremenekd51217e2010-02-15 23:02:46 +0000293
Ted Kremenekef910042009-11-04 04:24:16 +0000294 // Update which NodeSet is the current one.
295 PrevSet = CurrSet;
296 }
Ted Kremenekd51217e2010-02-15 23:02:46 +0000297
Argyrios Kyrtzidis183f0fb2011-02-28 01:26:35 +0000298 getCheckerManager().runCheckersForBind(Dst, CheckerV1Tmp, location, val,
299 StoreE, *this);
300
Ted Kremenekef910042009-11-04 04:24:16 +0000301 // Don't autotransition. The CheckerContext objects should do this
302 // automatically.
303}
Ted Kremenek49513cc2009-07-22 21:43:51 +0000304//===----------------------------------------------------------------------===//
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000305// Engine construction and deletion.
306//===----------------------------------------------------------------------===//
307
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000308ExprEngine::ExprEngine(AnalysisManager &mgr, TransferFuncs *tf)
Zhongxing Xue1190f72009-08-15 03:17:38 +0000309 : AMgr(mgr),
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000310 Engine(*this),
311 G(Engine.getGraph()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000312 Builder(NULL),
Zhongxing Xubf81ed12010-07-01 07:10:59 +0000313 StateMgr(getContext(), mgr.getStoreManagerCreator(),
Ted Kremenekde8e7442010-01-05 00:15:18 +0000314 mgr.getConstraintManagerCreator(), G.getAllocator(),
315 *this),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000316 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek90af9092010-12-02 07:49:45 +0000317 svalBuilder(StateMgr.getSValBuilder()),
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000318 EntryNode(NULL), currentStmt(NULL),
Zhongxing Xu4ee570a2008-12-22 08:30:52 +0000319 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Zhongxing Xubf81ed12010-07-01 07:10:59 +0000320 RaiseSel(GetNullarySelector("raise", getContext())),
Ted Kremenekde8e7442010-01-05 00:15:18 +0000321 BR(mgr, *this), TF(tf) {
Ted Kremenekd51217e2010-02-15 23:02:46 +0000322
Ted Kremenekde8e7442010-01-05 00:15:18 +0000323 // FIXME: Eventually remove the TF object entirely.
324 TF->RegisterChecks(*this);
325 TF->RegisterPrinters(getStateManager().Printers);
Ted Kremeneka40f8eb2011-02-09 01:27:33 +0000326
327 if (mgr.shouldEagerlyTrimExplodedGraph()) {
328 // Enable eager node reclaimation when constructing the ExplodedGraph.
329 G.enableNodeReclamation();
330 }
Ted Kremenekefb50032009-11-25 21:45:48 +0000331}
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000332
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000333ExprEngine::~ExprEngine() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000334 BR.FlushReports();
Ted Kremenek7f824732008-05-01 18:33:28 +0000335 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000336
337 // Delete the set of checkers.
Ted Kremenek6f2a7052009-10-30 17:47:32 +0000338 for (CheckersOrdered::iterator I=Checkers.begin(), E=Checkers.end(); I!=E;++I)
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000339 delete I->second;
Ted Kremenekfe97a1a2010-06-25 20:59:31 +0000340
341 for (CheckersOrderedCache::iterator I=COCache.begin(), E=COCache.end();
342 I!=E;++I)
343 delete I->second;
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000344}
345
Ted Kremenek667cacb2008-04-15 23:06:53 +0000346//===----------------------------------------------------------------------===//
347// Utility methods.
348//===----------------------------------------------------------------------===//
349
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000350const GRState* ExprEngine::getInitialState(const LocationContext *InitLoc) {
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000351 const GRState *state = StateMgr.getInitialState(InitLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000353 // Preconditions.
354
Ted Kremenek50546632009-04-10 00:59:50 +0000355 // FIXME: It would be nice if we had a more general mechanism to add
356 // such preconditions. Some day.
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000357 do {
Ted Kremenekd51217e2010-02-15 23:02:46 +0000358 const Decl *D = InitLoc->getDecl();
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
360 // Precondition: the first argument of 'main' is an integer guaranteed
361 // to be > 0.
362 const IdentifierInfo *II = FD->getIdentifier();
363 if (!II || !(II->getName() == "main" && FD->getNumParams() > 0))
364 break;
Ted Kremenekf9906842009-06-18 22:57:13 +0000365
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000366 const ParmVarDecl *PD = FD->getParamDecl(0);
367 QualType T = PD->getType();
368 if (!T->isIntegerType())
369 break;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000370
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000371 const MemRegion *R = state->getRegion(PD, InitLoc);
372 if (!R)
373 break;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000374
Ted Kremenek57f09892010-02-08 16:18:51 +0000375 SVal V = state->getSVal(loc::MemRegionVal(R));
Ted Kremenekdc891422010-12-01 21:57:22 +0000376 SVal Constraint_untested = evalBinOp(state, BO_GT, V,
Ted Kremenek90af9092010-12-02 07:49:45 +0000377 svalBuilder.makeZeroVal(T),
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000378 getContext().IntTy);
379
380 DefinedOrUnknownSVal *Constraint =
381 dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested);
Ted Kremenekd51217e2010-02-15 23:02:46 +0000382
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000383 if (!Constraint)
384 break;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000385
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000386 if (const GRState *newState = state->assume(*Constraint, true))
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000387 state = newState;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000388
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000389 break;
390 }
391
Ted Kremenekd51217e2010-02-15 23:02:46 +0000392 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000393 // Precondition: 'self' is always non-null upon entry to an Objective-C
394 // method.
395 const ImplicitParamDecl *SelfD = MD->getSelfDecl();
396 const MemRegion *R = state->getRegion(SelfD, InitLoc);
Ted Kremenek57f09892010-02-08 16:18:51 +0000397 SVal V = state->getSVal(loc::MemRegionVal(R));
Ted Kremenekd51217e2010-02-15 23:02:46 +0000398
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000399 if (const Loc *LV = dyn_cast<Loc>(&V)) {
400 // Assume that the pointer value in 'self' is non-null.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000401 state = state->assume(*LV, true);
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000402 assert(state && "'self' cannot be null");
Ted Kremenek2e2b2582009-12-17 01:20:43 +0000403 }
Ted Kremenek50546632009-04-10 00:59:50 +0000404 }
Ted Kremenekda7d55a2009-12-17 19:17:27 +0000405 } while (0);
Ted Kremenekd51217e2010-02-15 23:02:46 +0000406
Ted Kremenek50546632009-04-10 00:59:50 +0000407 return state;
Ted Kremenek723fe3f2008-02-04 21:59:01 +0000408}
409
Ted Kremenek667cacb2008-04-15 23:06:53 +0000410//===----------------------------------------------------------------------===//
411// Top-level transfer function logic (Dispatcher).
412//===----------------------------------------------------------------------===//
413
Ted Kremenekdc891422010-12-01 21:57:22 +0000414/// evalAssume - Called by ConstraintManager. Used to call checker-specific
Ted Kremenekde8e7442010-01-05 00:15:18 +0000415/// logic for handling assumptions on symbolic values.
Ted Kremenek926c9622011-01-11 02:34:45 +0000416const GRState *ExprEngine::processAssume(const GRState *state, SVal cond,
Ted Kremenekd51217e2010-02-15 23:02:46 +0000417 bool assumption) {
Jordy Rosec36df4d2010-08-04 07:10:57 +0000418 // Determine if we already have a cached 'CheckersOrdered' vector
419 // specifically tailored for processing assumptions. This
420 // can reduce the number of checkers actually called.
421 CheckersOrdered *CO = &Checkers;
422 llvm::OwningPtr<CheckersOrdered> NewCO;
Ted Kremenekde8e7442010-01-05 00:15:18 +0000423
Ted Kremenek926c9622011-01-11 02:34:45 +0000424 CallbackTag K = GetCallbackTag(processAssumeCallback);
Jordy Rosec36df4d2010-08-04 07:10:57 +0000425 CheckersOrdered *& CO_Ref = COCache[K];
Ted Kremenekd51217e2010-02-15 23:02:46 +0000426
Jordy Rosec36df4d2010-08-04 07:10:57 +0000427 if (!CO_Ref) {
428 // If we have no previously cached CheckersOrdered vector for this
429 // statement kind, then create one.
430 NewCO.reset(new CheckersOrdered);
431 }
432 else {
433 // Use the already cached set.
434 CO = CO_Ref;
Ted Kremenekde8e7442010-01-05 00:15:18 +0000435 }
Ted Kremenekd51217e2010-02-15 23:02:46 +0000436
Jordy Rosec36df4d2010-08-04 07:10:57 +0000437 if (!CO->empty()) {
438 // Let the checkers have a crack at the assume before the transfer functions
439 // get their turn.
Jordy Rose2f7ee3c2010-08-12 04:05:07 +0000440 for (CheckersOrdered::iterator I = CO->begin(), E = CO->end(); I!=E; ++I) {
Jordy Rosec36df4d2010-08-04 07:10:57 +0000441
442 // If any checker declares the state infeasible (or if it starts that
443 // way), bail out.
444 if (!state)
445 return NULL;
446
447 Checker *C = I->second;
448 bool respondsToCallback = true;
449
Ted Kremenekdc891422010-12-01 21:57:22 +0000450 state = C->evalAssume(state, cond, assumption, &respondsToCallback);
Jordy Rosec36df4d2010-08-04 07:10:57 +0000451
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000452 // Check if we're building the cache of checkers that care about
453 // assumptions.
Jordy Rosec36df4d2010-08-04 07:10:57 +0000454 if (NewCO.get() && respondsToCallback)
455 NewCO->push_back(*I);
456 }
457
458 // If we got through all the checkers, and we built a list of those that
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000459 // care about assumptions, save it.
Jordy Rosec36df4d2010-08-04 07:10:57 +0000460 if (NewCO.get())
461 CO_Ref = NewCO.take();
462 }
463
Argyrios Kyrtzidis183f0fb2011-02-28 01:26:35 +0000464 state = getCheckerManager().runCheckersForEvalAssume(state, cond, assumption);
465
Jordy Rosec36df4d2010-08-04 07:10:57 +0000466 // If the state is infeasible at this point, bail out.
Ted Kremenekde8e7442010-01-05 00:15:18 +0000467 if (!state)
468 return NULL;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000469
Ted Kremenekdc891422010-12-01 21:57:22 +0000470 return TF->evalAssume(state, cond, assumption);
Ted Kremenekde8e7442010-01-05 00:15:18 +0000471}
472
Ted Kremenek926c9622011-01-11 02:34:45 +0000473bool ExprEngine::wantsRegionChangeUpdate(const GRState* state) {
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000474 CallbackTag K = GetCallbackTag(EvalRegionChangesCallback);
475 CheckersOrdered *CO = COCache[K];
476
477 if (!CO)
478 CO = &Checkers;
479
480 for (CheckersOrdered::iterator I = CO->begin(), E = CO->end(); I != E; ++I) {
481 Checker *C = I->second;
Ted Kremenek926c9622011-01-11 02:34:45 +0000482 if (C->wantsRegionChangeUpdate(state))
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000483 return true;
484 }
485
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000486 return getCheckerManager().wantsRegionChangeUpdate(state);
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000487}
488
489const GRState *
Ted Kremenek926c9622011-01-11 02:34:45 +0000490ExprEngine::processRegionChanges(const GRState *state,
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000491 const MemRegion * const *Begin,
492 const MemRegion * const *End) {
Ted Kremenek926c9622011-01-11 02:34:45 +0000493 // FIXME: Most of this method is copy-pasted from processAssume.
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000494
495 // Determine if we already have a cached 'CheckersOrdered' vector
496 // specifically tailored for processing region changes. This
497 // can reduce the number of checkers actually called.
498 CheckersOrdered *CO = &Checkers;
499 llvm::OwningPtr<CheckersOrdered> NewCO;
500
501 CallbackTag K = GetCallbackTag(EvalRegionChangesCallback);
502 CheckersOrdered *& CO_Ref = COCache[K];
503
504 if (!CO_Ref) {
505 // If we have no previously cached CheckersOrdered vector for this
506 // callback, then create one.
507 NewCO.reset(new CheckersOrdered);
508 }
509 else {
510 // Use the already cached set.
511 CO = CO_Ref;
512 }
513
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000514 // If there are no checkers, just delegate to the checker manager.
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000515 if (CO->empty())
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000516 return getCheckerManager().runCheckersForRegionChanges(state, Begin, End);
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000517
518 for (CheckersOrdered::iterator I = CO->begin(), E = CO->end(); I != E; ++I) {
519 // If any checker declares the state infeasible (or if it starts that way),
520 // bail out.
521 if (!state)
522 return NULL;
523
524 Checker *C = I->second;
525 bool respondsToCallback = true;
526
527 state = C->EvalRegionChanges(state, Begin, End, &respondsToCallback);
528
529 // See if we're building a cache of checkers that care about region changes.
530 if (NewCO.get() && respondsToCallback)
531 NewCO->push_back(*I);
532 }
533
534 // If we got through all the checkers, and we built a list of those that
535 // care about region changes, save it.
536 if (NewCO.get())
537 CO_Ref = NewCO.take();
538
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000539 return getCheckerManager().runCheckersForRegionChanges(state, Begin, End);
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000540}
541
Ted Kremenek926c9622011-01-11 02:34:45 +0000542void ExprEngine::processEndWorklist(bool hasWorkRemaining) {
Ted Kremenek574f3042010-06-23 22:08:00 +0000543 for (CheckersOrdered::iterator I = Checkers.begin(), E = Checkers.end();
544 I != E; ++I) {
Tom Care44081fb2010-08-03 01:55:07 +0000545 I->second->VisitEndAnalysis(G, BR, *this);
Ted Kremenek574f3042010-06-23 22:08:00 +0000546 }
Argyrios Kyrtzidisbf61d972011-02-23 07:19:23 +0000547 getCheckerManager().runCheckersForEndAnalysis(G, BR, *this);
Ted Kremenek574f3042010-06-23 22:08:00 +0000548}
549
Ted Kremenek926c9622011-01-11 02:34:45 +0000550void ExprEngine::processCFGElement(const CFGElement E,
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000551 StmtNodeBuilder& builder) {
Zhongxing Xu5d30c692010-11-15 08:48:43 +0000552 switch (E.getKind()) {
553 case CFGElement::Statement:
Zhongxing Xu5d30c692010-11-15 08:48:43 +0000554 ProcessStmt(E.getAs<CFGStmt>(), builder);
555 break;
556 case CFGElement::Initializer:
557 ProcessInitializer(E.getAs<CFGInitializer>(), builder);
558 break;
559 case CFGElement::ImplicitDtor:
560 ProcessImplicitDtor(E.getAs<CFGImplicitDtor>(), builder);
561 break;
562 default:
563 // Suppress compiler warning.
564 llvm_unreachable("Unexpected CFGElement kind.");
565 }
566}
567
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000568void ExprEngine::ProcessStmt(const CFGStmt S, StmtNodeBuilder& builder) {
Ted Kremeneka40f8eb2011-02-09 01:27:33 +0000569 // Reclaim any unnecessary nodes in the ExplodedGraph.
570 G.reclaimRecentlyAllocatedNodes();
Ted Kremenekade45d92011-01-25 19:13:54 +0000571 // Recycle any unused states in the GRStateManager.
572 StateMgr.recycleUnusedStates();
573
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000574 currentStmt = S.getStmt();
Ted Kremenek91076ca2009-03-11 02:41:36 +0000575 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000576 currentStmt->getLocStart(),
Ted Kremenek91076ca2009-03-11 02:41:36 +0000577 "Error evaluating statement");
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek667cacb2008-04-15 23:06:53 +0000579 Builder = &builder;
Ted Kremenekf41bdd72011-01-13 04:36:46 +0000580 EntryNode = builder.getPredecessor();
Mike Stump11289f42009-09-09 15:08:12 +0000581
Mike Stump11289f42009-09-09 15:08:12 +0000582 // Create the cleaned state.
Jordy Rose7fa9bf02010-08-14 20:18:45 +0000583 const LocationContext *LC = EntryNode->getLocationContext();
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000584 SymbolReaper SymReaper(LC, currentStmt, SymMgr);
Zhongxing Xue7358432010-03-05 04:45:36 +0000585
Jordy Rose7fa9bf02010-08-14 20:18:45 +0000586 if (AMgr.shouldPurgeDead()) {
587 const GRState *St = EntryNode->getState();
Zhongxing Xue7358432010-03-05 04:45:36 +0000588
Jordy Rose7fa9bf02010-08-14 20:18:45 +0000589 for (CheckersOrdered::iterator I = Checkers.begin(), E = Checkers.end();
590 I != E; ++I) {
591 Checker *checker = I->second;
592 checker->MarkLiveSymbols(St, SymReaper);
593 }
594
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000595 getCheckerManager().runCheckersForLiveSymbols(St, SymReaper);
596
Jordy Rose7fa9bf02010-08-14 20:18:45 +0000597 const StackFrameContext *SFC = LC->getCurrentStackFrame();
Ted Kremenek44e2c5c2011-01-14 20:34:15 +0000598 CleanedState = StateMgr.removeDeadBindings(St, SFC, SymReaper);
Jordy Rose7fa9bf02010-08-14 20:18:45 +0000599 } else {
600 CleanedState = EntryNode->getState();
601 }
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000602
Ted Kremenek3812b762008-04-24 18:31:42 +0000603 // Process any special transfer function for dead symbols.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000604 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +0000605
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000606 if (!SymReaper.hasDeadSymbols())
Ted Kremenekae8014c2008-04-24 23:35:58 +0000607 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000608 else {
609 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekf41bdd72011-01-13 04:36:46 +0000610 SaveOr OldHasGen(Builder->hasGeneratedNode);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000611
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000612 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
613 Builder->PurgingDeadSymbols = true;
Mike Stump11289f42009-09-09 15:08:12 +0000614
Zhongxing Xua4276b02009-11-13 06:53:04 +0000615 // FIXME: This should soon be removed.
616 ExplodedNodeSet Tmp2;
Ted Kremenekdc891422010-12-01 21:57:22 +0000617 getTF().evalDeadSymbols(Tmp2, *this, *Builder, EntryNode,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000618 CleanedState, SymReaper);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000619
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000620 ExplodedNodeSet checkersV1Tmp;
Zhongxing Xua4276b02009-11-13 06:53:04 +0000621 if (Checkers.empty())
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000622 checkersV1Tmp.insert(Tmp2);
Zhongxing Xua4276b02009-11-13 06:53:04 +0000623 else {
624 ExplodedNodeSet Tmp3;
625 ExplodedNodeSet *SrcSet = &Tmp2;
626 for (CheckersOrdered::iterator I = Checkers.begin(), E = Checkers.end();
627 I != E; ++I) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000628 ExplodedNodeSet *DstSet = 0;
629 if (I+1 == E)
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000630 DstSet = &checkersV1Tmp;
Ted Kremenek32c32892009-12-09 02:45:41 +0000631 else {
632 DstSet = (SrcSet == &Tmp2) ? &Tmp3 : &Tmp2;
633 DstSet->clear();
634 }
635
Zhongxing Xua4276b02009-11-13 06:53:04 +0000636 void *tag = I->first;
637 Checker *checker = I->second;
638 for (ExplodedNodeSet::iterator NI = SrcSet->begin(), NE = SrcSet->end();
639 NI != NE; ++NI)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000640 checker->GR_evalDeadSymbols(*DstSet, *Builder, *this, currentStmt,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000641 *NI, SymReaper, tag);
Zhongxing Xua4276b02009-11-13 06:53:04 +0000642 SrcSet = DstSet;
643 }
644 }
645
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000646 getCheckerManager().runCheckersForDeadSymbols(Tmp, checkersV1Tmp,
647 SymReaper, currentStmt, *this);
648
Ted Kremenekf41bdd72011-01-13 04:36:46 +0000649 if (!Builder->BuildSinks && !Builder->hasGeneratedNode)
Ted Kremenekae8014c2008-04-24 23:35:58 +0000650 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000651 }
Mike Stump11289f42009-09-09 15:08:12 +0000652
Ted Kremenekae8014c2008-04-24 23:35:58 +0000653 bool HasAutoGenerated = false;
654
Zhongxing Xu107f7592009-08-06 12:48:26 +0000655 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000656 ExplodedNodeSet Dst;
Mike Stump11289f42009-09-09 15:08:12 +0000657
658 // Set the cleaned state.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000659 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
Mike Stump11289f42009-09-09 15:08:12 +0000660
661 // Visit the statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000662 Visit(currentStmt, *I, Dst);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000663
664 // Do we need to auto-generate a node? We only need to do this to generate
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000665 // a node with a "cleaned" state; CoreEngine will actually handle
Mike Stump11289f42009-09-09 15:08:12 +0000666 // auto-transitions for other cases.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000667 if (Dst.size() == 1 && *Dst.begin() == EntryNode
Ted Kremenekf41bdd72011-01-13 04:36:46 +0000668 && !Builder->hasGeneratedNode && !HasAutoGenerated) {
Ted Kremenekae8014c2008-04-24 23:35:58 +0000669 HasAutoGenerated = true;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000670 builder.generateNode(currentStmt, GetState(EntryNode), *I);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000671 }
Ted Kremenek3812b762008-04-24 18:31:42 +0000672 }
Mike Stump11289f42009-09-09 15:08:12 +0000673
Ted Kremenek667cacb2008-04-15 23:06:53 +0000674 // NULL out these variables to cleanup.
Ted Kremenek667cacb2008-04-15 23:06:53 +0000675 CleanedState = NULL;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000676 EntryNode = NULL;
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000677
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000678 currentStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenekae8014c2008-04-24 23:35:58 +0000680 Builder = NULL;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000681}
682
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000683void ExprEngine::ProcessInitializer(const CFGInitializer Init,
684 StmtNodeBuilder &builder) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000685 // We don't set EntryNode and currentStmt. And we don't clean up state.
Alexis Hunt1d792652011-01-08 20:30:50 +0000686 const CXXCtorInitializer *BMI = Init.getInitializer();
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000687
Zhongxing Xu0d87e0c2011-01-13 12:30:12 +0000688 ExplodedNode *pred = builder.getPredecessor();
689
690 const StackFrameContext *stackFrame = cast<StackFrameContext>(pred->getLocationContext());
691 const CXXConstructorDecl *decl = cast<CXXConstructorDecl>(stackFrame->getDecl());
692 const CXXThisRegion *thisReg = getCXXThisRegion(decl, stackFrame);
693
694 SVal thisVal = pred->getState()->getSVal(thisReg);
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000695
Francois Pichetd583da02010-12-04 09:14:42 +0000696 if (BMI->isAnyMemberInitializer()) {
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000697 ExplodedNodeSet Dst;
698
699 // Evaluate the initializer.
Zhongxing Xu0d87e0c2011-01-13 12:30:12 +0000700 Visit(BMI->getInit(), pred, Dst);
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000701
702 for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); I != E; ++I){
703 ExplodedNode *Pred = *I;
704 const GRState *state = Pred->getState();
705
Francois Pichetd583da02010-12-04 09:14:42 +0000706 const FieldDecl *FD = BMI->getAnyMember();
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000707
Zhongxing Xu0d87e0c2011-01-13 12:30:12 +0000708 SVal FieldLoc = state->getLValue(FD, thisVal);
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000709 SVal InitVal = state->getSVal(BMI->getInit());
710 state = state->bindLoc(FieldLoc, InitVal);
711
712 // Use a custom node building process.
Zhongxing Xu0d87e0c2011-01-13 12:30:12 +0000713 PostInitializer PP(BMI, stackFrame);
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000714 // Builder automatically add the generated node to the deferred set,
715 // which are processed in the builder's dtor.
716 builder.generateNode(PP, state, Pred);
717 }
Zhongxing Xu0d87e0c2011-01-13 12:30:12 +0000718 return;
Zhongxing Xu1ade3262010-11-16 07:52:17 +0000719 }
Zhongxing Xu0d87e0c2011-01-13 12:30:12 +0000720
721 assert(BMI->isBaseInitializer());
722
723 // Get the base class declaration.
724 const CXXConstructExpr *ctorExpr = cast<CXXConstructExpr>(BMI->getInit());
725
726 // Create the base object region.
727 SVal baseVal =
728 getStoreManager().evalDerivedToBase(thisVal, ctorExpr->getType());
729 const MemRegion *baseReg = baseVal.getAsRegion();
730 assert(baseReg);
731 Builder = &builder;
732 ExplodedNodeSet dst;
733 VisitCXXConstructExpr(ctorExpr, baseReg, pred, dst);
Zhongxing Xu5d30c692010-11-15 08:48:43 +0000734}
735
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000736void ExprEngine::ProcessImplicitDtor(const CFGImplicitDtor D,
737 StmtNodeBuilder &builder) {
Zhongxing Xu2c966712010-11-20 06:53:12 +0000738 Builder = &builder;
739
Zhongxing Xu33d7ea92010-11-17 09:16:19 +0000740 switch (D.getDtorKind()) {
741 case CFGElement::AutomaticObjectDtor:
742 ProcessAutomaticObjDtor(cast<CFGAutomaticObjDtor>(D), builder);
743 break;
744 case CFGElement::BaseDtor:
745 ProcessBaseDtor(cast<CFGBaseDtor>(D), builder);
746 break;
747 case CFGElement::MemberDtor:
748 ProcessMemberDtor(cast<CFGMemberDtor>(D), builder);
749 break;
750 case CFGElement::TemporaryDtor:
751 ProcessTemporaryDtor(cast<CFGTemporaryDtor>(D), builder);
752 break;
753 default:
754 llvm_unreachable("Unexpected dtor kind.");
755 }
756}
757
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000758void ExprEngine::ProcessAutomaticObjDtor(const CFGAutomaticObjDtor dtor,
759 StmtNodeBuilder &builder) {
Ted Kremenekf41bdd72011-01-13 04:36:46 +0000760 ExplodedNode *pred = builder.getPredecessor();
Zhongxing Xu16278852010-11-25 06:35:14 +0000761 const GRState *state = pred->getState();
762 const VarDecl *varDecl = dtor.getVarDecl();
Zhongxing Xu2c966712010-11-20 06:53:12 +0000763
Zhongxing Xu16278852010-11-25 06:35:14 +0000764 QualType varType = varDecl->getType();
Zhongxing Xu2c966712010-11-20 06:53:12 +0000765
Zhongxing Xu16278852010-11-25 06:35:14 +0000766 if (const ReferenceType *refType = varType->getAs<ReferenceType>())
767 varType = refType->getPointeeType();
768
769 const CXXRecordDecl *recordDecl = varType->getAsCXXRecordDecl();
770 assert(recordDecl && "get CXXRecordDecl fail");
771 const CXXDestructorDecl *dtorDecl = recordDecl->getDestructor();
772
773 Loc dest = state->getLValue(varDecl, pred->getLocationContext());
774
775 ExplodedNodeSet dstSet;
776 VisitCXXDestructor(dtorDecl, cast<loc::MemRegionVal>(dest).getRegion(),
777 dtor.getTriggerStmt(), pred, dstSet);
Zhongxing Xu33d7ea92010-11-17 09:16:19 +0000778}
779
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000780void ExprEngine::ProcessBaseDtor(const CFGBaseDtor D,
781 StmtNodeBuilder &builder) {
Zhongxing Xu33d7ea92010-11-17 09:16:19 +0000782}
783
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000784void ExprEngine::ProcessMemberDtor(const CFGMemberDtor D,
785 StmtNodeBuilder &builder) {
Zhongxing Xu33d7ea92010-11-17 09:16:19 +0000786}
787
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000788void ExprEngine::ProcessTemporaryDtor(const CFGTemporaryDtor D,
789 StmtNodeBuilder &builder) {
Zhongxing Xu5d30c692010-11-15 08:48:43 +0000790}
791
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000792void ExprEngine::Visit(const Stmt* S, ExplodedNode* Pred,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000793 ExplodedNodeSet& Dst) {
Ted Kremenek91076ca2009-03-11 02:41:36 +0000794 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
795 S->getLocStart(),
796 "Error evaluating statement");
797
John McCall34376a62010-12-04 03:47:34 +0000798 // Expressions to ignore.
799 if (const Expr *Ex = dyn_cast<Expr>(S))
Ted Kremenek8219b822010-12-16 07:46:53 +0000800 S = Ex->IgnoreParens();
John McCall34376a62010-12-04 03:47:34 +0000801
Ted Kremenek667cacb2008-04-15 23:06:53 +0000802 // FIXME: add metadata to the CFG so that we can disable
803 // this check when we KNOW that there is no block-level subexpression.
804 // The motivation is that this check requires a hashtable lookup.
Mike Stump11289f42009-09-09 15:08:12 +0000805
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000806 if (S != currentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000807 Dst.Add(Pred);
808 return;
809 }
Mike Stump11289f42009-09-09 15:08:12 +0000810
Ted Kremenek667cacb2008-04-15 23:06:53 +0000811 switch (S->getStmtClass()) {
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000812 // C++ stuff we don't support yet.
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000813 case Stmt::CXXBindTemporaryExprClass:
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000814 case Stmt::CXXCatchStmtClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000815 case Stmt::CXXDefaultArgExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000816 case Stmt::CXXDependentScopeMemberExprClass:
John McCall5d413782010-12-06 08:20:24 +0000817 case Stmt::ExprWithCleanupsClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000818 case Stmt::CXXNullPtrLiteralExprClass:
819 case Stmt::CXXPseudoDestructorExprClass:
820 case Stmt::CXXTemporaryObjectExprClass:
821 case Stmt::CXXThrowExprClass:
822 case Stmt::CXXTryStmtClass:
823 case Stmt::CXXTypeidExprClass:
Francois Pichet5cc0a672010-09-08 23:47:05 +0000824 case Stmt::CXXUuidofExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000825 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor747eb782010-07-08 06:14:04 +0000826 case Stmt::CXXScalarValueInitExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000827 case Stmt::DependentScopeDeclRefExprClass:
828 case Stmt::UnaryTypeTraitExprClass:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +0000829 case Stmt::BinaryTypeTraitExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000830 case Stmt::UnresolvedLookupExprClass:
831 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl9ac55dd2010-09-10 20:55:54 +0000832 case Stmt::CXXNoexceptExprClass:
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000833 case Stmt::PackExpansionExprClass:
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000834 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000835 {
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000836 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
837 Builder->BuildSinks = true;
838 MakeNode(Dst, S, Pred, GetState(Pred));
839 break;
840 }
John McCall34376a62010-12-04 03:47:34 +0000841
842 case Stmt::ParenExprClass:
843 llvm_unreachable("ParenExprs already handled.");
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000844 // Cases that should never be evaluated simply because they shouldn't
845 // appear in the CFG.
846 case Stmt::BreakStmtClass:
847 case Stmt::CaseStmtClass:
848 case Stmt::CompoundStmtClass:
849 case Stmt::ContinueStmtClass:
850 case Stmt::DefaultStmtClass:
851 case Stmt::DoStmtClass:
852 case Stmt::GotoStmtClass:
853 case Stmt::IndirectGotoStmtClass:
854 case Stmt::LabelStmtClass:
855 case Stmt::NoStmtClass:
856 case Stmt::NullStmtClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000857 llvm_unreachable("Stmt should not be in analyzer evaluation loop");
858 break;
859
Ted Kremenek55081f92010-06-22 19:05:10 +0000860 case Stmt::GNUNullExprClass: {
Ted Kremenek90af9092010-12-02 07:49:45 +0000861 MakeNode(Dst, S, Pred, GetState(Pred)->BindExpr(S, svalBuilder.makeNull()));
Ted Kremenek55081f92010-06-22 19:05:10 +0000862 break;
863 }
864
Ted Kremeneked12f1b2010-09-10 03:05:33 +0000865 case Stmt::ObjCAtSynchronizedStmtClass:
866 VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S), Pred, Dst);
867 break;
868
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +0000869 case Stmt::ObjCPropertyRefExprClass:
870 VisitObjCPropertyRefExpr(cast<ObjCPropertyRefExpr>(S), Pred, Dst);
871 break;
872
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000873 // Cases not handled yet; but will handle some day.
874 case Stmt::DesignatedInitExprClass:
875 case Stmt::ExtVectorElementExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000876 case Stmt::ImaginaryLiteralClass:
877 case Stmt::ImplicitValueInitExprClass:
878 case Stmt::ObjCAtCatchStmtClass:
879 case Stmt::ObjCAtFinallyStmtClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000880 case Stmt::ObjCAtTryStmtClass:
881 case Stmt::ObjCEncodeExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000882 case Stmt::ObjCIsaExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000883 case Stmt::ObjCProtocolExprClass:
884 case Stmt::ObjCSelectorExprClass:
885 case Stmt::ObjCStringLiteralClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000886 case Stmt::ParenListExprClass:
887 case Stmt::PredefinedExprClass:
888 case Stmt::ShuffleVectorExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000889 case Stmt::VAArgExprClass:
Peter Collingbourne41f85462011-02-09 21:07:24 +0000890 case Stmt::CUDAKernelCallExprClass:
John McCallc07a0c72011-02-17 10:25:35 +0000891 case Stmt::OpaqueValueExprClass:
Ted Kremenek8db54ff2010-04-15 17:33:31 +0000892 // Fall through.
893
894 // Cases we intentionally don't evaluate, since they don't need
895 // to be explicitly evaluated.
Zhongxing Xu17b33ed2010-04-13 13:15:19 +0000896 case Stmt::AddrLabelExprClass:
897 case Stmt::IntegerLiteralClass:
898 case Stmt::CharacterLiteralClass:
Zhongxing Xu3fd05092010-04-14 06:29:29 +0000899 case Stmt::CXXBoolLiteralExprClass:
Zhongxing Xu17b33ed2010-04-13 13:15:19 +0000900 case Stmt::FloatingLiteralClass:
Douglas Gregor35c7e842011-01-04 18:46:34 +0000901 case Stmt::SizeOfPackExprClass:
Ted Kremenek667cacb2008-04-15 23:06:53 +0000902 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
903 break;
Mike Stump11289f42009-09-09 15:08:12 +0000904
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000905 case Stmt::ArraySubscriptExprClass:
Ted Kremenek8219b822010-12-16 07:46:53 +0000906 VisitLvalArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst);
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000907 break;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenek667cacb2008-04-15 23:06:53 +0000909 case Stmt::AsmStmtClass:
910 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
911 break;
Ted Kremenekd51217e2010-02-15 23:02:46 +0000912
Ted Kremenek8219b822010-12-16 07:46:53 +0000913 case Stmt::BlockDeclRefExprClass: {
914 const BlockDeclRefExpr *BE = cast<BlockDeclRefExpr>(S);
915 VisitCommonDeclRefExpr(BE, BE->getDecl(), Pred, Dst);
Ted Kremenek04af9f22009-12-07 22:05:27 +0000916 break;
Ted Kremenek8219b822010-12-16 07:46:53 +0000917 }
Mike Stump11289f42009-09-09 15:08:12 +0000918
Ted Kremenekcfe223f2009-11-25 01:33:13 +0000919 case Stmt::BlockExprClass:
920 VisitBlockExpr(cast<BlockExpr>(S), Pred, Dst);
921 break;
922
Ted Kremenek667cacb2008-04-15 23:06:53 +0000923 case Stmt::BinaryOperatorClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000924 const BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000925 if (B->isLogicalOp()) {
926 VisitLogicalExpr(B, Pred, Dst);
927 break;
928 }
John McCalle3027922010-08-25 11:45:40 +0000929 else if (B->getOpcode() == BO_Comma) {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000930 const GRState* state = GetState(Pred);
Ted Kremenek57f09892010-02-08 16:18:51 +0000931 MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenek667cacb2008-04-15 23:06:53 +0000932 break;
933 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000934
Ted Kremenekd51217e2010-02-15 23:02:46 +0000935 if (AMgr.shouldEagerlyAssume() &&
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000936 (B->isRelationalOp() || B->isEqualityOp())) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000937 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +0000938 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekdc891422010-12-01 21:57:22 +0000939 evalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000940 }
941 else
Ted Kremenek8219b822010-12-16 07:46:53 +0000942 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000943
Ted Kremenek667cacb2008-04-15 23:06:53 +0000944 break;
945 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000946
Marcin Swiderskie5a1e8a2010-11-18 06:29:23 +0000947 case Stmt::CallExprClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000948 const CallExpr* C = cast<CallExpr>(S);
Ted Kremenek8219b822010-12-16 07:46:53 +0000949 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek537f6382008-11-14 19:47:18 +0000950 break;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000951 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000952
Zhongxing Xu627a1862010-11-01 09:09:44 +0000953 case Stmt::CXXConstructExprClass: {
954 const CXXConstructExpr *C = cast<CXXConstructExpr>(S);
955 // For block-level CXXConstructExpr, we don't have a destination region.
956 // Let VisitCXXConstructExpr() create one.
Ted Kremenek8219b822010-12-16 07:46:53 +0000957 VisitCXXConstructExpr(C, 0, Pred, Dst);
Zhongxing Xu627a1862010-11-01 09:09:44 +0000958 break;
959 }
960
Zhongxing Xu920070c2010-04-01 07:58:50 +0000961 case Stmt::CXXMemberCallExprClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000962 const CXXMemberCallExpr *MCE = cast<CXXMemberCallExpr>(S);
Zhongxing Xu920070c2010-04-01 07:58:50 +0000963 VisitCXXMemberCallExpr(MCE, Pred, Dst);
964 break;
965 }
966
Marcin Swiderskie5a1e8a2010-11-18 06:29:23 +0000967 case Stmt::CXXOperatorCallExprClass: {
968 const CXXOperatorCallExpr *C = cast<CXXOperatorCallExpr>(S);
969 VisitCXXOperatorCallExpr(C, Pred, Dst);
970 break;
971 }
972
Zhongxing Xub6843f52010-04-19 11:47:28 +0000973 case Stmt::CXXNewExprClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000974 const CXXNewExpr *NE = cast<CXXNewExpr>(S);
Zhongxing Xub6843f52010-04-19 11:47:28 +0000975 VisitCXXNewExpr(NE, Pred, Dst);
976 break;
977 }
978
Zhongxing Xud80755d2010-04-21 02:17:31 +0000979 case Stmt::CXXDeleteExprClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000980 const CXXDeleteExpr *CDE = cast<CXXDeleteExpr>(S);
Zhongxing Xud80755d2010-04-21 02:17:31 +0000981 VisitCXXDeleteExpr(CDE, Pred, Dst);
982 break;
983 }
Ted Kremenek667cacb2008-04-15 23:06:53 +0000984 // FIXME: ChooseExpr is really a constant. We need to fix
985 // the CFG do not model them as explicit control-flow.
Mike Stump11289f42009-09-09 15:08:12 +0000986
Ted Kremenek667cacb2008-04-15 23:06:53 +0000987 case Stmt::ChooseExprClass: { // __builtin_choose_expr
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000988 const ChooseExpr* C = cast<ChooseExpr>(S);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000989 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
990 break;
991 }
Mike Stump11289f42009-09-09 15:08:12 +0000992
Ted Kremenek667cacb2008-04-15 23:06:53 +0000993 case Stmt::CompoundAssignOperatorClass:
Ted Kremenek8219b822010-12-16 07:46:53 +0000994 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000995 break;
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000996
997 case Stmt::CompoundLiteralExprClass:
Ted Kremenek8219b822010-12-16 07:46:53 +0000998 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst);
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000999 break;
Mike Stump11289f42009-09-09 15:08:12 +00001000
John McCallc07a0c72011-02-17 10:25:35 +00001001 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek667cacb2008-04-15 23:06:53 +00001002 case Stmt::ConditionalOperatorClass: { // '?' operator
John McCallc07a0c72011-02-17 10:25:35 +00001003 const AbstractConditionalOperator *C
1004 = cast<AbstractConditionalOperator>(S);
1005 VisitGuardedExpr(C, C->getTrueExpr(), C->getFalseExpr(), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001006 break;
1007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Zhongxing Xu6df9f542009-12-16 11:27:52 +00001009 case Stmt::CXXThisExprClass:
1010 VisitCXXThisExpr(cast<CXXThisExpr>(S), Pred, Dst);
1011 break;
1012
Ted Kremenek8219b822010-12-16 07:46:53 +00001013 case Stmt::DeclRefExprClass: {
1014 const DeclRefExpr *DE = cast<DeclRefExpr>(S);
1015 VisitCommonDeclRefExpr(DE, DE->getDecl(), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001016 break;
Ted Kremenek8219b822010-12-16 07:46:53 +00001017 }
Mike Stump11289f42009-09-09 15:08:12 +00001018
Ted Kremenek667cacb2008-04-15 23:06:53 +00001019 case Stmt::DeclStmtClass:
1020 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1021 break;
Mike Stump11289f42009-09-09 15:08:12 +00001022
Ted Kremenekb135a132009-12-24 01:49:25 +00001023 case Stmt::ForStmtClass:
1024 // This case isn't for branch processing, but for handling the
1025 // initialization of a condition variable.
1026 VisitCondInit(cast<ForStmt>(S)->getConditionVariable(), S, Pred, Dst);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001027 break;
Ted Kremenekb135a132009-12-24 01:49:25 +00001028
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001029 case Stmt::ImplicitCastExprClass:
Zhongxing Xub6f02c32010-04-13 12:38:32 +00001030 case Stmt::CStyleCastExprClass:
1031 case Stmt::CXXStaticCastExprClass:
1032 case Stmt::CXXDynamicCastExprClass:
1033 case Stmt::CXXReinterpretCastExprClass:
1034 case Stmt::CXXConstCastExprClass:
1035 case Stmt::CXXFunctionalCastExprClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001036 const CastExpr* C = cast<CastExpr>(S);
Ted Kremenek8219b822010-12-16 07:46:53 +00001037 VisitCast(C, C->getSubExpr(), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001038 break;
1039 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001040
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001041 case Stmt::IfStmtClass:
1042 // This case isn't for branch processing, but for handling the
1043 // initialization of a condition variable.
Ted Kremenek58949322009-12-24 00:40:03 +00001044 VisitCondInit(cast<IfStmt>(S)->getConditionVariable(), S, Pred, Dst);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001045 break;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00001046
1047 case Stmt::InitListExprClass:
1048 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
1049 break;
Mike Stump11289f42009-09-09 15:08:12 +00001050
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001051 case Stmt::MemberExprClass:
Ted Kremenek8219b822010-12-16 07:46:53 +00001052 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst);
Ted Kremenek38213f92008-04-21 23:43:38 +00001053 break;
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001054 case Stmt::ObjCIvarRefExprClass:
Ted Kremenek8219b822010-12-16 07:46:53 +00001055 VisitLvalObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst);
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001056 break;
Ted Kremenek17810802008-11-12 19:24:17 +00001057
1058 case Stmt::ObjCForCollectionStmtClass:
1059 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
1060 break;
Mike Stump11289f42009-09-09 15:08:12 +00001061
Ted Kremeneke19711d2009-12-22 22:13:46 +00001062 case Stmt::ObjCMessageExprClass:
Ted Kremenek8219b822010-12-16 07:46:53 +00001063 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001064 break;
Mike Stump11289f42009-09-09 15:08:12 +00001065
Ted Kremenek1857ff42008-12-09 20:18:58 +00001066 case Stmt::ObjCAtThrowStmtClass: {
1067 // FIXME: This is not complete. We basically treat @throw as
1068 // an abort.
1069 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1070 Builder->BuildSinks = true;
1071 MakeNode(Dst, S, Pred, GetState(Pred));
1072 break;
1073 }
Mike Stump11289f42009-09-09 15:08:12 +00001074
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001075 case Stmt::ReturnStmtClass:
1076 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
1077 break;
Mike Stump11289f42009-09-09 15:08:12 +00001078
Douglas Gregor882211c2010-04-28 22:16:22 +00001079 case Stmt::OffsetOfExprClass:
1080 VisitOffsetOfExpr(cast<OffsetOfExpr>(S), Pred, Dst);
1081 break;
1082
Sebastian Redl6f282892008-11-11 17:56:53 +00001083 case Stmt::SizeOfAlignOfExprClass:
1084 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001085 break;
Mike Stump11289f42009-09-09 15:08:12 +00001086
Ted Kremenek667cacb2008-04-15 23:06:53 +00001087 case Stmt::StmtExprClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001088 const StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekd25fb7a62009-02-14 05:55:08 +00001089
1090 if (SE->getSubStmt()->body_empty()) {
1091 // Empty statement expression.
1092 assert(SE->getType() == getContext().VoidTy
1093 && "Empty statement expression must have void type.");
1094 Dst.Add(Pred);
1095 break;
1096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
Ted Kremenekd25fb7a62009-02-14 05:55:08 +00001098 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
1099 const GRState* state = GetState(Pred);
Ted Kremenek57f09892010-02-08 16:18:51 +00001100 MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekd25fb7a62009-02-14 05:55:08 +00001101 }
Ted Kremenek667cacb2008-04-15 23:06:53 +00001102 else
1103 Dst.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001104
Ted Kremenek667cacb2008-04-15 23:06:53 +00001105 break;
1106 }
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001107
Ted Kremenek8219b822010-12-16 07:46:53 +00001108 case Stmt::StringLiteralClass: {
1109 const GRState* state = GetState(Pred);
1110 SVal V = state->getLValue(cast<StringLiteral>(S));
1111 MakeNode(Dst, S, Pred, state->BindExpr(S, V));
1112 return;
1113 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001114
Ted Kremenek58949322009-12-24 00:40:03 +00001115 case Stmt::SwitchStmtClass:
1116 // This case isn't for branch processing, but for handling the
1117 // initialization of a condition variable.
1118 VisitCondInit(cast<SwitchStmt>(S)->getConditionVariable(), S, Pred, Dst);
1119 break;
Mike Stump11289f42009-09-09 15:08:12 +00001120
Ted Kremenek891642e2009-03-18 23:49:26 +00001121 case Stmt::UnaryOperatorClass: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001122 const UnaryOperator *U = cast<UnaryOperator>(S);
John McCalle3027922010-08-25 11:45:40 +00001123 if (AMgr.shouldEagerlyAssume()&&(U->getOpcode() == UO_LNot)) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001124 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +00001125 VisitUnaryOperator(U, Pred, Tmp);
Ted Kremenekdc891422010-12-01 21:57:22 +00001126 evalEagerlyAssume(Dst, Tmp, U);
Ted Kremenek891642e2009-03-18 23:49:26 +00001127 }
1128 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001129 VisitUnaryOperator(U, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001130 break;
Ted Kremenek891642e2009-03-18 23:49:26 +00001131 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001132
Ted Kremenek09bc3b72009-12-24 00:54:56 +00001133 case Stmt::WhileStmtClass:
1134 // This case isn't for branch processing, but for handling the
1135 // initialization of a condition variable.
1136 VisitCondInit(cast<WhileStmt>(S)->getConditionVariable(), S, Pred, Dst);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001137 break;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001138 }
1139}
1140
Ted Kremenek667cacb2008-04-15 23:06:53 +00001141//===----------------------------------------------------------------------===//
1142// Block entrance. (Update counters).
1143//===----------------------------------------------------------------------===//
1144
Ted Kremeneka00bccc2011-01-11 06:37:47 +00001145void ExprEngine::processCFGBlockEntrance(ExplodedNodeSet &dstNodes,
1146 GenericNodeBuilder<BlockEntrance> &nodeBuilder){
1147
1148 // FIXME: Refactor this into a checker.
1149 const CFGBlock *block = nodeBuilder.getProgramPoint().getBlock();
1150 ExplodedNode *pred = nodeBuilder.getPredecessor();
1151
1152 if (nodeBuilder.getBlockCounter().getNumVisited(
1153 pred->getLocationContext()->getCurrentStackFrame(),
1154 block->getBlockID()) >= AMgr.getMaxVisit()) {
1155
1156 static int tag = 0;
Ted Kremenek841df112011-01-11 16:53:44 +00001157 nodeBuilder.generateNode(pred->getState(), pred, &tag, true);
Ted Kremeneka00bccc2011-01-11 06:37:47 +00001158 }
Ted Kremenek667cacb2008-04-15 23:06:53 +00001159}
1160
1161//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +00001162// Generic node creation.
1163//===----------------------------------------------------------------------===//
1164
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001165ExplodedNode* ExprEngine::MakeNode(ExplodedNodeSet& Dst, const Stmt* S,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001166 ExplodedNode* Pred, const GRState* St,
1167 ProgramPoint::Kind K, const void *tag) {
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001168 assert (Builder && "StmtNodeBuilder not present.");
Ted Kremenekdf240002009-04-11 00:11:10 +00001169 SaveAndRestore<const void*> OldTag(Builder->Tag);
1170 Builder->Tag = tag;
1171 return Builder->MakeNode(Dst, S, Pred, St, K);
1172}
1173
1174//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001175// Branch processing.
1176//===----------------------------------------------------------------------===//
1177
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001178const GRState* ExprEngine::MarkBranch(const GRState* state,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001179 const Stmt* Terminator,
1180 bool branchTaken) {
Mike Stump11289f42009-09-09 15:08:12 +00001181
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001182 switch (Terminator->getStmtClass()) {
1183 default:
Ted Kremenek17d541d2009-02-13 01:45:31 +00001184 return state;
Mike Stump11289f42009-09-09 15:08:12 +00001185
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001186 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump11289f42009-09-09 15:08:12 +00001187
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001188 const BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001189 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00001190
John McCalle3027922010-08-25 11:45:40 +00001191 assert (Op == BO_LAnd || Op == BO_LOr);
Mike Stump11289f42009-09-09 15:08:12 +00001192
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001193 // For &&, if we take the true branch, then the value of the whole
1194 // expression is that of the RHS expression.
1195 //
1196 // For ||, if we take the false branch, then the value of the whole
1197 // expression is that of the RHS expression.
Mike Stump11289f42009-09-09 15:08:12 +00001198
John McCalle3027922010-08-25 11:45:40 +00001199 const Expr* Ex = (Op == BO_LAnd && branchTaken) ||
1200 (Op == BO_LOr && !branchTaken)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001201 ? B->getRHS() : B->getLHS();
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001203 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001204 }
Mike Stump11289f42009-09-09 15:08:12 +00001205
John McCallc07a0c72011-02-17 10:25:35 +00001206 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001207 case Stmt::ConditionalOperatorClass: { // ?:
John McCallc07a0c72011-02-17 10:25:35 +00001208 const AbstractConditionalOperator* C
1209 = cast<AbstractConditionalOperator>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001211 // For ?, if branchTaken == true then the value is either the LHS or
1212 // the condition itself. (GNU extension).
Mike Stump11289f42009-09-09 15:08:12 +00001213
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001214 const Expr* Ex;
Mike Stump11289f42009-09-09 15:08:12 +00001215
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001216 if (branchTaken)
John McCallc07a0c72011-02-17 10:25:35 +00001217 Ex = C->getTrueExpr();
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001218 else
John McCallc07a0c72011-02-17 10:25:35 +00001219 Ex = C->getFalseExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001220
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001221 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001224 case Stmt::ChooseExprClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +00001225
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001226 const ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001227
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001228 const Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001229 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001230 }
1231 }
1232}
1233
Ted Kremenek22358bd2009-03-13 16:32:54 +00001234/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
1235/// to try to recover some path-sensitivity for casts of symbolic
1236/// integers that promote their values (which are currently not tracked well).
1237/// This function returns the SVal bound to Condition->IgnoreCasts if all the
1238// cast(s) did was sign-extend the original value.
1239static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001240 const Stmt* Condition, ASTContext& Ctx) {
Ted Kremenek22358bd2009-03-13 16:32:54 +00001241
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001242 const Expr *Ex = dyn_cast<Expr>(Condition);
Ted Kremenek22358bd2009-03-13 16:32:54 +00001243 if (!Ex)
1244 return UnknownVal();
1245
1246 uint64_t bits = 0;
1247 bool bitsInit = false;
Mike Stump11289f42009-09-09 15:08:12 +00001248
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001249 while (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
Ted Kremenek22358bd2009-03-13 16:32:54 +00001250 QualType T = CE->getType();
1251
1252 if (!T->isIntegerType())
1253 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001254
Ted Kremenek22358bd2009-03-13 16:32:54 +00001255 uint64_t newBits = Ctx.getTypeSize(T);
1256 if (!bitsInit || newBits < bits) {
1257 bitsInit = true;
1258 bits = newBits;
1259 }
Mike Stump11289f42009-09-09 15:08:12 +00001260
Ted Kremenek22358bd2009-03-13 16:32:54 +00001261 Ex = CE->getSubExpr();
1262 }
1263
1264 // We reached a non-cast. Is it a symbolic value?
1265 QualType T = Ex->getType();
1266
1267 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
1268 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001269
Ted Kremenek57f09892010-02-08 16:18:51 +00001270 return state->getSVal(Ex);
Ted Kremenek22358bd2009-03-13 16:32:54 +00001271}
1272
Ted Kremenek926c9622011-01-11 02:34:45 +00001273void ExprEngine::processBranch(const Stmt* Condition, const Stmt* Term,
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001274 BranchNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001275
Ted Kremenek8db4b112008-02-15 22:29:00 +00001276 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump11289f42009-09-09 15:08:12 +00001277 if (!Condition) {
Ted Kremenek8db4b112008-02-15 22:29:00 +00001278 builder.markInfeasible(false);
Ted Kremenek8db4b112008-02-15 22:29:00 +00001279 return;
1280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Ted Kremenek32c41ec2009-03-11 03:54:24 +00001282 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
1283 Condition->getLocStart(),
1284 "Error evaluating branch");
Ted Kremenek907a7112009-08-27 01:39:13 +00001285
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001286 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
1287 void *tag = I->first;
1288 Checker *checker = I->second;
1289 checker->VisitBranchCondition(builder, *this, Condition, tag);
1290 }
1291
Argyrios Kyrtzidis753b3ca2011-02-28 01:27:33 +00001292 getCheckerManager().runCheckersForBranchCondition(Condition, builder, *this);
1293
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001294 // If the branch condition is undefined, return;
1295 if (!builder.isFeasible(true) && !builder.isFeasible(false))
1296 return;
1297
Mike Stump11289f42009-09-09 15:08:12 +00001298 const GRState* PrevState = builder.getState();
Ted Kremenek57f09892010-02-08 16:18:51 +00001299 SVal X = PrevState->getSVal(Condition);
Mike Stump11289f42009-09-09 15:08:12 +00001300
Argyrios Kyrtzidis4f7745a2011-02-28 01:27:57 +00001301 if (X.isUnknownOrUndef()) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001302 // Give it a chance to recover from unknown.
1303 if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
1304 if (Ex->getType()->isIntegerType()) {
1305 // Try to recover some path-sensitivity. Right now casts of symbolic
1306 // integers that promote their values are currently not tracked well.
1307 // If 'Condition' is such an expression, try and recover the
1308 // underlying value and use that instead.
1309 SVal recovered = RecoverCastedSymbol(getStateManager(),
1310 builder.getState(), Condition,
1311 getContext());
Ted Kremenekd51217e2010-02-15 23:02:46 +00001312
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001313 if (!recovered.isUnknown()) {
1314 X = recovered;
1315 }
Ted Kremenek22358bd2009-03-13 16:32:54 +00001316 }
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001317 }
1318 // If the condition is still unknown, give up.
Argyrios Kyrtzidis4f7745a2011-02-28 01:27:57 +00001319 if (X.isUnknownOrUndef()) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001320 builder.generateNode(MarkBranch(PrevState, Term, true), true);
1321 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremeneka50d9852008-01-30 23:03:39 +00001322 return;
Mike Stump11289f42009-09-09 15:08:12 +00001323 }
Ted Kremeneka50d9852008-01-30 23:03:39 +00001324 }
Mike Stump11289f42009-09-09 15:08:12 +00001325
Zhongxing Xu56dd5f02009-11-23 03:20:54 +00001326 DefinedSVal V = cast<DefinedSVal>(X);
1327
Ted Kremenek17f4dbd2008-02-29 20:27:50 +00001328 // Process the true branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001329 if (builder.isFeasible(true)) {
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00001330 if (const GRState *state = PrevState->assume(V, true))
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001331 builder.generateNode(MarkBranch(state, Term, true), true);
1332 else
1333 builder.markInfeasible(true);
1334 }
Mike Stump11289f42009-09-09 15:08:12 +00001335
1336 // Process the false branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001337 if (builder.isFeasible(false)) {
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00001338 if (const GRState *state = PrevState->assume(V, false))
Ted Kremenekaf9f3622009-07-20 18:44:36 +00001339 builder.generateNode(MarkBranch(state, Term, false), false);
1340 else
1341 builder.markInfeasible(false);
1342 }
Ted Kremenek7ff18932008-01-29 23:32:35 +00001343}
1344
Ted Kremenek926c9622011-01-11 02:34:45 +00001345/// processIndirectGoto - Called by CoreEngine. Used to generate successor
Ted Kremenek7022efb2008-02-13 00:24:44 +00001346/// nodes by processing the 'effects' of a computed goto jump.
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001347void ExprEngine::processIndirectGoto(IndirectGotoNodeBuilder &builder) {
Ted Kremenek7022efb2008-02-13 00:24:44 +00001348
Mike Stump11289f42009-09-09 15:08:12 +00001349 const GRState *state = builder.getState();
Ted Kremenek57f09892010-02-08 16:18:51 +00001350 SVal V = state->getSVal(builder.getTarget());
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenek7022efb2008-02-13 00:24:44 +00001352 // Three possibilities:
1353 //
1354 // (1) We know the computed label.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001355 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek7022efb2008-02-13 00:24:44 +00001356 // (3) We have no clue about the label. Dispatch to all targets.
1357 //
Mike Stump11289f42009-09-09 15:08:12 +00001358
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001359 typedef IndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek7022efb2008-02-13 00:24:44 +00001360
Zhongxing Xu27f17422008-10-17 05:57:07 +00001361 if (isa<loc::GotoLabel>(V)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001362 const LabelDecl *L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00001363
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001364 for (iterator I = builder.begin(), E = builder.end(); I != E; ++I) {
Ted Kremenek2bba9012008-02-13 17:27:37 +00001365 if (I.getLabel() == L) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001366 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001367 return;
1368 }
1369 }
Mike Stump11289f42009-09-09 15:08:12 +00001370
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001371 assert(false && "No block with label.");
Ted Kremenek7022efb2008-02-13 00:24:44 +00001372 return;
1373 }
1374
Zhongxing Xu27f17422008-10-17 05:57:07 +00001375 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek7022efb2008-02-13 00:24:44 +00001376 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu9e200792009-11-24 07:06:39 +00001377 //ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
1378 // FIXME: add checker visit.
1379 // UndefBranches.insert(N);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001380 return;
1381 }
Mike Stump11289f42009-09-09 15:08:12 +00001382
Ted Kremenek7022efb2008-02-13 00:24:44 +00001383 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenek9c03f682009-04-23 17:49:43 +00001384 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump11289f42009-09-09 15:08:12 +00001385
Ted Kremenek7022efb2008-02-13 00:24:44 +00001386 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek17d541d2009-02-13 01:45:31 +00001387 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +00001388}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001389
Ted Kremenek667cacb2008-04-15 23:06:53 +00001390
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001391void ExprEngine::VisitGuardedExpr(const Expr* Ex, const Expr* L,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001392 const Expr* R,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001393 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001394
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001395 assert(Ex == currentStmt &&
Zhongxing Xu6df9f542009-12-16 11:27:52 +00001396 Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump11289f42009-09-09 15:08:12 +00001397
Ted Kremenek17d541d2009-02-13 01:45:31 +00001398 const GRState* state = GetState(Pred);
Ted Kremenek57f09892010-02-08 16:18:51 +00001399 SVal X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001400
Ted Kremenek667cacb2008-04-15 23:06:53 +00001401 assert (X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001402
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001403 const Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump11289f42009-09-09 15:08:12 +00001404 assert(SE);
Ted Kremenek57f09892010-02-08 16:18:51 +00001405 X = state->getSVal(SE);
Mike Stump11289f42009-09-09 15:08:12 +00001406
Ted Kremenek667cacb2008-04-15 23:06:53 +00001407 // Make sure that we invalidate the previous binding.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001408 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremenek667cacb2008-04-15 23:06:53 +00001409}
1410
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001411/// ProcessEndPath - Called by CoreEngine. Used to generate end-of-path
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001412/// nodes when the control reaches the end of a function.
Ted Kremenek926c9622011-01-11 02:34:45 +00001413void ExprEngine::processEndOfFunction(EndOfFunctionNodeBuilder& builder) {
Ted Kremenekdc891422010-12-01 21:57:22 +00001414 getTF().evalEndPath(*this, builder);
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001415 StateMgr.EndPath(builder.getState());
Zhongxing Xu4668c7e2009-11-17 07:54:15 +00001416 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E;++I){
1417 void *tag = I->first;
1418 Checker *checker = I->second;
Argyrios Kyrtzidisf1b5d1f2011-02-23 21:04:49 +00001419 EndOfFunctionNodeBuilder B = builder.withCheckerTag(tag);
1420 checker->evalEndPath(B, tag, *this);
Zhongxing Xu4668c7e2009-11-17 07:54:15 +00001421 }
Argyrios Kyrtzidis506220f2011-02-23 21:04:54 +00001422 getCheckerManager().runCheckersForEndPath(builder, *this);
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +00001423}
1424
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001425/// ProcessSwitch - Called by CoreEngine. Used to generate successor
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001426/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek926c9622011-01-11 02:34:45 +00001427void ExprEngine::processSwitch(SwitchNodeBuilder& builder) {
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001428 typedef SwitchNodeBuilder::iterator iterator;
Mike Stump11289f42009-09-09 15:08:12 +00001429 const GRState* state = builder.getState();
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001430 const Expr* CondE = builder.getCondition();
Ted Kremenek57f09892010-02-08 16:18:51 +00001431 SVal CondV_untested = state->getSVal(CondE);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001432
Ted Kremenek7020eae2009-09-11 22:07:28 +00001433 if (CondV_untested.isUndef()) {
Zhongxing Xu9e200792009-11-24 07:06:39 +00001434 //ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001435 // FIXME: add checker
Zhongxing Xu9e200792009-11-24 07:06:39 +00001436 //UndefBranches.insert(N);
1437
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001438 return;
1439 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001440 DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
Ted Kremenek346169f2008-02-18 22:57:02 +00001441
Ted Kremenek7020eae2009-09-11 22:07:28 +00001442 const GRState *DefaultSt = state;
Ted Kremenek036223b2010-08-26 22:19:33 +00001443
1444 iterator I = builder.begin(), EI = builder.end();
1445 bool defaultIsFeasible = I == EI;
Mike Stump11289f42009-09-09 15:08:12 +00001446
Ted Kremenek036223b2010-08-26 22:19:33 +00001447 for ( ; I != EI; ++I) {
Ted Kremenekc8bd9672010-08-26 22:04:01 +00001448 const CaseStmt* Case = I.getCase();
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001449
1450 // Evaluate the LHS of the case value.
1451 Expr::EvalResult V1;
Mike Stump11289f42009-09-09 15:08:12 +00001452 bool b = Case->getLHS()->Evaluate(V1, getContext());
1453
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001454 // Sanity checks. These go away in Release builds.
Mike Stump11289f42009-09-09 15:08:12 +00001455 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001456 && "Case condition must evaluate to an integer constant.");
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001457 (void)b; // silence unused variable warning
Mike Stump11289f42009-09-09 15:08:12 +00001458 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001459 getContext().getTypeSize(CondE->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001460
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001461 // Get the RHS of the case, if it exists.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001462 Expr::EvalResult V2;
Mike Stump11289f42009-09-09 15:08:12 +00001463
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001464 if (const Expr* E = Case->getRHS()) {
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001465 b = E->Evaluate(V2, getContext());
Mike Stump11289f42009-09-09 15:08:12 +00001466 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001467 && "Case condition must evaluate to an integer constant.");
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001468 (void)b; // silence unused variable warning
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001469 }
Ted Kremenek9eae4032008-03-17 22:17:56 +00001470 else
1471 V2 = V1;
Mike Stump11289f42009-09-09 15:08:12 +00001472
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001473 // FIXME: Eventually we should replace the logic below with a range
1474 // comparison, rather than concretize the values within the range.
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001475 // This should be easy once we have "ranges" for NonLVals.
Mike Stump11289f42009-09-09 15:08:12 +00001476
Ted Kremenek9eae4032008-03-17 22:17:56 +00001477 do {
Mike Stump11289f42009-09-09 15:08:12 +00001478 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenekdc891422010-12-01 21:57:22 +00001479 DefinedOrUnknownSVal Res = svalBuilder.evalEQ(DefaultSt ? DefaultSt : state,
Ted Kremenekb92304b2010-01-08 18:54:04 +00001480 CondV, CaseVal);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001481
Mike Stump11289f42009-09-09 15:08:12 +00001482 // Now "assume" that the case matches.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00001483 if (const GRState* stateNew = state->assume(Res, true)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001484 builder.generateCaseStmtNode(I, stateNew);
Mike Stump11289f42009-09-09 15:08:12 +00001485
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001486 // If CondV evaluates to a constant, then we know that this
1487 // is the *only* case that we can take, so stop evaluating the
1488 // others.
Zhongxing Xu27f17422008-10-17 05:57:07 +00001489 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001490 return;
1491 }
Mike Stump11289f42009-09-09 15:08:12 +00001492
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001493 // Now "assume" that the case doesn't match. Add this state
1494 // to the default state (if it is feasible).
Ted Kremenekb92304b2010-01-08 18:54:04 +00001495 if (DefaultSt) {
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00001496 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
Ted Kremenekb92304b2010-01-08 18:54:04 +00001497 defaultIsFeasible = true;
1498 DefaultSt = stateNew;
1499 }
1500 else {
1501 defaultIsFeasible = false;
1502 DefaultSt = NULL;
1503 }
Ted Kremenekd2419a02008-04-23 05:03:18 +00001504 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001505
Ted Kremenek9eae4032008-03-17 22:17:56 +00001506 // Concretize the next value in the range.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001507 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek9eae4032008-03-17 22:17:56 +00001508 break;
Mike Stump11289f42009-09-09 15:08:12 +00001509
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001510 ++V1.Val.getInt();
1511 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump11289f42009-09-09 15:08:12 +00001512
Ted Kremenek9eae4032008-03-17 22:17:56 +00001513 } while (true);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001514 }
Mike Stump11289f42009-09-09 15:08:12 +00001515
Ted Kremenek8f0e8342010-09-09 00:40:40 +00001516 if (!defaultIsFeasible)
1517 return;
1518
1519 // If we have switch(enum value), the default branch is not
1520 // feasible if all of the enum constants not covered by 'case:' statements
1521 // are not feasible values for the switch condition.
1522 //
1523 // Note that this isn't as accurate as it could be. Even if there isn't
1524 // a case for a particular enum value as long as that enum value isn't
1525 // feasible then it shouldn't be considered for making 'default:' reachable.
1526 const SwitchStmt *SS = builder.getSwitch();
1527 const Expr *CondExpr = SS->getCond()->IgnoreParenImpCasts();
1528 if (CondExpr->getType()->getAs<EnumType>()) {
1529 if (SS->isAllEnumCasesCovered())
1530 return;
1531 }
1532
1533 builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001534}
1535
Ted Kremenek926c9622011-01-11 02:34:45 +00001536void ExprEngine::processCallEnter(CallEnterNodeBuilder &B) {
Ted Kremenek7c211622011-01-14 20:34:10 +00001537 const GRState *state = B.getState()->enterStackFrame(B.getCalleeContext());
Ted Kremenek750b7ac2010-12-20 21:19:09 +00001538 B.generateNode(state);
Douglas Gregora2fbc942010-02-25 19:01:53 +00001539}
1540
Ted Kremenek926c9622011-01-11 02:34:45 +00001541void ExprEngine::processCallExit(CallExitNodeBuilder &B) {
Douglas Gregora2fbc942010-02-25 19:01:53 +00001542 const GRState *state = B.getState();
1543 const ExplodedNode *Pred = B.getPredecessor();
Zhongxing Xucb298022010-11-24 08:53:20 +00001544 const StackFrameContext *calleeCtx =
Douglas Gregora2fbc942010-02-25 19:01:53 +00001545 cast<StackFrameContext>(Pred->getLocationContext());
Zhongxing Xucb298022010-11-24 08:53:20 +00001546 const Stmt *CE = calleeCtx->getCallSite();
Douglas Gregora2fbc942010-02-25 19:01:53 +00001547
Zhongxing Xu5c075842010-02-26 15:43:34 +00001548 // If the callee returns an expression, bind its value to CallExpr.
1549 const Stmt *ReturnedExpr = state->get<ReturnExpr>();
1550 if (ReturnedExpr) {
1551 SVal RetVal = state->getSVal(ReturnedExpr);
1552 state = state->BindExpr(CE, RetVal);
Zhongxing Xubf2f0d72010-03-23 08:09:29 +00001553 // Clear the return expr GDM.
Zhongxing Xub6e1c132010-03-25 01:39:39 +00001554 state = state->remove<ReturnExpr>();
Zhongxing Xu5c075842010-02-26 15:43:34 +00001555 }
1556
Zhongxing Xue248dca2010-03-23 09:13:17 +00001557 // Bind the constructed object value to CXXConstructExpr.
1558 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) {
Zhongxing Xu1ade3262010-11-16 07:52:17 +00001559 const CXXThisRegion *ThisR =
Zhongxing Xucb298022010-11-24 08:53:20 +00001560 getCXXThisRegion(CCE->getConstructor()->getParent(), calleeCtx);
Zhongxing Xua1a9ba12010-11-24 13:08:51 +00001561
Zhongxing Xue248dca2010-03-23 09:13:17 +00001562 SVal ThisV = state->getSVal(ThisR);
Zhongxing Xu70892502010-12-22 07:20:27 +00001563 // Always bind the region to the CXXConstructExpr.
1564 state = state->BindExpr(CCE, ThisV);
Zhongxing Xue248dca2010-03-23 09:13:17 +00001565 }
1566
Ted Kremenek750b7ac2010-12-20 21:19:09 +00001567 B.generateNode(state);
Douglas Gregora2fbc942010-02-25 19:01:53 +00001568}
1569
Ted Kremenek667cacb2008-04-15 23:06:53 +00001570//===----------------------------------------------------------------------===//
1571// Transfer functions: logical operations ('&&', '||').
1572//===----------------------------------------------------------------------===//
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001573
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001574void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001575 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001576
John McCalle3027922010-08-25 11:45:40 +00001577 assert(B->getOpcode() == BO_LAnd ||
1578 B->getOpcode() == BO_LOr);
Mike Stump11289f42009-09-09 15:08:12 +00001579
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001580 assert(B==currentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump11289f42009-09-09 15:08:12 +00001581
Ted Kremenek17d541d2009-02-13 01:45:31 +00001582 const GRState* state = GetState(Pred);
Ted Kremenek57f09892010-02-08 16:18:51 +00001583 SVal X = state->getSVal(B);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001584 assert(X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001585
Ted Kremenek7020eae2009-09-11 22:07:28 +00001586 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001587 assert(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001588
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001589 if (Ex == B->getRHS()) {
Ted Kremenek57f09892010-02-08 16:18:51 +00001590 X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001591
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001592 // Handle undefined values.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001593 if (X.isUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001594 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenekbc543902008-02-26 19:40:44 +00001595 return;
1596 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001597
Ted Kremenek7020eae2009-09-11 22:07:28 +00001598 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
Mike Stump11289f42009-09-09 15:08:12 +00001599
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001600 // We took the RHS. Because the value of the '&&' or '||' expression must
1601 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
1602 // or 1. Alternatively, we could take a lazy approach, and calculate this
1603 // value later when necessary. We don't have the machinery in place for
1604 // this right now, and since most logical expressions are used for branches,
Mike Stump11289f42009-09-09 15:08:12 +00001605 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00001606 if (const GRState *newState = state->assume(XD, true))
Mike Stump11289f42009-09-09 15:08:12 +00001607 MakeNode(Dst, B, Pred,
Ted Kremenek90af9092010-12-02 07:49:45 +00001608 newState->BindExpr(B, svalBuilder.makeIntVal(1U, B->getType())));
Mike Stump11289f42009-09-09 15:08:12 +00001609
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00001610 if (const GRState *newState = state->assume(XD, false))
Mike Stump11289f42009-09-09 15:08:12 +00001611 MakeNode(Dst, B, Pred,
Ted Kremenek90af9092010-12-02 07:49:45 +00001612 newState->BindExpr(B, svalBuilder.makeIntVal(0U, B->getType())));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001613 }
1614 else {
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001615 // We took the LHS expression. Depending on whether we are '&&' or
1616 // '||' we know what the value of the expression is via properties of
1617 // the short-circuiting.
Ted Kremenek90af9092010-12-02 07:49:45 +00001618 X = svalBuilder.makeIntVal(B->getOpcode() == BO_LAnd ? 0U : 1U,
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001619 B->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001620 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001621 }
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001622}
Mike Stump11289f42009-09-09 15:08:12 +00001623
Ted Kremenek667cacb2008-04-15 23:06:53 +00001624//===----------------------------------------------------------------------===//
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001625// Transfer functions: Loads and stores.
Ted Kremenek667cacb2008-04-15 23:06:53 +00001626//===----------------------------------------------------------------------===//
Ted Kremenekde8d62b2008-01-15 23:55:06 +00001627
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001628void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001629 ExplodedNodeSet &Dst) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00001630
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001631 ExplodedNodeSet Tmp;
Ted Kremenekd51217e2010-02-15 23:02:46 +00001632
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001633 CanQualType T = getContext().getCanonicalType(BE->getType());
Ted Kremenek90af9092010-12-02 07:49:45 +00001634 SVal V = svalBuilder.getBlockPointer(BE->getBlockDecl(), T,
Ted Kremenekb63ad7a2009-11-25 23:53:07 +00001635 Pred->getLocationContext());
1636
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001637 MakeNode(Tmp, BE, Pred, GetState(Pred)->BindExpr(BE, V),
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001638 ProgramPoint::PostLValueKind);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001639
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001640 // Post-visit the BlockExpr.
Jordy Rosec36df4d2010-08-04 07:10:57 +00001641 CheckerVisit(BE, Dst, Tmp, PostVisitStmtCallback);
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001642}
1643
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001644void ExprEngine::VisitCommonDeclRefExpr(const Expr *Ex, const NamedDecl *D,
1645 ExplodedNode *Pred,
1646 ExplodedNodeSet &Dst) {
Ted Kremenek7020eae2009-09-11 22:07:28 +00001647 const GRState *state = GetState(Pred);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001648
1649 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek8219b822010-12-16 07:46:53 +00001650 assert(Ex->isLValue());
Ted Kremenek14536f62009-08-21 22:28:32 +00001651 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xu252fe5c2008-10-17 02:20:14 +00001652
Ted Kremenek8219b822010-12-16 07:46:53 +00001653 // For references, the 'lvalue' is the pointer address stored in the
1654 // reference region.
1655 if (VD->getType()->isReferenceType()) {
1656 if (const MemRegion *R = V.getAsRegion())
1657 V = state->getSVal(R);
1658 else
1659 V = UnknownVal();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001660 }
Zhongxing Xu232c7922008-10-16 06:09:51 +00001661
Ted Kremenek8219b822010-12-16 07:46:53 +00001662 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
1663 ProgramPoint::PostLValueKind);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001664 return;
Ted Kremenek8219b822010-12-16 07:46:53 +00001665 }
1666 if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
1667 assert(!Ex->isLValue());
Ted Kremenek90af9092010-12-02 07:49:45 +00001668 SVal V = svalBuilder.makeIntVal(ED->getInitVal());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001669 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu232c7922008-10-16 06:09:51 +00001670 return;
Ted Kremenek8219b822010-12-16 07:46:53 +00001671 }
1672 if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek90af9092010-12-02 07:49:45 +00001673 SVal V = svalBuilder.getFunctionPointer(FD);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001674 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001675 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001676 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001677 }
Zhongxing Xu232c7922008-10-16 06:09:51 +00001678 assert (false &&
1679 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek88da1de2008-02-07 04:16:04 +00001680}
1681
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001682/// VisitArraySubscriptExpr - Transfer function for array accesses
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001683void ExprEngine::VisitLvalArraySubscriptExpr(const ArraySubscriptExpr* A,
1684 ExplodedNode* Pred,
1685 ExplodedNodeSet& Dst){
Mike Stump11289f42009-09-09 15:08:12 +00001686
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00001687 const Expr* Base = A->getBase()->IgnoreParens();
1688 const Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek8219b822010-12-16 07:46:53 +00001689
1690 // Evaluate the base.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001691 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +00001692 Visit(Base, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001693
Zhongxing Xu107f7592009-08-06 12:48:26 +00001694 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1695 ExplodedNodeSet Tmp2;
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001696 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Zhongxing Xub1667122009-11-11 13:42:54 +00001697 ExplodedNodeSet Tmp3;
Jordy Rosec36df4d2010-08-04 07:10:57 +00001698 CheckerVisit(A, Tmp3, Tmp2, PreVisitStmtCallback);
Zhongxing Xub1667122009-11-11 13:42:54 +00001699
1700 for (ExplodedNodeSet::iterator I2=Tmp3.begin(),E2=Tmp3.end();I2!=E2; ++I2) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001701 const GRState* state = GetState(*I2);
Ted Kremenek57f09892010-02-08 16:18:51 +00001702 SVal V = state->getLValue(A->getType(), state->getSVal(Idx),
1703 state->getSVal(Base));
Ted Kremenek8219b822010-12-16 07:46:53 +00001704 assert(A->isLValue());
1705 MakeNode(Dst, A, *I2, state->BindExpr(A, V), ProgramPoint::PostLValueKind);
Ted Kremenek10246e82008-04-29 23:24:44 +00001706 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001707 }
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001708}
1709
Ted Kremenek38213f92008-04-21 23:43:38 +00001710/// VisitMemberExpr - Transfer function for member expressions.
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001711void ExprEngine::VisitMemberExpr(const MemberExpr* M, ExplodedNode* Pred,
1712 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001713
Ted Kremenek8219b822010-12-16 07:46:53 +00001714 Expr *baseExpr = M->getBase()->IgnoreParens();
1715 ExplodedNodeSet dstBase;
1716 Visit(baseExpr, Pred, dstBase);
Mike Stump11289f42009-09-09 15:08:12 +00001717
Ted Kremenek8219b822010-12-16 07:46:53 +00001718 FieldDecl *field = dyn_cast<FieldDecl>(M->getMemberDecl());
1719 if (!field) // FIXME: skipping member expressions for non-fields
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001720 return;
1721
Ted Kremenek8219b822010-12-16 07:46:53 +00001722 for (ExplodedNodeSet::iterator I = dstBase.begin(), E = dstBase.end();
1723 I != E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001724 const GRState* state = GetState(*I);
Ted Kremenek8219b822010-12-16 07:46:53 +00001725 SVal baseExprVal = state->getSVal(baseExpr);
1726 if (isa<nonloc::LazyCompoundVal>(baseExprVal) ||
Argyrios Kyrtzidis58f8b592011-02-03 22:01:32 +00001727 isa<nonloc::CompoundVal>(baseExprVal) ||
1728 // FIXME: This can originate by conjuring a symbol for an unknown
1729 // temporary struct object, see test/Analysis/fields.c:
1730 // (p = getit()).x
1731 isa<nonloc::SymbolVal>(baseExprVal)) {
Ted Kremenek8219b822010-12-16 07:46:53 +00001732 MakeNode(Dst, M, *I, state->BindExpr(M, UnknownVal()));
1733 continue;
1734 }
1735
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001736 // FIXME: Should we insert some assumption logic in here to determine
1737 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001738 // later when using FieldOffset lvals (which we no longer have).
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001739
Ted Kremenek8219b822010-12-16 07:46:53 +00001740 // For all other cases, compute an lvalue.
1741 SVal L = state->getLValue(field, baseExprVal);
1742 if (M->isLValue())
Zhongxing Xub9eda672009-10-30 07:19:39 +00001743 MakeNode(Dst, M, *I, state->BindExpr(M, L), ProgramPoint::PostLValueKind);
1744 else
Ted Kremenekdc891422010-12-01 21:57:22 +00001745 evalLoad(Dst, M, *I, state, L);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001746 }
Ted Kremenek38213f92008-04-21 23:43:38 +00001747}
1748
Ted Kremenekdc891422010-12-01 21:57:22 +00001749/// evalBind - Handle the semantics of binding a value to a specific location.
1750/// This method is used by evalStore and (soon) VisitDeclStmt, and others.
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001751void ExprEngine::evalBind(ExplodedNodeSet& Dst, const Stmt* StoreE,
Ted Kremenek07343c02010-09-02 00:56:20 +00001752 ExplodedNode* Pred, const GRState* state,
1753 SVal location, SVal Val, bool atDeclInit) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00001754
1755
Ted Kremenekef910042009-11-04 04:24:16 +00001756 // Do a previsit of the bind.
1757 ExplodedNodeSet CheckedSet, Src;
1758 Src.Add(Pred);
Ted Kremenek07343c02010-09-02 00:56:20 +00001759 CheckerVisitBind(StoreE, CheckedSet, Src, location, Val, true);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001760
Ted Kremenekef910042009-11-04 04:24:16 +00001761 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
1762 I!=E; ++I) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00001763
Ted Kremenekef910042009-11-04 04:24:16 +00001764 if (Pred != *I)
1765 state = GetState(*I);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001766
Ted Kremenekef910042009-11-04 04:24:16 +00001767 const GRState* newState = 0;
Ted Kremenek17d541d2009-02-13 01:45:31 +00001768
Ted Kremenekef910042009-11-04 04:24:16 +00001769 if (atDeclInit) {
1770 const VarRegion *VR =
1771 cast<VarRegion>(cast<loc::MemRegionVal>(location).getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001772
Ted Kremenekef910042009-11-04 04:24:16 +00001773 newState = state->bindDecl(VR, Val);
Ted Kremenekb006b822009-11-04 00:09:15 +00001774 }
1775 else {
Ted Kremenekef910042009-11-04 04:24:16 +00001776 if (location.isUnknown()) {
1777 // We know that the new state will be the same as the old state since
1778 // the location of the binding is "unknown". Consequently, there
1779 // is no reason to just create a new node.
1780 newState = state;
1781 }
1782 else {
1783 // We are binding to a value other than 'unknown'. Perform the binding
1784 // using the StoreManager.
1785 newState = state->bindLoc(cast<Loc>(location), Val);
1786 }
Ted Kremenekb006b822009-11-04 00:09:15 +00001787 }
Ted Kremenekef910042009-11-04 04:24:16 +00001788
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001789 // The next thing to do is check if the TransferFuncs object wants to
Ted Kremenekef910042009-11-04 04:24:16 +00001790 // update the state based on the new binding. If the GRTransferFunc object
1791 // doesn't do anything, just auto-propagate the current state.
Ted Kremenek07343c02010-09-02 00:56:20 +00001792
1793 // NOTE: We use 'AssignE' for the location of the PostStore if 'AssignE'
1794 // is non-NULL. Checkers typically care about
1795
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001796 StmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, newState, StoreE,
Ted Kremenek5f256da2010-09-09 07:13:00 +00001797 true);
Ted Kremenekef910042009-11-04 04:24:16 +00001798
Ted Kremenekdc891422010-12-01 21:57:22 +00001799 getTF().evalBind(BuilderRef, location, Val);
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001800 }
Ted Kremenek17d541d2009-02-13 01:45:31 +00001801}
1802
Ted Kremenekdc891422010-12-01 21:57:22 +00001803/// evalStore - Handle the semantics of a store via an assignment.
Ted Kremenek17d541d2009-02-13 01:45:31 +00001804/// @param Dst The node set to store generated state nodes
Zhongxing Xudcf7b352010-09-02 01:56:39 +00001805/// @param AssignE The assignment expression if the store happens in an
1806/// assignment.
1807/// @param LocatioinE The location expression that is stored to.
Ted Kremenek17d541d2009-02-13 01:45:31 +00001808/// @param state The current simulation state
1809/// @param location The location to store the value
1810/// @param Val The value to be stored
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001811void ExprEngine::evalStore(ExplodedNodeSet& Dst, const Expr *AssignE,
Ted Kremenek07343c02010-09-02 00:56:20 +00001812 const Expr* LocationE,
Ted Kremenek209e31b2009-11-05 00:42:23 +00001813 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001814 const GRState* state, SVal location, SVal Val,
1815 const void *tag) {
Mike Stump11289f42009-09-09 15:08:12 +00001816
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001817 assert(Builder && "StmtNodeBuilder must be defined.");
Mike Stump11289f42009-09-09 15:08:12 +00001818
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00001819 // Proceed with the store. We use AssignE as the anchor for the PostStore
1820 // ProgramPoint if it is non-NULL, and LocationE otherwise.
1821 const Expr *StoreE = AssignE ? AssignE : LocationE;
1822
1823 if (isa<loc::ObjCPropRef>(location)) {
1824 loc::ObjCPropRef prop = cast<loc::ObjCPropRef>(location);
1825 ExplodedNodeSet src = Pred;
1826 return VisitObjCMessage(ObjCPropertySetter(prop.getPropRefExpr(),
1827 StoreE, Val), src, Dst);
1828 }
1829
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001830 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001831 ExplodedNodeSet Tmp;
Ted Kremenekdc891422010-12-01 21:57:22 +00001832 evalLocation(Tmp, LocationE, Pred, state, location, tag, false);
Mike Stump11289f42009-09-09 15:08:12 +00001833
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001834 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001835 return;
Ted Kremenekc072b822008-04-18 20:35:30 +00001836
Argyrios Kyrtzidis4f7745a2011-02-28 01:27:57 +00001837 if (location.isUndef())
1838 return;
Ted Kremenek17d541d2009-02-13 01:45:31 +00001839
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001840 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind,
1841 ProgramPoint::PostStoreKind);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001842
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001843 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
Ted Kremenekdc891422010-12-01 21:57:22 +00001844 evalBind(Dst, StoreE, *NI, GetState(*NI), location, Val);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001845}
1846
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001847void ExprEngine::evalLoad(ExplodedNodeSet& Dst, const Expr *Ex,
Zhongxing Xuc2acbe02010-07-20 02:41:28 +00001848 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001849 const GRState* state, SVal location,
Zhongxing Xu731f4622009-11-16 04:49:44 +00001850 const void *tag, QualType LoadTy) {
Zhanyong Wan8e82c632010-11-24 01:47:11 +00001851 assert(!isa<NonLoc>(location) && "location cannot be a NonLoc.");
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001852
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00001853 if (isa<loc::ObjCPropRef>(location)) {
1854 loc::ObjCPropRef prop = cast<loc::ObjCPropRef>(location);
1855 ExplodedNodeSet src = Pred;
1856 return VisitObjCMessage(ObjCPropertyGetter(prop.getPropRefExpr(), Ex),
1857 src, Dst);
1858 }
1859
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001860 // Are we loading from a region? This actually results in two loads; one
1861 // to fetch the address of the referenced value and one to fetch the
1862 // referenced value.
Ted Kremenekd51217e2010-02-15 23:02:46 +00001863 if (const TypedRegion *TR =
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001864 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00001865
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001866 QualType ValTy = TR->getValueType();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001867 if (const ReferenceType *RT = ValTy->getAs<ReferenceType>()) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00001868 static int loadReferenceTag = 0;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001869 ExplodedNodeSet Tmp;
Ted Kremenekdc891422010-12-01 21:57:22 +00001870 evalLoadCommon(Tmp, Ex, Pred, state, location, &loadReferenceTag,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001871 getContext().getPointerType(RT->getPointeeType()));
1872
1873 // Perform the load from the referenced value.
1874 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end() ; I!=E; ++I) {
1875 state = GetState(*I);
Ted Kremenek57f09892010-02-08 16:18:51 +00001876 location = state->getSVal(Ex);
Ted Kremenekdc891422010-12-01 21:57:22 +00001877 evalLoadCommon(Dst, Ex, *I, state, location, tag, LoadTy);
Ted Kremenekd51217e2010-02-15 23:02:46 +00001878 }
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001879 return;
1880 }
1881 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001882
Ted Kremenekdc891422010-12-01 21:57:22 +00001883 evalLoadCommon(Dst, Ex, Pred, state, location, tag, LoadTy);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001884}
1885
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001886void ExprEngine::evalLoadCommon(ExplodedNodeSet& Dst, const Expr *Ex,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001887 ExplodedNode* Pred,
1888 const GRState* state, SVal location,
1889 const void *tag, QualType LoadTy) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00001890
Mike Stump11289f42009-09-09 15:08:12 +00001891 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001892 ExplodedNodeSet Tmp;
Ted Kremenekdc891422010-12-01 21:57:22 +00001893 evalLocation(Tmp, Ex, Pred, state, location, tag, true);
Mike Stump11289f42009-09-09 15:08:12 +00001894
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001895 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001896 return;
Ted Kremenekd51217e2010-02-15 23:02:46 +00001897
Argyrios Kyrtzidis4f7745a2011-02-28 01:27:57 +00001898 if (location.isUndef())
1899 return;
Ted Kremenekd51217e2010-02-15 23:02:46 +00001900
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001901 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Mike Stump11289f42009-09-09 15:08:12 +00001902
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001903 // Proceed with the load.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001904 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1905 state = GetState(*NI);
Ted Kremenek5f256da2010-09-09 07:13:00 +00001906
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001907 if (location.isUnknown()) {
1908 // This is important. We must nuke the old binding.
1909 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, UnknownVal()),
1910 ProgramPoint::PostLoadKind, tag);
1911 }
1912 else {
Ted Kremenek5f256da2010-09-09 07:13:00 +00001913 if (LoadTy.isNull())
1914 LoadTy = Ex->getType();
1915 SVal V = state->getSVal(cast<Loc>(location), LoadTy);
1916 MakeNode(Dst, Ex, *NI, state->bindExprAndLocation(Ex, location, V),
1917 ProgramPoint::PostLoadKind, tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001918 }
Zhongxing Xu33178a02008-11-28 08:34:30 +00001919 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001920}
1921
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001922void ExprEngine::evalLocation(ExplodedNodeSet &Dst, const Stmt *S,
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001923 ExplodedNode* Pred,
1924 const GRState* state, SVal location,
1925 const void *tag, bool isLoad) {
Zhongxing Xuab0ae212009-11-20 03:50:46 +00001926 // Early checks for performance reason.
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001927 if (location.isUnknown()) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001928 Dst.Add(Pred);
1929 return;
Ted Kremenekfac290d2009-11-02 23:19:29 +00001930 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001931
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001932 if (Checkers.empty()) {
Argyrios Kyrtzidis8f38c382011-02-24 08:42:04 +00001933 ExplodedNodeSet Src;
1934 if (Builder->GetState(Pred) == state) {
1935 Src.Add(Pred);
1936 } else {
1937 // Associate this new state with an ExplodedNode.
1938 Src.Add(Builder->generateNode(S, state, Pred));
1939 }
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001940 getCheckerManager().runCheckersForLocation(Dst, Src, location, isLoad, S,
Argyrios Kyrtzidis8f38c382011-02-24 08:42:04 +00001941 *this);
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001942 return;
1943 }
1944
Argyrios Kyrtzidis8f38c382011-02-24 08:42:04 +00001945 ExplodedNodeSet Src;
1946 Src.Add(Pred);
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001947 ExplodedNodeSet CheckersV1Dst;
1948 ExplodedNodeSet Tmp;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001949 ExplodedNodeSet *PrevSet = &Src;
Ted Kremenekd51217e2010-02-15 23:02:46 +00001950
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001951 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
1952 {
Ted Kremenek32c32892009-12-09 02:45:41 +00001953 ExplodedNodeSet *CurrSet = 0;
1954 if (I+1 == E)
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001955 CurrSet = &CheckersV1Dst;
Ted Kremenek32c32892009-12-09 02:45:41 +00001956 else {
1957 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
1958 CurrSet->clear();
1959 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001960
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001961 void *tag = I->first;
1962 Checker *checker = I->second;
Ted Kremenekd51217e2010-02-15 23:02:46 +00001963
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001964 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenekf5735152009-11-23 22:22:01 +00001965 NI != NE; ++NI) {
1966 // Use the 'state' argument only when the predecessor node is the
1967 // same as Pred. This allows us to catch updates to the state.
Ted Kremenek6fee7c22010-12-20 21:22:47 +00001968 checker->GR_visitLocation(*CurrSet, *Builder, *this, S, *NI,
Ted Kremenekf5735152009-11-23 22:22:01 +00001969 *NI == Pred ? state : GetState(*NI),
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001970 location, tag, isLoad);
Ted Kremenekf5735152009-11-23 22:22:01 +00001971 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00001972
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001973 // Update which NodeSet is the current one.
1974 PrevSet = CurrSet;
1975 }
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +00001976
1977 getCheckerManager().runCheckersForLocation(Dst, CheckersV1Dst, location,
Argyrios Kyrtzidis8f38c382011-02-24 08:42:04 +00001978 isLoad, S, *this);
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001979}
1980
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00001981bool ExprEngine::InlineCall(ExplodedNodeSet &Dst, const CallExpr *CE,
Zhongxing Xu1a56a482010-05-06 03:38:27 +00001982 ExplodedNode *Pred) {
1983 const GRState *state = GetState(Pred);
1984 const Expr *Callee = CE->getCallee();
1985 SVal L = state->getSVal(Callee);
1986
1987 const FunctionDecl *FD = L.getAsFunctionDecl();
1988 if (!FD)
1989 return false;
1990
Zhongxing Xu84f65e02010-07-19 01:31:21 +00001991 // Check if the function definition is in the same translation unit.
1992 if (FD->hasBody(FD)) {
Zhongxing Xucb298022010-11-24 08:53:20 +00001993 const StackFrameContext *stackFrame =
1994 AMgr.getStackFrame(AMgr.getAnalysisContext(FD),
1995 Pred->getLocationContext(),
Ted Kremenek8219b822010-12-16 07:46:53 +00001996 CE, Builder->getBlock(), Builder->getIndex());
Zhongxing Xu84f65e02010-07-19 01:31:21 +00001997 // Now we have the definition of the callee, create a CallEnter node.
Zhongxing Xucb298022010-11-24 08:53:20 +00001998 CallEnter Loc(CE, stackFrame, Pred->getLocationContext());
Zhongxing Xu1a56a482010-05-06 03:38:27 +00001999
Zhongxing Xu84f65e02010-07-19 01:31:21 +00002000 ExplodedNode *N = Builder->generateNode(Loc, state, Pred);
Zhongxing Xu1a56a482010-05-06 03:38:27 +00002001 Dst.Add(N);
Zhongxing Xu84f65e02010-07-19 01:31:21 +00002002 return true;
2003 }
2004
2005 // Check if we can find the function definition in other translation units.
2006 if (AMgr.hasIndexer()) {
Zhongxing Xucb298022010-11-24 08:53:20 +00002007 AnalysisContext *C = AMgr.getAnalysisContextInAnotherTU(FD);
Zhongxing Xu84f65e02010-07-19 01:31:21 +00002008 if (C == 0)
2009 return false;
Zhongxing Xucb298022010-11-24 08:53:20 +00002010 const StackFrameContext *stackFrame =
2011 AMgr.getStackFrame(C, Pred->getLocationContext(),
Ted Kremenek8219b822010-12-16 07:46:53 +00002012 CE, Builder->getBlock(), Builder->getIndex());
Zhongxing Xucb298022010-11-24 08:53:20 +00002013 CallEnter Loc(CE, stackFrame, Pred->getLocationContext());
Zhongxing Xu84f65e02010-07-19 01:31:21 +00002014 ExplodedNode *N = Builder->generateNode(Loc, state, Pred);
2015 Dst.Add(N);
2016 return true;
2017 }
2018
2019 return false;
Zhongxing Xu1a56a482010-05-06 03:38:27 +00002020}
2021
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002022void ExprEngine::VisitCall(const CallExpr* CE, ExplodedNode* Pred,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002023 CallExpr::const_arg_iterator AI,
2024 CallExpr::const_arg_iterator AE,
Ted Kremenek8219b822010-12-16 07:46:53 +00002025 ExplodedNodeSet& Dst) {
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002026
Douglas Gregor6b754842008-10-28 00:22:11 +00002027 // Determine the type of function we're calling (if available).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002028 const FunctionProtoType *Proto = NULL;
Douglas Gregor6b754842008-10-28 00:22:11 +00002029 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002030 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
John McCall9dd450b2009-09-21 23:43:11 +00002031 Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>();
Douglas Gregor6b754842008-10-28 00:22:11 +00002032
Ted Kremenekfd5856a2010-09-23 05:14:51 +00002033 // Evaluate the arguments.
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002034 ExplodedNodeSet ArgsEvaluated;
Ted Kremenekdc891422010-12-01 21:57:22 +00002035 evalArguments(CE->arg_begin(), CE->arg_end(), Proto, Pred, ArgsEvaluated);
Ted Kremeneke0188e62008-02-19 01:44:53 +00002036
Ted Kremenek48af0e02009-12-17 20:10:17 +00002037 // Now process the call itself.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002038 ExplodedNodeSet DstTmp;
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002039 const Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekd51217e2010-02-15 23:02:46 +00002040
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002041 for (ExplodedNodeSet::iterator NI=ArgsEvaluated.begin(),
Ted Kremenek48af0e02009-12-17 20:10:17 +00002042 NE=ArgsEvaluated.end(); NI != NE; ++NI) {
2043 // Evaluate the callee.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002044 ExplodedNodeSet DstTmp2;
Ted Kremenekd51217e2010-02-15 23:02:46 +00002045 Visit(Callee, *NI, DstTmp2);
Ted Kremenek49513cc2009-07-22 21:43:51 +00002046 // Perform the previsit of the CallExpr, storing the results in DstTmp.
Jordy Rosec36df4d2010-08-04 07:10:57 +00002047 CheckerVisit(CE, DstTmp, DstTmp2, PreVisitStmtCallback);
Ted Kremenek49513cc2009-07-22 21:43:51 +00002048 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00002049
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002050 // Finally, evaluate the function call. We try each of the checkers
2051 // to see if the can evaluate the function call.
Ted Kremenek32c32892009-12-09 02:45:41 +00002052 ExplodedNodeSet DstTmp3;
Ted Kremenekaf1bdd72009-12-18 20:13:39 +00002053
Mike Stump11289f42009-09-09 15:08:12 +00002054 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00002055 DI != DE; ++DI) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00002056
Ted Kremenek17d541d2009-02-13 01:45:31 +00002057 const GRState* state = GetState(*DI);
Ted Kremenek57f09892010-02-08 16:18:51 +00002058 SVal L = state->getSVal(Callee);
Ted Kremenekd51217e2010-02-15 23:02:46 +00002059
Ted Kremenek8efd6b4e2008-03-03 16:47:31 +00002060 // FIXME: Add support for symbolic function calls (calls involving
2061 // function pointer values that are symbolic).
Ted Kremenek70342362008-03-05 21:15:02 +00002062 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek32c32892009-12-09 02:45:41 +00002063 ExplodedNodeSet DstChecker;
Ted Kremenekd51217e2010-02-15 23:02:46 +00002064
Zhongxing Xu175447f2009-12-07 09:17:35 +00002065 // If the callee is processed by a checker, skip the rest logic.
2066 if (CheckerEvalCall(CE, DstChecker, *DI))
Zhongxing Xud1dee7e2009-12-09 05:48:53 +00002067 DstTmp3.insert(DstChecker);
Zhongxing Xu1a56a482010-05-06 03:38:27 +00002068 else if (AMgr.shouldInlineCall() && InlineCall(Dst, CE, *DI)) {
2069 // Callee is inlined. We shouldn't do post call checking.
2070 return;
2071 }
Zhongxing Xu175447f2009-12-07 09:17:35 +00002072 else {
Ted Kremenek32c32892009-12-09 02:45:41 +00002073 for (ExplodedNodeSet::iterator DI_Checker = DstChecker.begin(),
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002074 DE_Checker = DstChecker.end();
2075 DI_Checker != DE_Checker; ++DI_Checker) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00002076
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002077 // Dispatch to the plug-in transfer function.
Ted Kremenekdc891422010-12-01 21:57:22 +00002078 unsigned oldSize = DstTmp3.size();
Ted Kremenekf41bdd72011-01-13 04:36:46 +00002079 SaveOr OldHasGen(Builder->hasGeneratedNode);
Ted Kremenek32c32892009-12-09 02:45:41 +00002080 Pred = *DI_Checker;
Ted Kremenekd51217e2010-02-15 23:02:46 +00002081
Ted Kremenek32c32892009-12-09 02:45:41 +00002082 // Dispatch to transfer function logic to handle the call itself.
2083 // FIXME: Allow us to chain together transfer functions.
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002084 assert(Builder && "StmtNodeBuilder must be defined.");
Ted Kremenekdc891422010-12-01 21:57:22 +00002085 getTF().evalCall(DstTmp3, *this, *Builder, CE, L, Pred);
Ted Kremenekd51217e2010-02-15 23:02:46 +00002086
Ted Kremenek32c32892009-12-09 02:45:41 +00002087 // Handle the case where no nodes where generated. Auto-generate that
2088 // contains the updated state if we aren't generating sinks.
Ted Kremenekdc891422010-12-01 21:57:22 +00002089 if (!Builder->BuildSinks && DstTmp3.size() == oldSize &&
Ted Kremenekf41bdd72011-01-13 04:36:46 +00002090 !Builder->hasGeneratedNode)
Ted Kremenek32c32892009-12-09 02:45:41 +00002091 MakeNode(DstTmp3, CE, Pred, state);
2092 }
Zhongxing Xu175447f2009-12-07 09:17:35 +00002093 }
Ted Kremeneke0188e62008-02-19 01:44:53 +00002094 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00002095
Ted Kremenek94cc33f2009-12-17 20:06:29 +00002096 // Finally, perform the post-condition check of the CallExpr and store
2097 // the created nodes in 'Dst'.
Ted Kremenek8219b822010-12-16 07:46:53 +00002098 CheckerVisit(CE, Dst, DstTmp3, PostVisitStmtCallback);
Ted Kremeneke0188e62008-02-19 01:44:53 +00002099}
2100
Ted Kremenek667cacb2008-04-15 23:06:53 +00002101//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00002102// Transfer function: Objective-C dot-syntax to access a property.
2103//===----------------------------------------------------------------------===//
2104
2105void ExprEngine::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Ex,
2106 ExplodedNode *Pred,
2107 ExplodedNodeSet &Dst) {
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00002108 ExplodedNodeSet dstBase;
Argyrios Kyrtzidisadd754a2011-01-27 16:17:11 +00002109
2110 // Visit the receiver (if any).
2111 if (Ex->isObjectReceiver())
2112 Visit(Ex->getBase(), Pred, dstBase);
2113 else
2114 dstBase = Pred;
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00002115
2116 ExplodedNodeSet dstPropRef;
2117
2118 // Using the base, compute the lvalue of the instance variable.
2119 for (ExplodedNodeSet::iterator I = dstBase.begin(), E = dstBase.end();
2120 I!=E; ++I) {
2121 ExplodedNode *nodeBase = *I;
2122 const GRState *state = GetState(nodeBase);
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00002123 MakeNode(dstPropRef, Ex, *I, state->BindExpr(Ex, loc::ObjCPropRef(Ex)));
2124 }
2125
2126 Dst.insert(dstPropRef);
2127}
2128
2129//===----------------------------------------------------------------------===//
Ted Kremenek12dd55b2008-10-17 00:03:18 +00002130// Transfer function: Objective-C ivar references.
2131//===----------------------------------------------------------------------===//
2132
Ted Kremenek111a6bd2009-02-28 20:50:43 +00002133static std::pair<const void*,const void*> EagerlyAssumeTag
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00002134 = std::pair<const void*,const void*>(&EagerlyAssumeTag,static_cast<void*>(0));
Ted Kremenek111a6bd2009-02-28 20:50:43 +00002135
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002136void ExprEngine::evalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002137 const Expr *Ex) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00002138 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
2139 ExplodedNode *Pred = *I;
Mike Stump11289f42009-09-09 15:08:12 +00002140
Ted Kremenekff290ca2009-02-25 23:32:10 +00002141 // Test if the previous node was as the same expression. This can happen
2142 // when the expression fails to evaluate to anything meaningful and
2143 // (as an optimization) we don't generate a node.
Mike Stump11289f42009-09-09 15:08:12 +00002144 ProgramPoint P = Pred->getLocation();
Ted Kremenekff290ca2009-02-25 23:32:10 +00002145 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump11289f42009-09-09 15:08:12 +00002146 Dst.Add(Pred);
Ted Kremenekff290ca2009-02-25 23:32:10 +00002147 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002148 }
Ted Kremenekff290ca2009-02-25 23:32:10 +00002149
Zhongxing Xu4d4b8d82010-04-20 04:53:09 +00002150 const GRState* state = GetState(Pred);
Ted Kremenek57f09892010-02-08 16:18:51 +00002151 SVal V = state->getSVal(Ex);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002152 if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) {
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00002153 // First assume that the condition is true.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00002154 if (const GRState *stateTrue = state->assume(*SEV, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002155 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek90af9092010-12-02 07:49:45 +00002156 svalBuilder.makeIntVal(1U, Ex->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002157 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xue1190f72009-08-15 03:17:38 +00002158 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00002159 stateTrue, Pred));
2160 }
Mike Stump11289f42009-09-09 15:08:12 +00002161
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00002162 // Next, assume that the condition is false.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00002163 if (const GRState *stateFalse = state->assume(*SEV, false)) {
Mike Stump11289f42009-09-09 15:08:12 +00002164 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek90af9092010-12-02 07:49:45 +00002165 svalBuilder.makeIntVal(0U, Ex->getType()));
Zhongxing Xue1190f72009-08-15 03:17:38 +00002166 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
2167 Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00002168 stateFalse, Pred));
2169 }
2170 }
2171 else
2172 Dst.Add(Pred);
2173 }
2174}
2175
2176//===----------------------------------------------------------------------===//
Ted Kremeneked12f1b2010-09-10 03:05:33 +00002177// Transfer function: Objective-C @synchronized.
2178//===----------------------------------------------------------------------===//
2179
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002180void ExprEngine::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S,
Ted Kremeneked12f1b2010-09-10 03:05:33 +00002181 ExplodedNode *Pred,
2182 ExplodedNodeSet &Dst) {
2183
2184 // The mutex expression is a CFGElement, so we don't need to explicitly
2185 // visit it since it will already be processed.
2186
2187 // Pre-visit the ObjCAtSynchronizedStmt.
2188 ExplodedNodeSet Tmp;
2189 Tmp.Add(Pred);
2190 CheckerVisit(S, Dst, Tmp, PreVisitStmtCallback);
2191}
2192
2193//===----------------------------------------------------------------------===//
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00002194// Transfer function: Objective-C ivar references.
2195//===----------------------------------------------------------------------===//
2196
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002197void ExprEngine::VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr* Ex,
2198 ExplodedNode* Pred,
2199 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00002200
Ted Kremenek8219b822010-12-16 07:46:53 +00002201 // Visit the base expression, which is needed for computing the lvalue
2202 // of the ivar.
2203 ExplodedNodeSet dstBase;
2204 const Expr *baseExpr = Ex->getBase();
2205 Visit(baseExpr, Pred, dstBase);
Mike Stump11289f42009-09-09 15:08:12 +00002206
Argyrios Kyrtzidis9c23e6c2011-01-11 19:45:20 +00002207 ExplodedNodeSet dstIvar;
2208
Ted Kremenek8219b822010-12-16 07:46:53 +00002209 // Using the base, compute the lvalue of the instance variable.
2210 for (ExplodedNodeSet::iterator I = dstBase.begin(), E = dstBase.end();
2211 I!=E; ++I) {
2212 ExplodedNode *nodeBase = *I;
2213 const GRState *state = GetState(nodeBase);
2214 SVal baseVal = state->getSVal(baseExpr);
2215 SVal location = state->getLValue(Ex->getDecl(), baseVal);
Argyrios Kyrtzidis9c23e6c2011-01-11 19:45:20 +00002216 MakeNode(dstIvar, Ex, *I, state->BindExpr(Ex, location));
Ted Kremenek12dd55b2008-10-17 00:03:18 +00002217 }
Argyrios Kyrtzidis9c23e6c2011-01-11 19:45:20 +00002218
2219 // Perform the post-condition check of the ObjCIvarRefExpr and store
2220 // the created nodes in 'Dst'.
2221 CheckerVisit(Ex, Dst, dstIvar, PostVisitStmtCallback);
Ted Kremenek12dd55b2008-10-17 00:03:18 +00002222}
2223
2224//===----------------------------------------------------------------------===//
Ted Kremenek17810802008-11-12 19:24:17 +00002225// Transfer function: Objective-C fast enumeration 'for' statements.
2226//===----------------------------------------------------------------------===//
2227
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002228void ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002229 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00002230
Ted Kremenek17810802008-11-12 19:24:17 +00002231 // ObjCForCollectionStmts are processed in two places. This method
2232 // handles the case where an ObjCForCollectionStmt* occurs as one of the
2233 // statements within a basic block. This transfer function does two things:
2234 //
2235 // (1) binds the next container value to 'element'. This creates a new
2236 // node in the ExplodedGraph.
2237 //
2238 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
2239 // whether or not the container has any more elements. This value
2240 // will be tested in ProcessBranch. We need to explicitly bind
2241 // this value because a container can contain nil elements.
Mike Stump11289f42009-09-09 15:08:12 +00002242 //
Ted Kremenek17810802008-11-12 19:24:17 +00002243 // FIXME: Eventually this logic should actually do dispatches to
2244 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
2245 // This will require simulating a temporary NSFastEnumerationState, either
2246 // through an SVal or through the use of MemRegions. This value can
2247 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
2248 // terminates we reclaim the temporary (it goes out of scope) and we
2249 // we can test if the SVal is 0 or if the MemRegion is null (depending
2250 // on what approach we take).
2251 //
2252 // For now: simulate (1) by assigning either a symbol or nil if the
2253 // container is empty. Thus this transfer function will by default
2254 // result in state splitting.
Mike Stump11289f42009-09-09 15:08:12 +00002255
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002256 const Stmt* elem = S->getElement();
Ted Kremenek537f6382008-11-14 19:47:18 +00002257 SVal ElementV;
Mike Stump11289f42009-09-09 15:08:12 +00002258
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002259 if (const DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
2260 const VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek17810802008-11-12 19:24:17 +00002261 assert (ElemD->getInit() == 0);
Ted Kremenek14536f62009-08-21 22:28:32 +00002262 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek537f6382008-11-14 19:47:18 +00002263 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
2264 return;
Ted Kremenek17810802008-11-12 19:24:17 +00002265 }
Ted Kremenek537f6382008-11-14 19:47:18 +00002266
Zhongxing Xu107f7592009-08-06 12:48:26 +00002267 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +00002268 Visit(cast<Expr>(elem), Pred, Tmp);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002269 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek537f6382008-11-14 19:47:18 +00002270 const GRState* state = GetState(*I);
Ted Kremenek57f09892010-02-08 16:18:51 +00002271 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek537f6382008-11-14 19:47:18 +00002272 }
2273}
2274
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002275void ExprEngine::VisitObjCForCollectionStmtAux(const ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002276 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek537f6382008-11-14 19:47:18 +00002277 SVal ElementV) {
Ted Kremenek537f6382008-11-14 19:47:18 +00002278
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002279 // Check if the location we are writing back to is a null pointer.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002280 const Stmt* elem = S->getElement();
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002281 ExplodedNodeSet Tmp;
Ted Kremenekdc891422010-12-01 21:57:22 +00002282 evalLocation(Tmp, elem, Pred, GetState(Pred), ElementV, NULL, false);
Ted Kremenekd51217e2010-02-15 23:02:46 +00002283
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002284 if (Tmp.empty())
Ted Kremenek537f6382008-11-14 19:47:18 +00002285 return;
Ted Kremenekd51217e2010-02-15 23:02:46 +00002286
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002287 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2288 Pred = *NI;
2289 const GRState *state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002290
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002291 // Handle the case where the container still has elements.
Ted Kremenek90af9092010-12-02 07:49:45 +00002292 SVal TrueV = svalBuilder.makeTruthVal(1);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002293 const GRState *hasElems = state->BindExpr(S, TrueV);
Ted Kremenek537f6382008-11-14 19:47:18 +00002294
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002295 // Handle the case where the container has no elements.
Ted Kremenek90af9092010-12-02 07:49:45 +00002296 SVal FalseV = svalBuilder.makeTruthVal(0);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002297 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump11289f42009-09-09 15:08:12 +00002298
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002299 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
2300 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
2301 // FIXME: The proper thing to do is to really iterate over the
2302 // container. We will do this with dispatch logic to the store.
2303 // For now, just 'conjure' up a symbolic value.
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00002304 QualType T = R->getValueType();
Zhanyong Wan85a203e2011-02-16 21:13:32 +00002305 assert(Loc::isLocType(T));
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002306 unsigned Count = Builder->getCurrentBlockCount();
2307 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
Ted Kremenek90af9092010-12-02 07:49:45 +00002308 SVal V = svalBuilder.makeLoc(Sym);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002309 hasElems = hasElems->bindLoc(ElementV, V);
Mike Stump11289f42009-09-09 15:08:12 +00002310
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002311 // Bind the location to 'nil' on the false branch.
Ted Kremenek90af9092010-12-02 07:49:45 +00002312 SVal nilV = svalBuilder.makeIntVal(0, T);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002313 noElems = noElems->bindLoc(ElementV, nilV);
2314 }
Ted Kremenekdf317922008-11-12 21:12:46 +00002315
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00002316 // Create the new nodes.
2317 MakeNode(Dst, S, Pred, hasElems);
2318 MakeNode(Dst, S, Pred, noElems);
2319 }
Ted Kremenek17810802008-11-12 19:24:17 +00002320}
2321
2322//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00002323// Transfer function: Objective-C message expressions.
2324//===----------------------------------------------------------------------===//
2325
Ted Kremenekd51217e2010-02-15 23:02:46 +00002326namespace {
2327class ObjCMsgWLItem {
2328public:
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002329 ObjCMessageExpr::const_arg_iterator I;
Ted Kremenekd51217e2010-02-15 23:02:46 +00002330 ExplodedNode *N;
2331
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002332 ObjCMsgWLItem(const ObjCMessageExpr::const_arg_iterator &i, ExplodedNode *n)
Ted Kremenekd51217e2010-02-15 23:02:46 +00002333 : I(i), N(n) {}
2334};
2335} // end anonymous namespace
2336
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002337void ExprEngine::VisitObjCMessageExpr(const ObjCMessageExpr* ME,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002338 ExplodedNode* Pred,
Ted Kremenek8219b822010-12-16 07:46:53 +00002339 ExplodedNodeSet& Dst){
Mike Stump11289f42009-09-09 15:08:12 +00002340
Ted Kremenekd51217e2010-02-15 23:02:46 +00002341 // Create a worklist to process both the arguments.
2342 llvm::SmallVector<ObjCMsgWLItem, 20> WL;
Ted Kremenek667cacb2008-04-15 23:06:53 +00002343
Ted Kremenekd51217e2010-02-15 23:02:46 +00002344 // But first evaluate the receiver (if any).
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002345 ObjCMessageExpr::const_arg_iterator AI = ME->arg_begin(), AE = ME->arg_end();
2346 if (const Expr *Receiver = ME->getInstanceReceiver()) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00002347 ExplodedNodeSet Tmp;
2348 Visit(Receiver, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002349
Ted Kremenekd51217e2010-02-15 23:02:46 +00002350 if (Tmp.empty())
Ted Kremenek667cacb2008-04-15 23:06:53 +00002351 return;
Ted Kremenekd51217e2010-02-15 23:02:46 +00002352
2353 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I)
2354 WL.push_back(ObjCMsgWLItem(AI, *I));
2355 }
2356 else
2357 WL.push_back(ObjCMsgWLItem(AI, Pred));
2358
2359 // Evaluate the arguments.
2360 ExplodedNodeSet ArgsEvaluated;
2361 while (!WL.empty()) {
2362 ObjCMsgWLItem Item = WL.back();
2363 WL.pop_back();
2364
2365 if (Item.I == AE) {
2366 ArgsEvaluated.insert(Item.N);
2367 continue;
Ted Kremenek667cacb2008-04-15 23:06:53 +00002368 }
Mike Stump11289f42009-09-09 15:08:12 +00002369
Ted Kremenekd51217e2010-02-15 23:02:46 +00002370 // Evaluate the subexpression.
2371 ExplodedNodeSet Tmp;
2372
2373 // FIXME: [Objective-C++] handle arguments that are references
2374 Visit(*Item.I, Item.N, Tmp);
2375
2376 // Enqueue evaluating the next argument on the worklist.
2377 ++(Item.I);
2378 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
2379 WL.push_back(ObjCMsgWLItem(Item.I, *NI));
Ted Kremenek667cacb2008-04-15 23:06:53 +00002380 }
Mike Stump11289f42009-09-09 15:08:12 +00002381
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002382 // Now that the arguments are processed, handle the ObjC message.
2383 VisitObjCMessage(ME, ArgsEvaluated, Dst);
2384}
2385
2386void ExprEngine::VisitObjCMessage(const ObjCMessage &msg,
2387 ExplodedNodeSet &Src, ExplodedNodeSet& Dst) {
2388
2389 // Handle the previsits checks.
Ted Kremenekd51217e2010-02-15 23:02:46 +00002390 ExplodedNodeSet DstPrevisit;
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002391 CheckerVisitObjCMessage(msg, DstPrevisit, Src, /*isPreVisit=*/true);
Mike Stump11289f42009-09-09 15:08:12 +00002392
Ted Kremenekd51217e2010-02-15 23:02:46 +00002393 // Proceed with evaluate the message expression.
Ted Kremenekdc891422010-12-01 21:57:22 +00002394 ExplodedNodeSet dstEval;
Mike Stump11289f42009-09-09 15:08:12 +00002395
Ted Kremenekd51217e2010-02-15 23:02:46 +00002396 for (ExplodedNodeSet::iterator DI = DstPrevisit.begin(),
2397 DE = DstPrevisit.end(); DI != DE; ++DI) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00002398
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002399 ExplodedNode *Pred = *DI;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002400 bool RaisesException = false;
Ted Kremenekdc891422010-12-01 21:57:22 +00002401 unsigned oldSize = dstEval.size();
Ted Kremeneke19711d2009-12-22 22:13:46 +00002402 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekf41bdd72011-01-13 04:36:46 +00002403 SaveOr OldHasGen(Builder->hasGeneratedNode);
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002404
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002405 if (const Expr *Receiver = msg.getInstanceReceiver()) {
Zhongxing Xu4d4b8d82010-04-20 04:53:09 +00002406 const GRState *state = GetState(Pred);
Argyrios Kyrtzidis64fe4562011-02-28 01:28:08 +00002407 SVal recVal = state->getSVal(Receiver);
2408 if (!recVal.isUndef()) {
2409 // Bifurcate the state into nil and non-nil ones.
2410 DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal);
2411
2412 const GRState *notNilState, *nilState;
2413 llvm::tie(notNilState, nilState) = state->assume(receiverVal);
2414
2415 // There are three cases: can be nil or non-nil, must be nil, must be
2416 // non-nil. We handle must be nil, and merge the rest two into non-nil.
2417 if (nilState && !notNilState) {
2418 CheckerEvalNilReceiver(msg, dstEval, nilState, Pred);
2419 continue;
2420 }
2421
2422 // Check if the "raise" message was sent.
2423 assert(notNilState);
2424 if (msg.getSelector() == RaiseSel)
2425 RaisesException = true;
2426
2427 // Check if we raise an exception. For now treat these as sinks.
2428 // Eventually we will want to handle exceptions properly.
2429 if (RaisesException)
2430 Builder->BuildSinks = true;
2431
2432 // Dispatch to plug-in transfer function.
2433 evalObjCMessage(dstEval, msg, Pred, notNilState);
Zhongxing Xuaf353292009-12-02 05:49:12 +00002434 }
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002435 }
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002436 else if (const ObjCInterfaceDecl *Iface = msg.getReceiverInterface()) {
Douglas Gregor9a129192010-04-21 00:45:42 +00002437 IdentifierInfo* ClsName = Iface->getIdentifier();
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002438 Selector S = msg.getSelector();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002439
2440 // Check for special instance methods.
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002441 if (!NSExceptionII) {
2442 ASTContext& Ctx = getContext();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002443 NSExceptionII = &Ctx.Idents.get("NSException");
2444 }
2445
2446 if (ClsName == NSExceptionII) {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002447 enum { NUM_RAISE_SELECTORS = 2 };
2448
2449 // Lazily create a cache of the selectors.
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002450 if (!NSExceptionInstanceRaiseSelectors) {
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002451 ASTContext& Ctx = getContext();
Ted Kremenekd51217e2010-02-15 23:02:46 +00002452 NSExceptionInstanceRaiseSelectors =
2453 new Selector[NUM_RAISE_SELECTORS];
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002454 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
2455 unsigned idx = 0;
2456
2457 // raise:format:
2458 II.push_back(&Ctx.Idents.get("raise"));
2459 II.push_back(&Ctx.Idents.get("format"));
2460 NSExceptionInstanceRaiseSelectors[idx++] =
2461 Ctx.Selectors.getSelector(II.size(), &II[0]);
2462
2463 // raise:format::arguments:
2464 II.push_back(&Ctx.Idents.get("arguments"));
2465 NSExceptionInstanceRaiseSelectors[idx++] =
2466 Ctx.Selectors.getSelector(II.size(), &II[0]);
2467 }
2468
2469 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2470 if (S == NSExceptionInstanceRaiseSelectors[i]) {
Ted Kremeneke19711d2009-12-22 22:13:46 +00002471 RaisesException = true;
2472 break;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002473 }
2474 }
Zhongxing Xuaf353292009-12-02 05:49:12 +00002475
2476 // Check if we raise an exception. For now treat these as sinks.
2477 // Eventually we will want to handle exceptions properly.
Zhongxing Xuaf353292009-12-02 05:49:12 +00002478 if (RaisesException)
2479 Builder->BuildSinks = true;
2480
2481 // Dispatch to plug-in transfer function.
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002482 evalObjCMessage(dstEval, msg, Pred, Builder->GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002483 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00002484
Ted Kremeneke19711d2009-12-22 22:13:46 +00002485 // Handle the case where no nodes where generated. Auto-generate that
2486 // contains the updated state if we aren't generating sinks.
Ted Kremenekdc891422010-12-01 21:57:22 +00002487 if (!Builder->BuildSinks && dstEval.size() == oldSize &&
Ted Kremenekf41bdd72011-01-13 04:36:46 +00002488 !Builder->hasGeneratedNode)
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002489 MakeNode(dstEval, msg.getOriginExpr(), Pred, GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002490 }
Mike Stump11289f42009-09-09 15:08:12 +00002491
Ted Kremeneke19711d2009-12-22 22:13:46 +00002492 // Finally, perform the post-condition check of the ObjCMessageExpr and store
2493 // the created nodes in 'Dst'.
Argyrios Kyrtzidisfdbcd542011-01-25 00:03:57 +00002494 CheckerVisitObjCMessage(msg, Dst, dstEval, /*isPreVisit=*/false);
Ted Kremenek667cacb2008-04-15 23:06:53 +00002495}
2496
2497//===----------------------------------------------------------------------===//
2498// Transfer functions: Miscellaneous statements.
2499//===----------------------------------------------------------------------===//
2500
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002501void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
2502 ExplodedNode *Pred, ExplodedNodeSet &Dst) {
Ted Kremenek8219b822010-12-16 07:46:53 +00002503
Zhongxing Xu107f7592009-08-06 12:48:26 +00002504 ExplodedNodeSet S1;
Ted Kremenek8219b822010-12-16 07:46:53 +00002505 Visit(Ex, Pred, S1);
2506 ExplodedNodeSet S2;
2507 CheckerVisit(CastE, S2, S1, PreVisitStmtCallback);
2508
Argyrios Kyrtzidisffb08c42011-01-25 00:04:03 +00002509 if (CastE->getCastKind() == CK_LValueToRValue ||
2510 CastE->getCastKind() == CK_GetObjCProperty) {
Ted Kremenek8219b822010-12-16 07:46:53 +00002511 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I!=E; ++I) {
2512 ExplodedNode *subExprNode = *I;
2513 const GRState *state = GetState(subExprNode);
2514 evalLoad(Dst, CastE, subExprNode, state, state->getSVal(Ex));
2515 }
2516 return;
2517 }
2518
2519 // All other casts.
Ted Kremenek9fd25312008-02-19 18:52:54 +00002520 QualType T = CastE->getType();
Zhongxing Xudab76fd2008-10-21 06:54:23 +00002521 QualType ExTy = Ex->getType();
Zhongxing Xuc2721522008-10-22 08:02:16 +00002522
Zhongxing Xu4de1c852008-10-31 07:26:14 +00002523 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregore200adc2008-10-27 19:41:14 +00002524 T = ExCast->getTypeAsWritten();
Ted Kremenek8219b822010-12-16 07:46:53 +00002525
2526#if 0
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002527 // If we are evaluating the cast in an lvalue context, we implicitly want
2528 // the cast to evaluate to a location.
2529 if (asLValue) {
2530 ASTContext &Ctx = getContext();
2531 T = Ctx.getPointerType(Ctx.getCanonicalType(T));
Ted Kremenek343b5122009-12-23 01:19:20 +00002532 ExTy = Ctx.getPointerType(Ctx.getCanonicalType(ExTy));
Ted Kremenek22cc1a82009-12-23 00:26:16 +00002533 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002534#endif
Ted Kremenekac7c7242009-07-21 21:03:30 +00002535
Zhongxing Xua1293a62010-01-22 04:30:00 +00002536 switch (CastE->getCastKind()) {
John McCalle3027922010-08-25 11:45:40 +00002537 case CK_ToVoid:
Zhongxing Xua1293a62010-01-22 04:30:00 +00002538 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I)
2539 Dst.Add(*I);
2540 return;
2541
John McCallf3735e02010-12-01 04:43:34 +00002542 case CK_LValueToRValue:
John McCalle3027922010-08-25 11:45:40 +00002543 case CK_NoOp:
2544 case CK_FunctionToPointerDecay:
Zhongxing Xua1293a62010-01-22 04:30:00 +00002545 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2546 // Copy the SVal of Ex to CastE.
2547 ExplodedNode *N = *I;
2548 const GRState *state = GetState(N);
Ted Kremenek57f09892010-02-08 16:18:51 +00002549 SVal V = state->getSVal(Ex);
Zhongxing Xua1293a62010-01-22 04:30:00 +00002550 state = state->BindExpr(CastE, V);
2551 MakeNode(Dst, CastE, N, state);
2552 }
2553 return;
2554
John McCall34376a62010-12-04 03:47:34 +00002555 case CK_GetObjCProperty:
John McCall8cb679e2010-11-15 09:13:47 +00002556 case CK_Dependent:
John McCalle3027922010-08-25 11:45:40 +00002557 case CK_ArrayToPointerDecay:
2558 case CK_BitCast:
2559 case CK_LValueBitCast:
2560 case CK_IntegralCast:
John McCalle84af4e2010-11-13 01:35:44 +00002561 case CK_NullToPointer:
John McCalle3027922010-08-25 11:45:40 +00002562 case CK_IntegralToPointer:
2563 case CK_PointerToIntegral:
John McCall8cb679e2010-11-15 09:13:47 +00002564 case CK_PointerToBoolean:
2565 case CK_IntegralToBoolean:
John McCalle3027922010-08-25 11:45:40 +00002566 case CK_IntegralToFloating:
2567 case CK_FloatingToIntegral:
John McCall8cb679e2010-11-15 09:13:47 +00002568 case CK_FloatingToBoolean:
John McCalle3027922010-08-25 11:45:40 +00002569 case CK_FloatingCast:
John McCallc5e62b42010-11-13 09:02:35 +00002570 case CK_FloatingRealToComplex:
John McCalld7646252010-11-14 08:17:51 +00002571 case CK_FloatingComplexToReal:
2572 case CK_FloatingComplexToBoolean:
John McCallc5e62b42010-11-13 09:02:35 +00002573 case CK_FloatingComplexCast:
John McCalld7646252010-11-14 08:17:51 +00002574 case CK_FloatingComplexToIntegralComplex:
John McCallc5e62b42010-11-13 09:02:35 +00002575 case CK_IntegralRealToComplex:
John McCalld7646252010-11-14 08:17:51 +00002576 case CK_IntegralComplexToReal:
2577 case CK_IntegralComplexToBoolean:
John McCallc5e62b42010-11-13 09:02:35 +00002578 case CK_IntegralComplexCast:
John McCalld7646252010-11-14 08:17:51 +00002579 case CK_IntegralComplexToFloatingComplex:
John McCalle3027922010-08-25 11:45:40 +00002580 case CK_AnyPointerToObjCPointerCast:
2581 case CK_AnyPointerToBlockPointerCast:
Zhongxing Xuec0b8e32010-11-26 08:21:53 +00002582
John McCalle3027922010-08-25 11:45:40 +00002583 case CK_ObjCObjectLValueCast: {
Ted Kremenek9d0bb1e2010-12-01 21:28:31 +00002584 // Delegate to SValBuilder to process.
Zhongxing Xua1293a62010-01-22 04:30:00 +00002585 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2586 ExplodedNode* N = *I;
2587 const GRState* state = GetState(N);
Ted Kremenek57f09892010-02-08 16:18:51 +00002588 SVal V = state->getSVal(Ex);
Ted Kremenekdc891422010-12-01 21:57:22 +00002589 V = svalBuilder.evalCast(V, T, ExTy);
Zhongxing Xu319deb82010-02-04 04:56:43 +00002590 state = state->BindExpr(CastE, V);
Zhongxing Xua1293a62010-01-22 04:30:00 +00002591 MakeNode(Dst, CastE, N, state);
2592 }
2593 return;
Ted Kremenek55081f92010-06-22 19:05:10 +00002594 }
Zhongxing Xuec0b8e32010-11-26 08:21:53 +00002595
2596 case CK_DerivedToBase:
2597 case CK_UncheckedDerivedToBase:
2598 // For DerivedToBase cast, delegate to the store manager.
2599 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2600 ExplodedNode *node = *I;
2601 const GRState *state = GetState(node);
2602 SVal val = state->getSVal(Ex);
2603 val = getStoreManager().evalDerivedToBase(val, T);
2604 state = state->BindExpr(CastE, val);
2605 MakeNode(Dst, CastE, node, state);
2606 }
2607 return;
2608
Ted Kremenek55081f92010-06-22 19:05:10 +00002609 // Various C++ casts that are not handled yet.
John McCalle3027922010-08-25 11:45:40 +00002610 case CK_Dynamic:
2611 case CK_ToUnion:
2612 case CK_BaseToDerived:
2613 case CK_NullToMemberPointer:
2614 case CK_BaseToDerivedMemberPointer:
2615 case CK_DerivedToBaseMemberPointer:
2616 case CK_UserDefinedConversion:
2617 case CK_ConstructorConversion:
2618 case CK_VectorSplat:
2619 case CK_MemberPointerToBoolean: {
Ted Kremenek55081f92010-06-22 19:05:10 +00002620 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2621 Builder->BuildSinks = true;
2622 MakeNode(Dst, CastE, Pred, GetState(Pred));
2623 return;
2624 }
Ted Kremenek33d82852008-01-24 02:02:54 +00002625 }
Ted Kremenek05352742008-01-24 20:55:43 +00002626}
2627
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002628void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr* CL,
Mike Stump11289f42009-09-09 15:08:12 +00002629 ExplodedNode* Pred,
Ted Kremenek8219b822010-12-16 07:46:53 +00002630 ExplodedNodeSet& Dst) {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002631 const InitListExpr* ILE
2632 = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002633 ExplodedNodeSet Tmp;
Ted Kremenekbf263682008-10-27 21:54:31 +00002634 Visit(ILE, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002635
Zhongxing Xu107f7592009-08-06 12:48:26 +00002636 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002637 const GRState* state = GetState(*I);
Ted Kremenek57f09892010-02-08 16:18:51 +00002638 SVal ILV = state->getSVal(ILE);
Ted Kremenek04af9f22009-12-07 22:05:27 +00002639 const LocationContext *LC = (*I)->getLocationContext();
2640 state = state->bindCompoundLiteral(CL, LC, ILV);
Ted Kremenekbf263682008-10-27 21:54:31 +00002641
Ted Kremenek8219b822010-12-16 07:46:53 +00002642 if (CL->isLValue()) {
Ted Kremenek04af9f22009-12-07 22:05:27 +00002643 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL, LC)));
2644 }
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002645 else
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002646 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenekbf263682008-10-27 21:54:31 +00002647 }
2648}
2649
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002650void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +00002651 ExplodedNodeSet& Dst) {
Ted Kremenek3b427152008-04-22 22:25:27 +00002652
Mike Stump11289f42009-09-09 15:08:12 +00002653 // The CFG has one DeclStmt per Decl.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002654 const Decl* D = *DS->decl_begin();
Mike Stump11289f42009-09-09 15:08:12 +00002655
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002656 if (!D || !isa<VarDecl>(D))
Ted Kremenek3b427152008-04-22 22:25:27 +00002657 return;
Mike Stump11289f42009-09-09 15:08:12 +00002658
2659 const VarDecl* VD = dyn_cast<VarDecl>(D);
Zhongxing Xu692ac462010-07-23 02:54:53 +00002660 const Expr* InitEx = VD->getInit();
Ted Kremenek3b427152008-04-22 22:25:27 +00002661
2662 // FIXME: static variables may have an initializer, but the second
2663 // time a function is called those values may not be current.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002664 ExplodedNodeSet Tmp;
Ted Kremenek3b427152008-04-22 22:25:27 +00002665
Zhongxing Xu7e2a9fd2010-12-19 02:26:37 +00002666 if (InitEx) {
2667 if (VD->getType()->isReferenceType() && !InitEx->isLValue()) {
Zhongxing Xu70892502010-12-22 07:20:27 +00002668 // If the initializer is C++ record type, it should already has a
2669 // temp object.
2670 if (!InitEx->getType()->isRecordType())
2671 CreateCXXTemporaryObject(InitEx, Pred, Tmp);
2672 else
2673 Tmp.Add(Pred);
Zhongxing Xu7e2a9fd2010-12-19 02:26:37 +00002674 } else
2675 Visit(InitEx, Pred, Tmp);
2676 } else
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002677 Tmp.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002678
Ted Kremenekae3361d2009-11-07 03:56:57 +00002679 ExplodedNodeSet Tmp2;
Jordy Rosec36df4d2010-08-04 07:10:57 +00002680 CheckerVisit(DS, Tmp2, Tmp, PreVisitStmtCallback);
Ted Kremenekd51217e2010-02-15 23:02:46 +00002681
Ted Kremenekae3361d2009-11-07 03:56:57 +00002682 for (ExplodedNodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002683 ExplodedNode *N = *I;
Ted Kremenekae3361d2009-11-07 03:56:57 +00002684 const GRState *state = GetState(N);
Zhongxing Xu27fee832009-11-03 12:13:38 +00002685
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00002686 // Decls without InitExpr are not initialized explicitly.
Zhongxing Xu27fee832009-11-03 12:13:38 +00002687 const LocationContext *LC = N->getLocationContext();
Ted Kremenek14536f62009-08-21 22:28:32 +00002688
Ted Kremenek17810802008-11-12 19:24:17 +00002689 if (InitEx) {
Ted Kremenek57f09892010-02-08 16:18:51 +00002690 SVal InitVal = state->getSVal(InitEx);
Mike Stump11289f42009-09-09 15:08:12 +00002691
Zhongxing Xu70892502010-12-22 07:20:27 +00002692 // We bound the temp obj region to the CXXConstructExpr. Now recover
2693 // the lazy compound value when the variable is not a reference.
2694 if (AMgr.getLangOptions().CPlusPlus && VD->getType()->isRecordType() &&
2695 !VD->getType()->isReferenceType() && isa<loc::MemRegionVal>(InitVal)){
2696 InitVal = state->getSVal(cast<loc::MemRegionVal>(InitVal).getRegion());
2697 assert(isa<nonloc::LazyCompoundVal>(InitVal));
2698 }
2699
Ted Kremenek17810802008-11-12 19:24:17 +00002700 // Recover some path-sensitivity if a scalar value evaluated to
2701 // UnknownVal.
Ted Kremenekc3c1b102010-03-02 21:43:52 +00002702 if ((InitVal.isUnknown() ||
2703 !getConstraintManager().canReasonAbout(InitVal)) &&
2704 !VD->getType()->isReferenceType()) {
Ted Kremenek90af9092010-12-02 07:49:45 +00002705 InitVal = svalBuilder.getConjuredSymbolVal(NULL, InitEx,
Zhongxing Xu27fee832009-11-03 12:13:38 +00002706 Builder->getCurrentBlockCount());
Mike Stump11289f42009-09-09 15:08:12 +00002707 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00002708
Ted Kremenekdc891422010-12-01 21:57:22 +00002709 evalBind(Dst, DS, *I, state,
Ted Kremenekd51217e2010-02-15 23:02:46 +00002710 loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true);
Mike Stump11289f42009-09-09 15:08:12 +00002711 }
Ted Kremenek13363532009-02-14 01:54:57 +00002712 else {
Ted Kremenekb006b822009-11-04 00:09:15 +00002713 state = state->bindDeclWithNoInit(state->getRegion(VD, LC));
Ted Kremenek13363532009-02-14 01:54:57 +00002714 MakeNode(Dst, DS, *I, state);
Ted Kremenek8f7afdd2008-12-08 22:47:34 +00002715 }
Ted Kremenek3b427152008-04-22 22:25:27 +00002716 }
Ted Kremenek05352742008-01-24 20:55:43 +00002717}
Ted Kremenek33d82852008-01-24 02:02:54 +00002718
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002719void ExprEngine::VisitCondInit(const VarDecl *VD, const Stmt *S,
Ted Kremenek58949322009-12-24 00:40:03 +00002720 ExplodedNode *Pred, ExplodedNodeSet& Dst) {
Ted Kremenekd51217e2010-02-15 23:02:46 +00002721
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002722 const Expr* InitEx = VD->getInit();
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00002723 ExplodedNodeSet Tmp;
2724 Visit(InitEx, Pred, Tmp);
2725
2726 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2727 ExplodedNode *N = *I;
2728 const GRState *state = GetState(N);
Ted Kremenekd51217e2010-02-15 23:02:46 +00002729
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00002730 const LocationContext *LC = N->getLocationContext();
Ted Kremenek57f09892010-02-08 16:18:51 +00002731 SVal InitVal = state->getSVal(InitEx);
Ted Kremenekd51217e2010-02-15 23:02:46 +00002732
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00002733 // Recover some path-sensitivity if a scalar value evaluated to
2734 // UnknownVal.
2735 if (InitVal.isUnknown() ||
2736 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek90af9092010-12-02 07:49:45 +00002737 InitVal = svalBuilder.getConjuredSymbolVal(NULL, InitEx,
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00002738 Builder->getCurrentBlockCount());
2739 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00002740
Ted Kremenekdc891422010-12-01 21:57:22 +00002741 evalBind(Dst, S, N, state,
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00002742 loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true);
2743 }
2744}
2745
Ted Kremenekf68bf632008-10-30 17:47:32 +00002746namespace {
2747 // This class is used by VisitInitListExpr as an item in a worklist
2748 // for processing the values contained in an InitListExpr.
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002749class InitListWLItem {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002750public:
2751 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu107f7592009-08-06 12:48:26 +00002752 ExplodedNode* N;
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002753 InitListExpr::const_reverse_iterator Itr;
Mike Stump11289f42009-09-09 15:08:12 +00002754
Zhongxing Xu107f7592009-08-06 12:48:26 +00002755 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002756 InitListExpr::const_reverse_iterator itr)
Ted Kremenekf68bf632008-10-30 17:47:32 +00002757 : Vals(vals), N(n), Itr(itr) {}
2758};
2759}
2760
2761
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002762void ExprEngine::VisitInitListExpr(const InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002763 ExplodedNodeSet& Dst) {
Ted Kremenek828e6df2008-10-30 23:14:36 +00002764
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002765 const GRState* state = GetState(Pred);
Ted Kremenek45698bf2008-11-13 05:05:34 +00002766 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002767 unsigned NumInitElements = E->getNumInits();
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002768
Douglas Gregor8385a062010-04-26 21:31:17 +00002769 if (T->isArrayType() || T->isRecordType() || T->isVectorType()) {
Ted Kremenek828e6df2008-10-30 23:14:36 +00002770 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump11289f42009-09-09 15:08:12 +00002771
Ted Kremenek828e6df2008-10-30 23:14:36 +00002772 // Handle base case where the initializer has no elements.
2773 // e.g: static int* myArray[] = {};
2774 if (NumInitElements == 0) {
Ted Kremenek90af9092010-12-02 07:49:45 +00002775 SVal V = svalBuilder.makeCompoundVal(T, StartVals);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002776 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremenek828e6df2008-10-30 23:14:36 +00002777 return;
Mike Stump11289f42009-09-09 15:08:12 +00002778 }
2779
Ted Kremenek828e6df2008-10-30 23:14:36 +00002780 // Create a worklist to process the initializers.
2781 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump11289f42009-09-09 15:08:12 +00002782 WorkList.reserve(NumInitElements);
2783 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002784 InitListExpr::const_reverse_iterator ItrEnd = E->rend();
Ted Kremenek30030012009-09-22 21:19:14 +00002785 assert(!(E->rbegin() == E->rend()));
Mike Stump11289f42009-09-09 15:08:12 +00002786
Ted Kremenek828e6df2008-10-30 23:14:36 +00002787 // Process the worklist until it is empty.
Ted Kremenekf68bf632008-10-30 17:47:32 +00002788 while (!WorkList.empty()) {
2789 InitListWLItem X = WorkList.back();
2790 WorkList.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +00002791
Zhongxing Xu107f7592009-08-06 12:48:26 +00002792 ExplodedNodeSet Tmp;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002793 Visit(*X.Itr, X.N, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002794
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002795 InitListExpr::const_reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002796
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002797 for (ExplodedNodeSet::iterator NI=Tmp.begin(),NE=Tmp.end();NI!=NE;++NI) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002798 // Get the last initializer value.
2799 state = GetState(*NI);
Ted Kremenek57f09892010-02-08 16:18:51 +00002800 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump11289f42009-09-09 15:08:12 +00002801
Ted Kremenekf68bf632008-10-30 17:47:32 +00002802 // Construct the new list of values by prepending the new value to
2803 // the already constructed list.
2804 llvm::ImmutableList<SVal> NewVals =
2805 getBasicVals().consVals(InitV, X.Vals);
Mike Stump11289f42009-09-09 15:08:12 +00002806
Ted Kremenekf68bf632008-10-30 17:47:32 +00002807 if (NewItr == ItrEnd) {
Zhongxing Xu121a53a2008-10-31 03:01:26 +00002808 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenek90af9092010-12-02 07:49:45 +00002809 SVal V = svalBuilder.makeCompoundVal(T, NewVals);
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002810
Ted Kremenekf68bf632008-10-30 17:47:32 +00002811 // Make final state and node.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002812 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002813 }
2814 else {
2815 // Still some initializer values to go. Push them onto the worklist.
2816 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2817 }
2818 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002819 }
Mike Stump11289f42009-09-09 15:08:12 +00002820
Ted Kremenek28f41ba2008-10-30 18:34:31 +00002821 return;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002822 }
2823
Zhanyong Wan85a203e2011-02-16 21:13:32 +00002824 if (Loc::isLocType(T) || T->isIntegerType()) {
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002825 assert (E->getNumInits() == 1);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002826 ExplodedNodeSet Tmp;
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002827 const Expr* Init = E->getInit(0);
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002828 Visit(Init, Pred, Tmp);
Zhongxing Xu6df9f542009-12-16 11:27:52 +00002829 for (ExplodedNodeSet::iterator I=Tmp.begin(), EI=Tmp.end(); I != EI; ++I) {
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002830 state = GetState(*I);
Ted Kremenek57f09892010-02-08 16:18:51 +00002831 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002832 }
2833 return;
2834 }
2835
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002836 assert(0 && "unprocessed InitListExpr type");
2837}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00002838
Sebastian Redl6f282892008-11-11 17:56:53 +00002839/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002840void ExprEngine::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr* Ex,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002841 ExplodedNode* Pred,
2842 ExplodedNodeSet& Dst) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002843 QualType T = Ex->getTypeOfArgument();
Ken Dyck40775002010-01-11 17:06:35 +00002844 CharUnits amt;
Mike Stump11289f42009-09-09 15:08:12 +00002845
Ted Kremenekae5b7862008-03-15 03:13:20 +00002846 if (Ex->isSizeOf()) {
Mike Stump11289f42009-09-09 15:08:12 +00002847 if (T == getContext().VoidTy) {
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002848 // sizeof(void) == 1 byte.
Ken Dyck40775002010-01-11 17:06:35 +00002849 amt = CharUnits::One();
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002850 }
Jordy Rose0704a7f2010-07-05 04:42:43 +00002851 else if (!T->isConstantSizeType()) {
2852 assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
2853
2854 // FIXME: Add support for VLA type arguments, not just VLA expressions.
2855 // When that happens, we should probably refactor VLASizeChecker's code.
2856 if (Ex->isArgumentType()) {
2857 Dst.Add(Pred);
2858 return;
2859 }
2860
2861 // Get the size by getting the extent of the sub-expression.
2862 // First, visit the sub-expression to find its region.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002863 const Expr *Arg = Ex->getArgumentExpr();
Jordy Rose0704a7f2010-07-05 04:42:43 +00002864 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +00002865 Visit(Arg, Pred, Tmp);
Jordy Rose0704a7f2010-07-05 04:42:43 +00002866
2867 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2868 const GRState* state = GetState(*I);
2869 const MemRegion *MR = state->getSVal(Arg).getAsRegion();
2870
2871 // If the subexpression can't be resolved to a region, we don't know
2872 // anything about its size. Just leave the state as is and continue.
2873 if (!MR) {
2874 Dst.Add(*I);
2875 continue;
2876 }
2877
2878 // The result is the extent of the VLA.
Ted Kremenek90af9092010-12-02 07:49:45 +00002879 SVal Extent = cast<SubRegion>(MR)->getExtent(svalBuilder);
Jordy Rose0704a7f2010-07-05 04:42:43 +00002880 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, Extent));
2881 }
2882
Ted Kremenekae5b7862008-03-15 03:13:20 +00002883 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002884 }
John McCall8b07ec22010-05-15 11:32:37 +00002885 else if (T->getAs<ObjCObjectType>()) {
2886 // Some code tries to take the sizeof an ObjCObjectType, relying that
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002887 // the compiler has laid out its representation. Just report Unknown
Mike Stump11289f42009-09-09 15:08:12 +00002888 // for these.
Ted Kremenekfab459f2010-02-02 02:01:51 +00002889 Dst.Add(Pred);
Ted Kremenek99057462008-04-30 21:31:12 +00002890 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002891 }
2892 else {
2893 // All other cases.
Ken Dyck40775002010-01-11 17:06:35 +00002894 amt = getContext().getTypeSizeInChars(T);
Mike Stump11289f42009-09-09 15:08:12 +00002895 }
Ted Kremenekae5b7862008-03-15 03:13:20 +00002896 }
2897 else // Get alignment of the type.
Ken Dyck2c229a72010-01-27 12:54:25 +00002898 amt = getContext().getTypeAlignInChars(T);
Mike Stump11289f42009-09-09 15:08:12 +00002899
Ted Kremenek181f7232008-03-21 21:30:14 +00002900 MakeNode(Dst, Ex, Pred,
Ted Kremenekd51217e2010-02-15 23:02:46 +00002901 GetState(Pred)->BindExpr(Ex,
Ted Kremenek90af9092010-12-02 07:49:45 +00002902 svalBuilder.makeIntVal(amt.getQuantity(), Ex->getType())));
Ted Kremenek002bf742008-02-12 19:49:57 +00002903}
2904
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002905void ExprEngine::VisitOffsetOfExpr(const OffsetOfExpr* OOE,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002906 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Douglas Gregor882211c2010-04-28 22:16:22 +00002907 Expr::EvalResult Res;
2908 if (OOE->Evaluate(Res, getContext()) && Res.Val.isInt()) {
2909 const APSInt &IV = Res.Val.getInt();
2910 assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
2911 assert(OOE->getType()->isIntegerType());
2912 assert(IV.isSigned() == OOE->getType()->isSignedIntegerType());
Ted Kremenek90af9092010-12-02 07:49:45 +00002913 SVal X = svalBuilder.makeIntVal(IV);
Douglas Gregor882211c2010-04-28 22:16:22 +00002914 MakeNode(Dst, OOE, Pred, GetState(Pred)->BindExpr(OOE, X));
2915 return;
2916 }
2917 // FIXME: Handle the case where __builtin_offsetof is not a constant.
2918 Dst.Add(Pred);
2919}
Ted Kremenekb597bb92008-02-20 04:02:35 +00002920
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00002921void ExprEngine::VisitUnaryOperator(const UnaryOperator* U,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002922 ExplodedNode* Pred,
Ted Kremenek8219b822010-12-16 07:46:53 +00002923 ExplodedNodeSet& Dst) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002924
Ted Kremenekb597bb92008-02-20 04:02:35 +00002925 switch (U->getOpcode()) {
Mike Stump11289f42009-09-09 15:08:12 +00002926
Ted Kremenekb597bb92008-02-20 04:02:35 +00002927 default:
Ted Kremenekb597bb92008-02-20 04:02:35 +00002928 break;
Mike Stump11289f42009-09-09 15:08:12 +00002929
John McCalle3027922010-08-25 11:45:40 +00002930 case UO_Real: {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002931 const Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002932 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002933 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002934
Zhongxing Xu107f7592009-08-06 12:48:26 +00002935 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002936
Zhongxing Xu27f17422008-10-17 05:57:07 +00002937 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002938 if (Ex->getType()->isAnyComplexType()) {
2939 // Just report "Unknown."
2940 Dst.Add(*I);
2941 continue;
2942 }
Mike Stump11289f42009-09-09 15:08:12 +00002943
John McCalle3027922010-08-25 11:45:40 +00002944 // For all other types, UO_Real is an identity operation.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002945 assert (U->getType() == Ex->getType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002946 const GRState* state = GetState(*I);
Ted Kremenek57f09892010-02-08 16:18:51 +00002947 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump11289f42009-09-09 15:08:12 +00002948 }
2949
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002950 return;
2951 }
Mike Stump11289f42009-09-09 15:08:12 +00002952
John McCalle3027922010-08-25 11:45:40 +00002953 case UO_Imag: {
Mike Stump11289f42009-09-09 15:08:12 +00002954
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002955 const Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002956 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002957 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002958
Zhongxing Xu107f7592009-08-06 12:48:26 +00002959 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu27f17422008-10-17 05:57:07 +00002960 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002961 if (Ex->getType()->isAnyComplexType()) {
2962 // Just report "Unknown."
2963 Dst.Add(*I);
2964 continue;
2965 }
Mike Stump11289f42009-09-09 15:08:12 +00002966
John McCalle3027922010-08-25 11:45:40 +00002967 // For all other types, UO_Imag returns 0.
Ted Kremenek17d541d2009-02-13 01:45:31 +00002968 const GRState* state = GetState(*I);
Ted Kremenek90af9092010-12-02 07:49:45 +00002969 SVal X = svalBuilder.makeZeroVal(Ex->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002970 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002971 }
Mike Stump11289f42009-09-09 15:08:12 +00002972
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002973 return;
2974 }
Douglas Gregor882211c2010-04-28 22:16:22 +00002975
Ted Kremenek8219b822010-12-16 07:46:53 +00002976 case UO_Plus:
2977 assert(!U->isLValue());
2978 // FALL-THROUGH.
2979 case UO_Deref:
Zhongxing Xu1b477732010-12-18 05:16:43 +00002980 case UO_AddrOf:
John McCalle3027922010-08-25 11:45:40 +00002981 case UO_Extension: {
Mike Stump11289f42009-09-09 15:08:12 +00002982
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002983 // Unary "+" is a no-op, similar to a parentheses. We still have places
2984 // where it may be a block-level expression, so we need to
2985 // generate an extra node that just propagates the value of the
2986 // subexpression.
2987
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00002988 const Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002989 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +00002990 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002991
2992 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002993 const GRState* state = GetState(*I);
Ted Kremenek57f09892010-02-08 16:18:51 +00002994 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002995 }
Mike Stump11289f42009-09-09 15:08:12 +00002996
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002997 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002998 }
Mike Stump11289f42009-09-09 15:08:12 +00002999
John McCalle3027922010-08-25 11:45:40 +00003000 case UO_LNot:
3001 case UO_Minus:
3002 case UO_Not: {
Ted Kremenek8219b822010-12-16 07:46:53 +00003003 assert (!U->isLValue());
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003004 const Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00003005 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003006 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00003007
3008 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00003009 const GRState* state = GetState(*I);
Mike Stump11289f42009-09-09 15:08:12 +00003010
Ted Kremenek76bccf62008-09-30 05:32:44 +00003011 // Get the value of the subexpression.
Ted Kremenek57f09892010-02-08 16:18:51 +00003012 SVal V = state->getSVal(Ex);
Ted Kremenek76bccf62008-09-30 05:32:44 +00003013
Ted Kremenek1ca33462008-11-15 00:20:05 +00003014 if (V.isUnknownOrUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00003015 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremenek1ca33462008-11-15 00:20:05 +00003016 continue;
3017 }
Mike Stump11289f42009-09-09 15:08:12 +00003018
Ted Kremenek44137142008-11-15 04:01:56 +00003019// QualType DstT = getContext().getCanonicalType(U->getType());
3020// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump11289f42009-09-09 15:08:12 +00003021//
Ted Kremenek44137142008-11-15 04:01:56 +00003022// if (DstT != SrcT) // Perform promotions.
Ted Kremenekdc891422010-12-01 21:57:22 +00003023// V = evalCast(V, DstT);
Mike Stump11289f42009-09-09 15:08:12 +00003024//
Ted Kremenek44137142008-11-15 04:01:56 +00003025// if (V.isUnknownOrUndef()) {
3026// MakeNode(Dst, U, *I, BindExpr(St, U, V));
3027// continue;
3028// }
Mike Stump11289f42009-09-09 15:08:12 +00003029
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003030 switch (U->getOpcode()) {
3031 default:
3032 assert(false && "Invalid Opcode.");
3033 break;
Mike Stump11289f42009-09-09 15:08:12 +00003034
John McCalle3027922010-08-25 11:45:40 +00003035 case UO_Not:
Ted Kremenekd331d092008-10-01 00:21:14 +00003036 // FIXME: Do we need to handle promotions?
Ted Kremenekdc891422010-12-01 21:57:22 +00003037 state = state->BindExpr(U, evalComplement(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00003038 break;
3039
John McCalle3027922010-08-25 11:45:40 +00003040 case UO_Minus:
Ted Kremenekd331d092008-10-01 00:21:14 +00003041 // FIXME: Do we need to handle promotions?
Ted Kremenekdc891422010-12-01 21:57:22 +00003042 state = state->BindExpr(U, evalMinus(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00003043 break;
3044
John McCalle3027922010-08-25 11:45:40 +00003045 case UO_LNot:
Mike Stump11289f42009-09-09 15:08:12 +00003046
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003047 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
3048 //
3049 // Note: technically we do "E == 0", but this is the same in the
3050 // transfer functions as "0 == E".
Ted Kremenek1642bda2009-06-26 00:05:51 +00003051 SVal Result;
Mike Stump11289f42009-09-09 15:08:12 +00003052
Zhongxing Xu27f17422008-10-17 05:57:07 +00003053 if (isa<Loc>(V)) {
Ted Kremenek90af9092010-12-02 07:49:45 +00003054 Loc X = svalBuilder.makeNull();
Ted Kremenekdc891422010-12-01 21:57:22 +00003055 Result = evalBinOp(state, BO_EQ, cast<Loc>(V), X,
Ted Kremenek1642bda2009-06-26 00:05:51 +00003056 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003057 }
3058 else {
Ted Kremenek44137142008-11-15 04:01:56 +00003059 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdc891422010-12-01 21:57:22 +00003060 Result = evalBinOp(state, BO_EQ, cast<NonLoc>(V), X,
Ted Kremenek1642bda2009-06-26 00:05:51 +00003061 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003062 }
Mike Stump11289f42009-09-09 15:08:12 +00003063
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00003064 state = state->BindExpr(U, Result);
Mike Stump11289f42009-09-09 15:08:12 +00003065
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003066 break;
3067 }
Mike Stump11289f42009-09-09 15:08:12 +00003068
Ted Kremenek17d541d2009-02-13 01:45:31 +00003069 MakeNode(Dst, U, *I, state);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003070 }
Mike Stump11289f42009-09-09 15:08:12 +00003071
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003072 return;
3073 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003074 }
3075
3076 // Handle ++ and -- (both pre- and post-increment).
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003077 assert (U->isIncrementDecrementOp());
Zhongxing Xu107f7592009-08-06 12:48:26 +00003078 ExplodedNodeSet Tmp;
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003079 const Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenek8219b822010-12-16 07:46:53 +00003080 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00003081
Zhongxing Xu107f7592009-08-06 12:48:26 +00003082 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00003083
Ted Kremenek17d541d2009-02-13 01:45:31 +00003084 const GRState* state = GetState(*I);
Zhongxing Xu6f8a8f92010-12-22 08:38:13 +00003085 SVal loc = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00003086
3087 // Perform a load.
Zhongxing Xu107f7592009-08-06 12:48:26 +00003088 ExplodedNodeSet Tmp2;
Zhongxing Xu6f8a8f92010-12-22 08:38:13 +00003089 evalLoad(Tmp2, Ex, *I, state, loc);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003090
Zhongxing Xu6df9f542009-12-16 11:27:52 +00003091 for (ExplodedNodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end();I2!=E2;++I2) {
Mike Stump11289f42009-09-09 15:08:12 +00003092
Ted Kremenek17d541d2009-02-13 01:45:31 +00003093 state = GetState(*I2);
Ted Kremenek57f09892010-02-08 16:18:51 +00003094 SVal V2_untested = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00003095
3096 // Propagate unknown and undefined values.
Ted Kremenek7020eae2009-09-11 22:07:28 +00003097 if (V2_untested.isUnknownOrUndef()) {
3098 MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested));
Ted Kremenek7f0639b2008-02-21 18:02:17 +00003099 continue;
Ted Kremenekd51217e2010-02-15 23:02:46 +00003100 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00003101 DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
Mike Stump11289f42009-09-09 15:08:12 +00003102
3103 // Handle all other values.
John McCalle3027922010-08-25 11:45:40 +00003104 BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add
3105 : BO_Sub;
Ted Kremenek32c41ec2009-03-11 03:54:24 +00003106
Zhongxing Xufe971652009-08-05 02:51:59 +00003107 // If the UnaryOperator has non-location type, use its type to create the
3108 // constant value. If the UnaryOperator has location type, create the
3109 // constant with int type and pointer width.
3110 SVal RHS;
3111
3112 if (U->getType()->isAnyPointerType())
Ted Kremenek5614c462010-12-24 08:39:33 +00003113 RHS = svalBuilder.makeArrayIndex(1);
Zhongxing Xufe971652009-08-05 02:51:59 +00003114 else
Ted Kremenek90af9092010-12-02 07:49:45 +00003115 RHS = svalBuilder.makeIntVal(1, U->getType());
Zhongxing Xufe971652009-08-05 02:51:59 +00003116
Ted Kremenekdc891422010-12-01 21:57:22 +00003117 SVal Result = evalBinOp(state, Op, V2, RHS, U->getType());
Mike Stump11289f42009-09-09 15:08:12 +00003118
Ted Kremenek6b315332009-03-20 20:10:45 +00003119 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek35f875c2009-04-21 22:38:05 +00003120 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek7020eae2009-09-11 22:07:28 +00003121 DefinedOrUnknownSVal SymVal =
Ted Kremenek90af9092010-12-02 07:49:45 +00003122 svalBuilder.getConjuredSymbolVal(NULL, Ex,
Ted Kremeneke41b81e2009-09-27 20:45:21 +00003123 Builder->getCurrentBlockCount());
Ted Kremenek7020eae2009-09-11 22:07:28 +00003124 Result = SymVal;
Mike Stump11289f42009-09-09 15:08:12 +00003125
Ted Kremenek35f875c2009-04-21 22:38:05 +00003126 // If the value is a location, ++/-- should always preserve
Ted Kremenek1642bda2009-06-26 00:05:51 +00003127 // non-nullness. Check if the original value was non-null, and if so
Mike Stump11289f42009-09-09 15:08:12 +00003128 // propagate that constraint.
Zhanyong Wan85a203e2011-02-16 21:13:32 +00003129 if (Loc::isLocType(U->getType())) {
Ted Kremenek7020eae2009-09-11 22:07:28 +00003130 DefinedOrUnknownSVal Constraint =
Zhongxing Xu6f8a8f92010-12-22 08:38:13 +00003131 svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00003132
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00003133 if (!state->assume(Constraint, true)) {
Ted Kremenek35f875c2009-04-21 22:38:05 +00003134 // It isn't feasible for the original value to be null.
3135 // Propagate this constraint.
Ted Kremenekdc891422010-12-01 21:57:22 +00003136 Constraint = svalBuilder.evalEQ(state, SymVal,
Ted Kremenek90af9092010-12-02 07:49:45 +00003137 svalBuilder.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00003138
Ted Kremenek7020eae2009-09-11 22:07:28 +00003139
Ted Kremenekc5bea1e2010-12-01 22:16:56 +00003140 state = state->assume(Constraint, false);
Ted Kremenekf9906842009-06-18 22:57:13 +00003141 assert(state);
Mike Stump11289f42009-09-09 15:08:12 +00003142 }
3143 }
Ted Kremenek35f875c2009-04-21 22:38:05 +00003144 }
Mike Stump11289f42009-09-09 15:08:12 +00003145
Zhongxing Xu6f8a8f92010-12-22 08:38:13 +00003146 // Since the lvalue-to-rvalue conversion is explicit in the AST,
3147 // we bind an l-value if the operator is prefix and an lvalue (in C++).
Zhongxing Xu0710f5c2011-01-10 03:22:57 +00003148 if (U->isLValue())
Zhongxing Xu6f8a8f92010-12-22 08:38:13 +00003149 state = state->BindExpr(U, loc);
3150 else
3151 state = state->BindExpr(U, V2);
Ted Kremenekcdd0be12008-02-06 22:50:25 +00003152
Mike Stump11289f42009-09-09 15:08:12 +00003153 // Perform the store.
Zhongxing Xu6f8a8f92010-12-22 08:38:13 +00003154 evalStore(Dst, NULL, U, *I2, state, loc, Result);
Ted Kremenek43523e02008-02-07 01:08:27 +00003155 }
Ted Kremenek38213f92008-04-21 23:43:38 +00003156 }
Ted Kremenek43523e02008-02-07 01:08:27 +00003157}
3158
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003159void ExprEngine::VisitAsmStmt(const AsmStmt* A, ExplodedNode* Pred,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00003160 ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003161 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00003162}
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003163
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003164void ExprEngine::VisitAsmStmtHelperOutputs(const AsmStmt* A,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003165 AsmStmt::const_outputs_iterator I,
3166 AsmStmt::const_outputs_iterator E,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00003167 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003168 if (I == E) {
3169 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
3170 return;
3171 }
Mike Stump11289f42009-09-09 15:08:12 +00003172
Zhongxing Xu107f7592009-08-06 12:48:26 +00003173 ExplodedNodeSet Tmp;
Ted Kremenek8219b822010-12-16 07:46:53 +00003174 Visit(*I, Pred, Tmp);
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003175 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00003176
Zhongxing Xu6df9f542009-12-16 11:27:52 +00003177 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003178 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
3179}
3180
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003181void ExprEngine::VisitAsmStmtHelperInputs(const AsmStmt* A,
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003182 AsmStmt::const_inputs_iterator I,
3183 AsmStmt::const_inputs_iterator E,
Ted Kremenekd51217e2010-02-15 23:02:46 +00003184 ExplodedNode* Pred,
Zhongxing Xu6df9f542009-12-16 11:27:52 +00003185 ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003186 if (I == E) {
Mike Stump11289f42009-09-09 15:08:12 +00003187
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003188 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu27f17422008-10-17 05:57:07 +00003189 // should evaluate to Locs. Nuke all of their values.
Mike Stump11289f42009-09-09 15:08:12 +00003190
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003191 // FIXME: Some day in the future it would be nice to allow a "plug-in"
3192 // which interprets the inline asm and stores proper results in the
3193 // outputs.
Mike Stump11289f42009-09-09 15:08:12 +00003194
Ted Kremenek17d541d2009-02-13 01:45:31 +00003195 const GRState* state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00003196
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003197 for (AsmStmt::const_outputs_iterator OI = A->begin_outputs(),
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003198 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump11289f42009-09-09 15:08:12 +00003199
Ted Kremenek57f09892010-02-08 16:18:51 +00003200 SVal X = state->getSVal(*OI);
Zhongxing Xu27f17422008-10-17 05:57:07 +00003201 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump11289f42009-09-09 15:08:12 +00003202
Zhongxing Xu27f17422008-10-17 05:57:07 +00003203 if (isa<Loc>(X))
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00003204 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003205 }
Mike Stump11289f42009-09-09 15:08:12 +00003206
Ted Kremenek17d541d2009-02-13 01:45:31 +00003207 MakeNode(Dst, A, Pred, state);
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003208 return;
3209 }
Mike Stump11289f42009-09-09 15:08:12 +00003210
Zhongxing Xu107f7592009-08-06 12:48:26 +00003211 ExplodedNodeSet Tmp;
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003212 Visit(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00003213
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003214 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00003215
Ted Kremenek17a02962009-09-03 03:02:58 +00003216 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00003217 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
3218}
3219
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003220void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred,
Ted Kremenekbee01e52009-11-06 02:24:13 +00003221 ExplodedNodeSet &Dst) {
Ted Kremenekbee01e52009-11-06 02:24:13 +00003222 ExplodedNodeSet Src;
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003223 if (const Expr *RetE = RS->getRetValue()) {
Zhongxing Xubf2f0d72010-03-23 08:09:29 +00003224 // Record the returned expression in the state. It will be used in
Ted Kremenek926c9622011-01-11 02:34:45 +00003225 // processCallExit to bind the return value to the call expr.
Zhongxing Xu5c075842010-02-26 15:43:34 +00003226 {
Ted Kremenekf41bdd72011-01-13 04:36:46 +00003227 static int tag = 0;
Zhongxing Xu5c075842010-02-26 15:43:34 +00003228 const GRState *state = GetState(Pred);
3229 state = state->set<ReturnExpr>(RetE);
Ted Kremenekf41bdd72011-01-13 04:36:46 +00003230 Pred = Builder->generateNode(RetE, state, Pred, &tag);
Zhongxing Xu5c075842010-02-26 15:43:34 +00003231 }
3232 // We may get a NULL Pred because we generated a cached node.
3233 if (Pred)
3234 Visit(RetE, Pred, Src);
Ted Kremenekf6467742008-03-31 15:02:58 +00003235 }
Ted Kremenekbee01e52009-11-06 02:24:13 +00003236 else {
3237 Src.Add(Pred);
3238 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00003239
Ted Kremenekbee01e52009-11-06 02:24:13 +00003240 ExplodedNodeSet CheckedSet;
Jordy Rosec36df4d2010-08-04 07:10:57 +00003241 CheckerVisit(RS, CheckedSet, Src, PreVisitStmtCallback);
Ted Kremenekd51217e2010-02-15 23:02:46 +00003242
Ted Kremenekbee01e52009-11-06 02:24:13 +00003243 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
3244 I != E; ++I) {
Ted Kremenek9c375152008-04-16 23:05:51 +00003245
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003246 assert(Builder && "StmtNodeBuilder must be defined.");
Ted Kremenekd51217e2010-02-15 23:02:46 +00003247
Ted Kremenekbee01e52009-11-06 02:24:13 +00003248 Pred = *I;
3249 unsigned size = Dst.size();
Ted Kremenekd51217e2010-02-15 23:02:46 +00003250
Ted Kremenekbee01e52009-11-06 02:24:13 +00003251 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekf41bdd72011-01-13 04:36:46 +00003252 SaveOr OldHasGen(Builder->hasGeneratedNode);
Ted Kremenekd51217e2010-02-15 23:02:46 +00003253
Ted Kremenekdc891422010-12-01 21:57:22 +00003254 getTF().evalReturn(Dst, *this, *Builder, RS, Pred);
Ted Kremenekd51217e2010-02-15 23:02:46 +00003255
3256 // Handle the case where no nodes where generated.
3257 if (!Builder->BuildSinks && Dst.size() == size &&
Ted Kremenekf41bdd72011-01-13 04:36:46 +00003258 !Builder->hasGeneratedNode)
Ted Kremenekbee01e52009-11-06 02:24:13 +00003259 MakeNode(Dst, RS, Pred, GetState(Pred));
Ted Kremenek0b63f962008-11-21 00:27:44 +00003260 }
Ted Kremenekf6467742008-03-31 15:02:58 +00003261}
Ted Kremenek64100da2008-03-25 00:34:37 +00003262
Ted Kremenek667cacb2008-04-15 23:06:53 +00003263//===----------------------------------------------------------------------===//
3264// Transfer functions: Binary operators.
3265//===----------------------------------------------------------------------===//
3266
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003267void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003268 ExplodedNode* Pred,
Ted Kremenek8219b822010-12-16 07:46:53 +00003269 ExplodedNodeSet& Dst) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00003270 ExplodedNodeSet Tmp1;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003271 Expr* LHS = B->getLHS()->IgnoreParens();
3272 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00003273
Ted Kremenek8219b822010-12-16 07:46:53 +00003274 Visit(LHS, Pred, Tmp1);
Zhongxing Xuc6123a12009-11-24 08:24:26 +00003275 ExplodedNodeSet Tmp3;
3276
Ted Kremenek17a02962009-09-03 03:02:58 +00003277 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Zhongxing Xu4d4b8d82010-04-20 04:53:09 +00003278 SVal LeftV = GetState(*I1)->getSVal(LHS);
Zhongxing Xu107f7592009-08-06 12:48:26 +00003279 ExplodedNodeSet Tmp2;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003280 Visit(RHS, *I1, Tmp2);
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00003281
3282 ExplodedNodeSet CheckedSet;
Jordy Rosec36df4d2010-08-04 07:10:57 +00003283 CheckerVisit(B, CheckedSet, Tmp2, PreVisitStmtCallback);
Mike Stump11289f42009-09-09 15:08:12 +00003284
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003285 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump11289f42009-09-09 15:08:12 +00003286
3287 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00003288 I2 != E2; ++I2) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00003289
Ted Kremenek7020eae2009-09-11 22:07:28 +00003290 const GRState *state = GetState(*I2);
Ted Kremenek57f09892010-02-08 16:18:51 +00003291 SVal RightV = state->getSVal(RHS);
Ted Kremenek7020eae2009-09-11 22:07:28 +00003292
Ted Kremenekcdd0be12008-02-06 22:50:25 +00003293 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00003294
John McCalle3027922010-08-25 11:45:40 +00003295 if (Op == BO_Assign) {
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003296 // EXPERIMENTAL: "Conjured" symbols.
3297 // FIXME: Handle structs.
Ted Kremenek1008a2a2010-07-29 00:28:33 +00003298 if (RightV.isUnknown() ||!getConstraintManager().canReasonAbout(RightV))
3299 {
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003300 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +00003301 RightV = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), Count);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003302 }
Zhongxing Xub9eda672009-10-30 07:19:39 +00003303
Ted Kremenek8219b822010-12-16 07:46:53 +00003304 SVal ExprVal = B->isLValue() ? LeftV : RightV;
Zhongxing Xub9eda672009-10-30 07:19:39 +00003305
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003306 // Simulate the effects of a "store": bind the value of the RHS
3307 // to the L-Value represented by the LHS.
Ted Kremenekdc891422010-12-01 21:57:22 +00003308 evalStore(Tmp3, B, LHS, *I2, state->BindExpr(B, ExprVal), LeftV,RightV);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003309 continue;
3310 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00003311
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003312 if (!B->isAssignmentOp()) {
3313 // Process non-assignments except commas or short-circuited
3314 // logical expressions (LAnd and LOr).
Ted Kremenekdc891422010-12-01 21:57:22 +00003315 SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenekd51217e2010-02-15 23:02:46 +00003316
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003317 if (Result.isUnknown()) {
Ted Kremenek5f256da2010-09-09 07:13:00 +00003318 MakeNode(Tmp3, B, *I2, state);
Ted Kremenek2044a512008-04-16 18:21:25 +00003319 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00003320 }
Ted Kremenekd51217e2010-02-15 23:02:46 +00003321
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003322 state = state->BindExpr(B, Result);
Ted Kremenekd51217e2010-02-15 23:02:46 +00003323
Zhongxing Xuc6123a12009-11-24 08:24:26 +00003324 MakeNode(Tmp3, B, *I2, state);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00003325 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00003326 }
Mike Stump11289f42009-09-09 15:08:12 +00003327
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003328 assert (B->isCompoundAssignmentOp());
3329
Ted Kremenek5d7662c2009-02-07 00:52:24 +00003330 switch (Op) {
3331 default:
3332 assert(0 && "Invalid opcode for compound assignment.");
John McCalle3027922010-08-25 11:45:40 +00003333 case BO_MulAssign: Op = BO_Mul; break;
3334 case BO_DivAssign: Op = BO_Div; break;
3335 case BO_RemAssign: Op = BO_Rem; break;
3336 case BO_AddAssign: Op = BO_Add; break;
3337 case BO_SubAssign: Op = BO_Sub; break;
3338 case BO_ShlAssign: Op = BO_Shl; break;
3339 case BO_ShrAssign: Op = BO_Shr; break;
3340 case BO_AndAssign: Op = BO_And; break;
3341 case BO_XorAssign: Op = BO_Xor; break;
3342 case BO_OrAssign: Op = BO_Or; break;
Ted Kremenek54d399a2008-10-27 23:02:39 +00003343 }
Mike Stump11289f42009-09-09 15:08:12 +00003344
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003345 // Perform a load (the LHS). This performs the checks for
3346 // null dereferences, and so on.
Zhongxing Xucd179542010-01-19 09:25:53 +00003347 ExplodedNodeSet Tmp4;
Ted Kremenek57f09892010-02-08 16:18:51 +00003348 SVal location = state->getSVal(LHS);
Ted Kremenekdc891422010-12-01 21:57:22 +00003349 evalLoad(Tmp4, LHS, *I2, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00003350
Zhongxing Xucd179542010-01-19 09:25:53 +00003351 for (ExplodedNodeSet::iterator I4=Tmp4.begin(), E4=Tmp4.end(); I4!=E4;
3352 ++I4) {
3353 state = GetState(*I4);
Ted Kremenek57f09892010-02-08 16:18:51 +00003354 SVal V = state->getSVal(LHS);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003355
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003356 // Get the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00003357 QualType CTy =
3358 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek44137142008-11-15 04:01:56 +00003359 CTy = getContext().getCanonicalType(CTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00003360
Ted Kremenekac7c7242009-07-21 21:03:30 +00003361 QualType CLHSTy =
3362 cast<CompoundAssignOperator>(B)->getComputationLHSType();
3363 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00003364
Ted Kremenek44137142008-11-15 04:01:56 +00003365 QualType LTy = getContext().getCanonicalType(LHS->getType());
Eli Friedman8b7b1b12009-03-28 01:22:36 +00003366
3367 // Promote LHS.
Ted Kremenekdc891422010-12-01 21:57:22 +00003368 V = svalBuilder.evalCast(V, CLHSTy, LTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00003369
Mike Stump11289f42009-09-09 15:08:12 +00003370 // Compute the result of the operation.
Ted Kremenekdc891422010-12-01 21:57:22 +00003371 SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
Zhongxing Xu319deb82010-02-04 04:56:43 +00003372 B->getType(), CTy);
Mike Stump11289f42009-09-09 15:08:12 +00003373
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00003374 // EXPERIMENTAL: "Conjured" symbols.
3375 // FIXME: Handle structs.
Mike Stump11289f42009-09-09 15:08:12 +00003376
Ted Kremenek44137142008-11-15 04:01:56 +00003377 SVal LHSVal;
Mike Stump11289f42009-09-09 15:08:12 +00003378
Ted Kremenek1008a2a2010-07-29 00:28:33 +00003379 if (Result.isUnknown() ||
3380 !getConstraintManager().canReasonAbout(Result)) {
Mike Stump11289f42009-09-09 15:08:12 +00003381
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00003382 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00003383
Ted Kremenek44137142008-11-15 04:01:56 +00003384 // The symbolic value is actually for the type of the left-hand side
3385 // expression, not the computation type, as this is the value the
3386 // LValue on the LHS will bind to.
Ted Kremenek90af9092010-12-02 07:49:45 +00003387 LHSVal = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count);
Mike Stump11289f42009-09-09 15:08:12 +00003388
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00003389 // However, we need to convert the symbol to the computation type.
Ted Kremenekdc891422010-12-01 21:57:22 +00003390 Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00003391 }
Ted Kremenek44137142008-11-15 04:01:56 +00003392 else {
3393 // The left-hand side may bind to a different value then the
3394 // computation type.
Ted Kremenekdc891422010-12-01 21:57:22 +00003395 LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
Ted Kremenek44137142008-11-15 04:01:56 +00003396 }
Mike Stump11289f42009-09-09 15:08:12 +00003397
Zhongxing Xu5609e212011-01-10 03:54:19 +00003398 // In C++, assignment and compound assignment operators return an
3399 // lvalue.
3400 if (B->isLValue())
3401 state = state->BindExpr(B, location);
3402 else
3403 state = state->BindExpr(B, Result);
3404
3405 evalStore(Tmp3, B, LHS, *I4, state, location, LHSVal);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00003406 }
Ted Kremenekfb553542008-01-16 00:53:15 +00003407 }
Ted Kremenekde8d62b2008-01-15 23:55:06 +00003408 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +00003409
Jordy Rosec36df4d2010-08-04 07:10:57 +00003410 CheckerVisit(B, Dst, Tmp3, PostVisitStmtCallback);
Ted Kremenekde8d62b2008-01-15 23:55:06 +00003411}
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003412
3413//===----------------------------------------------------------------------===//
Ted Kremenek6f2a7052009-10-30 17:47:32 +00003414// Checker registration/lookup.
3415//===----------------------------------------------------------------------===//
3416
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003417Checker *ExprEngine::lookupChecker(void *tag) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +00003418 CheckerMap::const_iterator I = CheckerM.find(tag);
Ted Kremenek6f2a7052009-10-30 17:47:32 +00003419 return (I == CheckerM.end()) ? NULL : Checkers[I->second].second;
3420}
3421
3422//===----------------------------------------------------------------------===//
Ted Kremenekd3122cb2008-02-14 22:36:46 +00003423// Visualization.
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003424//===----------------------------------------------------------------------===//
3425
Ted Kremenekac886cb2008-01-16 21:46:15 +00003426#ifndef NDEBUG
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003427static ExprEngine* GraphPrintCheckerState;
Ted Kremenek6947a792008-03-07 20:57:30 +00003428static SourceManager* GraphPrintSourceManager;
Ted Kremenek2531fce2008-01-30 23:24:39 +00003429
Ted Kremenekac886cb2008-01-16 21:46:15 +00003430namespace llvm {
3431template<>
Douglas Gregor693ba202009-11-30 16:08:24 +00003432struct DOTGraphTraits<ExplodedNode*> :
Ted Kremenekac886cb2008-01-16 21:46:15 +00003433 public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003434
3435 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3436
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003437 // FIXME: Since we do not cache error nodes in ExprEngine now, this does not
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +00003438 // work.
Zhongxing Xu107f7592009-08-06 12:48:26 +00003439 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump11289f42009-09-09 15:08:12 +00003440
Ted Kremenek7cf82382009-11-11 20:16:36 +00003441#if 0
3442 // FIXME: Replace with a general scheme to tell if the node is
3443 // an error node.
Ted Kremenek5b70a222008-02-14 22:54:53 +00003444 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek0f7130a2008-02-19 00:22:37 +00003445 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek93d1fed2008-02-28 09:25:22 +00003446 GraphPrintCheckerState->isUndefDeref(N) ||
3447 GraphPrintCheckerState->isUndefStore(N) ||
3448 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek6f5fca72008-02-29 23:14:48 +00003449 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek51e87ea2008-02-29 23:53:11 +00003450 GraphPrintCheckerState->isBadCall(N) ||
3451 GraphPrintCheckerState->isUndefArg(N))
Ted Kremenek5b70a222008-02-14 22:54:53 +00003452 return "color=\"red\",style=\"filled\"";
Mike Stump11289f42009-09-09 15:08:12 +00003453
Ted Kremeneke0c79382008-02-28 20:32:03 +00003454 if (GraphPrintCheckerState->isNoReturnCall(N))
3455 return "color=\"blue\",style=\"filled\"";
Zhongxing Xu9e200792009-11-24 07:06:39 +00003456#endif
Ted Kremenek5b70a222008-02-14 22:54:53 +00003457 return "";
3458 }
Mike Stump11289f42009-09-09 15:08:12 +00003459
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003460 static std::string getNodeLabel(const ExplodedNode* N, void*){
Mike Stump11289f42009-09-09 15:08:12 +00003461
Ted Kremenek799bb6e2009-06-24 23:06:47 +00003462 std::string sbuf;
3463 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek930191c2008-01-23 22:30:44 +00003464
3465 // Program Location.
Ted Kremenekac886cb2008-01-16 21:46:15 +00003466 ProgramPoint Loc = N->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003467
Ted Kremenekac886cb2008-01-16 21:46:15 +00003468 switch (Loc.getKind()) {
3469 case ProgramPoint::BlockEntranceKind:
Mike Stump11289f42009-09-09 15:08:12 +00003470 Out << "Block Entrance: B"
Ted Kremenekac886cb2008-01-16 21:46:15 +00003471 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3472 break;
Mike Stump11289f42009-09-09 15:08:12 +00003473
Ted Kremenekac886cb2008-01-16 21:46:15 +00003474 case ProgramPoint::BlockExitKind:
3475 assert (false);
3476 break;
Mike Stump11289f42009-09-09 15:08:12 +00003477
Douglas Gregora2fbc942010-02-25 19:01:53 +00003478 case ProgramPoint::CallEnterKind:
3479 Out << "CallEnter";
3480 break;
3481
3482 case ProgramPoint::CallExitKind:
3483 Out << "CallExit";
3484 break;
3485
Ted Kremenekac886cb2008-01-16 21:46:15 +00003486 default: {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00003487 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
3488 const Stmt* S = L->getStmt();
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003489 SourceLocation SLoc = S->getLocStart();
3490
Mike Stump11289f42009-09-09 15:08:12 +00003491 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattnerc61089a2009-06-30 01:26:17 +00003492 LangOptions LO; // FIXME.
3493 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00003494
3495 if (SLoc.isFileID()) {
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003496 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00003497 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3498 << " col="
3499 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3500 << "\\l";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003501 }
Mike Stump11289f42009-09-09 15:08:12 +00003502
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00003503 if (isa<PreStmt>(Loc))
Mike Stump11289f42009-09-09 15:08:12 +00003504 Out << "\\lPreStmt\\l;";
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00003505 else if (isa<PostLoad>(Loc))
Ted Kremeneka6e08322009-05-07 18:27:16 +00003506 Out << "\\lPostLoad\\l;";
3507 else if (isa<PostStore>(Loc))
3508 Out << "\\lPostStore\\l";
3509 else if (isa<PostLValue>(Loc))
3510 Out << "\\lPostLValue\\l";
Mike Stump11289f42009-09-09 15:08:12 +00003511
Ted Kremenek7cf82382009-11-11 20:16:36 +00003512#if 0
3513 // FIXME: Replace with a general scheme to determine
3514 // the name of the check.
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003515 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3516 Out << "\\|Implicit-Null Dereference.\\l";
3517 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3518 Out << "\\|Explicit-Null Dereference.\\l";
3519 else if (GraphPrintCheckerState->isUndefDeref(N))
3520 Out << "\\|Dereference of undefialied value.\\l";
3521 else if (GraphPrintCheckerState->isUndefStore(N))
3522 Out << "\\|Store to Undefined Loc.";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003523 else if (GraphPrintCheckerState->isUndefResult(N))
3524 Out << "\\|Result of operation is undefined.";
3525 else if (GraphPrintCheckerState->isNoReturnCall(N))
3526 Out << "\\|Call to function marked \"noreturn\".";
3527 else if (GraphPrintCheckerState->isBadCall(N))
3528 Out << "\\|Call to NULL/Undefined.";
3529 else if (GraphPrintCheckerState->isUndefArg(N))
3530 Out << "\\|Argument in call is undefined";
Ted Kremenek7cf82382009-11-11 20:16:36 +00003531#endif
Mike Stump11289f42009-09-09 15:08:12 +00003532
Ted Kremenek9e08ff42008-12-16 22:02:27 +00003533 break;
3534 }
3535
Ted Kremenekac886cb2008-01-16 21:46:15 +00003536 const BlockEdge& E = cast<BlockEdge>(Loc);
3537 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3538 << E.getDst()->getBlockID() << ')';
Mike Stump11289f42009-09-09 15:08:12 +00003539
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003540 if (const Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump11289f42009-09-09 15:08:12 +00003541
Ted Kremenek6947a792008-03-07 20:57:30 +00003542 SourceLocation SLoc = T->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00003543
Ted Kremeneka50d9852008-01-30 23:03:39 +00003544 Out << "\\|Terminator: ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003545 LangOptions LO; // FIXME.
3546 E.getSrc()->printTerminator(Out, LO);
Mike Stump11289f42009-09-09 15:08:12 +00003547
Ted Kremenek03ab1562008-03-09 03:30:59 +00003548 if (SLoc.isFileID()) {
3549 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00003550 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3551 << " col="
3552 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek03ab1562008-03-09 03:30:59 +00003553 }
Mike Stump11289f42009-09-09 15:08:12 +00003554
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003555 if (isa<SwitchStmt>(T)) {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003556 const Stmt* Label = E.getDst()->getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00003557
3558 if (Label) {
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003559 if (const CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003560 Out << "\\lcase ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003561 LangOptions LO; // FIXME.
3562 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00003563
Zhongxing Xuedb77fe2010-07-20 06:22:24 +00003564 if (const Stmt* RHS = C->getRHS()) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003565 Out << " .. ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003566 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003567 }
Mike Stump11289f42009-09-09 15:08:12 +00003568
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003569 Out << ":";
3570 }
3571 else {
3572 assert (isa<DefaultStmt>(Label));
3573 Out << "\\ldefault:";
3574 }
3575 }
Mike Stump11289f42009-09-09 15:08:12 +00003576 else
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003577 Out << "\\l(implicit) default:";
3578 }
3579 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremeneka50d9852008-01-30 23:03:39 +00003580 // FIXME
3581 }
3582 else {
3583 Out << "\\lCondition: ";
3584 if (*E.getSrc()->succ_begin() == E.getDst())
3585 Out << "true";
3586 else
Mike Stump11289f42009-09-09 15:08:12 +00003587 Out << "false";
Ted Kremeneka50d9852008-01-30 23:03:39 +00003588 }
Mike Stump11289f42009-09-09 15:08:12 +00003589
Ted Kremeneka50d9852008-01-30 23:03:39 +00003590 Out << "\\l";
3591 }
Mike Stump11289f42009-09-09 15:08:12 +00003592
Ted Kremenek7cf82382009-11-11 20:16:36 +00003593#if 0
3594 // FIXME: Replace with a general scheme to determine
3595 // the name of the check.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00003596 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3597 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek2531fce2008-01-30 23:24:39 +00003598 }
Ted Kremenek7cf82382009-11-11 20:16:36 +00003599#endif
Ted Kremenekac886cb2008-01-16 21:46:15 +00003600 }
3601 }
Mike Stump11289f42009-09-09 15:08:12 +00003602
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003603 const GRState *state = N->getState();
Ted Kremenek57859c52010-12-03 06:52:26 +00003604 Out << "\\|StateID: " << (void*) state
3605 << " NodeID: " << (void*) N << "\\|";
Zhongxing Xue7358432010-03-05 04:45:36 +00003606 state->printDOT(Out, *N->getLocationContext()->getCFG());
Ted Kremenek930191c2008-01-23 22:30:44 +00003607 Out << "\\l";
Ted Kremenekac886cb2008-01-16 21:46:15 +00003608 return Out.str();
3609 }
3610};
Mike Stump11289f42009-09-09 15:08:12 +00003611} // end llvm namespace
Ted Kremenekac886cb2008-01-16 21:46:15 +00003612#endif
3613
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003614#ifndef NDEBUG
Ted Kremenek576b76a2008-03-12 17:18:20 +00003615template <typename ITERATOR>
Zhongxing Xu107f7592009-08-06 12:48:26 +00003616ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek576b76a2008-03-12 17:18:20 +00003617
Zhongxing Xu107f7592009-08-06 12:48:26 +00003618template <> ExplodedNode*
3619GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3620 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek576b76a2008-03-12 17:18:20 +00003621 return I->first;
3622}
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003623#endif
3624
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003625void ExprEngine::ViewGraph(bool trim) {
Mike Stump11289f42009-09-09 15:08:12 +00003626#ifndef NDEBUG
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003627 if (trim) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00003628 std::vector<ExplodedNode*> Src;
Ted Kremenek95175052009-03-11 01:41:22 +00003629
3630 // Flush any outstanding reports to make sure we cover all the nodes.
3631 // This does not cause them to get displayed.
3632 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3633 const_cast<BugType*>(*I)->FlushReports(BR);
3634
3635 // Iterate through the reports and get their nodes.
Argyrios Kyrtzidisa1540db2011-02-23 00:16:01 +00003636 for (BugReporter::EQClasses_iterator
3637 EI = BR.EQClasses_begin(), EE = BR.EQClasses_end(); EI != EE; ++EI) {
3638 BugReportEquivClass& EQ = *EI;
3639 const BugReport &R = **EQ.begin();
3640 ExplodedNode *N = const_cast<ExplodedNode*>(R.getErrorNode());
3641 if (N) Src.push_back(N);
Ted Kremenek95175052009-03-11 01:41:22 +00003642 }
Mike Stump11289f42009-09-09 15:08:12 +00003643
Ted Kremenek576b76a2008-03-12 17:18:20 +00003644 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003645 }
Ted Kremeneka7178c72008-03-11 18:25:33 +00003646 else {
3647 GraphPrintCheckerState = this;
3648 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek16306102008-08-13 21:24:49 +00003649
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003650 llvm::ViewGraph(*G.roots_begin(), "ExprEngine");
Mike Stump11289f42009-09-09 15:08:12 +00003651
Ted Kremeneka7178c72008-03-11 18:25:33 +00003652 GraphPrintCheckerState = NULL;
3653 GraphPrintSourceManager = NULL;
3654 }
3655#endif
3656}
3657
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003658void ExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremeneka7178c72008-03-11 18:25:33 +00003659#ifndef NDEBUG
3660 GraphPrintCheckerState = this;
3661 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00003662
Zhongxing Xu107f7592009-08-06 12:48:26 +00003663 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneka7178c72008-03-11 18:25:33 +00003664
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003665 if (!TrimmedG.get())
Benjamin Kramer89b422c2009-08-23 12:08:50 +00003666 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003667 else
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +00003668 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedExprEngine");
Mike Stump11289f42009-09-09 15:08:12 +00003669
Ted Kremenek2531fce2008-01-30 23:24:39 +00003670 GraphPrintCheckerState = NULL;
Ted Kremenek6947a792008-03-07 20:57:30 +00003671 GraphPrintSourceManager = NULL;
Ted Kremenekd3122cb2008-02-14 22:36:46 +00003672#endif
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003673}