blob: b4b69cdcd61e8baf6494bddd59269ccf19aadb6c [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-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 Kremenek50df4f42008-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 Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenekc9058362009-07-22 21:43:51 +000018#include "clang/Analysis/PathSensitive/Checker.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000025#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2ec17292009-08-23 12:08:50 +000027#include "llvm/ADT/ImmutableList.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000028
Ted Kremenek9f6b1612008-02-27 06:07:00 +000029#ifndef NDEBUG
30#include "llvm/Support/GraphWriter.h"
Ted Kremenek9f6b1612008-02-27 06:07:00 +000031#endif
32
Ted Kremenekd4467432008-02-14 22:16:04 +000033using namespace clang;
34using llvm::dyn_cast;
35using llvm::cast;
36using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000037
Ted Kremenekca5f6202008-04-15 23:06:53 +000038//===----------------------------------------------------------------------===//
Ted Kremenekc9058362009-07-22 21:43:51 +000039// Batch auditor. DEPRECATED.
Ted Kremenekca5f6202008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
41
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000042namespace {
43
44class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
45 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
46 typedef llvm::DenseMap<void*,Checks> MapTy;
Mike Stump25cf7602009-09-09 15:08:12 +000047
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000048 MapTy M;
49 Checks::Factory F;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000050 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000051
52public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000053 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
54 F(Alloc), AllStmts(F.GetEmptyList()) {}
Mike Stump25cf7602009-09-09 15:08:12 +000055
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000056 virtual ~MappedBatchAuditor() {
57 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
Mike Stump25cf7602009-09-09 15:08:12 +000058
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000059 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
60 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
61
62 GRSimpleAPICheck* check = *I;
Mike Stump25cf7602009-09-09 15:08:12 +000063
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000064 if (AlreadyVisited.count(check))
65 continue;
Mike Stump25cf7602009-09-09 15:08:12 +000066
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000067 AlreadyVisited.insert(check);
68 delete check;
69 }
70 }
71
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000072 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000073 assert (A && "Check cannot be null.");
74 void* key = reinterpret_cast<void*>((uintptr_t) C);
75 MapTy::iterator I = M.find(key);
76 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
77 }
Mike Stump25cf7602009-09-09 15:08:12 +000078
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000079 void AddCheck(GRSimpleAPICheck *A) {
80 assert (A && "Check cannot be null.");
Mike Stump25cf7602009-09-09 15:08:12 +000081 AllStmts = F.Concat(A, AllStmts);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000082 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000083
Zhongxing Xu0ace2712009-08-06 12:48:26 +000084 virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000085 // First handle the auditors that accept all statements.
86 bool isSink = false;
87 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
88 isSink |= (*I)->Audit(N, VMgr);
Mike Stump25cf7602009-09-09 15:08:12 +000089
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000090 // Next handle the auditors that accept only specific statements.
Ted Kremenekc08c21e2009-07-22 22:35:28 +000091 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000092 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
93 MapTy::iterator MI = M.find(key);
Mike Stump25cf7602009-09-09 15:08:12 +000094 if (MI != M.end()) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000095 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
96 isSink |= (*I)->Audit(N, VMgr);
97 }
Mike Stump25cf7602009-09-09 15:08:12 +000098
99 return isSink;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000100 }
101};
102
103} // end anonymous namespace
104
105//===----------------------------------------------------------------------===//
Ted Kremenekc9058362009-07-22 21:43:51 +0000106// Checker worklist routines.
107//===----------------------------------------------------------------------===//
Mike Stump25cf7602009-09-09 15:08:12 +0000108
109void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst,
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000110 ExplodedNodeSet &Src, bool isPrevisit) {
Mike Stump25cf7602009-09-09 15:08:12 +0000111
Ted Kremenekc9058362009-07-22 21:43:51 +0000112 if (Checkers.empty()) {
113 Dst = Src;
114 return;
115 }
Mike Stump25cf7602009-09-09 15:08:12 +0000116
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000117 ExplodedNodeSet Tmp;
118 ExplodedNodeSet *PrevSet = &Src;
Mike Stump25cf7602009-09-09 15:08:12 +0000119
Ted Kremenekc9058362009-07-22 21:43:51 +0000120 for (std::vector<Checker*>::iterator I = Checkers.begin(), E = Checkers.end();
121 I != E; ++I) {
122
Mike Stump25cf7602009-09-09 15:08:12 +0000123 ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000124 : (PrevSet == &Tmp) ? &Src : &Tmp;
Ted Kremenekc9058362009-07-22 21:43:51 +0000125 CurrSet->clear();
126 Checker *checker = *I;
Mike Stump25cf7602009-09-09 15:08:12 +0000127
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000128 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenekc9058362009-07-22 21:43:51 +0000129 NI != NE; ++NI)
130 checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, isPrevisit);
Mike Stump25cf7602009-09-09 15:08:12 +0000131
Ted Kremenekc9058362009-07-22 21:43:51 +0000132 // Update which NodeSet is the current one.
133 PrevSet = CurrSet;
134 }
135
136 // Don't autotransition. The CheckerContext objects should do this
137 // automatically.
138}
139
140//===----------------------------------------------------------------------===//
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000141// Engine construction and deletion.
142//===----------------------------------------------------------------------===//
143
Ted Kremenek5f20a632008-05-01 18:33:28 +0000144static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
145 IdentifierInfo* II = &Ctx.Idents.get(name);
146 return Ctx.Selectors.getSelector(0, &II);
147}
148
Ted Kremenekf973eb02008-03-09 18:05:48 +0000149
Zhongxing Xub4fa87b2009-08-25 06:51:30 +0000150GRExprEngine::GRExprEngine(AnalysisManager &mgr)
Zhongxing Xu2ac46a52009-08-15 03:17:38 +0000151 : AMgr(mgr),
Mike Stump25cf7602009-09-09 15:08:12 +0000152 CoreEngine(mgr.getASTContext(), *this),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000153 G(CoreEngine.getGraph()),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000154 Builder(NULL),
Mike Stump25cf7602009-09-09 15:08:12 +0000155 StateMgr(G.getContext(), mgr.getStoreManagerCreator(),
Zhongxing Xub4fa87b2009-08-25 06:51:30 +0000156 mgr.getConstraintManagerCreator(), G.getAllocator()),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000157 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekcda58d22009-04-09 16:46:55 +0000158 ValMgr(StateMgr.getValueManager()),
Ted Kremenek40d9c582009-07-16 01:32:00 +0000159 SVator(ValMgr.getSValuator()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000160 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000161 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Mike Stump25cf7602009-09-09 15:08:12 +0000162 RaiseSel(GetNullarySelector("raise", G.getContext())),
Zhongxing Xub4fa87b2009-08-25 06:51:30 +0000163 BR(mgr, *this) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000164
Mike Stump25cf7602009-09-09 15:08:12 +0000165GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000166 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000167 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenekc9058362009-07-22 21:43:51 +0000168 for (std::vector<Checker*>::iterator I=Checkers.begin(), E=Checkers.end();
169 I!=E; ++I)
170 delete *I;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000171}
172
Ted Kremenekca5f6202008-04-15 23:06:53 +0000173//===----------------------------------------------------------------------===//
174// Utility methods.
175//===----------------------------------------------------------------------===//
176
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000177
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000178void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000179 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000180 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000181 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000182}
183
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000184void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
185 if (!BatchAuditor)
186 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
Mike Stump25cf7602009-09-09 15:08:12 +0000187
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000188 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000189}
190
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000191void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
192 if (!BatchAuditor)
193 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
194
195 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
196}
197
Zhongxing Xu43c60b82009-08-17 06:19:58 +0000198const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) {
199 const GRState *state = StateMgr.getInitialState(InitLoc);
Mike Stump25cf7602009-09-09 15:08:12 +0000200
Ted Kremenek04c0add2009-04-10 00:59:50 +0000201 // Precondition: the first argument of 'main' is an integer guaranteed
202 // to be > 0.
203 // FIXME: It would be nice if we had a more general mechanism to add
204 // such preconditions. Some day.
Zhongxing Xudd0ae7d2009-08-21 03:05:36 +0000205 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(InitLoc->getDecl()))
Ted Kremenek04c0add2009-04-10 00:59:50 +0000206 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
207 FD->getNumParams() > 0) {
208 const ParmVarDecl *PD = FD->getParamDecl(0);
209 QualType T = PD->getType();
210 if (T->isIntegerType())
Ted Kremenek6555ff92009-08-21 22:28:32 +0000211 if (const MemRegion *R = state->getRegion(PD, InitLoc)) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000212 SVal V = state->getSVal(loc::MemRegionVal(R));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000213 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek04c0add2009-04-10 00:59:50 +0000214 ValMgr.makeZeroVal(T),
Mike Stump25cf7602009-09-09 15:08:12 +0000215 getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +0000216
217 if (const GRState *newState = state->assume(Constraint, true))
218 state = newState;
Ted Kremenek04c0add2009-04-10 00:59:50 +0000219 }
220 }
Mike Stump25cf7602009-09-09 15:08:12 +0000221
Ted Kremenek04c0add2009-04-10 00:59:50 +0000222 return state;
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000223}
224
Ted Kremenekca5f6202008-04-15 23:06:53 +0000225//===----------------------------------------------------------------------===//
226// Top-level transfer function logic (Dispatcher).
227//===----------------------------------------------------------------------===//
228
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000229void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) {
Mike Stump25cf7602009-09-09 15:08:12 +0000230
Ted Kremenek820c73b2009-03-11 02:41:36 +0000231 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
232 S->getLocStart(),
233 "Error evaluating statement");
Mike Stump25cf7602009-09-09 15:08:12 +0000234
Ted Kremenekca5f6202008-04-15 23:06:53 +0000235 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000236 EntryNode = builder.getLastNode();
Mike Stump25cf7602009-09-09 15:08:12 +0000237
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000238 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000239 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000240 StateMgr.CurrentStmt = S;
Mike Stump25cf7602009-09-09 15:08:12 +0000241
Ted Kremenekca5f6202008-04-15 23:06:53 +0000242 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000243 if (BatchAuditor)
244 Builder->setAuditor(BatchAuditor.get());
Mike Stump25cf7602009-09-09 15:08:12 +0000245
246 // Create the cleaned state.
247 SymbolReaper SymReaper(*AMgr.getLiveVariables(), SymMgr);
Zhongxing Xu998cd552009-08-27 06:55:26 +0000248 CleanedState = AMgr.shouldPurgeDead()
249 ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
250 : EntryNode->getState();
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000251
Ted Kremenek7487f942008-04-24 18:31:42 +0000252 // Process any special transfer function for dead symbols.
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000253 ExplodedNodeSet Tmp;
Mike Stump25cf7602009-09-09 15:08:12 +0000254
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000255 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000256 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000257 else {
258 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000259 SaveOr OldHasGen(Builder->HasGeneratedNode);
260
Ted Kremenekf05eec42008-06-18 05:34:07 +0000261 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
262 Builder->PurgingDeadSymbols = true;
Mike Stump25cf7602009-09-09 15:08:12 +0000263
264 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000265 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000266
267 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
268 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000269 }
Mike Stump25cf7602009-09-09 15:08:12 +0000270
Ted Kremenekfa7be362008-04-24 23:35:58 +0000271 bool HasAutoGenerated = false;
272
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000273 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000274
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000275 ExplodedNodeSet Dst;
Mike Stump25cf7602009-09-09 15:08:12 +0000276
277 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000278 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
Mike Stump25cf7602009-09-09 15:08:12 +0000279
280 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000281 Visit(S, *I, Dst);
282
283 // Do we need to auto-generate a node? We only need to do this to generate
284 // a node with a "cleaned" state; GRCoreEngine will actually handle
Mike Stump25cf7602009-09-09 15:08:12 +0000285 // auto-transitions for other cases.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000286 if (Dst.size() == 1 && *Dst.begin() == EntryNode
287 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
288 HasAutoGenerated = true;
289 builder.generateNode(S, GetState(EntryNode), *I);
290 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000291 }
Mike Stump25cf7602009-09-09 15:08:12 +0000292
Ted Kremenekca5f6202008-04-15 23:06:53 +0000293 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000294 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000295 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000296
297 // FIXME: Consolidate.
298 StateMgr.CurrentStmt = 0;
299 CurrentStmt = 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000300
Ted Kremenekfa7be362008-04-24 23:35:58 +0000301 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000302}
303
Mike Stump25cf7602009-09-09 15:08:12 +0000304void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000305 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
306 S->getLocStart(),
307 "Error evaluating statement");
308
Ted Kremenekca5f6202008-04-15 23:06:53 +0000309 // FIXME: add metadata to the CFG so that we can disable
310 // this check when we KNOW that there is no block-level subexpression.
311 // The motivation is that this check requires a hashtable lookup.
Mike Stump25cf7602009-09-09 15:08:12 +0000312
Zhongxing Xudd5c0332009-08-25 03:33:41 +0000313 if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000314 Dst.Add(Pred);
315 return;
316 }
Mike Stump25cf7602009-09-09 15:08:12 +0000317
Ted Kremenekca5f6202008-04-15 23:06:53 +0000318 switch (S->getStmtClass()) {
Mike Stump25cf7602009-09-09 15:08:12 +0000319
Ted Kremenekca5f6202008-04-15 23:06:53 +0000320 default:
321 // Cases we intentionally have "default" handle:
322 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Mike Stump25cf7602009-09-09 15:08:12 +0000323
Ted Kremenekca5f6202008-04-15 23:06:53 +0000324 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
325 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000326
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000327 case Stmt::ArraySubscriptExprClass:
328 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
329 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000330
Ted Kremenekca5f6202008-04-15 23:06:53 +0000331 case Stmt::AsmStmtClass:
332 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
333 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000334
Ted Kremenekca5f6202008-04-15 23:06:53 +0000335 case Stmt::BinaryOperatorClass: {
336 BinaryOperator* B = cast<BinaryOperator>(S);
Mike Stump25cf7602009-09-09 15:08:12 +0000337
Ted Kremenekca5f6202008-04-15 23:06:53 +0000338 if (B->isLogicalOp()) {
339 VisitLogicalExpr(B, Pred, Dst);
340 break;
341 }
342 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000343 const GRState* state = GetState(Pred);
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000344 MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000345 break;
346 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000347
Zhongxing Xub4fa87b2009-08-25 06:51:30 +0000348 if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000349 ExplodedNodeSet Tmp;
Ted Kremenek8f520972009-02-25 22:32:02 +0000350 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +0000351 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000352 }
353 else
354 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
355
Ted Kremenekca5f6202008-04-15 23:06:53 +0000356 break;
357 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000358
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000359 case Stmt::CallExprClass:
360 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000361 CallExpr* C = cast<CallExpr>(S);
362 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000363 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000364 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000365
Ted Kremenekca5f6202008-04-15 23:06:53 +0000366 // FIXME: ChooseExpr is really a constant. We need to fix
367 // the CFG do not model them as explicit control-flow.
Mike Stump25cf7602009-09-09 15:08:12 +0000368
Ted Kremenekca5f6202008-04-15 23:06:53 +0000369 case Stmt::ChooseExprClass: { // __builtin_choose_expr
370 ChooseExpr* C = cast<ChooseExpr>(S);
371 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
372 break;
373 }
Mike Stump25cf7602009-09-09 15:08:12 +0000374
Ted Kremenekca5f6202008-04-15 23:06:53 +0000375 case Stmt::CompoundAssignOperatorClass:
376 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
377 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000378
379 case Stmt::CompoundLiteralExprClass:
380 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
381 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000382
Ted Kremenekca5f6202008-04-15 23:06:53 +0000383 case Stmt::ConditionalOperatorClass: { // '?' operator
384 ConditionalOperator* C = cast<ConditionalOperator>(S);
385 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
386 break;
387 }
Mike Stump25cf7602009-09-09 15:08:12 +0000388
Ted Kremenekca5f6202008-04-15 23:06:53 +0000389 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000390 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000391 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000392 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000393
Ted Kremenekca5f6202008-04-15 23:06:53 +0000394 case Stmt::DeclStmtClass:
395 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
396 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000397
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000398 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000399 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000400 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000401 VisitCast(C, C->getSubExpr(), Pred, Dst);
402 break;
403 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000404
405 case Stmt::InitListExprClass:
406 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
407 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000408
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000409 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000410 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
411 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000412
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000413 case Stmt::ObjCIvarRefExprClass:
414 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
415 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000416
417 case Stmt::ObjCForCollectionStmtClass:
418 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
419 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000420
Ted Kremenekca5f6202008-04-15 23:06:53 +0000421 case Stmt::ObjCMessageExprClass: {
422 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
423 break;
424 }
Mike Stump25cf7602009-09-09 15:08:12 +0000425
Ted Kremenek3c186252008-12-09 20:18:58 +0000426 case Stmt::ObjCAtThrowStmtClass: {
427 // FIXME: This is not complete. We basically treat @throw as
428 // an abort.
429 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
430 Builder->BuildSinks = true;
431 MakeNode(Dst, S, Pred, GetState(Pred));
432 break;
433 }
Mike Stump25cf7602009-09-09 15:08:12 +0000434
Ted Kremenekca5f6202008-04-15 23:06:53 +0000435 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000436 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000437 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000438
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000439 case Stmt::ReturnStmtClass:
440 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
441 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000442
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000443 case Stmt::SizeOfAlignOfExprClass:
444 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000445 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000446
Ted Kremenekca5f6202008-04-15 23:06:53 +0000447 case Stmt::StmtExprClass: {
448 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000449
450 if (SE->getSubStmt()->body_empty()) {
451 // Empty statement expression.
452 assert(SE->getType() == getContext().VoidTy
453 && "Empty statement expression must have void type.");
454 Dst.Add(Pred);
455 break;
456 }
Mike Stump25cf7602009-09-09 15:08:12 +0000457
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000458 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
459 const GRState* state = GetState(Pred);
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000460 MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000461 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000462 else
463 Dst.Add(Pred);
Mike Stump25cf7602009-09-09 15:08:12 +0000464
Ted Kremenekca5f6202008-04-15 23:06:53 +0000465 break;
466 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000467
468 case Stmt::StringLiteralClass:
469 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
470 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000471
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000472 case Stmt::UnaryOperatorClass: {
473 UnaryOperator *U = cast<UnaryOperator>(S);
Zhongxing Xub4fa87b2009-08-25 06:51:30 +0000474 if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000475 ExplodedNodeSet Tmp;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000476 VisitUnaryOperator(U, Pred, Tmp, false);
477 EvalEagerlyAssume(Dst, Tmp, U);
478 }
479 else
480 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000481 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000482 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000483 }
484}
485
Mike Stump25cf7602009-09-09 15:08:12 +0000486void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred,
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000487 ExplodedNodeSet& Dst) {
Mike Stump25cf7602009-09-09 15:08:12 +0000488
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000489 Ex = Ex->IgnoreParens();
Mike Stump25cf7602009-09-09 15:08:12 +0000490
Zhongxing Xudd5c0332009-08-25 03:33:41 +0000491 if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000492 Dst.Add(Pred);
493 return;
494 }
Mike Stump25cf7602009-09-09 15:08:12 +0000495
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000496 switch (Ex->getStmtClass()) {
Mike Stump25cf7602009-09-09 15:08:12 +0000497
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000498 case Stmt::ArraySubscriptExprClass:
499 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
500 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000501
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000502 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000503 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000504 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
505 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000506
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000507 case Stmt::ObjCIvarRefExprClass:
508 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
509 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000510
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000511 case Stmt::UnaryOperatorClass:
512 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
513 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000514
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000515 case Stmt::MemberExprClass:
516 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
517 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000518
Ted Kremenekd83daa52008-10-27 21:54:31 +0000519 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000520 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000521 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000522
Ted Kremenek71c707b2008-10-17 17:24:14 +0000523 case Stmt::ObjCPropertyRefExprClass:
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000524 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Ted Kremenek71c707b2008-10-17 17:24:14 +0000525 // FIXME: Property assignments are lvalues, but not really "locations".
526 // e.g.: self.x = something;
527 // Here the "self.x" really can translate to a method call (setter) when
528 // the assignment is made. Moreover, the entire assignment expression
529 // evaluate to whatever "something" is, not calling the "getter" for
530 // the property (which would make sense since it can have side effects).
531 // We'll probably treat this as a location, but not one that we can
532 // take the address of. Perhaps we need a new SVal class for cases
533 // like thsis?
534 // Note that we have a similar problem for bitfields, since they don't
535 // have "locations" in the sense that we can take their address.
536 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000537 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000538
539 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000540 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000541 SVal V = state->getLValue(cast<StringLiteral>(Ex));
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000542 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000543 return;
544 }
Mike Stump25cf7602009-09-09 15:08:12 +0000545
Ted Kremenek2c829a32008-10-18 04:15:35 +0000546 default:
547 // Arbitrary subexpressions can return aggregate temporaries that
548 // can be used in a lvalue context. We need to enhance our support
549 // of such temporaries in both the environment and the store, so right
550 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000551 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000552 "Other kinds of expressions with non-aggregate/union types do"
553 " not have lvalues.");
Mike Stump25cf7602009-09-09 15:08:12 +0000554
Ted Kremenek2c829a32008-10-18 04:15:35 +0000555 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000556 }
557}
558
559//===----------------------------------------------------------------------===//
560// Block entrance. (Update counters).
561//===----------------------------------------------------------------------===//
562
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000563bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000564 GRBlockCounter BC) {
Mike Stump25cf7602009-09-09 15:08:12 +0000565
Ted Kremenekca5f6202008-04-15 23:06:53 +0000566 return BC.getNumVisited(B->getBlockID()) < 3;
567}
568
569//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000570// Generic node creation.
571//===----------------------------------------------------------------------===//
572
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000573ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
574 ExplodedNode* Pred, const GRState* St,
575 ProgramPoint::Kind K, const void *tag) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000576 assert (Builder && "GRStmtNodeBuilder not present.");
577 SaveAndRestore<const void*> OldTag(Builder->Tag);
578 Builder->Tag = tag;
579 return Builder->MakeNode(Dst, S, Pred, St, K);
580}
581
582//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000583// Branch processing.
584//===----------------------------------------------------------------------===//
585
Ted Kremeneke66ba682009-02-13 01:45:31 +0000586const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000587 Stmt* Terminator,
588 bool branchTaken) {
Mike Stump25cf7602009-09-09 15:08:12 +0000589
Ted Kremenek99ecce72008-02-26 19:05:15 +0000590 switch (Terminator->getStmtClass()) {
591 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000592 return state;
Mike Stump25cf7602009-09-09 15:08:12 +0000593
Ted Kremenek99ecce72008-02-26 19:05:15 +0000594 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump25cf7602009-09-09 15:08:12 +0000595
Ted Kremenek99ecce72008-02-26 19:05:15 +0000596 BinaryOperator* B = cast<BinaryOperator>(Terminator);
597 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump25cf7602009-09-09 15:08:12 +0000598
Ted Kremenek99ecce72008-02-26 19:05:15 +0000599 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
Mike Stump25cf7602009-09-09 15:08:12 +0000600
Ted Kremenek99ecce72008-02-26 19:05:15 +0000601 // For &&, if we take the true branch, then the value of the whole
602 // expression is that of the RHS expression.
603 //
604 // For ||, if we take the false branch, then the value of the whole
605 // expression is that of the RHS expression.
Mike Stump25cf7602009-09-09 15:08:12 +0000606
Ted Kremenek99ecce72008-02-26 19:05:15 +0000607 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
Mike Stump25cf7602009-09-09 15:08:12 +0000608 (Op == BinaryOperator::LOr && !branchTaken)
Ted Kremenek99ecce72008-02-26 19:05:15 +0000609 ? B->getRHS() : B->getLHS();
Mike Stump25cf7602009-09-09 15:08:12 +0000610
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000611 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000612 }
Mike Stump25cf7602009-09-09 15:08:12 +0000613
Ted Kremenek99ecce72008-02-26 19:05:15 +0000614 case Stmt::ConditionalOperatorClass: { // ?:
Mike Stump25cf7602009-09-09 15:08:12 +0000615
Ted Kremenek99ecce72008-02-26 19:05:15 +0000616 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Mike Stump25cf7602009-09-09 15:08:12 +0000617
Ted Kremenek99ecce72008-02-26 19:05:15 +0000618 // For ?, if branchTaken == true then the value is either the LHS or
619 // the condition itself. (GNU extension).
Mike Stump25cf7602009-09-09 15:08:12 +0000620
621 Expr* Ex;
622
Ted Kremenek99ecce72008-02-26 19:05:15 +0000623 if (branchTaken)
Mike Stump25cf7602009-09-09 15:08:12 +0000624 Ex = C->getLHS() ? C->getLHS() : C->getCond();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000625 else
626 Ex = C->getRHS();
Mike Stump25cf7602009-09-09 15:08:12 +0000627
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000628 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000629 }
Mike Stump25cf7602009-09-09 15:08:12 +0000630
Ted Kremenek99ecce72008-02-26 19:05:15 +0000631 case Stmt::ChooseExprClass: { // ?:
Mike Stump25cf7602009-09-09 15:08:12 +0000632
Ted Kremenek99ecce72008-02-26 19:05:15 +0000633 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump25cf7602009-09-09 15:08:12 +0000634
635 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000636 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000637 }
638 }
639}
640
Ted Kremenekc39c2172009-03-13 16:32:54 +0000641/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
642/// to try to recover some path-sensitivity for casts of symbolic
643/// integers that promote their values (which are currently not tracked well).
644/// This function returns the SVal bound to Condition->IgnoreCasts if all the
645// cast(s) did was sign-extend the original value.
646static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
647 Stmt* Condition, ASTContext& Ctx) {
648
649 Expr *Ex = dyn_cast<Expr>(Condition);
650 if (!Ex)
651 return UnknownVal();
652
653 uint64_t bits = 0;
654 bool bitsInit = false;
Mike Stump25cf7602009-09-09 15:08:12 +0000655
Ted Kremenekc39c2172009-03-13 16:32:54 +0000656 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
657 QualType T = CE->getType();
658
659 if (!T->isIntegerType())
660 return UnknownVal();
Mike Stump25cf7602009-09-09 15:08:12 +0000661
Ted Kremenekc39c2172009-03-13 16:32:54 +0000662 uint64_t newBits = Ctx.getTypeSize(T);
663 if (!bitsInit || newBits < bits) {
664 bitsInit = true;
665 bits = newBits;
666 }
Mike Stump25cf7602009-09-09 15:08:12 +0000667
Ted Kremenekc39c2172009-03-13 16:32:54 +0000668 Ex = CE->getSubExpr();
669 }
670
671 // We reached a non-cast. Is it a symbolic value?
672 QualType T = Ex->getType();
673
674 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
675 return UnknownVal();
Mike Stump25cf7602009-09-09 15:08:12 +0000676
Ted Kremenekc0cccca2009-06-18 23:58:37 +0000677 return state->getSVal(Ex);
Ted Kremenekc39c2172009-03-13 16:32:54 +0000678}
679
Ted Kremenek13e167f2008-11-12 19:24:17 +0000680void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000681 GRBranchNodeBuilder& builder) {
Mike Stump25cf7602009-09-09 15:08:12 +0000682
Ted Kremenek022b6052008-02-15 22:29:00 +0000683 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump25cf7602009-09-09 15:08:12 +0000684 if (!Condition) {
Ted Kremenek022b6052008-02-15 22:29:00 +0000685 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000686 return;
687 }
Mike Stump25cf7602009-09-09 15:08:12 +0000688
Ted Kremeneke43de222009-03-11 03:54:24 +0000689 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
690 Condition->getLocStart(),
691 "Error evaluating branch");
Ted Kremenek5b76b832009-08-27 01:39:13 +0000692
Mike Stump25cf7602009-09-09 15:08:12 +0000693 const GRState* PrevState = builder.getState();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000694 SVal V = PrevState->getSVal(Condition);
Mike Stump25cf7602009-09-09 15:08:12 +0000695
Ted Kremenek90960972008-01-30 23:03:39 +0000696 switch (V.getBaseKind()) {
697 default:
698 break;
699
Ted Kremenekc39c2172009-03-13 16:32:54 +0000700 case SVal::UnknownKind: {
701 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
702 if (Ex->getType()->isIntegerType()) {
703 // Try to recover some path-sensitivity. Right now casts of symbolic
704 // integers that promote their values are currently not tracked well.
705 // If 'Condition' is such an expression, try and recover the
706 // underlying value and use that instead.
707 SVal recovered = RecoverCastedSymbol(getStateManager(),
708 builder.getState(), Condition,
709 getContext());
Mike Stump25cf7602009-09-09 15:08:12 +0000710
Ted Kremenekc39c2172009-03-13 16:32:54 +0000711 if (!recovered.isUnknown()) {
712 V = recovered;
713 break;
714 }
715 }
716 }
Mike Stump25cf7602009-09-09 15:08:12 +0000717
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000718 builder.generateNode(MarkBranch(PrevState, Term, true), true);
719 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000720 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000721 }
Mike Stump25cf7602009-09-09 15:08:12 +0000722
723 case SVal::UndefinedKind: {
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000724 ExplodedNode* N = builder.generateNode(PrevState, true);
Ted Kremenek90960972008-01-30 23:03:39 +0000725
726 if (N) {
727 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000728 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000729 }
Mike Stump25cf7602009-09-09 15:08:12 +0000730
Ted Kremenek90960972008-01-30 23:03:39 +0000731 builder.markInfeasible(false);
732 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000733 }
Ted Kremenek90960972008-01-30 23:03:39 +0000734 }
Mike Stump25cf7602009-09-09 15:08:12 +0000735
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000736 // Process the true branch.
Ted Kremenek5f302192009-07-20 18:44:36 +0000737 if (builder.isFeasible(true)) {
738 if (const GRState *state = PrevState->assume(V, true))
739 builder.generateNode(MarkBranch(state, Term, true), true);
740 else
741 builder.markInfeasible(true);
742 }
Mike Stump25cf7602009-09-09 15:08:12 +0000743
744 // Process the false branch.
Ted Kremenek5f302192009-07-20 18:44:36 +0000745 if (builder.isFeasible(false)) {
746 if (const GRState *state = PrevState->assume(V, false))
747 builder.generateNode(MarkBranch(state, Term, false), false);
748 else
749 builder.markInfeasible(false);
750 }
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000751}
752
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000753/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000754/// nodes by processing the 'effects' of a computed goto jump.
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000755void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000756
Mike Stump25cf7602009-09-09 15:08:12 +0000757 const GRState *state = builder.getState();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000758 SVal V = state->getSVal(builder.getTarget());
Mike Stump25cf7602009-09-09 15:08:12 +0000759
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000760 // Three possibilities:
761 //
762 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000763 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000764 // (3) We have no clue about the label. Dispatch to all targets.
765 //
Mike Stump25cf7602009-09-09 15:08:12 +0000766
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000767 typedef GRIndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000768
Zhongxing Xu097fc982008-10-17 05:57:07 +0000769 if (isa<loc::GotoLabel>(V)) {
770 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump25cf7602009-09-09 15:08:12 +0000771
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000772 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000773 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000774 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000775 return;
776 }
777 }
Mike Stump25cf7602009-09-09 15:08:12 +0000778
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000779 assert (false && "No block with label.");
780 return;
781 }
782
Zhongxing Xu097fc982008-10-17 05:57:07 +0000783 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000784 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000785 ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000786 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000787 return;
788 }
Mike Stump25cf7602009-09-09 15:08:12 +0000789
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000790 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekcdc3a3c2009-04-23 17:49:43 +0000791 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump25cf7602009-09-09 15:08:12 +0000792
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000793 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000794 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000795}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000796
Ted Kremenekca5f6202008-04-15 23:06:53 +0000797
798void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000799 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump25cf7602009-09-09 15:08:12 +0000800
Zhongxing Xudd5c0332009-08-25 03:33:41 +0000801 assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump25cf7602009-09-09 15:08:12 +0000802
Ted Kremeneke66ba682009-02-13 01:45:31 +0000803 const GRState* state = GetState(Pred);
Ted Kremenek5b76b832009-08-27 01:39:13 +0000804 SVal X = state->getSVal(Ex);
Mike Stump25cf7602009-09-09 15:08:12 +0000805
Ted Kremenekca5f6202008-04-15 23:06:53 +0000806 assert (X.isUndef());
Mike Stump25cf7602009-09-09 15:08:12 +0000807
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000808 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump25cf7602009-09-09 15:08:12 +0000809 assert(SE);
Ted Kremenek5b76b832009-08-27 01:39:13 +0000810 X = state->getSVal(SE);
Mike Stump25cf7602009-09-09 15:08:12 +0000811
Ted Kremenekca5f6202008-04-15 23:06:53 +0000812 // Make sure that we invalidate the previous binding.
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000813 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000814}
815
Ted Kremenekaee121c2008-02-13 23:08:21 +0000816/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
817/// nodes by processing the 'effects' of a switch statement.
Mike Stump25cf7602009-09-09 15:08:12 +0000818void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
819 typedef GRSwitchNodeBuilder::iterator iterator;
820 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000821 Expr* CondE = builder.getCondition();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000822 SVal CondV = state->getSVal(CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000823
Ted Kremenekb31af242008-02-28 09:25:22 +0000824 if (CondV.isUndef()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000825 ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000826 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000827 return;
828 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000829
Mike Stump25cf7602009-09-09 15:08:12 +0000830 const GRState* DefaultSt = state;
Ted Kremenek70970bf2009-06-18 22:57:13 +0000831 bool defaultIsFeasible = false;
Mike Stump25cf7602009-09-09 15:08:12 +0000832
Ted Kremenek07baa252008-02-21 18:02:17 +0000833 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000834 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000835
836 // Evaluate the LHS of the case value.
837 Expr::EvalResult V1;
Mike Stump25cf7602009-09-09 15:08:12 +0000838 bool b = Case->getLHS()->Evaluate(V1, getContext());
839
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000840 // Sanity checks. These go away in Release builds.
Mike Stump25cf7602009-09-09 15:08:12 +0000841 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000842 && "Case condition must evaluate to an integer constant.");
Mike Stump25cf7602009-09-09 15:08:12 +0000843 b = b; // silence unused variable warning
844 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000845 getContext().getTypeSize(CondE->getType()));
Mike Stump25cf7602009-09-09 15:08:12 +0000846
Ted Kremenekaee121c2008-02-13 23:08:21 +0000847 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000848 Expr::EvalResult V2;
Mike Stump25cf7602009-09-09 15:08:12 +0000849
Ted Kremenekaee121c2008-02-13 23:08:21 +0000850 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000851 b = E->Evaluate(V2, getContext());
Mike Stump25cf7602009-09-09 15:08:12 +0000852 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000853 && "Case condition must evaluate to an integer constant.");
854 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000855 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000856 else
857 V2 = V1;
Mike Stump25cf7602009-09-09 15:08:12 +0000858
Ted Kremenekaee121c2008-02-13 23:08:21 +0000859 // FIXME: Eventually we should replace the logic below with a range
860 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000861 // This should be easy once we have "ranges" for NonLVals.
Mike Stump25cf7602009-09-09 15:08:12 +0000862
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000863 do {
Mike Stump25cf7602009-09-09 15:08:12 +0000864 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000865 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremenek74556a12009-03-26 03:35:11 +0000866 getContext().IntTy);
Mike Stump25cf7602009-09-09 15:08:12 +0000867
868 // Now "assume" that the case matches.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000869 if (const GRState* stateNew = state->assume(Res, true)) {
870 builder.generateCaseStmtNode(I, stateNew);
Mike Stump25cf7602009-09-09 15:08:12 +0000871
Ted Kremenekaee121c2008-02-13 23:08:21 +0000872 // If CondV evaluates to a constant, then we know that this
873 // is the *only* case that we can take, so stop evaluating the
874 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000875 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000876 return;
877 }
Mike Stump25cf7602009-09-09 15:08:12 +0000878
Ted Kremenekaee121c2008-02-13 23:08:21 +0000879 // Now "assume" that the case doesn't match. Add this state
880 // to the default state (if it is feasible).
Ted Kremenek70970bf2009-06-18 22:57:13 +0000881 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
882 defaultIsFeasible = true;
883 DefaultSt = stateNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000884 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000885
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000886 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000887 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000888 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000889
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000890 ++V1.Val.getInt();
891 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump25cf7602009-09-09 15:08:12 +0000892
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000893 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000894 }
Mike Stump25cf7602009-09-09 15:08:12 +0000895
Ted Kremenekaee121c2008-02-13 23:08:21 +0000896 // If we reach here, than we know that the default branch is
Mike Stump25cf7602009-09-09 15:08:12 +0000897 // possible.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000898 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000899}
900
Ted Kremenekca5f6202008-04-15 23:06:53 +0000901//===----------------------------------------------------------------------===//
902// Transfer functions: logical operations ('&&', '||').
903//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000904
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000905void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
906 ExplodedNodeSet& Dst) {
Mike Stump25cf7602009-09-09 15:08:12 +0000907
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000908 assert(B->getOpcode() == BinaryOperator::LAnd ||
909 B->getOpcode() == BinaryOperator::LOr);
Mike Stump25cf7602009-09-09 15:08:12 +0000910
Zhongxing Xudd5c0332009-08-25 03:33:41 +0000911 assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump25cf7602009-09-09 15:08:12 +0000912
Ted Kremeneke66ba682009-02-13 01:45:31 +0000913 const GRState* state = GetState(Pred);
Ted Kremenek5b76b832009-08-27 01:39:13 +0000914 SVal X = state->getSVal(B);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000915 assert(X.isUndef());
Mike Stump25cf7602009-09-09 15:08:12 +0000916
Ted Kremenekb31af242008-02-28 09:25:22 +0000917 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump25cf7602009-09-09 15:08:12 +0000918
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000919 assert(Ex);
Mike Stump25cf7602009-09-09 15:08:12 +0000920
Ted Kremenek99ecce72008-02-26 19:05:15 +0000921 if (Ex == B->getRHS()) {
Mike Stump25cf7602009-09-09 15:08:12 +0000922
Ted Kremenek5b76b832009-08-27 01:39:13 +0000923 X = state->getSVal(Ex);
Mike Stump25cf7602009-09-09 15:08:12 +0000924
Ted Kremenekb31af242008-02-28 09:25:22 +0000925 // Handle undefined values.
Mike Stump25cf7602009-09-09 15:08:12 +0000926
Ted Kremenekb31af242008-02-28 09:25:22 +0000927 if (X.isUndef()) {
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000928 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000929 return;
930 }
Mike Stump25cf7602009-09-09 15:08:12 +0000931
Ted Kremenek99ecce72008-02-26 19:05:15 +0000932 // We took the RHS. Because the value of the '&&' or '||' expression must
933 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
934 // or 1. Alternatively, we could take a lazy approach, and calculate this
935 // value later when necessary. We don't have the machinery in place for
936 // this right now, and since most logical expressions are used for branches,
Mike Stump25cf7602009-09-09 15:08:12 +0000937 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000938 if (const GRState *newState = state->assume(X, true))
Mike Stump25cf7602009-09-09 15:08:12 +0000939 MakeNode(Dst, B, Pred,
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000940 newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Mike Stump25cf7602009-09-09 15:08:12 +0000941
Ted Kremenek70970bf2009-06-18 22:57:13 +0000942 if (const GRState *newState = state->assume(X, false))
Mike Stump25cf7602009-09-09 15:08:12 +0000943 MakeNode(Dst, B, Pred,
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000944 newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000945 }
946 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000947 // We took the LHS expression. Depending on whether we are '&&' or
948 // '||' we know what the value of the expression is via properties of
949 // the short-circuiting.
Mike Stump25cf7602009-09-09 15:08:12 +0000950 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
Zhongxing Xue32c7652009-06-23 09:02:15 +0000951 B->getType());
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000952 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000953 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000954}
Mike Stump25cf7602009-09-09 15:08:12 +0000955
Ted Kremenekca5f6202008-04-15 23:06:53 +0000956//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000957// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000958//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000959
Mike Stump25cf7602009-09-09 15:08:12 +0000960void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
Ted Kremenek6555ff92009-08-21 22:28:32 +0000961 ExplodedNodeSet &Dst, bool asLValue) {
Mike Stump25cf7602009-09-09 15:08:12 +0000962
Ted Kremeneke66ba682009-02-13 01:45:31 +0000963 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000964
Douglas Gregord2baafd2008-10-21 16:13:35 +0000965 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000966
967 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
968
Ted Kremenek6555ff92009-08-21 22:28:32 +0000969 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xude186ae2008-10-17 02:20:14 +0000970
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000971 if (asLValue)
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000972 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000973 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000974 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000975 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000976 return;
977
978 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
979 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
980
Zhongxing Xu3c2828d2009-06-23 06:13:19 +0000981 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000982 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000983 return;
984
985 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000986 assert(asLValue);
Zhongxing Xucac107a2009-04-20 05:24:46 +0000987 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremeneka7b9c662009-08-27 22:17:37 +0000988 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000989 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000990 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000991 }
Mike Stump25cf7602009-09-09 15:08:12 +0000992
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000993 assert (false &&
994 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000995}
996
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000997/// VisitArraySubscriptExpr - Transfer function for array accesses
Mike Stump25cf7602009-09-09 15:08:12 +0000998void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
Zhongxing Xu0ace2712009-08-06 12:48:26 +0000999 ExplodedNode* Pred,
1000 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump25cf7602009-09-09 15:08:12 +00001001
Ted Kremenekbb7c1562008-04-22 04:56:29 +00001002 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +00001003 Expr* Idx = A->getIdx()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001004 ExplodedNodeSet Tmp;
Mike Stump25cf7602009-09-09 15:08:12 +00001005
Ted Kremenekbe9fe042009-02-24 02:23:11 +00001006 if (Base->getType()->isVectorType()) {
1007 // For vector types get its lvalue.
1008 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1009 // In fact, I think this is just a hack. We need to get the right
1010 // semantics.
1011 VisitLValue(Base, Pred, Tmp);
1012 }
Mike Stump25cf7602009-09-09 15:08:12 +00001013 else
Ted Kremenekbe9fe042009-02-24 02:23:11 +00001014 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Mike Stump25cf7602009-09-09 15:08:12 +00001015
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001016 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1017 ExplodedNodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001018 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Mike Stump25cf7602009-09-09 15:08:12 +00001019
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001020 for (ExplodedNodeSet::iterator I2=Tmp2.begin(),E2=Tmp2.end();I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001021 const GRState* state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001022 SVal V = state->getLValue(A->getType(), state->getSVal(Base),
1023 state->getSVal(Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +00001024
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001025 if (asLValue)
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001026 MakeNode(Dst, A, *I2, state->BindExpr(A, V),
Ted Kremenek0441f112009-05-07 18:27:16 +00001027 ProgramPoint::PostLValueKind);
Ted Kremenekc4385b42008-04-29 23:24:44 +00001028 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001029 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +00001030 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001031 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +00001032}
1033
Ted Kremenekd0d86202008-04-21 23:43:38 +00001034/// VisitMemberExpr - Transfer function for member expressions.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001035void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
1036 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump25cf7602009-09-09 15:08:12 +00001037
Ted Kremenekd0d86202008-04-21 23:43:38 +00001038 Expr* Base = M->getBase()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001039 ExplodedNodeSet Tmp;
Mike Stump25cf7602009-09-09 15:08:12 +00001040
1041 if (M->isArrow())
Ted Kremenek66f07b12008-10-18 03:28:48 +00001042 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1043 else
1044 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
Mike Stump25cf7602009-09-09 15:08:12 +00001045
Douglas Gregor82d44772008-12-20 23:49:58 +00001046 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1047 if (!Field) // FIXME: skipping member expressions for non-fields
1048 return;
1049
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001050 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001051 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001052 // FIXME: Should we insert some assumption logic in here to determine
1053 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001054 // later when using FieldOffset lvals (which we no longer have).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001055 SVal L = state->getLValue(state->getSVal(Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001056
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001057 if (asLValue)
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001058 MakeNode(Dst, M, *I, state->BindExpr(M, L),
Ted Kremenek0441f112009-05-07 18:27:16 +00001059 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001060 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001061 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001062 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001063}
1064
Ted Kremeneke66ba682009-02-13 01:45:31 +00001065/// EvalBind - Handle the semantics of binding a value to a specific location.
1066/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001067void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek6cd31072009-07-21 21:03:30 +00001068 const GRState* state, SVal location, SVal Val) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001069
Ted Kremeneka42be302009-02-14 01:43:44 +00001070 const GRState* newState = 0;
Mike Stump25cf7602009-09-09 15:08:12 +00001071
Ted Kremeneka42be302009-02-14 01:43:44 +00001072 if (location.isUnknown()) {
1073 // We know that the new state will be the same as the old state since
1074 // the location of the binding is "unknown". Consequently, there
1075 // is no reason to just create a new node.
1076 newState = state;
1077 }
1078 else {
1079 // We are binding to a value other than 'unknown'. Perform the binding
1080 // using the StoreManager.
Ted Kremenek6cd31072009-07-21 21:03:30 +00001081 newState = state->bindLoc(cast<Loc>(location), Val);
Ted Kremeneka42be302009-02-14 01:43:44 +00001082 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001083
Ted Kremeneka42be302009-02-14 01:43:44 +00001084 // The next thing to do is check if the GRTransferFuncs object wants to
1085 // update the state based on the new binding. If the GRTransferFunc object
1086 // doesn't do anything, just auto-propagate the current state.
1087 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1088 newState != state);
Mike Stump25cf7602009-09-09 15:08:12 +00001089
Ted Kremeneka42be302009-02-14 01:43:44 +00001090 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001091}
1092
1093/// EvalStore - Handle the semantics of a store via an assignment.
1094/// @param Dst The node set to store generated state nodes
1095/// @param Ex The expression representing the location of the store
1096/// @param state The current simulation state
1097/// @param location The location to store the value
1098/// @param Val The value to be stored
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001099void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001100 const GRState* state, SVal location, SVal Val,
1101 const void *tag) {
Mike Stump25cf7602009-09-09 15:08:12 +00001102
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001103 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump25cf7602009-09-09 15:08:12 +00001104
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001105 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001106 Pred = EvalLocation(Ex, Pred, state, location, tag);
Mike Stump25cf7602009-09-09 15:08:12 +00001107
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001108 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001109 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001110
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001111 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001112 state = GetState(Pred);
1113
Mike Stump25cf7602009-09-09 15:08:12 +00001114 // Proceed with the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001115 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001116 SaveAndRestore<const void*> OldTag(Builder->Tag);
1117 Builder->PointKind = ProgramPoint::PostStoreKind;
1118 Builder->Tag = tag;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001119 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001120}
1121
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001122void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001123 const GRState* state, SVal location,
1124 const void *tag) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001125
Mike Stump25cf7602009-09-09 15:08:12 +00001126 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001127 Pred = EvalLocation(Ex, Pred, state, location, tag);
Mike Stump25cf7602009-09-09 15:08:12 +00001128
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001129 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001130 return;
Mike Stump25cf7602009-09-09 15:08:12 +00001131
Ted Kremeneke66ba682009-02-13 01:45:31 +00001132 state = GetState(Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00001133
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001134 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001135 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001136
1137 // FIXME: Currently symbolic analysis "generates" new symbols
1138 // for the contents of values. We need a better approach.
1139
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001140 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001141 // This is important. We must nuke the old binding.
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001142 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, UnknownVal()),
Zhongxing Xub4fa87b2009-08-25 06:51:30 +00001143 K, tag);
Ted Kremenekbf573852008-04-30 04:23:07 +00001144 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001145 else {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001146 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
Mike Stump25cf7602009-09-09 15:08:12 +00001147
Ted Kremenek1369d902009-07-11 04:38:49 +00001148 // Casts can create weird scenarios where a location must be implicitly
1149 // converted to something else. For example:
1150 //
1151 // void *x;
1152 // int *y = (int*) &x; // void** -> int* cast.
1153 // invalidate(y); // 'x' now binds to a symbolic region
1154 // int z = *y;
Mike Stump25cf7602009-09-09 15:08:12 +00001155 //
Zhongxing Xu92be7ce2009-07-14 01:12:46 +00001156 //if (isa<Loc>(V) && !Loc::IsLocType(Ex->getType())) {
1157 // V = EvalCast(V, Ex->getType());
1158 //}
Mike Stump25cf7602009-09-09 15:08:12 +00001159
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001160 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), K, tag);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001161 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001162}
1163
Mike Stump25cf7602009-09-09 15:08:12 +00001164void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, Expr* StoreE,
1165 ExplodedNode* Pred, const GRState* state,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001166 SVal location, SVal Val, const void *tag) {
Mike Stump25cf7602009-09-09 15:08:12 +00001167
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001168 ExplodedNodeSet TmpDst;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001169 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001170
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001171 for (ExplodedNodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001172 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001173}
1174
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001175ExplodedNode* GRExprEngine::EvalLocation(Stmt* Ex, ExplodedNode* Pred,
1176 const GRState* state, SVal location,
1177 const void *tag) {
Mike Stump25cf7602009-09-09 15:08:12 +00001178
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001179 SaveAndRestore<const void*> OldTag(Builder->Tag);
1180 Builder->Tag = tag;
Mike Stump25cf7602009-09-09 15:08:12 +00001181
1182 // Check for loads/stores from/to undefined values.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001183 if (location.isUndef()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001184 ExplodedNode* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001185 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001186 ProgramPoint::PostUndefLocationCheckFailedKind);
Mike Stump25cf7602009-09-09 15:08:12 +00001187
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001188 if (N) {
1189 N->markAsSink();
1190 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001191 }
Mike Stump25cf7602009-09-09 15:08:12 +00001192
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001193 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001194 }
Mike Stump25cf7602009-09-09 15:08:12 +00001195
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001196 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1197 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001198 return Pred;
Mike Stump25cf7602009-09-09 15:08:12 +00001199
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001200 // During a load, one of two possible situations arise:
1201 // (1) A crash, because the location (pointer) was NULL.
1202 // (2) The location (pointer) is not NULL, and the dereference works.
Mike Stump25cf7602009-09-09 15:08:12 +00001203 //
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001204 // We add these assumptions.
Mike Stump25cf7602009-09-09 15:08:12 +00001205
1206 Loc LV = cast<Loc>(location);
1207
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001208 // "Assume" that the pointer is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001209 const GRState *StNotNull = state->assume(LV, true);
Mike Stump25cf7602009-09-09 15:08:12 +00001210
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001211 // "Assume" that the pointer is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001212 const GRState *StNull = state->assume(LV, false);
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001213
Mike Stump25cf7602009-09-09 15:08:12 +00001214 if (StNull) {
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001215 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001216 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001217 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Mike Stump25cf7602009-09-09 15:08:12 +00001218
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001219 // We don't use "MakeNode" here because the node will be a sink
1220 // and we have no intention of processing it later.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001221 ExplodedNode* NullNode =
Mike Stump25cf7602009-09-09 15:08:12 +00001222 Builder->generateNode(Ex, StNull, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001223 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001224
Mike Stump25cf7602009-09-09 15:08:12 +00001225 if (NullNode) {
1226 NullNode->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001227 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001228 else ExplicitNullDeref.insert(NullNode);
1229 }
1230 }
Mike Stump25cf7602009-09-09 15:08:12 +00001231
Ted Kremenek70970bf2009-06-18 22:57:13 +00001232 if (!StNotNull)
1233 return NULL;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001234
Ted Kremenek2ef09982009-08-01 05:59:39 +00001235 // FIXME: Temporarily disable out-of-bounds checking until we make
1236 // the logic reflect recent changes to CastRegion and friends.
1237#if 0
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001238 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001239 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001240 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1241 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1242 // Get the index of the accessed element.
1243 SVal Idx = ER->getIndex();
1244 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001245 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1246 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001247
Mike Stump25cf7602009-09-09 15:08:12 +00001248 const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
Ted Kremenek70970bf2009-06-18 22:57:13 +00001249 true);
Mike Stump25cf7602009-09-09 15:08:12 +00001250 const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
Ted Kremenek70970bf2009-06-18 22:57:13 +00001251 false);
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001252
Ted Kremenek70970bf2009-06-18 22:57:13 +00001253 if (StOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001254 // Report warning. Make sink node manually.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001255 ExplodedNode* OOBNode =
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001256 Builder->generateNode(Ex, StOutBound, Pred,
1257 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001258
1259 if (OOBNode) {
1260 OOBNode->markAsSink();
1261
Ted Kremenek70970bf2009-06-18 22:57:13 +00001262 if (StInBound)
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001263 ImplicitOOBMemAccesses.insert(OOBNode);
1264 else
1265 ExplicitOOBMemAccesses.insert(OOBNode);
1266 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001267 }
1268
Ted Kremenek70970bf2009-06-18 22:57:13 +00001269 if (!StInBound)
1270 return NULL;
1271
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001272 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001273 }
1274 }
Ted Kremenek2ef09982009-08-01 05:59:39 +00001275#endif
Mike Stump25cf7602009-09-09 15:08:12 +00001276
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001277 // Generate a new node indicating the checks succeed.
1278 return Builder->generateNode(Ex, StNotNull, Pred,
1279 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001280}
1281
Ted Kremenekca5f6202008-04-15 23:06:53 +00001282//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001283// Transfer function: OSAtomics.
1284//
1285// FIXME: Eventually refactor into a more "plugin" infrastructure.
1286//===----------------------------------------------------------------------===//
1287
1288// Mac OS X:
1289// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1290// atomic.3.html
1291//
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001292static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001293 GRExprEngine& Engine,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001294 GRStmtNodeBuilder& Builder,
Mike Stump25cf7602009-09-09 15:08:12 +00001295 CallExpr* CE, SVal L,
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001296 ExplodedNode* Pred) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001297
1298 // Not enough arguments to match OSAtomicCompareAndSwap?
1299 if (CE->getNumArgs() != 3)
1300 return false;
Mike Stump25cf7602009-09-09 15:08:12 +00001301
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001302 ASTContext &C = Engine.getContext();
1303 Expr *oldValueExpr = CE->getArg(0);
1304 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1305
1306 Expr *newValueExpr = CE->getArg(1);
1307 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
Mike Stump25cf7602009-09-09 15:08:12 +00001308
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001309 // Do the types of 'oldValue' and 'newValue' match?
1310 if (oldValueType != newValueType)
1311 return false;
Mike Stump25cf7602009-09-09 15:08:12 +00001312
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001313 Expr *theValueExpr = CE->getArg(2);
Ted Kremenek2ef09982009-08-01 05:59:39 +00001314 const PointerType *theValueType =
1315 theValueExpr->getType()->getAs<PointerType>();
Mike Stump25cf7602009-09-09 15:08:12 +00001316
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001317 // theValueType not a pointer?
1318 if (!theValueType)
1319 return false;
Mike Stump25cf7602009-09-09 15:08:12 +00001320
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001321 QualType theValueTypePointee =
1322 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
Mike Stump25cf7602009-09-09 15:08:12 +00001323
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001324 // The pointee must match newValueType and oldValueType.
1325 if (theValueTypePointee != newValueType)
1326 return false;
Mike Stump25cf7602009-09-09 15:08:12 +00001327
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001328 static unsigned magic_load = 0;
1329 static unsigned magic_store = 0;
1330
1331 const void *OSAtomicLoadTag = &magic_load;
1332 const void *OSAtomicStoreTag = &magic_store;
Mike Stump25cf7602009-09-09 15:08:12 +00001333
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001334 // Load 'theValue'.
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001335 const GRState *state = Pred->getState();
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001336 ExplodedNodeSet Tmp;
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001337 SVal location = state->getSVal(theValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001338 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1339
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001340 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001341 I != E; ++I) {
Mike Stump25cf7602009-09-09 15:08:12 +00001342
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001343 ExplodedNode *N = *I;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001344 const GRState *stateLoad = N->getState();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001345 SVal theValueVal = stateLoad->getSVal(theValueExpr);
1346 SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
Mike Stump25cf7602009-09-09 15:08:12 +00001347
Ted Kremenek0e808282009-07-20 20:38:59 +00001348 // FIXME: Issue an error.
1349 if (theValueVal.isUndef() || oldValueVal.isUndef()) {
Mike Stump25cf7602009-09-09 15:08:12 +00001350 return false;
Ted Kremenek0e808282009-07-20 20:38:59 +00001351 }
Mike Stump25cf7602009-09-09 15:08:12 +00001352
Ted Kremenekd7a00492009-08-25 18:44:25 +00001353 SValuator &SVator = Engine.getSValuator();
Mike Stump25cf7602009-09-09 15:08:12 +00001354
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001355 // Perform the comparison.
Ted Kremenekd7a00492009-08-25 18:44:25 +00001356 SVal Cmp = SVator.EvalBinOp(stateLoad, BinaryOperator::EQ, theValueVal,
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00001357 oldValueVal, Engine.getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +00001358
1359 const GRState *stateEqual = stateLoad->assume(Cmp, true);
Mike Stump25cf7602009-09-09 15:08:12 +00001360
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001361 // Were they equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001362 if (stateEqual) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001363 // Perform the store.
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001364 ExplodedNodeSet TmpStore;
Ted Kremenek6cd31072009-07-21 21:03:30 +00001365 SVal val = stateEqual->getSVal(newValueExpr);
Mike Stump25cf7602009-09-09 15:08:12 +00001366
Ted Kremenek6cd31072009-07-21 21:03:30 +00001367 // Handle implicit value casts.
1368 if (const TypedRegion *R =
1369 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
Ted Kremenekd7a00492009-08-25 18:44:25 +00001370 llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C),
1371 newValueExpr->getType());
Mike Stump25cf7602009-09-09 15:08:12 +00001372 }
1373
1374 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenek6cd31072009-07-21 21:03:30 +00001375 val, OSAtomicStoreTag);
Mike Stump25cf7602009-09-09 15:08:12 +00001376
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001377 // Now bind the result of the comparison.
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001378 for (ExplodedNodeSet::iterator I2 = TmpStore.begin(),
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001379 E2 = TmpStore.end(); I2 != E2; ++I2) {
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001380 ExplodedNode *predNew = *I2;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001381 const GRState *stateNew = predNew->getState();
1382 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001383 Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001384 }
1385 }
Mike Stump25cf7602009-09-09 15:08:12 +00001386
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001387 // Were they not equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001388 if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001389 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001390 Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001391 }
1392 }
Mike Stump25cf7602009-09-09 15:08:12 +00001393
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001394 return true;
1395}
1396
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001397static bool EvalOSAtomic(ExplodedNodeSet& Dst,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001398 GRExprEngine& Engine,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001399 GRStmtNodeBuilder& Builder,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001400 CallExpr* CE, SVal L,
Zhongxing Xuff71ed02009-08-06 01:32:16 +00001401 ExplodedNode* Pred) {
Zhongxing Xucac107a2009-04-20 05:24:46 +00001402 const FunctionDecl* FD = L.getAsFunctionDecl();
1403 if (!FD)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001404 return false;
Zhongxing Xucac107a2009-04-20 05:24:46 +00001405
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001406 const char *FName = FD->getNameAsCString();
Mike Stump25cf7602009-09-09 15:08:12 +00001407
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001408 // Check for compare and swap.
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001409 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1410 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001411 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001412
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001413 // FIXME: Other atomics.
1414 return false;
1415}
1416
1417//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001418// Transfer function: Function calls.
1419//===----------------------------------------------------------------------===//
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001420static void MarkNoReturnFunction(const FunctionDecl *FD, CallExpr *CE,
Mike Stump25cf7602009-09-09 15:08:12 +00001421 const GRState *state,
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001422 GRStmtNodeBuilder *Builder) {
Zhongxing Xu7bb7b852009-09-04 02:17:35 +00001423 if (!FD)
1424 return;
1425
Mike Stump25cf7602009-09-09 15:08:12 +00001426 if (FD->getAttr<NoReturnAttr>() ||
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001427 FD->getAttr<AnalyzerNoReturnAttr>())
1428 Builder->BuildSinks = true;
1429 else {
1430 // HACK: Some functions are not marked noreturn, and don't return.
1431 // Here are a few hardwired ones. If this takes too long, we can
1432 // potentially cache these results.
1433 const char* s = FD->getIdentifier()->getName();
1434 unsigned n = strlen(s);
Mike Stump25cf7602009-09-09 15:08:12 +00001435
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001436 switch (n) {
1437 default:
1438 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001439
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001440 case 4:
1441 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1442 break;
1443
1444 case 5:
1445 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1446 else if (!memcmp(s, "error", 5)) {
1447 if (CE->getNumArgs() > 0) {
1448 SVal X = state->getSVal(*CE->arg_begin());
1449 // FIXME: use Assume to inspect the possible symbolic value of
1450 // X. Also check the specific signature of error().
1451 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
1452 if (CI && CI->getValue() != 0)
1453 Builder->BuildSinks = true;
1454 }
1455 }
1456 break;
1457
1458 case 6:
1459 if (!memcmp(s, "Assert", 6)) {
1460 Builder->BuildSinks = true;
1461 break;
1462 }
Mike Stump25cf7602009-09-09 15:08:12 +00001463
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001464 // FIXME: This is just a wrapper around throwing an exception.
1465 // Eventually inter-procedural analysis should handle this easily.
1466 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1467
1468 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001469
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001470 case 7:
1471 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1472 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001473
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001474 case 8:
Mike Stump25cf7602009-09-09 15:08:12 +00001475 if (!memcmp(s ,"db_error", 8) ||
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001476 !memcmp(s, "__assert", 8))
1477 Builder->BuildSinks = true;
1478 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001479
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001480 case 12:
1481 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1482 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001483
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001484 case 13:
1485 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1486 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001487
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001488 case 14:
1489 if (!memcmp(s, "dtrace_assfail", 14) ||
1490 !memcmp(s, "yy_fatal_error", 14))
1491 Builder->BuildSinks = true;
1492 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001493
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001494 case 26:
1495 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1496 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1497 !memcmp(s, "_TSAssertionFailureHandler", 26))
1498 Builder->BuildSinks = true;
1499
1500 break;
1501 }
Mike Stump25cf7602009-09-09 15:08:12 +00001502
Zhongxing Xu6f98a832009-09-04 02:13:36 +00001503 }
1504}
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001505
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001506bool GRExprEngine::EvalBuiltinFunction(const FunctionDecl *FD, CallExpr *CE,
1507 ExplodedNode *Pred,
1508 ExplodedNodeSet &Dst) {
1509 if (!FD)
1510 return false;
Mike Stump25cf7602009-09-09 15:08:12 +00001511
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001512 unsigned id = FD->getBuiltinID(getContext());
1513 if (!id)
1514 return false;
1515
1516 const GRState *state = Pred->getState();
1517
1518 switch (id) {
1519 case Builtin::BI__builtin_expect: {
1520 // For __builtin_expect, just return the value of the subexpression.
Mike Stump25cf7602009-09-09 15:08:12 +00001521 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001522 SVal X = state->getSVal(*(CE->arg_begin()));
1523 MakeNode(Dst, CE, Pred, state->BindExpr(CE, X));
1524 return true;
1525 }
Mike Stump25cf7602009-09-09 15:08:12 +00001526
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001527 case Builtin::BI__builtin_alloca: {
1528 // FIXME: Refactor into StoreManager itself?
1529 MemRegionManager& RM = getStateManager().getRegionManager();
1530 const MemRegion* R =
1531 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Mike Stump25cf7602009-09-09 15:08:12 +00001532
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001533 // Set the extent of the region in bytes. This enables us to use the
1534 // SVal of the argument directly. If we save the extent in bits, we
1535 // cannot represent values like symbol*8.
1536 SVal Extent = state->getSVal(*(CE->arg_begin()));
1537 state = getStoreManager().setExtent(state, R, Extent);
1538 MakeNode(Dst, CE, Pred, state->BindExpr(CE, loc::MemRegionVal(R)));
1539 return true;
1540 }
1541 }
1542
1543 return false;
1544}
1545
Mike Stump25cf7602009-09-09 15:08:12 +00001546void GRExprEngine::EvalCall(ExplodedNodeSet& Dst, CallExpr* CE, SVal L,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001547 ExplodedNode* Pred) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001548 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump25cf7602009-09-09 15:08:12 +00001549
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001550 // FIXME: Allow us to chain together transfer functions.
1551 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1552 return;
Mike Stump25cf7602009-09-09 15:08:12 +00001553
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001554 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1555}
1556
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001557void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001558 CallExpr::arg_iterator AI,
1559 CallExpr::arg_iterator AE,
Mike Stump25cf7602009-09-09 15:08:12 +00001560 ExplodedNodeSet& Dst) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001561 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001562 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001563 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001564 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001565 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001566
1567 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1568}
1569
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001570void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001571 CallExpr::arg_iterator AI,
1572 CallExpr::arg_iterator AE,
Mike Stump25cf7602009-09-09 15:08:12 +00001573 ExplodedNodeSet& Dst,
1574 const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001575 unsigned ParamIdx) {
Mike Stump25cf7602009-09-09 15:08:12 +00001576
Ted Kremenek07baa252008-02-21 18:02:17 +00001577 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001578 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001579 // If the call argument is being bound to a reference parameter,
1580 // visit it as an lvalue, not an rvalue.
1581 bool VisitAsLvalue = false;
1582 if (Proto && ParamIdx < Proto->getNumArgs())
1583 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1584
Mike Stump25cf7602009-09-09 15:08:12 +00001585 ExplodedNodeSet DstTmp;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001586 if (VisitAsLvalue)
Mike Stump25cf7602009-09-09 15:08:12 +00001587 VisitLValue(*AI, Pred, DstTmp);
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001588 else
Mike Stump25cf7602009-09-09 15:08:12 +00001589 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001590 ++AI;
Mike Stump25cf7602009-09-09 15:08:12 +00001591
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001592 for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE;
1593 ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001594 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Mike Stump25cf7602009-09-09 15:08:12 +00001595
Ted Kremenekd9268e32008-02-19 01:44:53 +00001596 return;
1597 }
1598
1599 // If we reach here we have processed all of the arguments. Evaluate
1600 // the callee expression.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001601 ExplodedNodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001602 Expr* Callee = CE->getCallee()->IgnoreParens();
Mike Stump25cf7602009-09-09 15:08:12 +00001603
Ted Kremenekc9058362009-07-22 21:43:51 +00001604 { // Enter new scope to make the lifetime of 'DstTmp2' bounded.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001605 ExplodedNodeSet DstTmp2;
Ted Kremenekc9058362009-07-22 21:43:51 +00001606 Visit(Callee, Pred, DstTmp2);
Mike Stump25cf7602009-09-09 15:08:12 +00001607
Ted Kremenekc9058362009-07-22 21:43:51 +00001608 // Perform the previsit of the CallExpr, storing the results in DstTmp.
1609 CheckerVisit(CE, DstTmp, DstTmp2, true);
1610 }
Mike Stump25cf7602009-09-09 15:08:12 +00001611
Ted Kremenekd9268e32008-02-19 01:44:53 +00001612 // Finally, evaluate the function call.
Mike Stump25cf7602009-09-09 15:08:12 +00001613 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001614 DI != DE; ++DI) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001615
Ted Kremeneke66ba682009-02-13 01:45:31 +00001616 const GRState* state = GetState(*DI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001617 SVal L = state->getSVal(Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001618
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001619 // FIXME: Add support for symbolic function calls (calls involving
1620 // function pointer values that are symbolic).
Zhongxing Xub72bce82009-09-02 08:10:35 +00001621
Ted Kremenekb451dd32008-03-05 21:15:02 +00001622 // Check for the "noreturn" attribute.
Mike Stump25cf7602009-09-09 15:08:12 +00001623
Ted Kremenekb451dd32008-03-05 21:15:02 +00001624 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xucac107a2009-04-20 05:24:46 +00001625 const FunctionDecl* FD = L.getAsFunctionDecl();
Zhongxing Xu7bb7b852009-09-04 02:17:35 +00001626
1627 MarkNoReturnFunction(FD, CE, state, Builder);
Mike Stump25cf7602009-09-09 15:08:12 +00001628
Ted Kremenekb451dd32008-03-05 21:15:02 +00001629 // Evaluate the call.
Zhongxing Xu4e8961b2009-09-05 06:46:12 +00001630 if (EvalBuiltinFunction(FD, CE, *DI, Dst))
Zhongxing Xu07d2c652009-09-05 05:00:57 +00001631 continue;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001632
Mike Stump25cf7602009-09-09 15:08:12 +00001633 // Dispatch to the plug-in transfer function.
1634
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001635 unsigned size = Dst.size();
1636 SaveOr OldHasGen(Builder->HasGeneratedNode);
1637 EvalCall(Dst, CE, L, *DI);
Mike Stump25cf7602009-09-09 15:08:12 +00001638
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001639 // Handle the case where no nodes where generated. Auto-generate that
1640 // contains the updated state if we aren't generating sinks.
Mike Stump25cf7602009-09-09 15:08:12 +00001641
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001642 if (!Builder->BuildSinks && Dst.size() == size &&
1643 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001644 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001645 }
1646}
1647
Ted Kremenekca5f6202008-04-15 23:06:53 +00001648//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001649// Transfer function: Objective-C ivar references.
1650//===----------------------------------------------------------------------===//
1651
Ted Kremenek9a48d862009-02-28 20:50:43 +00001652static std::pair<const void*,const void*> EagerlyAssumeTag
1653 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1654
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001655void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, Expr *Ex) {
1656 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1657 ExplodedNode *Pred = *I;
Mike Stump25cf7602009-09-09 15:08:12 +00001658
Ted Kremenek34a611b2009-02-25 23:32:10 +00001659 // Test if the previous node was as the same expression. This can happen
1660 // when the expression fails to evaluate to anything meaningful and
1661 // (as an optimization) we don't generate a node.
Mike Stump25cf7602009-09-09 15:08:12 +00001662 ProgramPoint P = Pred->getLocation();
Ted Kremenek34a611b2009-02-25 23:32:10 +00001663 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump25cf7602009-09-09 15:08:12 +00001664 Dst.Add(Pred);
Ted Kremenek34a611b2009-02-25 23:32:10 +00001665 continue;
Mike Stump25cf7602009-09-09 15:08:12 +00001666 }
Ted Kremenek34a611b2009-02-25 23:32:10 +00001667
Mike Stump25cf7602009-09-09 15:08:12 +00001668 const GRState* state = Pred->getState();
1669 SVal V = state->getSVal(Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001670 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001671 // First assume that the condition is true.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001672 if (const GRState *stateTrue = state->assume(V, true)) {
Mike Stump25cf7602009-09-09 15:08:12 +00001673 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek5b76b832009-08-27 01:39:13 +00001674 ValMgr.makeIntVal(1U, Ex->getType()));
Mike Stump25cf7602009-09-09 15:08:12 +00001675 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xu2ac46a52009-08-15 03:17:38 +00001676 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenek8f520972009-02-25 22:32:02 +00001677 stateTrue, Pred));
1678 }
Mike Stump25cf7602009-09-09 15:08:12 +00001679
Ted Kremenek8f520972009-02-25 22:32:02 +00001680 // Next, assume that the condition is false.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001681 if (const GRState *stateFalse = state->assume(V, false)) {
Mike Stump25cf7602009-09-09 15:08:12 +00001682 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek5b76b832009-08-27 01:39:13 +00001683 ValMgr.makeIntVal(0U, Ex->getType()));
Zhongxing Xu2ac46a52009-08-15 03:17:38 +00001684 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
1685 Pred->getLocationContext()),
Ted Kremenek8f520972009-02-25 22:32:02 +00001686 stateFalse, Pred));
1687 }
1688 }
1689 else
1690 Dst.Add(Pred);
1691 }
1692}
1693
1694//===----------------------------------------------------------------------===//
1695// Transfer function: Objective-C ivar references.
1696//===----------------------------------------------------------------------===//
1697
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001698void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001699 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001700 bool asLValue) {
Mike Stump25cf7602009-09-09 15:08:12 +00001701
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001702 Expr* Base = cast<Expr>(Ex->getBase());
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001703 ExplodedNodeSet Tmp;
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001704 Visit(Base, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00001705
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001706 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001707 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001708 SVal BaseVal = state->getSVal(Base);
1709 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Mike Stump25cf7602009-09-09 15:08:12 +00001710
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001711 if (asLValue)
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001712 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001713 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001714 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001715 }
1716}
1717
1718//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001719// Transfer function: Objective-C fast enumeration 'for' statements.
1720//===----------------------------------------------------------------------===//
1721
1722void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001723 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump25cf7602009-09-09 15:08:12 +00001724
Ted Kremenek13e167f2008-11-12 19:24:17 +00001725 // ObjCForCollectionStmts are processed in two places. This method
1726 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1727 // statements within a basic block. This transfer function does two things:
1728 //
1729 // (1) binds the next container value to 'element'. This creates a new
1730 // node in the ExplodedGraph.
1731 //
1732 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1733 // whether or not the container has any more elements. This value
1734 // will be tested in ProcessBranch. We need to explicitly bind
1735 // this value because a container can contain nil elements.
Mike Stump25cf7602009-09-09 15:08:12 +00001736 //
Ted Kremenek13e167f2008-11-12 19:24:17 +00001737 // FIXME: Eventually this logic should actually do dispatches to
1738 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1739 // This will require simulating a temporary NSFastEnumerationState, either
1740 // through an SVal or through the use of MemRegions. This value can
1741 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1742 // terminates we reclaim the temporary (it goes out of scope) and we
1743 // we can test if the SVal is 0 or if the MemRegion is null (depending
1744 // on what approach we take).
1745 //
1746 // For now: simulate (1) by assigning either a symbol or nil if the
1747 // container is empty. Thus this transfer function will by default
1748 // result in state splitting.
Mike Stump25cf7602009-09-09 15:08:12 +00001749
Ted Kremenek034a9472008-11-14 19:47:18 +00001750 Stmt* elem = S->getElement();
1751 SVal ElementV;
Mike Stump25cf7602009-09-09 15:08:12 +00001752
Ted Kremenek13e167f2008-11-12 19:24:17 +00001753 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001754 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001755 assert (ElemD->getInit() == 0);
Ted Kremenek6555ff92009-08-21 22:28:32 +00001756 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001757 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1758 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001759 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001760
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001761 ExplodedNodeSet Tmp;
Ted Kremenek034a9472008-11-14 19:47:18 +00001762 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00001763
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001764 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek034a9472008-11-14 19:47:18 +00001765 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001766 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek034a9472008-11-14 19:47:18 +00001767 }
1768}
1769
1770void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001771 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek034a9472008-11-14 19:47:18 +00001772 SVal ElementV) {
Ted Kremenek034a9472008-11-14 19:47:18 +00001773
Mike Stump25cf7602009-09-09 15:08:12 +00001774
1775
Ted Kremenek034a9472008-11-14 19:47:18 +00001776 // Get the current state. Use 'EvalLocation' to determine if it is a null
1777 // pointer, etc.
1778 Stmt* elem = S->getElement();
Mike Stump25cf7602009-09-09 15:08:12 +00001779
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001780 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1781 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001782 return;
Mike Stump25cf7602009-09-09 15:08:12 +00001783
Ted Kremenek18a636d2009-06-18 01:23:53 +00001784 const GRState *state = GetState(Pred);
Ted Kremenek034a9472008-11-14 19:47:18 +00001785
Ted Kremenek13e167f2008-11-12 19:24:17 +00001786 // Handle the case where the container still has elements.
Zhongxing Xue32c7652009-06-23 09:02:15 +00001787 SVal TrueV = ValMgr.makeTruthVal(1);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001788 const GRState *hasElems = state->BindExpr(S, TrueV);
Mike Stump25cf7602009-09-09 15:08:12 +00001789
Ted Kremenek13e167f2008-11-12 19:24:17 +00001790 // Handle the case where the container has no elements.
Zhongxing Xue32c7652009-06-23 09:02:15 +00001791 SVal FalseV = ValMgr.makeTruthVal(0);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001792 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump25cf7602009-09-09 15:08:12 +00001793
Ted Kremenek034a9472008-11-14 19:47:18 +00001794 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1795 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1796 // FIXME: The proper thing to do is to really iterate over the
1797 // container. We will do this with dispatch logic to the store.
1798 // For now, just 'conjure' up a symbolic value.
Zhongxing Xu20362702009-05-09 03:57:34 +00001799 QualType T = R->getValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001800 assert (Loc::IsLocType(T));
1801 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu0ed9d0c2009-04-09 06:49:52 +00001802 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
Zhongxing Xue32c7652009-06-23 09:02:15 +00001803 SVal V = ValMgr.makeLoc(Sym);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001804 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001805
Ted Kremenek034a9472008-11-14 19:47:18 +00001806 // Bind the location to 'nil' on the false branch.
Mike Stump25cf7602009-09-09 15:08:12 +00001807 SVal nilV = ValMgr.makeIntVal(0, T);
1808 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001809 }
Mike Stump25cf7602009-09-09 15:08:12 +00001810
Ted Kremenekd3789d72008-11-12 21:12:46 +00001811 // Create the new nodes.
1812 MakeNode(Dst, S, Pred, hasElems);
1813 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001814}
1815
1816//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001817// Transfer function: Objective-C message expressions.
1818//===----------------------------------------------------------------------===//
1819
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001820void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
1821 ExplodedNodeSet& Dst){
Mike Stump25cf7602009-09-09 15:08:12 +00001822
Ted Kremenekca5f6202008-04-15 23:06:53 +00001823 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1824 Pred, Dst);
Mike Stump25cf7602009-09-09 15:08:12 +00001825}
Ted Kremenekca5f6202008-04-15 23:06:53 +00001826
1827void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001828 ObjCMessageExpr::arg_iterator AI,
1829 ObjCMessageExpr::arg_iterator AE,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001830 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001831 if (AI == AE) {
Mike Stump25cf7602009-09-09 15:08:12 +00001832
Ted Kremenekca5f6202008-04-15 23:06:53 +00001833 // Process the receiver.
Mike Stump25cf7602009-09-09 15:08:12 +00001834
Ted Kremenekca5f6202008-04-15 23:06:53 +00001835 if (Expr* Receiver = ME->getReceiver()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001836 ExplodedNodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +00001837 Visit(Receiver, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00001838
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001839 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Daniel Dunbar50654362009-07-23 04:41:06 +00001840 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Mike Stump25cf7602009-09-09 15:08:12 +00001841
Ted Kremenekca5f6202008-04-15 23:06:53 +00001842 return;
1843 }
Mike Stump25cf7602009-09-09 15:08:12 +00001844
Daniel Dunbar50654362009-07-23 04:41:06 +00001845 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001846 return;
1847 }
Mike Stump25cf7602009-09-09 15:08:12 +00001848
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001849 ExplodedNodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +00001850 Visit(*AI, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00001851
Ted Kremenekca5f6202008-04-15 23:06:53 +00001852 ++AI;
Mike Stump25cf7602009-09-09 15:08:12 +00001853
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001854 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenekca5f6202008-04-15 23:06:53 +00001855 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1856}
1857
1858void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001859 ExplodedNode* Pred,
1860 ExplodedNodeSet& Dst) {
Mike Stump25cf7602009-09-09 15:08:12 +00001861
1862 // FIXME: More logic for the processing the method call.
1863
Ted Kremeneke66ba682009-02-13 01:45:31 +00001864 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001865 bool RaisesException = false;
Mike Stump25cf7602009-09-09 15:08:12 +00001866
1867
Ted Kremenekca5f6202008-04-15 23:06:53 +00001868 if (Expr* Receiver = ME->getReceiver()) {
Mike Stump25cf7602009-09-09 15:08:12 +00001869
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001870 SVal L = state->getSVal(Receiver);
Mike Stump25cf7602009-09-09 15:08:12 +00001871
1872 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001873 if (L.isUndef()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001874 ExplodedNode* N = Builder->generateNode(ME, state, Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00001875
Ted Kremenekca5f6202008-04-15 23:06:53 +00001876 if (N) {
1877 N->markAsSink();
1878 UndefReceivers.insert(N);
1879 }
Mike Stump25cf7602009-09-09 15:08:12 +00001880
Ted Kremenekca5f6202008-04-15 23:06:53 +00001881 return;
1882 }
Mike Stump25cf7602009-09-09 15:08:12 +00001883
1884 // "Assume" that the receiver is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001885 const GRState *StNotNull = state->assume(L, true);
Mike Stump25cf7602009-09-09 15:08:12 +00001886
1887 // "Assume" that the receiver is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001888 const GRState *StNull = state->assume(L, false);
Mike Stump25cf7602009-09-09 15:08:12 +00001889
Ted Kremenek70970bf2009-06-18 22:57:13 +00001890 if (StNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001891 QualType RetTy = ME->getType();
Mike Stump25cf7602009-09-09 15:08:12 +00001892
Ted Kremenek95a98252009-02-19 04:06:22 +00001893 // Check if the receiver was nil and the return value a struct.
Mike Stump25cf7602009-09-09 15:08:12 +00001894 if (RetTy->isRecordType()) {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001895 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001896 // The [0 ...] expressions will return garbage. Flag either an
1897 // explicit or implicit error. Because of the structure of this
1898 // function we currently do not bifurfacte the state graph at
1899 // this point.
1900 // FIXME: We should bifurcate and fill the returned struct with
Mike Stump25cf7602009-09-09 15:08:12 +00001901 // garbage.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001902 if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001903 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001904 if (StNotNull)
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001905 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001906 else
Mike Stump25cf7602009-09-09 15:08:12 +00001907 NilReceiverStructRetExplicit.insert(N);
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001908 }
1909 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001910 }
Ted Kremenekb3323002009-04-09 05:45:56 +00001911 else {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001912 ASTContext& Ctx = getContext();
Ted Kremenekb3323002009-04-09 05:45:56 +00001913 if (RetTy != Ctx.VoidTy) {
1914 if (BR.getParentMap().isConsumedExpr(ME)) {
1915 // sizeof(void *)
1916 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1917 // sizeof(return type)
1918 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenek5ab77002009-04-09 00:00:02 +00001919
Mike Stump25cf7602009-09-09 15:08:12 +00001920 if (voidPtrSize < returnTypeSize) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00001921 if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001922 N->markAsSink();
Mike Stump25cf7602009-09-09 15:08:12 +00001923 if (StNotNull)
Ted Kremenekb3323002009-04-09 05:45:56 +00001924 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001925 else
Mike Stump25cf7602009-09-09 15:08:12 +00001926 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekb3323002009-04-09 05:45:56 +00001927 }
1928 }
Ted Kremenek70970bf2009-06-18 22:57:13 +00001929 else if (!StNotNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001930 // Handle the safe cases where the return value is 0 if the
1931 // receiver is nil.
1932 //
1933 // FIXME: For now take the conservative approach that we only
1934 // return null values if we *know* that the receiver is nil.
1935 // This is because we can have surprises like:
1936 //
1937 // ... = [[NSScreens screens] objectAtIndex:0];
1938 //
1939 // What can happen is that [... screens] could return nil, but
1940 // it most likely isn't nil. We should assume the semantics
1941 // of this case unless we have *a lot* more knowledge.
1942 //
Ted Kremenekcda58d22009-04-09 16:46:55 +00001943 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremeneka7b9c662009-08-27 22:17:37 +00001944 MakeNode(Dst, ME, Pred, StNull->BindExpr(ME, V));
Ted Kremenek23712182009-04-09 04:06:51 +00001945 return;
1946 }
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001947 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001948 }
Ted Kremenek95a98252009-02-19 04:06:22 +00001949 }
Ted Kremenekf2895872009-04-08 18:51:08 +00001950 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001951 // of this method should assume that the receiver is not nil.
1952 if (!StNotNull)
1953 return;
Mike Stump25cf7602009-09-09 15:08:12 +00001954
Ted Kremenekf2895872009-04-08 18:51:08 +00001955 state = StNotNull;
Ted Kremenek95a98252009-02-19 04:06:22 +00001956 }
Mike Stump25cf7602009-09-09 15:08:12 +00001957
Ted Kremenek5f20a632008-05-01 18:33:28 +00001958 // Check if the "raise" message was sent.
1959 if (ME->getSelector() == RaiseSel)
1960 RaisesException = true;
1961 }
1962 else {
Mike Stump25cf7602009-09-09 15:08:12 +00001963
Ted Kremenek5f20a632008-05-01 18:33:28 +00001964 IdentifierInfo* ClsName = ME->getClassName();
1965 Selector S = ME->getSelector();
Mike Stump25cf7602009-09-09 15:08:12 +00001966
Ted Kremenek5f20a632008-05-01 18:33:28 +00001967 // Check for special instance methods.
Mike Stump25cf7602009-09-09 15:08:12 +00001968
1969 if (!NSExceptionII) {
Ted Kremenek5f20a632008-05-01 18:33:28 +00001970 ASTContext& Ctx = getContext();
Mike Stump25cf7602009-09-09 15:08:12 +00001971
Ted Kremenek5f20a632008-05-01 18:33:28 +00001972 NSExceptionII = &Ctx.Idents.get("NSException");
1973 }
Mike Stump25cf7602009-09-09 15:08:12 +00001974
Ted Kremenek5f20a632008-05-01 18:33:28 +00001975 if (ClsName == NSExceptionII) {
Mike Stump25cf7602009-09-09 15:08:12 +00001976
Ted Kremenek5f20a632008-05-01 18:33:28 +00001977 enum { NUM_RAISE_SELECTORS = 2 };
Mike Stump25cf7602009-09-09 15:08:12 +00001978
Ted Kremenek5f20a632008-05-01 18:33:28 +00001979 // Lazily create a cache of the selectors.
1980
1981 if (!NSExceptionInstanceRaiseSelectors) {
Mike Stump25cf7602009-09-09 15:08:12 +00001982
Ted Kremenek5f20a632008-05-01 18:33:28 +00001983 ASTContext& Ctx = getContext();
Mike Stump25cf7602009-09-09 15:08:12 +00001984
Ted Kremenek5f20a632008-05-01 18:33:28 +00001985 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
Mike Stump25cf7602009-09-09 15:08:12 +00001986
Ted Kremenek5f20a632008-05-01 18:33:28 +00001987 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1988 unsigned idx = 0;
Mike Stump25cf7602009-09-09 15:08:12 +00001989
1990 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001991 II.push_back(&Ctx.Idents.get("raise"));
Mike Stump25cf7602009-09-09 15:08:12 +00001992 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001993 NSExceptionInstanceRaiseSelectors[idx++] =
Mike Stump25cf7602009-09-09 15:08:12 +00001994 Ctx.Selectors.getSelector(II.size(), &II[0]);
1995
1996 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001997 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001998 NSExceptionInstanceRaiseSelectors[idx++] =
1999 Ctx.Selectors.getSelector(II.size(), &II[0]);
2000 }
Mike Stump25cf7602009-09-09 15:08:12 +00002001
Ted Kremenek5f20a632008-05-01 18:33:28 +00002002 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2003 if (S == NSExceptionInstanceRaiseSelectors[i]) {
2004 RaisesException = true; break;
2005 }
2006 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00002007 }
Mike Stump25cf7602009-09-09 15:08:12 +00002008
Ted Kremenekca5f6202008-04-15 23:06:53 +00002009 // Check for any arguments that are uninitialized/undefined.
Mike Stump25cf7602009-09-09 15:08:12 +00002010
Ted Kremenekca5f6202008-04-15 23:06:53 +00002011 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2012 I != E; ++I) {
Mike Stump25cf7602009-09-09 15:08:12 +00002013
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002014 if (state->getSVal(*I).isUndef()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002015
Ted Kremenekca5f6202008-04-15 23:06:53 +00002016 // Generate an error node for passing an uninitialized/undefined value
2017 // as an argument to a message expression. This node is a sink.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002018 ExplodedNode* N = Builder->generateNode(ME, state, Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00002019
Ted Kremenekca5f6202008-04-15 23:06:53 +00002020 if (N) {
2021 N->markAsSink();
2022 MsgExprUndefArgs[N] = *I;
2023 }
Mike Stump25cf7602009-09-09 15:08:12 +00002024
Ted Kremenekca5f6202008-04-15 23:06:53 +00002025 return;
Mike Stump25cf7602009-09-09 15:08:12 +00002026 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00002027 }
Mike Stump25cf7602009-09-09 15:08:12 +00002028
Ted Kremenek5f20a632008-05-01 18:33:28 +00002029 // Check if we raise an exception. For now treat these as sinks. Eventually
2030 // we will want to handle exceptions properly.
Mike Stump25cf7602009-09-09 15:08:12 +00002031
Ted Kremenek5f20a632008-05-01 18:33:28 +00002032 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2033
2034 if (RaisesException)
2035 Builder->BuildSinks = true;
Mike Stump25cf7602009-09-09 15:08:12 +00002036
Ted Kremenekca5f6202008-04-15 23:06:53 +00002037 // Dispatch to plug-in transfer function.
Mike Stump25cf7602009-09-09 15:08:12 +00002038
Ted Kremenekca5f6202008-04-15 23:06:53 +00002039 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002040 SaveOr OldHasGen(Builder->HasGeneratedNode);
Mike Stump25cf7602009-09-09 15:08:12 +00002041
Ted Kremenekca5f6202008-04-15 23:06:53 +00002042 EvalObjCMessageExpr(Dst, ME, Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00002043
Ted Kremenekca5f6202008-04-15 23:06:53 +00002044 // Handle the case where no nodes where generated. Auto-generate that
2045 // contains the updated state if we aren't generating sinks.
Mike Stump25cf7602009-09-09 15:08:12 +00002046
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002047 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002048 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00002049}
2050
2051//===----------------------------------------------------------------------===//
2052// Transfer functions: Miscellaneous statements.
2053//===----------------------------------------------------------------------===//
2054
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002055void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred, ExplodedNodeSet& Dst){
2056 ExplodedNodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00002057 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00002058 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00002059
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00002060 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00002061 T = ExCast->getTypeAsWritten();
2062
Zhongxing Xu943909c2008-10-22 08:02:16 +00002063 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002064 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00002065 else
2066 Visit(Ex, Pred, S1);
Mike Stump25cf7602009-09-09 15:08:12 +00002067
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002068 // Check for casting to "void".
Mike Stump25cf7602009-09-09 15:08:12 +00002069 if (T->isVoidType()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002070 for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00002071 Dst.Add(*I1);
2072
Ted Kremenek54eddae2008-01-24 02:02:54 +00002073 return;
2074 }
Ted Kremenek6cd31072009-07-21 21:03:30 +00002075
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002076 for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
2077 ExplodedNode* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002078 const GRState* state = GetState(N);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002079 SVal V = state->getSVal(Ex);
Ted Kremenek6cd31072009-07-21 21:03:30 +00002080 const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002081 state = Res.getState()->BindExpr(CastE, Res.getSVal());
Ted Kremenek6cd31072009-07-21 21:03:30 +00002082 MakeNode(Dst, CastE, N, state);
Ted Kremenek54eddae2008-01-24 02:02:54 +00002083 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002084}
2085
Ted Kremenekd83daa52008-10-27 21:54:31 +00002086void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Mike Stump25cf7602009-09-09 15:08:12 +00002087 ExplodedNode* Pred,
2088 ExplodedNodeSet& Dst,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002089 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002090 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002091 ExplodedNodeSet Tmp;
Ted Kremenekd83daa52008-10-27 21:54:31 +00002092 Visit(ILE, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002093
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002094 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002095 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002096 SVal ILV = state->getSVal(ILE);
2097 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002098
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002099 if (asLValue)
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002100 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002101 else
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002102 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002103 }
2104}
2105
Ted Kremenek6555ff92009-08-21 22:28:32 +00002106void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
Mike Stump25cf7602009-09-09 15:08:12 +00002107 ExplodedNodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002108
Mike Stump25cf7602009-09-09 15:08:12 +00002109 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002110 Decl* D = *DS->decl_begin();
Mike Stump25cf7602009-09-09 15:08:12 +00002111
Ted Kremenek448ab622008-08-28 18:34:26 +00002112 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002113 return;
Mike Stump25cf7602009-09-09 15:08:12 +00002114
2115 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002116 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002117
2118 // FIXME: static variables may have an initializer, but the second
2119 // time a function is called those values may not be current.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002120 ExplodedNodeSet Tmp;
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002121
Ted Kremenek13e167f2008-11-12 19:24:17 +00002122 if (InitEx)
2123 Visit(InitEx, Pred, Tmp);
Ted Kremenek9c677732009-07-17 23:48:26 +00002124 else
Ted Kremenek448ab622008-08-28 18:34:26 +00002125 Tmp.Add(Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00002126
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002127 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002128 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002129 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002130
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002131 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2132 QualType T = getContext().getCanonicalType(VD->getType());
2133 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2134 // FIXME: Handle multi-dimensional VLAs.
Mike Stump25cf7602009-09-09 15:08:12 +00002135
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002136 Expr* SE = VLA->getSizeExpr();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002137 SVal Size = state->getSVal(SE);
Mike Stump25cf7602009-09-09 15:08:12 +00002138
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002139 if (Size.isUndef()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002140 if (ExplodedNode* N = Builder->generateNode(DS, state, Pred)) {
Mike Stump25cf7602009-09-09 15:08:12 +00002141 N->markAsSink();
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002142 ExplicitBadSizedVLA.insert(N);
2143 }
2144 continue;
2145 }
Mike Stump25cf7602009-09-09 15:08:12 +00002146
2147 const GRState* zeroState = state->assume(Size, false);
Ted Kremenek70970bf2009-06-18 22:57:13 +00002148 state = state->assume(Size, true);
Mike Stump25cf7602009-09-09 15:08:12 +00002149
Ted Kremenek70970bf2009-06-18 22:57:13 +00002150 if (zeroState) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002151 if (ExplodedNode* N = Builder->generateNode(DS, zeroState, Pred)) {
Mike Stump25cf7602009-09-09 15:08:12 +00002152 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00002153 if (state)
2154 ImplicitBadSizedVLA.insert(N);
2155 else
2156 ExplicitBadSizedVLA.insert(N);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002157 }
2158 }
Mike Stump25cf7602009-09-09 15:08:12 +00002159
Ted Kremenek70970bf2009-06-18 22:57:13 +00002160 if (!state)
Mike Stump25cf7602009-09-09 15:08:12 +00002161 continue;
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002162 }
Mike Stump25cf7602009-09-09 15:08:12 +00002163
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002164 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek6555ff92009-08-21 22:28:32 +00002165 const LocationContext *LC = (*I)->getLocationContext();
2166
Ted Kremenek13e167f2008-11-12 19:24:17 +00002167 if (InitEx) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002168 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002169 QualType T = VD->getType();
Mike Stump25cf7602009-09-09 15:08:12 +00002170
Ted Kremenek13e167f2008-11-12 19:24:17 +00002171 // Recover some path-sensitivity if a scalar value evaluated to
2172 // UnknownVal.
Mike Stump25cf7602009-09-09 15:08:12 +00002173 if (InitVal.isUnknown() ||
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002174 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002175 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Mike Stump25cf7602009-09-09 15:08:12 +00002176 }
2177
Ted Kremenek6555ff92009-08-21 22:28:32 +00002178 state = state->bindDecl(VD, LC, InitVal);
Mike Stump25cf7602009-09-09 15:08:12 +00002179
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002180 // The next thing to do is check if the GRTransferFuncs object wants to
2181 // update the state based on the new binding. If the GRTransferFunc
2182 // object doesn't do anything, just auto-propagate the current state.
2183 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
Ted Kremenek6555ff92009-08-21 22:28:32 +00002184 getTF().EvalBind(BuilderRef, loc::MemRegionVal(state->getRegion(VD, LC)),
Mike Stump25cf7602009-09-09 15:08:12 +00002185 InitVal);
2186 }
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002187 else {
Ted Kremenek6555ff92009-08-21 22:28:32 +00002188 state = state->bindDeclWithNoInit(VD, LC);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002189 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002190 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002191 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002192}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002193
Ted Kremeneke56ece22008-10-30 17:47:32 +00002194namespace {
2195 // This class is used by VisitInitListExpr as an item in a worklist
2196 // for processing the values contained in an InitListExpr.
2197class VISIBILITY_HIDDEN InitListWLItem {
2198public:
2199 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002200 ExplodedNode* N;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002201 InitListExpr::reverse_iterator Itr;
Mike Stump25cf7602009-09-09 15:08:12 +00002202
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002203 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
2204 InitListExpr::reverse_iterator itr)
Ted Kremeneke56ece22008-10-30 17:47:32 +00002205 : Vals(vals), N(n), Itr(itr) {}
2206};
2207}
2208
2209
Mike Stump25cf7602009-09-09 15:08:12 +00002210void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002211 ExplodedNodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002212
Zhongxing Xuebcad732008-10-30 05:02:23 +00002213 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002214 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump25cf7602009-09-09 15:08:12 +00002215 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002216
Ted Kremenek3450a0e2009-07-28 20:46:55 +00002217 if (T->isArrayType() || T->isStructureType() ||
2218 T->isUnionType() || T->isVectorType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002219
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002220 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump25cf7602009-09-09 15:08:12 +00002221
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002222 // Handle base case where the initializer has no elements.
2223 // e.g: static int* myArray[] = {};
2224 if (NumInitElements == 0) {
Zhongxing Xue32c7652009-06-23 09:02:15 +00002225 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002226 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002227 return;
Mike Stump25cf7602009-09-09 15:08:12 +00002228 }
2229
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002230 // Create a worklist to process the initializers.
2231 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump25cf7602009-09-09 15:08:12 +00002232 WorkList.reserve(NumInitElements);
2233 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002234 InitListExpr::reverse_iterator ItrEnd = E->rend();
Mike Stump25cf7602009-09-09 15:08:12 +00002235
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002236 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002237 while (!WorkList.empty()) {
2238 InitListWLItem X = WorkList.back();
2239 WorkList.pop_back();
Mike Stump25cf7602009-09-09 15:08:12 +00002240
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002241 ExplodedNodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002242 Visit(*X.Itr, X.N, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002243
Ted Kremeneke56ece22008-10-30 17:47:32 +00002244 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002245
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002246 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002247 // Get the last initializer value.
2248 state = GetState(*NI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002249 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump25cf7602009-09-09 15:08:12 +00002250
Ted Kremeneke56ece22008-10-30 17:47:32 +00002251 // Construct the new list of values by prepending the new value to
2252 // the already constructed list.
2253 llvm::ImmutableList<SVal> NewVals =
2254 getBasicVals().consVals(InitV, X.Vals);
Mike Stump25cf7602009-09-09 15:08:12 +00002255
Ted Kremeneke56ece22008-10-30 17:47:32 +00002256 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002257 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xue32c7652009-06-23 09:02:15 +00002258 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xuebcad732008-10-30 05:02:23 +00002259
Ted Kremeneke56ece22008-10-30 17:47:32 +00002260 // Make final state and node.
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002261 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002262 }
2263 else {
2264 // Still some initializer values to go. Push them onto the worklist.
2265 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2266 }
2267 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002268 }
Mike Stump25cf7602009-09-09 15:08:12 +00002269
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002270 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002271 }
2272
Zhongxing Xuebcad732008-10-30 05:02:23 +00002273 if (Loc::IsLocType(T) || T->isIntegerType()) {
2274 assert (E->getNumInits() == 1);
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002275 ExplodedNodeSet Tmp;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002276 Expr* Init = E->getInit(0);
2277 Visit(Init, Pred, Tmp);
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002278 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
Zhongxing Xuebcad732008-10-30 05:02:23 +00002279 state = GetState(*I);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002280 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002281 }
2282 return;
2283 }
2284
Zhongxing Xuebcad732008-10-30 05:02:23 +00002285
2286 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2287 assert(0 && "unprocessed InitListExpr type");
2288}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002289
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002290/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2291void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002292 ExplodedNode* Pred,
2293 ExplodedNodeSet& Dst) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002294 QualType T = Ex->getTypeOfArgument();
Mike Stump25cf7602009-09-09 15:08:12 +00002295 uint64_t amt;
2296
Ted Kremenekc3b12832008-03-15 03:13:20 +00002297 if (Ex->isSizeOf()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002298 if (T == getContext().VoidTy) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002299 // sizeof(void) == 1 byte.
2300 amt = 1;
2301 }
2302 else if (!T.getTypePtr()->isConstantSizeType()) {
2303 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002304 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002305 }
2306 else if (T->isObjCInterfaceType()) {
2307 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2308 // the compiler has laid out its representation. Just report Unknown
Mike Stump25cf7602009-09-09 15:08:12 +00002309 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002310 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002311 }
2312 else {
2313 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002314 amt = getContext().getTypeSize(T) / 8;
Mike Stump25cf7602009-09-09 15:08:12 +00002315 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002316 }
2317 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002318 amt = getContext().getTypeAlign(T) / 8;
Mike Stump25cf7602009-09-09 15:08:12 +00002319
Ted Kremenekf10f2882008-03-21 21:30:14 +00002320 MakeNode(Dst, Ex, Pred,
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002321 GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002322}
2323
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002324
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002325void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
2326 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002327
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002328 switch (U->getOpcode()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002329
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002330 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002331 break;
Mike Stump25cf7602009-09-09 15:08:12 +00002332
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002333 case UnaryOperator::Deref: {
Mike Stump25cf7602009-09-09 15:08:12 +00002334
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002335 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002336 ExplodedNodeSet Tmp;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002337 Visit(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002338
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002339 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump25cf7602009-09-09 15:08:12 +00002340
Ted Kremeneke66ba682009-02-13 01:45:31 +00002341 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002342 SVal location = state->getSVal(Ex);
Mike Stump25cf7602009-09-09 15:08:12 +00002343
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002344 if (asLValue)
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002345 MakeNode(Dst, U, *I, state->BindExpr(U, location),
Ted Kremenek0441f112009-05-07 18:27:16 +00002346 ProgramPoint::PostLValueKind);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002347 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002348 EvalLoad(Dst, U, *I, state, location);
Mike Stump25cf7602009-09-09 15:08:12 +00002349 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002350
2351 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002352 }
Mike Stump25cf7602009-09-09 15:08:12 +00002353
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002354 case UnaryOperator::Real: {
Mike Stump25cf7602009-09-09 15:08:12 +00002355
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002356 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002357 ExplodedNodeSet Tmp;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002358 Visit(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002359
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002360 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump25cf7602009-09-09 15:08:12 +00002361
Zhongxing Xu097fc982008-10-17 05:57:07 +00002362 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002363 if (Ex->getType()->isAnyComplexType()) {
2364 // Just report "Unknown."
2365 Dst.Add(*I);
2366 continue;
2367 }
Mike Stump25cf7602009-09-09 15:08:12 +00002368
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002369 // For all other types, UnaryOperator::Real is an identity operation.
2370 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002371 const GRState* state = GetState(*I);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002372 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump25cf7602009-09-09 15:08:12 +00002373 }
2374
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002375 return;
2376 }
Mike Stump25cf7602009-09-09 15:08:12 +00002377
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002378 case UnaryOperator::Imag: {
Mike Stump25cf7602009-09-09 15:08:12 +00002379
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002380 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002381 ExplodedNodeSet Tmp;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002382 Visit(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002383
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002384 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002385 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002386 if (Ex->getType()->isAnyComplexType()) {
2387 // Just report "Unknown."
2388 Dst.Add(*I);
2389 continue;
2390 }
Mike Stump25cf7602009-09-09 15:08:12 +00002391
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002392 // For all other types, UnaryOperator::Float returns 0.
2393 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002394 const GRState* state = GetState(*I);
Zhongxing Xue32c7652009-06-23 09:02:15 +00002395 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002396 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002397 }
Mike Stump25cf7602009-09-09 15:08:12 +00002398
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002399 return;
2400 }
Mike Stump25cf7602009-09-09 15:08:12 +00002401
2402 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002403 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002404 Dst.Add(Pred);
2405 return;
Mike Stump25cf7602009-09-09 15:08:12 +00002406
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002407 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002408 case UnaryOperator::Extension: {
Mike Stump25cf7602009-09-09 15:08:12 +00002409
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002410 // Unary "+" is a no-op, similar to a parentheses. We still have places
2411 // where it may be a block-level expression, so we need to
2412 // generate an extra node that just propagates the value of the
2413 // subexpression.
2414
2415 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002416 ExplodedNodeSet Tmp;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002417 Visit(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002418
2419 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002420 const GRState* state = GetState(*I);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002421 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002422 }
Mike Stump25cf7602009-09-09 15:08:12 +00002423
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002424 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002425 }
Mike Stump25cf7602009-09-09 15:08:12 +00002426
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002427 case UnaryOperator::AddrOf: {
Mike Stump25cf7602009-09-09 15:08:12 +00002428
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002429 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002430 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002431 ExplodedNodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002432 VisitLValue(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002433
2434 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002435 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002436 SVal V = state->getSVal(Ex);
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002437 state = state->BindExpr(U, V);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002438 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002439 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002440
Mike Stump25cf7602009-09-09 15:08:12 +00002441 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002442 }
Mike Stump25cf7602009-09-09 15:08:12 +00002443
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002444 case UnaryOperator::LNot:
2445 case UnaryOperator::Minus:
2446 case UnaryOperator::Not: {
Mike Stump25cf7602009-09-09 15:08:12 +00002447
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002448 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002449 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002450 ExplodedNodeSet Tmp;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002451 Visit(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002452
2453 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002454 const GRState* state = GetState(*I);
Mike Stump25cf7602009-09-09 15:08:12 +00002455
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002456 // Get the value of the subexpression.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002457 SVal V = state->getSVal(Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002458
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002459 if (V.isUnknownOrUndef()) {
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002460 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002461 continue;
2462 }
Mike Stump25cf7602009-09-09 15:08:12 +00002463
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002464// QualType DstT = getContext().getCanonicalType(U->getType());
2465// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump25cf7602009-09-09 15:08:12 +00002466//
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002467// if (DstT != SrcT) // Perform promotions.
Mike Stump25cf7602009-09-09 15:08:12 +00002468// V = EvalCast(V, DstT);
2469//
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002470// if (V.isUnknownOrUndef()) {
2471// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2472// continue;
2473// }
Mike Stump25cf7602009-09-09 15:08:12 +00002474
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002475 switch (U->getOpcode()) {
2476 default:
2477 assert(false && "Invalid Opcode.");
2478 break;
Mike Stump25cf7602009-09-09 15:08:12 +00002479
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002480 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002481 // FIXME: Do we need to handle promotions?
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002482 state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
Mike Stump25cf7602009-09-09 15:08:12 +00002483 break;
2484
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002485 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002486 // FIXME: Do we need to handle promotions?
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002487 state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
Mike Stump25cf7602009-09-09 15:08:12 +00002488 break;
2489
2490 case UnaryOperator::LNot:
2491
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002492 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2493 //
2494 // Note: technically we do "E == 0", but this is the same in the
2495 // transfer functions as "0 == E".
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002496 SVal Result;
Mike Stump25cf7602009-09-09 15:08:12 +00002497
Zhongxing Xu097fc982008-10-17 05:57:07 +00002498 if (isa<Loc>(V)) {
Zhongxing Xue32c7652009-06-23 09:02:15 +00002499 Loc X = ValMgr.makeNull();
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002500 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2501 U->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002502 }
2503 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002504 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002505 Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X,
2506 U->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002507 }
Mike Stump25cf7602009-09-09 15:08:12 +00002508
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002509 state = state->BindExpr(U, Result);
Mike Stump25cf7602009-09-09 15:08:12 +00002510
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002511 break;
2512 }
Mike Stump25cf7602009-09-09 15:08:12 +00002513
Ted Kremeneke66ba682009-02-13 01:45:31 +00002514 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002515 }
Mike Stump25cf7602009-09-09 15:08:12 +00002516
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002517 return;
2518 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002519 }
2520
2521 // Handle ++ and -- (both pre- and post-increment).
2522
2523 assert (U->isIncrementDecrementOp());
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002524 ExplodedNodeSet Tmp;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002525 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002526 VisitLValue(Ex, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002527
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002528 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump25cf7602009-09-09 15:08:12 +00002529
Ted Kremeneke66ba682009-02-13 01:45:31 +00002530 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002531 SVal V1 = state->getSVal(Ex);
Mike Stump25cf7602009-09-09 15:08:12 +00002532
2533 // Perform a load.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002534 ExplodedNodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002535 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002536
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002537 for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
Mike Stump25cf7602009-09-09 15:08:12 +00002538
Ted Kremeneke66ba682009-02-13 01:45:31 +00002539 state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002540 SVal V2 = state->getSVal(Ex);
Mike Stump25cf7602009-09-09 15:08:12 +00002541
2542 // Propagate unknown and undefined values.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002543 if (V2.isUnknownOrUndef()) {
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002544 MakeNode(Dst, U, *I2, state->BindExpr(U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002545 continue;
2546 }
Mike Stump25cf7602009-09-09 15:08:12 +00002547
2548 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002549 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2550 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002551
Zhongxing Xu5f41efc2009-08-05 02:51:59 +00002552 // If the UnaryOperator has non-location type, use its type to create the
2553 // constant value. If the UnaryOperator has location type, create the
2554 // constant with int type and pointer width.
2555 SVal RHS;
2556
2557 if (U->getType()->isAnyPointerType())
2558 RHS = ValMgr.makeIntValWithPtrWidth(1, false);
2559 else
2560 RHS = ValMgr.makeIntVal(1, U->getType());
2561
Mike Stump25cf7602009-09-09 15:08:12 +00002562 SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
2563
Ted Kremenek607415e2009-03-20 20:10:45 +00002564 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek65411632009-04-21 22:38:05 +00002565 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002566 Result = ValMgr.getConjuredSymbolVal(Ex,
2567 Builder->getCurrentBlockCount());
Mike Stump25cf7602009-09-09 15:08:12 +00002568
Ted Kremenek65411632009-04-21 22:38:05 +00002569 // If the value is a location, ++/-- should always preserve
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002570 // non-nullness. Check if the original value was non-null, and if so
Mike Stump25cf7602009-09-09 15:08:12 +00002571 // propagate that constraint.
Ted Kremenek65411632009-04-21 22:38:05 +00002572 if (Loc::IsLocType(U->getType())) {
Zhongxing Xuc890e332009-05-20 09:00:16 +00002573 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenek65411632009-04-21 22:38:05 +00002574 ValMgr.makeZeroVal(U->getType()),
Mike Stump25cf7602009-09-09 15:08:12 +00002575 getContext().IntTy);
2576
Ted Kremenek70970bf2009-06-18 22:57:13 +00002577 if (!state->assume(Constraint, true)) {
Ted Kremenek65411632009-04-21 22:38:05 +00002578 // It isn't feasible for the original value to be null.
2579 // Propagate this constraint.
Zhongxing Xuc890e332009-05-20 09:00:16 +00002580 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenek65411632009-04-21 22:38:05 +00002581 ValMgr.makeZeroVal(U->getType()),
2582 getContext().IntTy);
Mike Stump25cf7602009-09-09 15:08:12 +00002583
Ted Kremenek70970bf2009-06-18 22:57:13 +00002584 state = state->assume(Constraint, false);
2585 assert(state);
Mike Stump25cf7602009-09-09 15:08:12 +00002586 }
2587 }
Ted Kremenek65411632009-04-21 22:38:05 +00002588 }
Mike Stump25cf7602009-09-09 15:08:12 +00002589
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002590 state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002591
Mike Stump25cf7602009-09-09 15:08:12 +00002592 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002593 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002594 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002595 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002596}
2597
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002598void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek31803c32008-03-17 21:11:24 +00002599 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump25cf7602009-09-09 15:08:12 +00002600}
Ted Kremenek31803c32008-03-17 21:11:24 +00002601
2602void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2603 AsmStmt::outputs_iterator I,
2604 AsmStmt::outputs_iterator E,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002605 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek31803c32008-03-17 21:11:24 +00002606 if (I == E) {
2607 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2608 return;
2609 }
Mike Stump25cf7602009-09-09 15:08:12 +00002610
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002611 ExplodedNodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002612 VisitLValue(*I, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002613
Ted Kremenek31803c32008-03-17 21:11:24 +00002614 ++I;
Mike Stump25cf7602009-09-09 15:08:12 +00002615
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002616 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenek31803c32008-03-17 21:11:24 +00002617 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2618}
2619
2620void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2621 AsmStmt::inputs_iterator I,
2622 AsmStmt::inputs_iterator E,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002623 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek31803c32008-03-17 21:11:24 +00002624 if (I == E) {
Mike Stump25cf7602009-09-09 15:08:12 +00002625
Ted Kremenek31803c32008-03-17 21:11:24 +00002626 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002627 // should evaluate to Locs. Nuke all of their values.
Mike Stump25cf7602009-09-09 15:08:12 +00002628
Ted Kremenek31803c32008-03-17 21:11:24 +00002629 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2630 // which interprets the inline asm and stores proper results in the
2631 // outputs.
Mike Stump25cf7602009-09-09 15:08:12 +00002632
Ted Kremeneke66ba682009-02-13 01:45:31 +00002633 const GRState* state = GetState(Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00002634
Ted Kremenek31803c32008-03-17 21:11:24 +00002635 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2636 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump25cf7602009-09-09 15:08:12 +00002637
2638 SVal X = state->getSVal(*OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002639 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump25cf7602009-09-09 15:08:12 +00002640
Zhongxing Xu097fc982008-10-17 05:57:07 +00002641 if (isa<Loc>(X))
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002642 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002643 }
Mike Stump25cf7602009-09-09 15:08:12 +00002644
Ted Kremeneke66ba682009-02-13 01:45:31 +00002645 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002646 return;
2647 }
Mike Stump25cf7602009-09-09 15:08:12 +00002648
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002649 ExplodedNodeSet Tmp;
Ted Kremenek31803c32008-03-17 21:11:24 +00002650 Visit(*I, Pred, Tmp);
Mike Stump25cf7602009-09-09 15:08:12 +00002651
Ted Kremenek31803c32008-03-17 21:11:24 +00002652 ++I;
Mike Stump25cf7602009-09-09 15:08:12 +00002653
Ted Kremenek4e38d702009-09-03 03:02:58 +00002654 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenek31803c32008-03-17 21:11:24 +00002655 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2656}
2657
Ted Kremenek4e38d702009-09-03 03:02:58 +00002658void GRExprEngine::EvalReturn(ExplodedNodeSet& Dst, ReturnStmt* S,
2659 ExplodedNode* Pred) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002660 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump25cf7602009-09-09 15:08:12 +00002661
2662 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002663
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002664 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2665 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002666
Ted Kremenekc7469542008-07-17 23:15:45 +00002667 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Mike Stump25cf7602009-09-09 15:08:12 +00002668
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002669 // Handle the case where no nodes where generated.
Mike Stump25cf7602009-09-09 15:08:12 +00002670
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002671 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002672 MakeNode(Dst, S, Pred, GetState(Pred));
2673}
2674
Ted Kremenek4e38d702009-09-03 03:02:58 +00002675void GRExprEngine::VisitReturnStmt(ReturnStmt* S, ExplodedNode* Pred,
2676 ExplodedNodeSet& Dst) {
Ted Kremenek108048c2008-03-31 15:02:58 +00002677
2678 Expr* R = S->getRetValue();
Mike Stump25cf7602009-09-09 15:08:12 +00002679
Ted Kremenek108048c2008-03-31 15:02:58 +00002680 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002681 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002682 return;
2683 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002684
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002685 ExplodedNodeSet Tmp;
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002686 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002687
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002688 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002689 SVal X = (*I)->getState()->getSVal(R);
Mike Stump25cf7602009-09-09 15:08:12 +00002690
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002691 // Check if we return the address of a stack variable.
2692 if (isa<loc::MemRegionVal>(X)) {
2693 // Determine if the value is on the stack.
2694 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Mike Stump25cf7602009-09-09 15:08:12 +00002695
Ted Kremenekdd2ec492009-06-23 18:05:21 +00002696 if (R && R->hasStackStorage()) {
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002697 // Create a special node representing the error.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002698 if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) {
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002699 N->markAsSink();
2700 RetsStackAddr.insert(N);
2701 }
2702 continue;
2703 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002704 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002705 // Check if we return an undefined value.
2706 else if (X.isUndef()) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002707 if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) {
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002708 N->markAsSink();
2709 RetsUndef.insert(N);
2710 }
2711 continue;
2712 }
Mike Stump25cf7602009-09-09 15:08:12 +00002713
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002714 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002715 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002716}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002717
Ted Kremenekca5f6202008-04-15 23:06:53 +00002718//===----------------------------------------------------------------------===//
2719// Transfer functions: Binary operators.
2720//===----------------------------------------------------------------------===//
2721
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002722void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002723 ExplodedNode* Pred,
2724 ExplodedNodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002725
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002726 ExplodedNodeSet Tmp1;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002727 Expr* LHS = B->getLHS()->IgnoreParens();
2728 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump25cf7602009-09-09 15:08:12 +00002729
Fariborz Jahanian128cdc52009-08-20 17:02:02 +00002730 // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
2731 if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
Mike Stump25cf7602009-09-09 15:08:12 +00002732 Visit(RHS, Pred, Dst);
Ted Kremenek52510d82008-12-06 02:39:30 +00002733 return;
2734 }
Mike Stump25cf7602009-09-09 15:08:12 +00002735
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002736 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002737 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002738 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002739 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002740
Ted Kremenek4e38d702009-09-03 03:02:58 +00002741 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002742
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002743 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Mike Stump25cf7602009-09-09 15:08:12 +00002744
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002745 // Process the RHS.
Mike Stump25cf7602009-09-09 15:08:12 +00002746
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002747 ExplodedNodeSet Tmp2;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002748 Visit(RHS, *I1, Tmp2);
Zhongxing Xue49d8f02009-09-02 13:26:26 +00002749
2750 ExplodedNodeSet CheckedSet;
2751 CheckerVisit(B, CheckedSet, Tmp2, true);
Mike Stump25cf7602009-09-09 15:08:12 +00002752
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002753 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump25cf7602009-09-09 15:08:12 +00002754
2755 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xue49d8f02009-09-02 13:26:26 +00002756 I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002757
Ted Kremeneke66ba682009-02-13 01:45:31 +00002758 const GRState* state = GetState(*I2);
2759 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002760
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002761 SVal RightV = state->getSVal(RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002762 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump25cf7602009-09-09 15:08:12 +00002763
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002764 switch (Op) {
Mike Stump25cf7602009-09-09 15:08:12 +00002765
Ted Kremenekf031b872008-01-23 19:59:44 +00002766 case BinaryOperator::Assign: {
Mike Stump25cf7602009-09-09 15:08:12 +00002767
Ted Kremenekd4676512008-03-12 21:45:47 +00002768 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002769 // FIXME: Handle structs.
2770 QualType T = RHS->getType();
Mike Stump25cf7602009-09-09 15:08:12 +00002771
2772 if ((RightV.isUnknown() ||
2773 !getConstraintManager().canReasonAbout(RightV))
2774 && (Loc::IsLocType(T) ||
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002775 (T->isScalarType() && T->isIntegerType()))) {
Mike Stump25cf7602009-09-09 15:08:12 +00002776 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002777 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002778 }
Mike Stump25cf7602009-09-09 15:08:12 +00002779
Ted Kremenekd4676512008-03-12 21:45:47 +00002780 // Simulate the effects of a "store": bind the value of the RHS
Mike Stump25cf7602009-09-09 15:08:12 +00002781 // to the L-Value represented by the LHS.
2782 EvalStore(Dst, B, LHS, *I2, state->BindExpr(B, RightV),
Zhongxing Xub4fa87b2009-08-25 06:51:30 +00002783 LeftV, RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002784 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002785 }
Mike Stump25cf7602009-09-09 15:08:12 +00002786
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002787 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002788
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002789 default: {
Mike Stump25cf7602009-09-09 15:08:12 +00002790
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002791 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002792 break;
Mike Stump25cf7602009-09-09 15:08:12 +00002793
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002794 // Process non-assignments except commas or short-circuited
Mike Stump25cf7602009-09-09 15:08:12 +00002795 // logical expressions (LAnd and LOr).
Zhongxing Xuc890e332009-05-20 09:00:16 +00002796 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Mike Stump25cf7602009-09-09 15:08:12 +00002797
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002798 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002799 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002800 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002801 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002802 }
2803 else
2804 Dst.Add(*I2);
Mike Stump25cf7602009-09-09 15:08:12 +00002805
Ted Kremenekb8782e12008-02-21 19:15:37 +00002806 continue;
2807 }
Mike Stump25cf7602009-09-09 15:08:12 +00002808
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002809 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002810
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002811 // The operands were *not* undefined, but the result is undefined.
2812 // This is a special node that should be flagged as an error.
Mike Stump25cf7602009-09-09 15:08:12 +00002813
Ted Kremenek5b76b832009-08-27 01:39:13 +00002814 if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I2)){
Mike Stump25cf7602009-09-09 15:08:12 +00002815 UndefNode->markAsSink();
Ted Kremenekc2d07202008-02-28 20:32:03 +00002816 UndefResults.insert(UndefNode);
2817 }
Mike Stump25cf7602009-09-09 15:08:12 +00002818
Ted Kremenekc2d07202008-02-28 20:32:03 +00002819 continue;
2820 }
Mike Stump25cf7602009-09-09 15:08:12 +00002821
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002822 // Otherwise, create a new node.
Mike Stump25cf7602009-09-09 15:08:12 +00002823
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002824 MakeNode(Dst, B, *I2, state->BindExpr(B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002825 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00002826 }
Ted Kremenekf031b872008-01-23 19:59:44 +00002827 }
Mike Stump25cf7602009-09-09 15:08:12 +00002828
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002829 assert (B->isCompoundAssignmentOp());
2830
Ted Kremenek570882a2009-02-07 00:52:24 +00002831 switch (Op) {
2832 default:
2833 assert(0 && "Invalid opcode for compound assignment.");
2834 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2835 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2836 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2837 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2838 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2839 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2840 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2841 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2842 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2843 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00002844 }
Mike Stump25cf7602009-09-09 15:08:12 +00002845
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002846 // Perform a load (the LHS). This performs the checks for
2847 // null dereferences, and so on.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002848 ExplodedNodeSet Tmp3;
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002849 SVal location = state->getSVal(LHS);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002850 EvalLoad(Tmp3, LHS, *I2, state, location);
Mike Stump25cf7602009-09-09 15:08:12 +00002851
2852 for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
Zhongxing Xue49d8f02009-09-02 13:26:26 +00002853 ++I3) {
Mike Stump25cf7602009-09-09 15:08:12 +00002854
Ted Kremeneke66ba682009-02-13 01:45:31 +00002855 state = GetState(*I3);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002856 SVal V = state->getSVal(LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002857
Mike Stump25cf7602009-09-09 15:08:12 +00002858 // Propagate undefined values (left-side).
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002859 if (V.isUndef()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002860 EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, V),
Zhongxing Xub4fa87b2009-08-25 06:51:30 +00002861 location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002862 continue;
2863 }
Mike Stump25cf7602009-09-09 15:08:12 +00002864
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002865 // Propagate unknown values (left and right-side).
2866 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002867 EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, UnknownVal()),
Ted Kremenek5b76b832009-08-27 01:39:13 +00002868 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002869 continue;
2870 }
2871
2872 // At this point:
2873 //
2874 // The LHS is not Undef/Unknown.
2875 // The RHS is not Unknown.
Mike Stump25cf7602009-09-09 15:08:12 +00002876
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002877 // Get the computation type.
Ted Kremenek6cd31072009-07-21 21:03:30 +00002878 QualType CTy =
2879 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002880 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00002881
Ted Kremenek6cd31072009-07-21 21:03:30 +00002882 QualType CLHSTy =
2883 cast<CompoundAssignOperator>(B)->getComputationLHSType();
2884 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00002885
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002886 QualType LTy = getContext().getCanonicalType(LHS->getType());
2887 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00002888
2889 // Promote LHS.
Ted Kremenek6cd31072009-07-21 21:03:30 +00002890 llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00002891
Mike Stump25cf7602009-09-09 15:08:12 +00002892 // Evaluate operands and promote to result type.
2893 if (RightV.isUndef()) {
2894 // Propagate undefined values (right-side).
Ted Kremeneka7b9c662009-08-27 22:17:37 +00002895 EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, RightV), location,
Ted Kremenek5b76b832009-08-27 01:39:13 +00002896 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002897 continue;
2898 }
Mike Stump25cf7602009-09-09 15:08:12 +00002899
2900 // Compute the result of the operation.
Ted Kremenek6cd31072009-07-21 21:03:30 +00002901 SVal Result;
2902 llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
2903 RightV, CTy),
2904 state, B->getType(), CTy);
Mike Stump25cf7602009-09-09 15:08:12 +00002905
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002906 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002907 // The operands were not undefined, but the result is undefined.
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002908 if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I3)) {
Mike Stump25cf7602009-09-09 15:08:12 +00002909 UndefNode->markAsSink();
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002910 UndefResults.insert(UndefNode);
2911 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002912 continue;
2913 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002914
2915 // EXPERIMENTAL: "Conjured" symbols.
2916 // FIXME: Handle structs.
Mike Stump25cf7602009-09-09 15:08:12 +00002917
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002918 SVal LHSVal;
Mike Stump25cf7602009-09-09 15:08:12 +00002919
2920 if ((Result.isUnknown() ||
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002921 !getConstraintManager().canReasonAbout(Result))
Mike Stump25cf7602009-09-09 15:08:12 +00002922 && (Loc::IsLocType(CTy)
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002923 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Mike Stump25cf7602009-09-09 15:08:12 +00002924
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002925 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump25cf7602009-09-09 15:08:12 +00002926
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002927 // The symbolic value is actually for the type of the left-hand side
2928 // expression, not the computation type, as this is the value the
2929 // LValue on the LHS will bind to.
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002930 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Mike Stump25cf7602009-09-09 15:08:12 +00002931
Zhongxing Xu5c70c772008-11-23 05:52:28 +00002932 // However, we need to convert the symbol to the computation type.
Ted Kremenek6cd31072009-07-21 21:03:30 +00002933 llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002934 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002935 else {
2936 // The left-hand side may bind to a different value then the
2937 // computation type.
Ted Kremenek6cd31072009-07-21 21:03:30 +00002938 llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002939 }
Mike Stump25cf7602009-09-09 15:08:12 +00002940
2941 EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, Result),
Zhongxing Xub4fa87b2009-08-25 06:51:30 +00002942 location, LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002943 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00002944 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00002945 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00002946}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00002947
2948//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00002949// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00002950//===----------------------------------------------------------------------===//
2951
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002952#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002953static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00002954static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00002955
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002956namespace llvm {
2957template<>
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002958struct VISIBILITY_HIDDEN DOTGraphTraits<ExplodedNode*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002959 public DefaultDOTGraphTraits {
Mike Stump25cf7602009-09-09 15:08:12 +00002960
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002961 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump25cf7602009-09-09 15:08:12 +00002962
Ted Kremeneka853de62008-02-14 22:54:53 +00002963 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00002964 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00002965 GraphPrintCheckerState->isUndefDeref(N) ||
2966 GraphPrintCheckerState->isUndefStore(N) ||
2967 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00002968 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2969 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00002970 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00002971 GraphPrintCheckerState->isBadCall(N) ||
2972 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00002973 return "color=\"red\",style=\"filled\"";
Mike Stump25cf7602009-09-09 15:08:12 +00002974
Ted Kremenekc2d07202008-02-28 20:32:03 +00002975 if (GraphPrintCheckerState->isNoReturnCall(N))
2976 return "color=\"blue\",style=\"filled\"";
Mike Stump25cf7602009-09-09 15:08:12 +00002977
Ted Kremeneka853de62008-02-14 22:54:53 +00002978 return "";
2979 }
Mike Stump25cf7602009-09-09 15:08:12 +00002980
Zhongxing Xu0ace2712009-08-06 12:48:26 +00002981 static std::string getNodeLabel(const ExplodedNode* N, void*,bool ShortNames){
Mike Stump25cf7602009-09-09 15:08:12 +00002982
Ted Kremenekdd04ed62009-06-24 23:06:47 +00002983 std::string sbuf;
2984 llvm::raw_string_ostream Out(sbuf);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00002985
2986 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002987 ProgramPoint Loc = N->getLocation();
Mike Stump25cf7602009-09-09 15:08:12 +00002988
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002989 switch (Loc.getKind()) {
2990 case ProgramPoint::BlockEntranceKind:
Mike Stump25cf7602009-09-09 15:08:12 +00002991 Out << "Block Entrance: B"
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002992 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2993 break;
Mike Stump25cf7602009-09-09 15:08:12 +00002994
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002995 case ProgramPoint::BlockExitKind:
2996 assert (false);
2997 break;
Mike Stump25cf7602009-09-09 15:08:12 +00002998
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002999 default: {
Ted Kremenekc08c21e2009-07-22 22:35:28 +00003000 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
3001 const Stmt* S = L->getStmt();
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003002 SourceLocation SLoc = S->getLocStart();
3003
Mike Stump25cf7602009-09-09 15:08:12 +00003004 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattner7099c782009-06-30 01:26:17 +00003005 LangOptions LO; // FIXME.
3006 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump25cf7602009-09-09 15:08:12 +00003007
3008 if (SLoc.isFileID()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003009 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003010 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3011 << " col="
3012 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3013 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003014 }
Mike Stump25cf7602009-09-09 15:08:12 +00003015
Ted Kremenekc08c21e2009-07-22 22:35:28 +00003016 if (isa<PreStmt>(Loc))
Mike Stump25cf7602009-09-09 15:08:12 +00003017 Out << "\\lPreStmt\\l;";
Ted Kremenekc08c21e2009-07-22 22:35:28 +00003018 else if (isa<PostLoad>(Loc))
Ted Kremenek0441f112009-05-07 18:27:16 +00003019 Out << "\\lPostLoad\\l;";
3020 else if (isa<PostStore>(Loc))
3021 Out << "\\lPostStore\\l";
3022 else if (isa<PostLValue>(Loc))
3023 Out << "\\lPostLValue\\l";
3024 else if (isa<PostLocationChecksSucceed>(Loc))
3025 Out << "\\lPostLocationChecksSucceed\\l";
3026 else if (isa<PostNullCheckFailed>(Loc))
3027 Out << "\\lPostNullCheckFailed\\l";
Mike Stump25cf7602009-09-09 15:08:12 +00003028
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003029 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3030 Out << "\\|Implicit-Null Dereference.\\l";
3031 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3032 Out << "\\|Explicit-Null Dereference.\\l";
3033 else if (GraphPrintCheckerState->isUndefDeref(N))
3034 Out << "\\|Dereference of undefialied value.\\l";
3035 else if (GraphPrintCheckerState->isUndefStore(N))
3036 Out << "\\|Store to Undefined Loc.";
3037 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3038 Out << "\\|Explicit divide-by zero or undefined value.";
3039 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3040 Out << "\\|Implicit divide-by zero or undefined value.";
3041 else if (GraphPrintCheckerState->isUndefResult(N))
3042 Out << "\\|Result of operation is undefined.";
3043 else if (GraphPrintCheckerState->isNoReturnCall(N))
3044 Out << "\\|Call to function marked \"noreturn\".";
3045 else if (GraphPrintCheckerState->isBadCall(N))
3046 Out << "\\|Call to NULL/Undefined.";
3047 else if (GraphPrintCheckerState->isUndefArg(N))
3048 Out << "\\|Argument in call is undefined";
Mike Stump25cf7602009-09-09 15:08:12 +00003049
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003050 break;
3051 }
3052
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003053 const BlockEdge& E = cast<BlockEdge>(Loc);
3054 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3055 << E.getDst()->getBlockID() << ')';
Mike Stump25cf7602009-09-09 15:08:12 +00003056
Ted Kremenek90960972008-01-30 23:03:39 +00003057 if (Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump25cf7602009-09-09 15:08:12 +00003058
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003059 SourceLocation SLoc = T->getLocStart();
Mike Stump25cf7602009-09-09 15:08:12 +00003060
Ted Kremenek90960972008-01-30 23:03:39 +00003061 Out << "\\|Terminator: ";
Chris Lattner7099c782009-06-30 01:26:17 +00003062 LangOptions LO; // FIXME.
3063 E.getSrc()->printTerminator(Out, LO);
Mike Stump25cf7602009-09-09 15:08:12 +00003064
Ted Kremenekf97c6682008-03-09 03:30:59 +00003065 if (SLoc.isFileID()) {
3066 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003067 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3068 << " col="
3069 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003070 }
Mike Stump25cf7602009-09-09 15:08:12 +00003071
Ted Kremenekaee121c2008-02-13 23:08:21 +00003072 if (isa<SwitchStmt>(T)) {
3073 Stmt* Label = E.getDst()->getLabel();
Mike Stump25cf7602009-09-09 15:08:12 +00003074
3075 if (Label) {
Ted Kremenekaee121c2008-02-13 23:08:21 +00003076 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3077 Out << "\\lcase ";
Chris Lattner7099c782009-06-30 01:26:17 +00003078 LangOptions LO; // FIXME.
3079 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump25cf7602009-09-09 15:08:12 +00003080
Ted Kremenekaee121c2008-02-13 23:08:21 +00003081 if (Stmt* RHS = C->getRHS()) {
3082 Out << " .. ";
Chris Lattner7099c782009-06-30 01:26:17 +00003083 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenekaee121c2008-02-13 23:08:21 +00003084 }
Mike Stump25cf7602009-09-09 15:08:12 +00003085
Ted Kremenekaee121c2008-02-13 23:08:21 +00003086 Out << ":";
3087 }
3088 else {
3089 assert (isa<DefaultStmt>(Label));
3090 Out << "\\ldefault:";
3091 }
3092 }
Mike Stump25cf7602009-09-09 15:08:12 +00003093 else
Ted Kremenekaee121c2008-02-13 23:08:21 +00003094 Out << "\\l(implicit) default:";
3095 }
3096 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003097 // FIXME
3098 }
3099 else {
3100 Out << "\\lCondition: ";
3101 if (*E.getSrc()->succ_begin() == E.getDst())
3102 Out << "true";
3103 else
Mike Stump25cf7602009-09-09 15:08:12 +00003104 Out << "false";
Ted Kremenek90960972008-01-30 23:03:39 +00003105 }
Mike Stump25cf7602009-09-09 15:08:12 +00003106
Ted Kremenek90960972008-01-30 23:03:39 +00003107 Out << "\\l";
3108 }
Mike Stump25cf7602009-09-09 15:08:12 +00003109
Ted Kremenekb31af242008-02-28 09:25:22 +00003110 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3111 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003112 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003113 }
3114 }
Mike Stump25cf7602009-09-09 15:08:12 +00003115
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003116 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003117
Ted Kremenek18a636d2009-06-18 01:23:53 +00003118 const GRState *state = N->getState();
3119 state->printDOT(Out);
Mike Stump25cf7602009-09-09 15:08:12 +00003120
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003121 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003122 return Out.str();
3123 }
3124};
Mike Stump25cf7602009-09-09 15:08:12 +00003125} // end llvm namespace
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003126#endif
3127
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003128#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003129template <typename ITERATOR>
Zhongxing Xu0ace2712009-08-06 12:48:26 +00003130ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003131
Zhongxing Xu0ace2712009-08-06 12:48:26 +00003132template <> ExplodedNode*
3133GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3134 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003135 return I->first;
3136}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003137#endif
3138
3139void GRExprEngine::ViewGraph(bool trim) {
Mike Stump25cf7602009-09-09 15:08:12 +00003140#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003141 if (trim) {
Zhongxing Xu0ace2712009-08-06 12:48:26 +00003142 std::vector<ExplodedNode*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003143
3144 // Flush any outstanding reports to make sure we cover all the nodes.
3145 // This does not cause them to get displayed.
3146 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3147 const_cast<BugType*>(*I)->FlushReports(BR);
3148
3149 // Iterate through the reports and get their nodes.
3150 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
Ted Kremenek4e38d702009-09-03 03:02:58 +00003151 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
Mike Stump25cf7602009-09-09 15:08:12 +00003152 I2!=E2; ++I2) {
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003153 const BugReportEquivClass& EQ = *I2;
3154 const BugReport &R = **EQ.begin();
Zhongxing Xu0ace2712009-08-06 12:48:26 +00003155 ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003156 if (N) Src.push_back(N);
3157 }
3158 }
Mike Stump25cf7602009-09-09 15:08:12 +00003159
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003160 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003161 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003162 else {
3163 GraphPrintCheckerState = this;
3164 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003165
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003166 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Mike Stump25cf7602009-09-09 15:08:12 +00003167
Ted Kremeneke44a8302008-03-11 18:25:33 +00003168 GraphPrintCheckerState = NULL;
3169 GraphPrintSourceManager = NULL;
3170 }
3171#endif
3172}
3173
Zhongxing Xu0ace2712009-08-06 12:48:26 +00003174void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003175#ifndef NDEBUG
3176 GraphPrintCheckerState = this;
3177 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump25cf7602009-09-09 15:08:12 +00003178
Zhongxing Xu0ace2712009-08-06 12:48:26 +00003179 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003180
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003181 if (!TrimmedG.get())
Benjamin Kramer2ec17292009-08-23 12:08:50 +00003182 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003183 else
Mike Stump25cf7602009-09-09 15:08:12 +00003184 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
3185
Ted Kremenek428d39e2008-01-30 23:24:39 +00003186 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003187 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003188#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003189}