blob: c7b803679e080d720f0024a5f15fcbba47d4badc [file] [log] [blame]
Ted Kremenek64de2072008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremeneka0be8262008-01-31 02:35:41 +00002//
Ted Kremenek6f4a9ef2008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekde8d62b2008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek64de2072008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekde8d62b2008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek64de2072008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke68c0fc2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek49513cc2009-07-22 21:43:51 +000018#include "clang/Analysis/PathSensitive/Checker.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek6947a792008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek91076ca2009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenekc50e1a12008-07-11 18:37:32 +000025#include "llvm/Support/Compiler.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000027#include "llvm/ADT/ImmutableList.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000028#include "llvm/ADT/StringSwitch.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000029
Ted Kremenekc0258412008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
Ted Kremenekc0258412008-02-27 06:07:00 +000032#endif
33
Ted Kremenekbd8957b2008-02-14 22:16:04 +000034using namespace clang;
35using llvm::dyn_cast;
36using llvm::cast;
37using llvm::APSInt;
Ted Kremenek0a8d3762008-01-23 19:59:44 +000038
Ted Kremenek667cacb2008-04-15 23:06:53 +000039//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +000040// Batch auditor. DEPRECATED.
Ted Kremenek667cacb2008-04-15 23:06:53 +000041//===----------------------------------------------------------------------===//
42
Ted Kremenekc50e1a12008-07-11 18:37:32 +000043namespace {
44
45class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
46 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
47 typedef llvm::DenseMap<void*,Checks> MapTy;
Mike Stump11289f42009-09-09 15:08:12 +000048
Ted Kremenekc50e1a12008-07-11 18:37:32 +000049 MapTy M;
50 Checks::Factory F;
Ted Kremenek4967c892009-03-30 17:53:05 +000051 Checks AllStmts;
Ted Kremenekc50e1a12008-07-11 18:37:32 +000052
53public:
Ted Kremenek4967c892009-03-30 17:53:05 +000054 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
55 F(Alloc), AllStmts(F.GetEmptyList()) {}
Mike Stump11289f42009-09-09 15:08:12 +000056
Ted Kremenekc50e1a12008-07-11 18:37:32 +000057 virtual ~MappedBatchAuditor() {
58 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
Mike Stump11289f42009-09-09 15:08:12 +000059
Ted Kremenekc50e1a12008-07-11 18:37:32 +000060 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
61 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
62
63 GRSimpleAPICheck* check = *I;
Mike Stump11289f42009-09-09 15:08:12 +000064
Ted Kremenekc50e1a12008-07-11 18:37:32 +000065 if (AlreadyVisited.count(check))
66 continue;
Mike Stump11289f42009-09-09 15:08:12 +000067
Ted Kremenekc50e1a12008-07-11 18:37:32 +000068 AlreadyVisited.insert(check);
69 delete check;
70 }
71 }
72
Ted Kremenek4967c892009-03-30 17:53:05 +000073 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekc50e1a12008-07-11 18:37:32 +000074 assert (A && "Check cannot be null.");
75 void* key = reinterpret_cast<void*>((uintptr_t) C);
76 MapTy::iterator I = M.find(key);
77 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
78 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Ted Kremenek4967c892009-03-30 17:53:05 +000080 void AddCheck(GRSimpleAPICheck *A) {
81 assert (A && "Check cannot be null.");
Mike Stump11289f42009-09-09 15:08:12 +000082 AllStmts = F.Concat(A, AllStmts);
Ted Kremenek4967c892009-03-30 17:53:05 +000083 }
Ted Kremenekfc5d0672009-02-04 23:49:09 +000084
Zhongxing Xu107f7592009-08-06 12:48:26 +000085 virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) {
Ted Kremenek4967c892009-03-30 17:53:05 +000086 // First handle the auditors that accept all statements.
87 bool isSink = false;
88 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
89 isSink |= (*I)->Audit(N, VMgr);
Mike Stump11289f42009-09-09 15:08:12 +000090
Ted Kremenek4967c892009-03-30 17:53:05 +000091 // Next handle the auditors that accept only specific statements.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +000092 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekc50e1a12008-07-11 18:37:32 +000093 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
94 MapTy::iterator MI = M.find(key);
Mike Stump11289f42009-09-09 15:08:12 +000095 if (MI != M.end()) {
Ted Kremenek4967c892009-03-30 17:53:05 +000096 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
97 isSink |= (*I)->Audit(N, VMgr);
98 }
Mike Stump11289f42009-09-09 15:08:12 +000099
100 return isSink;
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000101 }
102};
103
104} // end anonymous namespace
105
106//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +0000107// Checker worklist routines.
108//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000109
110void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000111 ExplodedNodeSet &Src, bool isPrevisit) {
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek49513cc2009-07-22 21:43:51 +0000113 if (Checkers.empty()) {
114 Dst = Src;
115 return;
116 }
Mike Stump11289f42009-09-09 15:08:12 +0000117
Zhongxing Xu107f7592009-08-06 12:48:26 +0000118 ExplodedNodeSet Tmp;
119 ExplodedNodeSet *PrevSet = &Src;
Mike Stump11289f42009-09-09 15:08:12 +0000120
Ted Kremenek6f2a7052009-10-30 17:47:32 +0000121 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
122 {
123 ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst
Zhongxing Xu107f7592009-08-06 12:48:26 +0000124 : (PrevSet == &Tmp) ? &Src : &Tmp;
Ted Kremenek6f2a7052009-10-30 17:47:32 +0000125
Ted Kremenek49513cc2009-07-22 21:43:51 +0000126 CurrSet->clear();
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000127 void *tag = I->first;
128 Checker *checker = I->second;
Mike Stump11289f42009-09-09 15:08:12 +0000129
Zhongxing Xu107f7592009-08-06 12:48:26 +0000130 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenek49513cc2009-07-22 21:43:51 +0000131 NI != NE; ++NI)
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000132 checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, tag, isPrevisit);
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenek49513cc2009-07-22 21:43:51 +0000134 // Update which NodeSet is the current one.
135 PrevSet = CurrSet;
136 }
137
138 // Don't autotransition. The CheckerContext objects should do this
139 // automatically.
140}
141
Ted Kremenekef910042009-11-04 04:24:16 +0000142// FIXME: This is largely copy-paste from CheckerVisit(). Need to
143// unify.
Ted Kremenek209e31b2009-11-05 00:42:23 +0000144void GRExprEngine::CheckerVisitBind(const Stmt *AssignE, const Stmt *StoreE,
145 ExplodedNodeSet &Dst,
Ted Kremenekef910042009-11-04 04:24:16 +0000146 ExplodedNodeSet &Src,
147 SVal location, SVal val, bool isPrevisit) {
148
149 if (Checkers.empty()) {
150 Dst = Src;
151 return;
152 }
153
154 ExplodedNodeSet Tmp;
155 ExplodedNodeSet *PrevSet = &Src;
156
157 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
158 {
159 ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst
160 : (PrevSet == &Tmp) ? &Src : &Tmp;
161
162 CurrSet->clear();
163 void *tag = I->first;
164 Checker *checker = I->second;
165
166 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
167 NI != NE; ++NI)
Ted Kremenek209e31b2009-11-05 00:42:23 +0000168 checker->GR_VisitBind(*CurrSet, *Builder, *this, AssignE, StoreE,
169 *NI, tag, location, val, isPrevisit);
Ted Kremenekef910042009-11-04 04:24:16 +0000170
171 // Update which NodeSet is the current one.
172 PrevSet = CurrSet;
173 }
174
175 // Don't autotransition. The CheckerContext objects should do this
176 // automatically.
177}
Ted Kremenek49513cc2009-07-22 21:43:51 +0000178//===----------------------------------------------------------------------===//
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000179// Engine construction and deletion.
180//===----------------------------------------------------------------------===//
181
Ted Kremenek7f824732008-05-01 18:33:28 +0000182static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
183 IdentifierInfo* II = &Ctx.Idents.get(name);
184 return Ctx.Selectors.getSelector(0, &II);
185}
186
Ted Kremenek2d063be2008-03-09 18:05:48 +0000187
Zhongxing Xu342950e2009-08-25 06:51:30 +0000188GRExprEngine::GRExprEngine(AnalysisManager &mgr)
Zhongxing Xue1190f72009-08-15 03:17:38 +0000189 : AMgr(mgr),
Mike Stump11289f42009-09-09 15:08:12 +0000190 CoreEngine(mgr.getASTContext(), *this),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000191 G(CoreEngine.getGraph()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000192 Builder(NULL),
Mike Stump11289f42009-09-09 15:08:12 +0000193 StateMgr(G.getContext(), mgr.getStoreManagerCreator(),
Zhongxing Xu342950e2009-08-25 06:51:30 +0000194 mgr.getConstraintManagerCreator(), G.getAllocator()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000195 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekf8cb51c2009-04-09 16:46:55 +0000196 ValMgr(StateMgr.getValueManager()),
Ted Kremenekf267a152009-07-16 01:32:00 +0000197 SVator(ValMgr.getSValuator()),
Ted Kremenek7f824732008-05-01 18:33:28 +0000198 CurrentStmt(NULL),
Zhongxing Xu4ee570a2008-12-22 08:30:52 +0000199 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Mike Stump11289f42009-09-09 15:08:12 +0000200 RaiseSel(GetNullarySelector("raise", G.getContext())),
Zhongxing Xu342950e2009-08-25 06:51:30 +0000201 BR(mgr, *this) {}
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000202
Mike Stump11289f42009-09-09 15:08:12 +0000203GRExprEngine::~GRExprEngine() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000204 BR.FlushReports();
Ted Kremenek7f824732008-05-01 18:33:28 +0000205 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek6f2a7052009-10-30 17:47:32 +0000206 for (CheckersOrdered::iterator I=Checkers.begin(), E=Checkers.end(); I!=E;++I)
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000207 delete I->second;
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000208}
209
Ted Kremenek667cacb2008-04-15 23:06:53 +0000210//===----------------------------------------------------------------------===//
211// Utility methods.
212//===----------------------------------------------------------------------===//
213
Ted Kremenekdd43aee2008-04-23 20:12:28 +0000214
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000215void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek9c32a1e2008-07-17 23:15:45 +0000216 StateMgr.TF = tf;
Ted Kremenek0fbbb082009-11-03 23:30:34 +0000217 tf->RegisterChecks(*this);
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000218 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000219}
220
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000221void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
222 if (!BatchAuditor)
223 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000225 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000226}
227
Ted Kremenek4967c892009-03-30 17:53:05 +0000228void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
229 if (!BatchAuditor)
230 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
231
232 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
233}
234
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000235const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) {
236 const GRState *state = StateMgr.getInitialState(InitLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000238 // Preconditions.
239
Ted Kremenek50546632009-04-10 00:59:50 +0000240 // FIXME: It would be nice if we had a more general mechanism to add
241 // such preconditions. Some day.
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000242 const Decl *D = InitLoc->getDecl();
243
244 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
245 // Precondition: the first argument of 'main' is an integer guaranteed
246 // to be > 0.
Daniel Dunbar07d07852009-10-18 21:17:35 +0000247 if (FD->getIdentifier()->getName() == "main" &&
Ted Kremenek50546632009-04-10 00:59:50 +0000248 FD->getNumParams() > 0) {
249 const ParmVarDecl *PD = FD->getParamDecl(0);
250 QualType T = PD->getType();
251 if (T->isIntegerType())
Ted Kremenek14536f62009-08-21 22:28:32 +0000252 if (const MemRegion *R = state->getRegion(PD, InitLoc)) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000253 SVal V = state->getSVal(loc::MemRegionVal(R));
Ted Kremenek7020eae2009-09-11 22:07:28 +0000254 SVal Constraint_untested = EvalBinOp(state, BinaryOperator::GT, V,
255 ValMgr.makeZeroVal(T),
256 getContext().IntTy);
Ted Kremenekf9906842009-06-18 22:57:13 +0000257
Ted Kremenek7020eae2009-09-11 22:07:28 +0000258 if (DefinedOrUnknownSVal *Constraint =
259 dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested)) {
260 if (const GRState *newState = state->Assume(*Constraint, true))
261 state = newState;
262 }
Ted Kremenek50546632009-04-10 00:59:50 +0000263 }
264 }
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000265 }
266 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
267 // Precondition: 'self' is always non-null upon entry to an Objective-C
268 // method.
269 const ImplicitParamDecl *SelfD = MD->getSelfDecl();
270 const MemRegion *R = state->getRegion(SelfD, InitLoc);
271 SVal V = state->getSVal(loc::MemRegionVal(R));
272
273 if (const Loc *LV = dyn_cast<Loc>(&V)) {
274 // Assume that the pointer value in 'self' is non-null.
Ted Kremenek7020eae2009-09-11 22:07:28 +0000275 state = state->Assume(*LV, true);
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000276 assert(state && "'self' cannot be null");
277 }
278 }
279
Ted Kremenek50546632009-04-10 00:59:50 +0000280 return state;
Ted Kremenek723fe3f2008-02-04 21:59:01 +0000281}
282
Ted Kremenek667cacb2008-04-15 23:06:53 +0000283//===----------------------------------------------------------------------===//
284// Top-level transfer function logic (Dispatcher).
285//===----------------------------------------------------------------------===//
286
Zhongxing Xu107f7592009-08-06 12:48:26 +0000287void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek91076ca2009-03-11 02:41:36 +0000289 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
290 S->getLocStart(),
291 "Error evaluating statement");
Mike Stump11289f42009-09-09 15:08:12 +0000292
Ted Kremenek667cacb2008-04-15 23:06:53 +0000293 Builder = &builder;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000294 EntryNode = builder.getLastNode();
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000296 // FIXME: Consolidate.
Ted Kremenek667cacb2008-04-15 23:06:53 +0000297 CurrentStmt = S;
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000298 StateMgr.CurrentStmt = S;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek667cacb2008-04-15 23:06:53 +0000300 // Set up our simple checks.
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000301 if (BatchAuditor)
302 Builder->setAuditor(BatchAuditor.get());
Mike Stump11289f42009-09-09 15:08:12 +0000303
304 // Create the cleaned state.
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000305 SymbolReaper SymReaper(Builder->getBasePredecessor()->getLiveVariables(),
306 SymMgr);
Zhongxing Xu3ca89b92009-08-27 06:55:26 +0000307 CleanedState = AMgr.shouldPurgeDead()
308 ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
309 : EntryNode->getState();
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000310
Ted Kremenek3812b762008-04-24 18:31:42 +0000311 // Process any special transfer function for dead symbols.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000312 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000314 if (!SymReaper.hasDeadSymbols())
Ted Kremenekae8014c2008-04-24 23:35:58 +0000315 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000316 else {
317 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000318 SaveOr OldHasGen(Builder->HasGeneratedNode);
319
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000320 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
321 Builder->PurgingDeadSymbols = true;
Mike Stump11289f42009-09-09 15:08:12 +0000322
323 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000324 CleanedState, SymReaper);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000325
326 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
327 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000328 }
Mike Stump11289f42009-09-09 15:08:12 +0000329
Ted Kremenekae8014c2008-04-24 23:35:58 +0000330 bool HasAutoGenerated = false;
331
Zhongxing Xu107f7592009-08-06 12:48:26 +0000332 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekae8014c2008-04-24 23:35:58 +0000333
Zhongxing Xu107f7592009-08-06 12:48:26 +0000334 ExplodedNodeSet Dst;
Mike Stump11289f42009-09-09 15:08:12 +0000335
336 // Set the cleaned state.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000337 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
Mike Stump11289f42009-09-09 15:08:12 +0000338
339 // Visit the statement.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000340 Visit(S, *I, Dst);
341
342 // Do we need to auto-generate a node? We only need to do this to generate
343 // a node with a "cleaned" state; GRCoreEngine will actually handle
Mike Stump11289f42009-09-09 15:08:12 +0000344 // auto-transitions for other cases.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000345 if (Dst.size() == 1 && *Dst.begin() == EntryNode
346 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
347 HasAutoGenerated = true;
348 builder.generateNode(S, GetState(EntryNode), *I);
349 }
Ted Kremenek3812b762008-04-24 18:31:42 +0000350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek667cacb2008-04-15 23:06:53 +0000352 // NULL out these variables to cleanup.
Ted Kremenek667cacb2008-04-15 23:06:53 +0000353 CleanedState = NULL;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000354 EntryNode = NULL;
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000355
356 // FIXME: Consolidate.
357 StateMgr.CurrentStmt = 0;
358 CurrentStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Ted Kremenekae8014c2008-04-24 23:35:58 +0000360 Builder = NULL;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000361}
362
Mike Stump11289f42009-09-09 15:08:12 +0000363void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek91076ca2009-03-11 02:41:36 +0000364 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
365 S->getLocStart(),
366 "Error evaluating statement");
367
Ted Kremenek667cacb2008-04-15 23:06:53 +0000368 // FIXME: add metadata to the CFG so that we can disable
369 // this check when we KNOW that there is no block-level subexpression.
370 // The motivation is that this check requires a hashtable lookup.
Mike Stump11289f42009-09-09 15:08:12 +0000371
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000372 if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000373 Dst.Add(Pred);
374 return;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremenek667cacb2008-04-15 23:06:53 +0000377 switch (S->getStmtClass()) {
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenek667cacb2008-04-15 23:06:53 +0000379 default:
380 // Cases we intentionally have "default" handle:
381 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremenek667cacb2008-04-15 23:06:53 +0000383 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
384 break;
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000386 case Stmt::ArraySubscriptExprClass:
387 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
388 break;
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek667cacb2008-04-15 23:06:53 +0000390 case Stmt::AsmStmtClass:
391 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
392 break;
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ted Kremenek667cacb2008-04-15 23:06:53 +0000394 case Stmt::BinaryOperatorClass: {
395 BinaryOperator* B = cast<BinaryOperator>(S);
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek667cacb2008-04-15 23:06:53 +0000397 if (B->isLogicalOp()) {
398 VisitLogicalExpr(B, Pred, Dst);
399 break;
400 }
401 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000402 const GRState* state = GetState(Pred);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000403 MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenek667cacb2008-04-15 23:06:53 +0000404 break;
405 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000406
Zhongxing Xu342950e2009-08-25 06:51:30 +0000407 if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000408 ExplodedNodeSet Tmp;
Zhongxing Xub9eda672009-10-30 07:19:39 +0000409 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp, false);
Mike Stump11289f42009-09-09 15:08:12 +0000410 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000411 }
412 else
Zhongxing Xub9eda672009-10-30 07:19:39 +0000413 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false);
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000414
Ted Kremenek667cacb2008-04-15 23:06:53 +0000415 break;
416 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000417
Douglas Gregor993603d2008-11-14 16:09:21 +0000418 case Stmt::CallExprClass:
419 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000420 CallExpr* C = cast<CallExpr>(S);
421 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek537f6382008-11-14 19:47:18 +0000422 break;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000423 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000424
Ted Kremenek667cacb2008-04-15 23:06:53 +0000425 // FIXME: ChooseExpr is really a constant. We need to fix
426 // the CFG do not model them as explicit control-flow.
Mike Stump11289f42009-09-09 15:08:12 +0000427
Ted Kremenek667cacb2008-04-15 23:06:53 +0000428 case Stmt::ChooseExprClass: { // __builtin_choose_expr
429 ChooseExpr* C = cast<ChooseExpr>(S);
430 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
431 break;
432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek667cacb2008-04-15 23:06:53 +0000434 case Stmt::CompoundAssignOperatorClass:
Zhongxing Xub9eda672009-10-30 07:19:39 +0000435 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000436 break;
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000437
438 case Stmt::CompoundLiteralExprClass:
439 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
440 break;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek667cacb2008-04-15 23:06:53 +0000442 case Stmt::ConditionalOperatorClass: { // '?' operator
443 ConditionalOperator* C = cast<ConditionalOperator>(S);
444 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
445 break;
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek667cacb2008-04-15 23:06:53 +0000448 case Stmt::DeclRefExprClass:
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000449 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000450 break;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenek667cacb2008-04-15 23:06:53 +0000452 case Stmt::DeclStmtClass:
453 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
454 break;
Mike Stump11289f42009-09-09 15:08:12 +0000455
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000456 case Stmt::ImplicitCastExprClass:
Douglas Gregorf19b2312008-10-28 15:36:24 +0000457 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000458 CastExpr* C = cast<CastExpr>(S);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000459 VisitCast(C, C->getSubExpr(), Pred, Dst);
460 break;
461 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +0000462
463 case Stmt::InitListExprClass:
464 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
465 break;
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000467 case Stmt::MemberExprClass:
Ted Kremenek38213f92008-04-21 23:43:38 +0000468 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
469 break;
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000471 case Stmt::ObjCIvarRefExprClass:
472 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
473 break;
Ted Kremenek17810802008-11-12 19:24:17 +0000474
475 case Stmt::ObjCForCollectionStmtClass:
476 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
477 break;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ted Kremenek667cacb2008-04-15 23:06:53 +0000479 case Stmt::ObjCMessageExprClass: {
480 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
481 break;
482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenek1857ff42008-12-09 20:18:58 +0000484 case Stmt::ObjCAtThrowStmtClass: {
485 // FIXME: This is not complete. We basically treat @throw as
486 // an abort.
487 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
488 Builder->BuildSinks = true;
489 MakeNode(Dst, S, Pred, GetState(Pred));
490 break;
491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek667cacb2008-04-15 23:06:53 +0000493 case Stmt::ParenExprClass:
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000494 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000495 break;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000497 case Stmt::ReturnStmtClass:
498 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
499 break;
Mike Stump11289f42009-09-09 15:08:12 +0000500
Sebastian Redl6f282892008-11-11 17:56:53 +0000501 case Stmt::SizeOfAlignOfExprClass:
502 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000503 break;
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek667cacb2008-04-15 23:06:53 +0000505 case Stmt::StmtExprClass: {
506 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000507
508 if (SE->getSubStmt()->body_empty()) {
509 // Empty statement expression.
510 assert(SE->getType() == getContext().VoidTy
511 && "Empty statement expression must have void type.");
512 Dst.Add(Pred);
513 break;
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000516 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
517 const GRState* state = GetState(Pred);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000518 MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000519 }
Ted Kremenek667cacb2008-04-15 23:06:53 +0000520 else
521 Dst.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000522
Ted Kremenek667cacb2008-04-15 23:06:53 +0000523 break;
524 }
Zhongxing Xud2fa1e02008-11-30 05:49:49 +0000525
526 case Stmt::StringLiteralClass:
527 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
528 break;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Ted Kremenek891642e2009-03-18 23:49:26 +0000530 case Stmt::UnaryOperatorClass: {
531 UnaryOperator *U = cast<UnaryOperator>(S);
Zhongxing Xu342950e2009-08-25 06:51:30 +0000532 if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000533 ExplodedNodeSet Tmp;
Ted Kremenek891642e2009-03-18 23:49:26 +0000534 VisitUnaryOperator(U, Pred, Tmp, false);
535 EvalEagerlyAssume(Dst, Tmp, U);
536 }
537 else
538 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000539 break;
Ted Kremenek891642e2009-03-18 23:49:26 +0000540 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000541 }
542}
543
Mike Stump11289f42009-09-09 15:08:12 +0000544void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000545 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000547 Ex = Ex->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +0000548
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000549 if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000550 Dst.Add(Pred);
551 return;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000554 switch (Ex->getStmtClass()) {
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000556 case Stmt::ArraySubscriptExprClass:
557 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
558 return;
Mike Stump11289f42009-09-09 15:08:12 +0000559
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000560 case Stmt::DeclRefExprClass:
561 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
562 return;
Mike Stump11289f42009-09-09 15:08:12 +0000563
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000564 case Stmt::ObjCIvarRefExprClass:
565 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
566 return;
Mike Stump11289f42009-09-09 15:08:12 +0000567
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000568 case Stmt::UnaryOperatorClass:
569 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
570 return;
Mike Stump11289f42009-09-09 15:08:12 +0000571
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000572 case Stmt::MemberExprClass:
573 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
574 return;
Mike Stump11289f42009-09-09 15:08:12 +0000575
Ted Kremenekbf263682008-10-27 21:54:31 +0000576 case Stmt::CompoundLiteralExprClass:
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000577 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekbf263682008-10-27 21:54:31 +0000578 return;
Mike Stump11289f42009-09-09 15:08:12 +0000579
Ted Kremenek58700462008-10-17 17:24:14 +0000580 case Stmt::ObjCPropertyRefExprClass:
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000581 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Ted Kremenek58700462008-10-17 17:24:14 +0000582 // FIXME: Property assignments are lvalues, but not really "locations".
583 // e.g.: self.x = something;
584 // Here the "self.x" really can translate to a method call (setter) when
585 // the assignment is made. Moreover, the entire assignment expression
586 // evaluate to whatever "something" is, not calling the "getter" for
587 // the property (which would make sense since it can have side effects).
588 // We'll probably treat this as a location, but not one that we can
589 // take the address of. Perhaps we need a new SVal class for cases
590 // like thsis?
591 // Note that we have a similar problem for bitfields, since they don't
592 // have "locations" in the sense that we can take their address.
593 Dst.Add(Pred);
Ted Kremenek8f5dc292008-10-18 04:08:49 +0000594 return;
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000595
596 case Stmt::StringLiteralClass: {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000597 const GRState* state = GetState(Pred);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000598 SVal V = state->getLValue(cast<StringLiteral>(Ex));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000599 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000600 return;
601 }
Mike Stump11289f42009-09-09 15:08:12 +0000602
Zhongxing Xub9eda672009-10-30 07:19:39 +0000603 case Stmt::BinaryOperatorClass:
604 case Stmt::CompoundAssignOperatorClass:
605 VisitBinaryOperator(cast<BinaryOperator>(Ex), Pred, Dst, true);
606 return;
607
Ted Kremenek850422e2008-10-18 04:15:35 +0000608 default:
609 // Arbitrary subexpressions can return aggregate temporaries that
610 // can be used in a lvalue context. We need to enhance our support
611 // of such temporaries in both the environment and the store, so right
612 // now we just do a regular visit.
Douglas Gregorddb24852009-01-30 17:31:00 +0000613 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremeneke69a1fa2008-10-25 20:09:21 +0000614 "Other kinds of expressions with non-aggregate/union types do"
615 " not have lvalues.");
Mike Stump11289f42009-09-09 15:08:12 +0000616
Ted Kremenek850422e2008-10-18 04:15:35 +0000617 Visit(Ex, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000618 }
619}
620
621//===----------------------------------------------------------------------===//
622// Block entrance. (Update counters).
623//===----------------------------------------------------------------------===//
624
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000625bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenek667cacb2008-04-15 23:06:53 +0000626 GRBlockCounter BC) {
Mike Stump11289f42009-09-09 15:08:12 +0000627
Ted Kremenek667cacb2008-04-15 23:06:53 +0000628 return BC.getNumVisited(B->getBlockID()) < 3;
629}
630
631//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +0000632// Generic node creation.
633//===----------------------------------------------------------------------===//
634
Zhongxing Xu107f7592009-08-06 12:48:26 +0000635ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
636 ExplodedNode* Pred, const GRState* St,
637 ProgramPoint::Kind K, const void *tag) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000638 assert (Builder && "GRStmtNodeBuilder not present.");
639 SaveAndRestore<const void*> OldTag(Builder->Tag);
640 Builder->Tag = tag;
641 return Builder->MakeNode(Dst, S, Pred, St, K);
642}
643
644//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +0000645// Branch processing.
646//===----------------------------------------------------------------------===//
647
Ted Kremenek17d541d2009-02-13 01:45:31 +0000648const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000649 Stmt* Terminator,
650 bool branchTaken) {
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000652 switch (Terminator->getStmtClass()) {
653 default:
Ted Kremenek17d541d2009-02-13 01:45:31 +0000654 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000655
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000656 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump11289f42009-09-09 15:08:12 +0000657
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000658 BinaryOperator* B = cast<BinaryOperator>(Terminator);
659 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000661 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000663 // For &&, if we take the true branch, then the value of the whole
664 // expression is that of the RHS expression.
665 //
666 // For ||, if we take the false branch, then the value of the whole
667 // expression is that of the RHS expression.
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000669 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
Mike Stump11289f42009-09-09 15:08:12 +0000670 (Op == BinaryOperator::LOr && !branchTaken)
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000671 ? B->getRHS() : B->getLHS();
Mike Stump11289f42009-09-09 15:08:12 +0000672
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000673 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000674 }
Mike Stump11289f42009-09-09 15:08:12 +0000675
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000676 case Stmt::ConditionalOperatorClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000677
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000678 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000680 // For ?, if branchTaken == true then the value is either the LHS or
681 // the condition itself. (GNU extension).
Mike Stump11289f42009-09-09 15:08:12 +0000682
683 Expr* Ex;
684
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000685 if (branchTaken)
Mike Stump11289f42009-09-09 15:08:12 +0000686 Ex = C->getLHS() ? C->getLHS() : C->getCond();
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000687 else
688 Ex = C->getRHS();
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000690 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000691 }
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000693 case Stmt::ChooseExprClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000694
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000695 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000696
697 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000698 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000699 }
700 }
701}
702
Ted Kremenek22358bd2009-03-13 16:32:54 +0000703/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
704/// to try to recover some path-sensitivity for casts of symbolic
705/// integers that promote their values (which are currently not tracked well).
706/// This function returns the SVal bound to Condition->IgnoreCasts if all the
707// cast(s) did was sign-extend the original value.
708static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
709 Stmt* Condition, ASTContext& Ctx) {
710
711 Expr *Ex = dyn_cast<Expr>(Condition);
712 if (!Ex)
713 return UnknownVal();
714
715 uint64_t bits = 0;
716 bool bitsInit = false;
Mike Stump11289f42009-09-09 15:08:12 +0000717
Ted Kremenek22358bd2009-03-13 16:32:54 +0000718 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
719 QualType T = CE->getType();
720
721 if (!T->isIntegerType())
722 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000723
Ted Kremenek22358bd2009-03-13 16:32:54 +0000724 uint64_t newBits = Ctx.getTypeSize(T);
725 if (!bitsInit || newBits < bits) {
726 bitsInit = true;
727 bits = newBits;
728 }
Mike Stump11289f42009-09-09 15:08:12 +0000729
Ted Kremenek22358bd2009-03-13 16:32:54 +0000730 Ex = CE->getSubExpr();
731 }
732
733 // We reached a non-cast. Is it a symbolic value?
734 QualType T = Ex->getType();
735
736 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
737 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000738
Ted Kremenek095f1a92009-06-18 23:58:37 +0000739 return state->getSVal(Ex);
Ted Kremenek22358bd2009-03-13 16:32:54 +0000740}
741
Ted Kremenek17810802008-11-12 19:24:17 +0000742void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000743 GRBranchNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000744
Ted Kremenek8db4b112008-02-15 22:29:00 +0000745 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump11289f42009-09-09 15:08:12 +0000746 if (!Condition) {
Ted Kremenek8db4b112008-02-15 22:29:00 +0000747 builder.markInfeasible(false);
Ted Kremenek8db4b112008-02-15 22:29:00 +0000748 return;
749 }
Mike Stump11289f42009-09-09 15:08:12 +0000750
Ted Kremenek32c41ec2009-03-11 03:54:24 +0000751 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
752 Condition->getLocStart(),
753 "Error evaluating branch");
Ted Kremenek907a7112009-08-27 01:39:13 +0000754
Mike Stump11289f42009-09-09 15:08:12 +0000755 const GRState* PrevState = builder.getState();
Ted Kremenek7020eae2009-09-11 22:07:28 +0000756 SVal X = PrevState->getSVal(Condition);
757 DefinedSVal *V = NULL;
758
759 while (true) {
760 V = dyn_cast<DefinedSVal>(&X);
Mike Stump11289f42009-09-09 15:08:12 +0000761
Ted Kremenek7020eae2009-09-11 22:07:28 +0000762 if (!V) {
763 if (X.isUnknown()) {
764 if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
Ted Kremenek22358bd2009-03-13 16:32:54 +0000765 if (Ex->getType()->isIntegerType()) {
Ted Kremenek7020eae2009-09-11 22:07:28 +0000766 // Try to recover some path-sensitivity. Right now casts of symbolic
767 // integers that promote their values are currently not tracked well.
768 // If 'Condition' is such an expression, try and recover the
769 // underlying value and use that instead.
770 SVal recovered = RecoverCastedSymbol(getStateManager(),
771 builder.getState(), Condition,
772 getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000773
Ted Kremenek7020eae2009-09-11 22:07:28 +0000774 if (!recovered.isUnknown()) {
775 X = recovered;
776 continue;
777 }
Ted Kremenek22358bd2009-03-13 16:32:54 +0000778 }
Ted Kremenek7020eae2009-09-11 22:07:28 +0000779 }
780
781 builder.generateNode(MarkBranch(PrevState, Term, true), true);
782 builder.generateNode(MarkBranch(PrevState, Term, false), false);
783 return;
Ted Kremenek22358bd2009-03-13 16:32:54 +0000784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Ted Kremenek7020eae2009-09-11 22:07:28 +0000786 assert(X.isUndef());
787 ExplodedNode *N = builder.generateNode(PrevState, true);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000788
789 if (N) {
790 N->markAsSink();
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000791 UndefBranches.insert(N);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ted Kremeneka50d9852008-01-30 23:03:39 +0000794 builder.markInfeasible(false);
795 return;
Mike Stump11289f42009-09-09 15:08:12 +0000796 }
Ted Kremenek7020eae2009-09-11 22:07:28 +0000797
798 break;
Ted Kremeneka50d9852008-01-30 23:03:39 +0000799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000801 // Process the true branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000802 if (builder.isFeasible(true)) {
Ted Kremenek7020eae2009-09-11 22:07:28 +0000803 if (const GRState *state = PrevState->Assume(*V, true))
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000804 builder.generateNode(MarkBranch(state, Term, true), true);
805 else
806 builder.markInfeasible(true);
807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
809 // Process the false branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000810 if (builder.isFeasible(false)) {
Ted Kremenek7020eae2009-09-11 22:07:28 +0000811 if (const GRState *state = PrevState->Assume(*V, false))
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000812 builder.generateNode(MarkBranch(state, Term, false), false);
813 else
814 builder.markInfeasible(false);
815 }
Ted Kremenek7ff18932008-01-29 23:32:35 +0000816}
817
Ted Kremenekf6c62f32008-02-13 17:41:41 +0000818/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7022efb2008-02-13 00:24:44 +0000819/// nodes by processing the 'effects' of a computed goto jump.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000820void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000821
Mike Stump11289f42009-09-09 15:08:12 +0000822 const GRState *state = builder.getState();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000823 SVal V = state->getSVal(builder.getTarget());
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek7022efb2008-02-13 00:24:44 +0000825 // Three possibilities:
826 //
827 // (1) We know the computed label.
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000828 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek7022efb2008-02-13 00:24:44 +0000829 // (3) We have no clue about the label. Dispatch to all targets.
830 //
Mike Stump11289f42009-09-09 15:08:12 +0000831
Zhongxing Xu107f7592009-08-06 12:48:26 +0000832 typedef GRIndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek7022efb2008-02-13 00:24:44 +0000833
Zhongxing Xu27f17422008-10-17 05:57:07 +0000834 if (isa<loc::GotoLabel>(V)) {
835 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenek7022efb2008-02-13 00:24:44 +0000837 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek2bba9012008-02-13 17:27:37 +0000838 if (I.getLabel() == L) {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000839 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000840 return;
841 }
842 }
Mike Stump11289f42009-09-09 15:08:12 +0000843
Ted Kremenek7022efb2008-02-13 00:24:44 +0000844 assert (false && "No block with label.");
845 return;
846 }
847
Zhongxing Xu27f17422008-10-17 05:57:07 +0000848 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000849 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000850 ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000851 UndefBranches.insert(N);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000852 return;
853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Ted Kremenek7022efb2008-02-13 00:24:44 +0000855 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenek9c03f682009-04-23 17:49:43 +0000856 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek7022efb2008-02-13 00:24:44 +0000858 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek17d541d2009-02-13 01:45:31 +0000859 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000860}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000861
Ted Kremenek667cacb2008-04-15 23:06:53 +0000862
863void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000864 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000865
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000866 assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump11289f42009-09-09 15:08:12 +0000867
Ted Kremenek17d541d2009-02-13 01:45:31 +0000868 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +0000869 SVal X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenek667cacb2008-04-15 23:06:53 +0000871 assert (X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +0000872
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000873 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump11289f42009-09-09 15:08:12 +0000874 assert(SE);
Ted Kremenek907a7112009-08-27 01:39:13 +0000875 X = state->getSVal(SE);
Mike Stump11289f42009-09-09 15:08:12 +0000876
Ted Kremenek667cacb2008-04-15 23:06:53 +0000877 // Make sure that we invalidate the previous binding.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000878 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremenek667cacb2008-04-15 23:06:53 +0000879}
880
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000881/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
882/// nodes by processing the 'effects' of a switch statement.
Mike Stump11289f42009-09-09 15:08:12 +0000883void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
884 typedef GRSwitchNodeBuilder::iterator iterator;
885 const GRState* state = builder.getState();
Ted Kremenek346169f2008-02-18 22:57:02 +0000886 Expr* CondE = builder.getCondition();
Ted Kremenek7020eae2009-09-11 22:07:28 +0000887 SVal CondV_untested = state->getSVal(CondE);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000888
Ted Kremenek7020eae2009-09-11 22:07:28 +0000889 if (CondV_untested.isUndef()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000890 ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000891 UndefBranches.insert(N);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000892 return;
893 }
Ted Kremenek7020eae2009-09-11 22:07:28 +0000894 DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
Ted Kremenek346169f2008-02-18 22:57:02 +0000895
Ted Kremenek7020eae2009-09-11 22:07:28 +0000896 const GRState *DefaultSt = state;
Ted Kremenekf9906842009-06-18 22:57:13 +0000897 bool defaultIsFeasible = false;
Mike Stump11289f42009-09-09 15:08:12 +0000898
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000899 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000900 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000901
902 // Evaluate the LHS of the case value.
903 Expr::EvalResult V1;
Mike Stump11289f42009-09-09 15:08:12 +0000904 bool b = Case->getLHS()->Evaluate(V1, getContext());
905
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000906 // Sanity checks. These go away in Release builds.
Mike Stump11289f42009-09-09 15:08:12 +0000907 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000908 && "Case condition must evaluate to an integer constant.");
Mike Stump11289f42009-09-09 15:08:12 +0000909 b = b; // silence unused variable warning
910 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000911 getContext().getTypeSize(CondE->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000912
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000913 // Get the RHS of the case, if it exists.
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000914 Expr::EvalResult V2;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000916 if (Expr* E = Case->getRHS()) {
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000917 b = E->Evaluate(V2, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000918 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000919 && "Case condition must evaluate to an integer constant.");
920 b = b; // silence unused variable warning
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000921 }
Ted Kremenek9eae4032008-03-17 22:17:56 +0000922 else
923 V2 = V1;
Mike Stump11289f42009-09-09 15:08:12 +0000924
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000925 // FIXME: Eventually we should replace the logic below with a range
926 // comparison, rather than concretize the values within the range.
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000927 // This should be easy once we have "ranges" for NonLVals.
Mike Stump11289f42009-09-09 15:08:12 +0000928
Ted Kremenek9eae4032008-03-17 22:17:56 +0000929 do {
Mike Stump11289f42009-09-09 15:08:12 +0000930 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek7020eae2009-09-11 22:07:28 +0000931 DefinedOrUnknownSVal Res = SVator.EvalEQ(DefaultSt, CondV, CaseVal);
932
Mike Stump11289f42009-09-09 15:08:12 +0000933 // Now "assume" that the case matches.
Ted Kremenek7020eae2009-09-11 22:07:28 +0000934 if (const GRState* stateNew = state->Assume(Res, true)) {
Ted Kremenekf9906842009-06-18 22:57:13 +0000935 builder.generateCaseStmtNode(I, stateNew);
Mike Stump11289f42009-09-09 15:08:12 +0000936
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000937 // If CondV evaluates to a constant, then we know that this
938 // is the *only* case that we can take, so stop evaluating the
939 // others.
Zhongxing Xu27f17422008-10-17 05:57:07 +0000940 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000941 return;
942 }
Mike Stump11289f42009-09-09 15:08:12 +0000943
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000944 // Now "assume" that the case doesn't match. Add this state
945 // to the default state (if it is feasible).
Ted Kremenek7020eae2009-09-11 22:07:28 +0000946 if (const GRState *stateNew = DefaultSt->Assume(Res, false)) {
Ted Kremenekf9906842009-06-18 22:57:13 +0000947 defaultIsFeasible = true;
948 DefaultSt = stateNew;
Ted Kremenekd2419a02008-04-23 05:03:18 +0000949 }
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000950
Ted Kremenek9eae4032008-03-17 22:17:56 +0000951 // Concretize the next value in the range.
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000952 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek9eae4032008-03-17 22:17:56 +0000953 break;
Mike Stump11289f42009-09-09 15:08:12 +0000954
Ted Kremenek1ab188f2009-01-17 01:54:16 +0000955 ++V1.Val.getInt();
956 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump11289f42009-09-09 15:08:12 +0000957
Ted Kremenek9eae4032008-03-17 22:17:56 +0000958 } while (true);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000959 }
Mike Stump11289f42009-09-09 15:08:12 +0000960
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000961 // If we reach here, than we know that the default branch is
Mike Stump11289f42009-09-09 15:08:12 +0000962 // possible.
Ted Kremenekf9906842009-06-18 22:57:13 +0000963 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000964}
965
Ted Kremenek667cacb2008-04-15 23:06:53 +0000966//===----------------------------------------------------------------------===//
967// Transfer functions: logical operations ('&&', '||').
968//===----------------------------------------------------------------------===//
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000969
Zhongxing Xu107f7592009-08-06 12:48:26 +0000970void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
971 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000972
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000973 assert(B->getOpcode() == BinaryOperator::LAnd ||
974 B->getOpcode() == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +0000975
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000976 assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump11289f42009-09-09 15:08:12 +0000977
Ted Kremenek17d541d2009-02-13 01:45:31 +0000978 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +0000979 SVal X = state->getSVal(B);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000980 assert(X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +0000981
Ted Kremenek7020eae2009-09-11 22:07:28 +0000982 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000983 assert(Ex);
Mike Stump11289f42009-09-09 15:08:12 +0000984
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000985 if (Ex == B->getRHS()) {
Ted Kremenek907a7112009-08-27 01:39:13 +0000986 X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +0000987
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000988 // Handle undefined values.
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000989 if (X.isUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000990 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenekbc543902008-02-26 19:40:44 +0000991 return;
992 }
Ted Kremenek7020eae2009-09-11 22:07:28 +0000993
994 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
Mike Stump11289f42009-09-09 15:08:12 +0000995
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000996 // We took the RHS. Because the value of the '&&' or '||' expression must
997 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
998 // or 1. Alternatively, we could take a lazy approach, and calculate this
999 // value later when necessary. We don't have the machinery in place for
1000 // this right now, and since most logical expressions are used for branches,
Mike Stump11289f42009-09-09 15:08:12 +00001001 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001002 if (const GRState *newState = state->Assume(XD, true))
Mike Stump11289f42009-09-09 15:08:12 +00001003 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001004 newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Mike Stump11289f42009-09-09 15:08:12 +00001005
Ted Kremenek7020eae2009-09-11 22:07:28 +00001006 if (const GRState *newState = state->Assume(XD, false))
Mike Stump11289f42009-09-09 15:08:12 +00001007 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001008 newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001009 }
1010 else {
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001011 // We took the LHS expression. Depending on whether we are '&&' or
1012 // '||' we know what the value of the expression is via properties of
1013 // the short-circuiting.
Mike Stump11289f42009-09-09 15:08:12 +00001014 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001015 B->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001016 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001017 }
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001018}
Mike Stump11289f42009-09-09 15:08:12 +00001019
Ted Kremenek667cacb2008-04-15 23:06:53 +00001020//===----------------------------------------------------------------------===//
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001021// Transfer functions: Loads and stores.
Ted Kremenek667cacb2008-04-15 23:06:53 +00001022//===----------------------------------------------------------------------===//
Ted Kremenekde8d62b2008-01-15 23:55:06 +00001023
Mike Stump11289f42009-09-09 15:08:12 +00001024void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
Ted Kremenek14536f62009-08-21 22:28:32 +00001025 ExplodedNodeSet &Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001026
Ted Kremenek7020eae2009-09-11 22:07:28 +00001027 const GRState *state = GetState(Pred);
1028 const NamedDecl *D = Ex->getDecl();
Zhongxing Xu232c7922008-10-16 06:09:51 +00001029
1030 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1031
Ted Kremenek14536f62009-08-21 22:28:32 +00001032 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xu252fe5c2008-10-17 02:20:14 +00001033
Zhongxing Xu232c7922008-10-16 06:09:51 +00001034 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001035 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001036 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001037 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001038 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001039 return;
1040
1041 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
1042 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
1043
Zhongxing Xud09b5202009-06-23 06:13:19 +00001044 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001045 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu232c7922008-10-16 06:09:51 +00001046 return;
1047
1048 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek1624a472009-09-23 01:30:01 +00001049 // This code is valid regardless of the value of 'isLValue'.
Zhongxing Xuac129432009-04-20 05:24:46 +00001050 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001051 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001052 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001053 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Zhongxing Xu232c7922008-10-16 06:09:51 +00001056 assert (false &&
1057 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek88da1de2008-02-07 04:16:04 +00001058}
1059
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001060/// VisitArraySubscriptExpr - Transfer function for array accesses
Mike Stump11289f42009-09-09 15:08:12 +00001061void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001062 ExplodedNode* Pred,
1063 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump11289f42009-09-09 15:08:12 +00001064
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001065 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek10246e82008-04-29 23:24:44 +00001066 Expr* Idx = A->getIdx()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001067 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001068
Ted Kremenekcce27f52009-02-24 02:23:11 +00001069 if (Base->getType()->isVectorType()) {
1070 // For vector types get its lvalue.
1071 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1072 // In fact, I think this is just a hack. We need to get the right
1073 // semantics.
1074 VisitLValue(Base, Pred, Tmp);
1075 }
Mike Stump11289f42009-09-09 15:08:12 +00001076 else
Ted Kremenekcce27f52009-02-24 02:23:11 +00001077 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Mike Stump11289f42009-09-09 15:08:12 +00001078
Zhongxing Xu107f7592009-08-06 12:48:26 +00001079 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1080 ExplodedNodeSet Tmp2;
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001081 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Mike Stump11289f42009-09-09 15:08:12 +00001082
Zhongxing Xu107f7592009-08-06 12:48:26 +00001083 for (ExplodedNodeSet::iterator I2=Tmp2.begin(),E2=Tmp2.end();I2!=E2; ++I2) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001084 const GRState* state = GetState(*I2);
Zhongxing Xu7d6387b2009-10-14 03:33:08 +00001085 SVal V = state->getLValue(A->getType(), state->getSVal(Idx),
1086 state->getSVal(Base));
Ted Kremenek10246e82008-04-29 23:24:44 +00001087
Zhongxing Xu232c7922008-10-16 06:09:51 +00001088 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001089 MakeNode(Dst, A, *I2, state->BindExpr(A, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001090 ProgramPoint::PostLValueKind);
Ted Kremenek10246e82008-04-29 23:24:44 +00001091 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001092 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek10246e82008-04-29 23:24:44 +00001093 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001094 }
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001095}
1096
Ted Kremenek38213f92008-04-21 23:43:38 +00001097/// VisitMemberExpr - Transfer function for member expressions.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001098void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
1099 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001100
Ted Kremenek38213f92008-04-21 23:43:38 +00001101 Expr* Base = M->getBase()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001102 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001103
1104 if (M->isArrow())
Ted Kremenekfef1f302008-10-18 03:28:48 +00001105 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1106 else
1107 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
Mike Stump11289f42009-09-09 15:08:12 +00001108
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001109 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1110 if (!Field) // FIXME: skipping member expressions for non-fields
1111 return;
1112
Zhongxing Xu107f7592009-08-06 12:48:26 +00001113 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001114 const GRState* state = GetState(*I);
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001115 // FIXME: Should we insert some assumption logic in here to determine
1116 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001117 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xub9eda672009-10-30 07:19:39 +00001118 SVal L = state->getLValue(Field, state->getSVal(Base));
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001119
Zhongxing Xub9eda672009-10-30 07:19:39 +00001120 if (asLValue)
1121 MakeNode(Dst, M, *I, state->BindExpr(M, L), ProgramPoint::PostLValueKind);
1122 else
1123 EvalLoad(Dst, M, *I, state, L);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001124 }
Ted Kremenek38213f92008-04-21 23:43:38 +00001125}
1126
Ted Kremenek17d541d2009-02-13 01:45:31 +00001127/// EvalBind - Handle the semantics of binding a value to a specific location.
1128/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001129void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Stmt *AssignE,
1130 Stmt* StoreE, ExplodedNode* Pred,
Ted Kremenekb006b822009-11-04 00:09:15 +00001131 const GRState* state, SVal location, SVal Val,
1132 bool atDeclInit) {
Ted Kremenekef910042009-11-04 04:24:16 +00001133
1134
1135 // Do a previsit of the bind.
1136 ExplodedNodeSet CheckedSet, Src;
1137 Src.Add(Pred);
Ted Kremenek209e31b2009-11-05 00:42:23 +00001138 CheckerVisitBind(AssignE, StoreE, CheckedSet, Src, location, Val, true);
Ted Kremenekef910042009-11-04 04:24:16 +00001139
1140 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
1141 I!=E; ++I) {
1142
1143 if (Pred != *I)
1144 state = GetState(*I);
1145
1146 const GRState* newState = 0;
Ted Kremenek17d541d2009-02-13 01:45:31 +00001147
Ted Kremenekef910042009-11-04 04:24:16 +00001148 if (atDeclInit) {
1149 const VarRegion *VR =
1150 cast<VarRegion>(cast<loc::MemRegionVal>(location).getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001151
Ted Kremenekef910042009-11-04 04:24:16 +00001152 newState = state->bindDecl(VR, Val);
Ted Kremenekb006b822009-11-04 00:09:15 +00001153 }
1154 else {
Ted Kremenekef910042009-11-04 04:24:16 +00001155 if (location.isUnknown()) {
1156 // We know that the new state will be the same as the old state since
1157 // the location of the binding is "unknown". Consequently, there
1158 // is no reason to just create a new node.
1159 newState = state;
1160 }
1161 else {
1162 // We are binding to a value other than 'unknown'. Perform the binding
1163 // using the StoreManager.
1164 newState = state->bindLoc(cast<Loc>(location), Val);
1165 }
Ted Kremenekb006b822009-11-04 00:09:15 +00001166 }
Ted Kremenekef910042009-11-04 04:24:16 +00001167
1168 // The next thing to do is check if the GRTransferFuncs object wants to
1169 // update the state based on the new binding. If the GRTransferFunc object
1170 // doesn't do anything, just auto-propagate the current state.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001171 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, newState, StoreE,
Ted Kremenekef910042009-11-04 04:24:16 +00001172 newState != state);
1173
1174 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001175 }
Ted Kremenek17d541d2009-02-13 01:45:31 +00001176}
1177
1178/// EvalStore - Handle the semantics of a store via an assignment.
1179/// @param Dst The node set to store generated state nodes
1180/// @param Ex The expression representing the location of the store
1181/// @param state The current simulation state
1182/// @param location The location to store the value
1183/// @param Val The value to be stored
Ted Kremenek209e31b2009-11-05 00:42:23 +00001184void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr *AssignE,
1185 Expr* StoreE,
1186 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001187 const GRState* state, SVal location, SVal Val,
1188 const void *tag) {
Mike Stump11289f42009-09-09 15:08:12 +00001189
Ted Kremenek209e31b2009-11-05 00:42:23 +00001190 assert(Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump11289f42009-09-09 15:08:12 +00001191
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001192 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001193 ExplodedNodeSet Tmp;
1194 EvalLocation(Tmp, StoreE, Pred, state, location, tag, false);
Mike Stump11289f42009-09-09 15:08:12 +00001195
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001196 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001197 return;
Ted Kremenekc072b822008-04-18 20:35:30 +00001198
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001199 assert(!location.isUndef());
Ted Kremenek17d541d2009-02-13 01:45:31 +00001200
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001201 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind,
1202 ProgramPoint::PostStoreKind);
1203 SaveAndRestore<const void*> OldTag(Builder->Tag, tag);
1204
Mike Stump11289f42009-09-09 15:08:12 +00001205 // Proceed with the store.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001206 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
1207 EvalBind(Dst, AssignE, StoreE, *NI, GetState(*NI), location, Val);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001208}
1209
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001210void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr *Ex, ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001211 const GRState* state, SVal location,
1212 const void *tag) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001213
Mike Stump11289f42009-09-09 15:08:12 +00001214 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001215 ExplodedNodeSet Tmp;
1216 EvalLocation(Tmp, Ex, Pred, state, location, tag, true);
Mike Stump11289f42009-09-09 15:08:12 +00001217
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001218 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001219 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001220
1221 assert(!location.isUndef());
1222
1223 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1224 SaveAndRestore<const void*> OldTag(Builder->Tag);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001226 // Proceed with the load.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001227 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1228 state = GetState(*NI);
1229 if (location.isUnknown()) {
1230 // This is important. We must nuke the old binding.
1231 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, UnknownVal()),
1232 ProgramPoint::PostLoadKind, tag);
1233 }
1234 else {
1235 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
1236 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, V), ProgramPoint::PostLoadKind,
1237 tag);
1238 }
Zhongxing Xu33178a02008-11-28 08:34:30 +00001239 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001240}
1241
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001242void GRExprEngine::EvalLocation(ExplodedNodeSet &Dst, Stmt *S,
1243 ExplodedNode* Pred,
1244 const GRState* state, SVal location,
1245 const void *tag, bool isLoad) {
Mike Stump11289f42009-09-09 15:08:12 +00001246
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001247 if (location.isUnknown() || Checkers.empty()) {
1248 Dst.Add(Pred);
1249 return;
Ted Kremenekfac290d2009-11-02 23:19:29 +00001250 }
1251
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001252 ExplodedNodeSet Src, Tmp;
1253 Src.Add(Pred);
1254 ExplodedNodeSet *PrevSet = &Src;
1255
1256 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
1257 {
1258 ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst
1259 : (PrevSet == &Tmp) ? &Src : &Tmp;
1260
1261 CurrSet->clear();
1262 void *tag = I->first;
1263 Checker *checker = I->second;
1264
1265 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
1266 NI != NE; ++NI)
1267 checker->GR_VisitLocation(*CurrSet, *Builder, *this, S, *NI, state,
1268 location, tag, isLoad);
1269
1270 // Update which NodeSet is the current one.
1271 PrevSet = CurrSet;
1272 }
1273
Ted Kremenekeb01ba62009-08-01 05:59:39 +00001274 // FIXME: Temporarily disable out-of-bounds checking until we make
1275 // the logic reflect recent changes to CastRegion and friends.
1276#if 0
Zhongxing Xu3d430152008-11-08 03:45:42 +00001277 // Check for out-of-bound array access.
Ted Kremenek9e08ff42008-12-16 22:02:27 +00001278 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu3d430152008-11-08 03:45:42 +00001279 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1280 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1281 // Get the index of the accessed element.
1282 SVal Idx = ER->getIndex();
1283 // Get the extent of the array.
Zhongxing Xu6587c752008-11-24 07:02:06 +00001284 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1285 ER->getSuperRegion());
Zhongxing Xu3d430152008-11-08 03:45:42 +00001286
Ted Kremenek7020eae2009-09-11 22:07:28 +00001287 const GRState * StInBound = StNotNull->AssumeInBound(Idx, NumElements,
Ted Kremenekf9906842009-06-18 22:57:13 +00001288 true);
Ted Kremenek7020eae2009-09-11 22:07:28 +00001289 const GRState* StOutBound = StNotNull->AssumeInBound(Idx, NumElements,
Ted Kremenekf9906842009-06-18 22:57:13 +00001290 false);
Zhongxing Xu3d430152008-11-08 03:45:42 +00001291
Ted Kremenekf9906842009-06-18 22:57:13 +00001292 if (StOutBound) {
Ted Kremenek9e08ff42008-12-16 22:02:27 +00001293 // Report warning. Make sink node manually.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001294 ExplodedNode* OOBNode =
Ted Kremenek9e08ff42008-12-16 22:02:27 +00001295 Builder->generateNode(Ex, StOutBound, Pred,
1296 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00001297
1298 if (OOBNode) {
1299 OOBNode->markAsSink();
1300
Ted Kremenekf9906842009-06-18 22:57:13 +00001301 if (StInBound)
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00001302 ImplicitOOBMemAccesses.insert(OOBNode);
1303 else
1304 ExplicitOOBMemAccesses.insert(OOBNode);
1305 }
Zhongxing Xu4d45b342008-11-22 13:21:46 +00001306 }
1307
Ted Kremenekf9906842009-06-18 22:57:13 +00001308 if (!StInBound)
1309 return NULL;
1310
Ted Kremenek9e08ff42008-12-16 22:02:27 +00001311 StNotNull = StInBound;
Zhongxing Xu3d430152008-11-08 03:45:42 +00001312 }
1313 }
Ted Kremenekeb01ba62009-08-01 05:59:39 +00001314#endif
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001315}
1316
Ted Kremenek667cacb2008-04-15 23:06:53 +00001317//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +00001318// Transfer function: OSAtomics.
1319//
1320// FIXME: Eventually refactor into a more "plugin" infrastructure.
1321//===----------------------------------------------------------------------===//
1322
1323// Mac OS X:
1324// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1325// atomic.3.html
1326//
Zhongxing Xu20227f72009-08-06 01:32:16 +00001327static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst,
Ted Kremenekdf240002009-04-11 00:11:10 +00001328 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001329 GRStmtNodeBuilder& Builder,
Mike Stump11289f42009-09-09 15:08:12 +00001330 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001331 ExplodedNode* Pred) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001332
1333 // Not enough arguments to match OSAtomicCompareAndSwap?
1334 if (CE->getNumArgs() != 3)
1335 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001336
Ted Kremenekdf240002009-04-11 00:11:10 +00001337 ASTContext &C = Engine.getContext();
1338 Expr *oldValueExpr = CE->getArg(0);
1339 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1340
1341 Expr *newValueExpr = CE->getArg(1);
1342 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001343
Ted Kremenekdf240002009-04-11 00:11:10 +00001344 // Do the types of 'oldValue' and 'newValue' match?
1345 if (oldValueType != newValueType)
1346 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001347
Ted Kremenekdf240002009-04-11 00:11:10 +00001348 Expr *theValueExpr = CE->getArg(2);
Ted Kremenekeb01ba62009-08-01 05:59:39 +00001349 const PointerType *theValueType =
1350 theValueExpr->getType()->getAs<PointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenekdf240002009-04-11 00:11:10 +00001352 // theValueType not a pointer?
1353 if (!theValueType)
1354 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001355
Ted Kremenekdf240002009-04-11 00:11:10 +00001356 QualType theValueTypePointee =
1357 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00001358
Ted Kremenekdf240002009-04-11 00:11:10 +00001359 // The pointee must match newValueType and oldValueType.
1360 if (theValueTypePointee != newValueType)
1361 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001362
Ted Kremenekdf240002009-04-11 00:11:10 +00001363 static unsigned magic_load = 0;
1364 static unsigned magic_store = 0;
1365
1366 const void *OSAtomicLoadTag = &magic_load;
1367 const void *OSAtomicStoreTag = &magic_store;
Mike Stump11289f42009-09-09 15:08:12 +00001368
Ted Kremenekdf240002009-04-11 00:11:10 +00001369 // Load 'theValue'.
Ted Kremenekdf240002009-04-11 00:11:10 +00001370 const GRState *state = Pred->getState();
Zhongxing Xu20227f72009-08-06 01:32:16 +00001371 ExplodedNodeSet Tmp;
Ted Kremenek095f1a92009-06-18 23:58:37 +00001372 SVal location = state->getSVal(theValueExpr);
Ted Kremenekdf240002009-04-11 00:11:10 +00001373 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1374
Zhongxing Xu20227f72009-08-06 01:32:16 +00001375 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
Ted Kremenekdf240002009-04-11 00:11:10 +00001376 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001377
Zhongxing Xu20227f72009-08-06 01:32:16 +00001378 ExplodedNode *N = *I;
Ted Kremenekdf240002009-04-11 00:11:10 +00001379 const GRState *stateLoad = N->getState();
Ted Kremenek7020eae2009-09-11 22:07:28 +00001380 SVal theValueVal_untested = stateLoad->getSVal(theValueExpr);
1381 SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001382
Ted Kremeneka1f9c7f2009-07-20 20:38:59 +00001383 // FIXME: Issue an error.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001384 if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) {
Mike Stump11289f42009-09-09 15:08:12 +00001385 return false;
Ted Kremeneka1f9c7f2009-07-20 20:38:59 +00001386 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001387
1388 DefinedOrUnknownSVal theValueVal =
1389 cast<DefinedOrUnknownSVal>(theValueVal_untested);
1390 DefinedOrUnknownSVal oldValueVal =
1391 cast<DefinedOrUnknownSVal>(oldValueVal_untested);
Mike Stump11289f42009-09-09 15:08:12 +00001392
Ted Kremenek3a459dc2009-08-25 18:44:25 +00001393 SValuator &SVator = Engine.getSValuator();
Mike Stump11289f42009-09-09 15:08:12 +00001394
Ted Kremenekdf240002009-04-11 00:11:10 +00001395 // Perform the comparison.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001396 DefinedOrUnknownSVal Cmp = SVator.EvalEQ(stateLoad, theValueVal,
1397 oldValueVal);
Ted Kremenekf9906842009-06-18 22:57:13 +00001398
Ted Kremenek7020eae2009-09-11 22:07:28 +00001399 const GRState *stateEqual = stateLoad->Assume(Cmp, true);
Mike Stump11289f42009-09-09 15:08:12 +00001400
Ted Kremenekdf240002009-04-11 00:11:10 +00001401 // Were they equal?
Ted Kremenekf9906842009-06-18 22:57:13 +00001402 if (stateEqual) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001403 // Perform the store.
Zhongxing Xu20227f72009-08-06 01:32:16 +00001404 ExplodedNodeSet TmpStore;
Ted Kremenekac7c7242009-07-21 21:03:30 +00001405 SVal val = stateEqual->getSVal(newValueExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001406
Ted Kremenekac7c7242009-07-21 21:03:30 +00001407 // Handle implicit value casts.
1408 if (const TypedRegion *R =
1409 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
Ted Kremenek3a459dc2009-08-25 18:44:25 +00001410 llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C),
1411 newValueExpr->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001412 }
1413
Ted Kremenek209e31b2009-11-05 00:42:23 +00001414 Engine.EvalStore(TmpStore, NULL, theValueExpr, N, stateEqual, location,
Ted Kremenekac7c7242009-07-21 21:03:30 +00001415 val, OSAtomicStoreTag);
Mike Stump11289f42009-09-09 15:08:12 +00001416
Ted Kremenekdf240002009-04-11 00:11:10 +00001417 // Now bind the result of the comparison.
Zhongxing Xu20227f72009-08-06 01:32:16 +00001418 for (ExplodedNodeSet::iterator I2 = TmpStore.begin(),
Ted Kremenekdf240002009-04-11 00:11:10 +00001419 E2 = TmpStore.end(); I2 != E2; ++I2) {
Zhongxing Xu20227f72009-08-06 01:32:16 +00001420 ExplodedNode *predNew = *I2;
Ted Kremenekdf240002009-04-11 00:11:10 +00001421 const GRState *stateNew = predNew->getState();
1422 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001423 Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res));
Ted Kremenekdf240002009-04-11 00:11:10 +00001424 }
1425 }
Mike Stump11289f42009-09-09 15:08:12 +00001426
Ted Kremenekdf240002009-04-11 00:11:10 +00001427 // Were they not equal?
Ted Kremenek7020eae2009-09-11 22:07:28 +00001428 if (const GRState *stateNotEqual = stateLoad->Assume(Cmp, false)) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001429 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001430 Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res));
Ted Kremenekdf240002009-04-11 00:11:10 +00001431 }
1432 }
Mike Stump11289f42009-09-09 15:08:12 +00001433
Ted Kremenekdf240002009-04-11 00:11:10 +00001434 return true;
1435}
1436
Zhongxing Xu20227f72009-08-06 01:32:16 +00001437static bool EvalOSAtomic(ExplodedNodeSet& Dst,
Ted Kremenekdf240002009-04-11 00:11:10 +00001438 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001439 GRStmtNodeBuilder& Builder,
Ted Kremenekdf240002009-04-11 00:11:10 +00001440 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001441 ExplodedNode* Pred) {
Zhongxing Xuac129432009-04-20 05:24:46 +00001442 const FunctionDecl* FD = L.getAsFunctionDecl();
1443 if (!FD)
Ted Kremenekdf240002009-04-11 00:11:10 +00001444 return false;
Zhongxing Xuac129432009-04-20 05:24:46 +00001445
Ted Kremenekdf240002009-04-11 00:11:10 +00001446 const char *FName = FD->getNameAsCString();
Mike Stump11289f42009-09-09 15:08:12 +00001447
Ted Kremenekdf240002009-04-11 00:11:10 +00001448 // Check for compare and swap.
Ted Kremenek4531be12009-04-11 00:54:13 +00001449 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1450 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenekdf240002009-04-11 00:11:10 +00001451 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek4531be12009-04-11 00:54:13 +00001452
Ted Kremenekdf240002009-04-11 00:11:10 +00001453 // FIXME: Other atomics.
1454 return false;
1455}
1456
1457//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001458// Transfer function: Function calls.
1459//===----------------------------------------------------------------------===//
Zhongxing Xu1748d8a2009-09-04 02:13:36 +00001460static void MarkNoReturnFunction(const FunctionDecl *FD, CallExpr *CE,
Mike Stump11289f42009-09-09 15:08:12 +00001461 const GRState *state,
Zhongxing Xu1748d8a2009-09-04 02:13:36 +00001462 GRStmtNodeBuilder *Builder) {
Zhongxing Xubfb000f32009-09-04 02:17:35 +00001463 if (!FD)
1464 return;
1465
Mike Stump11289f42009-09-09 15:08:12 +00001466 if (FD->getAttr<NoReturnAttr>() ||
Zhongxing Xu1748d8a2009-09-04 02:13:36 +00001467 FD->getAttr<AnalyzerNoReturnAttr>())
1468 Builder->BuildSinks = true;
1469 else {
1470 // HACK: Some functions are not marked noreturn, and don't return.
1471 // Here are a few hardwired ones. If this takes too long, we can
1472 // potentially cache these results.
Douglas Gregorf7b87cb2009-10-29 00:41:01 +00001473 using llvm::StringRef;
1474 bool BuildSinks
1475 = llvm::StringSwitch<bool>(StringRef(FD->getIdentifier()->getName()))
1476 .Case("exit", true)
1477 .Case("panic", true)
1478 .Case("error", true)
1479 .Case("Assert", true)
1480 // FIXME: This is just a wrapper around throwing an exception.
1481 // Eventually inter-procedural analysis should handle this easily.
1482 .Case("ziperr", true)
1483 .Case("assfail", true)
1484 .Case("db_error", true)
1485 .Case("__assert", true)
1486 .Case("__assert_rtn", true)
1487 .Case("__assert_fail", true)
1488 .Case("dtrace_assfail", true)
1489 .Case("yy_fatal_error", true)
1490 .Case("_XCAssertionFailureHandler", true)
1491 .Case("_DTAssertionFailureHandler", true)
1492 .Case("_TSAssertionFailureHandler", true)
1493 .Default(false);
1494
1495 if (BuildSinks)
1496 Builder->BuildSinks = true;
Zhongxing Xu1748d8a2009-09-04 02:13:36 +00001497 }
1498}
Ted Kremenekdf240002009-04-11 00:11:10 +00001499
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001500bool GRExprEngine::EvalBuiltinFunction(const FunctionDecl *FD, CallExpr *CE,
1501 ExplodedNode *Pred,
1502 ExplodedNodeSet &Dst) {
1503 if (!FD)
1504 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001505
Douglas Gregor15fc9562009-09-12 00:22:50 +00001506 unsigned id = FD->getBuiltinID();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001507 if (!id)
1508 return false;
1509
1510 const GRState *state = Pred->getState();
1511
1512 switch (id) {
1513 case Builtin::BI__builtin_expect: {
1514 // For __builtin_expect, just return the value of the subexpression.
Mike Stump11289f42009-09-09 15:08:12 +00001515 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001516 SVal X = state->getSVal(*(CE->arg_begin()));
1517 MakeNode(Dst, CE, Pred, state->BindExpr(CE, X));
1518 return true;
1519 }
Mike Stump11289f42009-09-09 15:08:12 +00001520
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001521 case Builtin::BI__builtin_alloca: {
1522 // FIXME: Refactor into StoreManager itself?
1523 MemRegionManager& RM = getStateManager().getRegionManager();
1524 const MemRegion* R =
1525 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Mike Stump11289f42009-09-09 15:08:12 +00001526
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001527 // Set the extent of the region in bytes. This enables us to use the
1528 // SVal of the argument directly. If we save the extent in bits, we
1529 // cannot represent values like symbol*8.
1530 SVal Extent = state->getSVal(*(CE->arg_begin()));
1531 state = getStoreManager().setExtent(state, R, Extent);
1532 MakeNode(Dst, CE, Pred, state->BindExpr(CE, loc::MemRegionVal(R)));
1533 return true;
1534 }
1535 }
1536
1537 return false;
1538}
1539
Mike Stump11289f42009-09-09 15:08:12 +00001540void GRExprEngine::EvalCall(ExplodedNodeSet& Dst, CallExpr* CE, SVal L,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001541 ExplodedNode* Pred) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001542 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump11289f42009-09-09 15:08:12 +00001543
Ted Kremenekdf240002009-04-11 00:11:10 +00001544 // FIXME: Allow us to chain together transfer functions.
1545 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1546 return;
Mike Stump11289f42009-09-09 15:08:12 +00001547
Ted Kremenekdf240002009-04-11 00:11:10 +00001548 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1549}
1550
Zhongxing Xu107f7592009-08-06 12:48:26 +00001551void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001552 CallExpr::arg_iterator AI,
1553 CallExpr::arg_iterator AE,
Mike Stump11289f42009-09-09 15:08:12 +00001554 ExplodedNodeSet& Dst) {
Douglas Gregor6b754842008-10-28 00:22:11 +00001555 // Determine the type of function we're calling (if available).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001556 const FunctionProtoType *Proto = NULL;
Douglas Gregor6b754842008-10-28 00:22:11 +00001557 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001558 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
John McCall9dd450b2009-09-21 23:43:11 +00001559 Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>();
Douglas Gregor6b754842008-10-28 00:22:11 +00001560
1561 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1562}
1563
Zhongxing Xu107f7592009-08-06 12:48:26 +00001564void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred,
Douglas Gregor6b754842008-10-28 00:22:11 +00001565 CallExpr::arg_iterator AI,
1566 CallExpr::arg_iterator AE,
Mike Stump11289f42009-09-09 15:08:12 +00001567 ExplodedNodeSet& Dst,
1568 const FunctionProtoType *Proto,
Douglas Gregor6b754842008-10-28 00:22:11 +00001569 unsigned ParamIdx) {
Mike Stump11289f42009-09-09 15:08:12 +00001570
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001571 // Process the arguments.
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001572 if (AI != AE) {
Douglas Gregor6b754842008-10-28 00:22:11 +00001573 // If the call argument is being bound to a reference parameter,
1574 // visit it as an lvalue, not an rvalue.
1575 bool VisitAsLvalue = false;
1576 if (Proto && ParamIdx < Proto->getNumArgs())
1577 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1578
Mike Stump11289f42009-09-09 15:08:12 +00001579 ExplodedNodeSet DstTmp;
Douglas Gregor6b754842008-10-28 00:22:11 +00001580 if (VisitAsLvalue)
Mike Stump11289f42009-09-09 15:08:12 +00001581 VisitLValue(*AI, Pred, DstTmp);
Douglas Gregor6b754842008-10-28 00:22:11 +00001582 else
Mike Stump11289f42009-09-09 15:08:12 +00001583 Visit(*AI, Pred, DstTmp);
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001584 ++AI;
Mike Stump11289f42009-09-09 15:08:12 +00001585
Zhongxing Xu107f7592009-08-06 12:48:26 +00001586 for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE;
1587 ++DI)
Douglas Gregor6b754842008-10-28 00:22:11 +00001588 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Mike Stump11289f42009-09-09 15:08:12 +00001589
Ted Kremeneke0188e62008-02-19 01:44:53 +00001590 return;
1591 }
1592
1593 // If we reach here we have processed all of the arguments. Evaluate
1594 // the callee expression.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001595 ExplodedNodeSet DstTmp;
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001596 Expr* Callee = CE->getCallee()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00001597
Ted Kremenek49513cc2009-07-22 21:43:51 +00001598 { // Enter new scope to make the lifetime of 'DstTmp2' bounded.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001599 ExplodedNodeSet DstTmp2;
Ted Kremenek49513cc2009-07-22 21:43:51 +00001600 Visit(Callee, Pred, DstTmp2);
Mike Stump11289f42009-09-09 15:08:12 +00001601
Ted Kremenek49513cc2009-07-22 21:43:51 +00001602 // Perform the previsit of the CallExpr, storing the results in DstTmp.
1603 CheckerVisit(CE, DstTmp, DstTmp2, true);
1604 }
Mike Stump11289f42009-09-09 15:08:12 +00001605
Ted Kremeneke0188e62008-02-19 01:44:53 +00001606 // Finally, evaluate the function call.
Mike Stump11289f42009-09-09 15:08:12 +00001607 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001608 DI != DE; ++DI) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001609
Ted Kremenek17d541d2009-02-13 01:45:31 +00001610 const GRState* state = GetState(*DI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001611 SVal L = state->getSVal(Callee);
Ted Kremeneke0188e62008-02-19 01:44:53 +00001612
Ted Kremenek8efd6b4e2008-03-03 16:47:31 +00001613 // FIXME: Add support for symbolic function calls (calls involving
1614 // function pointer values that are symbolic).
Zhongxing Xu79affb72009-09-02 08:10:35 +00001615
Ted Kremenek70342362008-03-05 21:15:02 +00001616 // Check for the "noreturn" attribute.
Mike Stump11289f42009-09-09 15:08:12 +00001617
Ted Kremenek70342362008-03-05 21:15:02 +00001618 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xuac129432009-04-20 05:24:46 +00001619 const FunctionDecl* FD = L.getAsFunctionDecl();
Zhongxing Xubfb000f32009-09-04 02:17:35 +00001620
1621 MarkNoReturnFunction(FD, CE, state, Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001622
Ted Kremenek70342362008-03-05 21:15:02 +00001623 // Evaluate the call.
Zhongxing Xu1309a852009-09-05 06:46:12 +00001624 if (EvalBuiltinFunction(FD, CE, *DI, Dst))
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001625 continue;
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001626
Mike Stump11289f42009-09-09 15:08:12 +00001627 // Dispatch to the plug-in transfer function.
1628
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001629 unsigned size = Dst.size();
1630 SaveOr OldHasGen(Builder->HasGeneratedNode);
1631 EvalCall(Dst, CE, L, *DI);
Mike Stump11289f42009-09-09 15:08:12 +00001632
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001633 // Handle the case where no nodes where generated. Auto-generate that
1634 // contains the updated state if we aren't generating sinks.
Mike Stump11289f42009-09-09 15:08:12 +00001635
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001636 if (!Builder->BuildSinks && Dst.size() == size &&
1637 !Builder->HasGeneratedNode)
Ted Kremenek17d541d2009-02-13 01:45:31 +00001638 MakeNode(Dst, CE, *DI, state);
Ted Kremeneke0188e62008-02-19 01:44:53 +00001639 }
1640}
1641
Ted Kremenek667cacb2008-04-15 23:06:53 +00001642//===----------------------------------------------------------------------===//
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001643// Transfer function: Objective-C ivar references.
1644//===----------------------------------------------------------------------===//
1645
Ted Kremenek111a6bd2009-02-28 20:50:43 +00001646static std::pair<const void*,const void*> EagerlyAssumeTag
1647 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1648
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001649void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
1650 Expr *Ex) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001651 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1652 ExplodedNode *Pred = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001653
Ted Kremenekff290ca2009-02-25 23:32:10 +00001654 // Test if the previous node was as the same expression. This can happen
1655 // when the expression fails to evaluate to anything meaningful and
1656 // (as an optimization) we don't generate a node.
Mike Stump11289f42009-09-09 15:08:12 +00001657 ProgramPoint P = Pred->getLocation();
Ted Kremenekff290ca2009-02-25 23:32:10 +00001658 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump11289f42009-09-09 15:08:12 +00001659 Dst.Add(Pred);
Ted Kremenekff290ca2009-02-25 23:32:10 +00001660 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001661 }
Ted Kremenekff290ca2009-02-25 23:32:10 +00001662
Mike Stump11289f42009-09-09 15:08:12 +00001663 const GRState* state = Pred->getState();
1664 SVal V = state->getSVal(Ex);
Ted Kremenek7020eae2009-09-11 22:07:28 +00001665 if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) {
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001666 // First assume that the condition is true.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001667 if (const GRState *stateTrue = state->Assume(*SEV, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00001668 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001669 ValMgr.makeIntVal(1U, Ex->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001670 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xue1190f72009-08-15 03:17:38 +00001671 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001672 stateTrue, Pred));
1673 }
Mike Stump11289f42009-09-09 15:08:12 +00001674
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001675 // Next, assume that the condition is false.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001676 if (const GRState *stateFalse = state->Assume(*SEV, false)) {
Mike Stump11289f42009-09-09 15:08:12 +00001677 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001678 ValMgr.makeIntVal(0U, Ex->getType()));
Zhongxing Xue1190f72009-08-15 03:17:38 +00001679 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
1680 Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001681 stateFalse, Pred));
1682 }
1683 }
1684 else
1685 Dst.Add(Pred);
1686 }
1687}
1688
1689//===----------------------------------------------------------------------===//
1690// Transfer function: Objective-C ivar references.
1691//===----------------------------------------------------------------------===//
1692
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001693void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, ExplodedNode* Pred,
1694 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001695
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001696 Expr* Base = cast<Expr>(Ex->getBase());
Zhongxing Xu107f7592009-08-06 12:48:26 +00001697 ExplodedNodeSet Tmp;
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001698 Visit(Base, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001699
Zhongxing Xu107f7592009-08-06 12:48:26 +00001700 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001701 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001702 SVal BaseVal = state->getSVal(Base);
1703 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Mike Stump11289f42009-09-09 15:08:12 +00001704
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001705 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001706 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001707 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001708 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001709 }
1710}
1711
1712//===----------------------------------------------------------------------===//
Ted Kremenek17810802008-11-12 19:24:17 +00001713// Transfer function: Objective-C fast enumeration 'for' statements.
1714//===----------------------------------------------------------------------===//
1715
1716void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001717 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001718
Ted Kremenek17810802008-11-12 19:24:17 +00001719 // ObjCForCollectionStmts are processed in two places. This method
1720 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1721 // statements within a basic block. This transfer function does two things:
1722 //
1723 // (1) binds the next container value to 'element'. This creates a new
1724 // node in the ExplodedGraph.
1725 //
1726 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1727 // whether or not the container has any more elements. This value
1728 // will be tested in ProcessBranch. We need to explicitly bind
1729 // this value because a container can contain nil elements.
Mike Stump11289f42009-09-09 15:08:12 +00001730 //
Ted Kremenek17810802008-11-12 19:24:17 +00001731 // FIXME: Eventually this logic should actually do dispatches to
1732 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1733 // This will require simulating a temporary NSFastEnumerationState, either
1734 // through an SVal or through the use of MemRegions. This value can
1735 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1736 // terminates we reclaim the temporary (it goes out of scope) and we
1737 // we can test if the SVal is 0 or if the MemRegion is null (depending
1738 // on what approach we take).
1739 //
1740 // For now: simulate (1) by assigning either a symbol or nil if the
1741 // container is empty. Thus this transfer function will by default
1742 // result in state splitting.
Mike Stump11289f42009-09-09 15:08:12 +00001743
Ted Kremenek537f6382008-11-14 19:47:18 +00001744 Stmt* elem = S->getElement();
1745 SVal ElementV;
Mike Stump11289f42009-09-09 15:08:12 +00001746
Ted Kremenek17810802008-11-12 19:24:17 +00001747 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner529efc72009-03-28 06:33:19 +00001748 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek17810802008-11-12 19:24:17 +00001749 assert (ElemD->getInit() == 0);
Ted Kremenek14536f62009-08-21 22:28:32 +00001750 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek537f6382008-11-14 19:47:18 +00001751 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1752 return;
Ted Kremenek17810802008-11-12 19:24:17 +00001753 }
Ted Kremenek537f6382008-11-14 19:47:18 +00001754
Zhongxing Xu107f7592009-08-06 12:48:26 +00001755 ExplodedNodeSet Tmp;
Ted Kremenek537f6382008-11-14 19:47:18 +00001756 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001757
Zhongxing Xu107f7592009-08-06 12:48:26 +00001758 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001759 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001760 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek537f6382008-11-14 19:47:18 +00001761 }
1762}
1763
1764void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001765 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek537f6382008-11-14 19:47:18 +00001766 SVal ElementV) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001767
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001768 // Check if the location we are writing back to is a null pointer.
Ted Kremenek537f6382008-11-14 19:47:18 +00001769 Stmt* elem = S->getElement();
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001770 ExplodedNodeSet Tmp;
1771 EvalLocation(Tmp, elem, Pred, GetState(Pred), ElementV, NULL, false);
1772
1773 if (Tmp.empty())
Ted Kremenek537f6382008-11-14 19:47:18 +00001774 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001775
1776 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1777 Pred = *NI;
1778 const GRState *state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001779
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001780 // Handle the case where the container still has elements.
1781 SVal TrueV = ValMgr.makeTruthVal(1);
1782 const GRState *hasElems = state->BindExpr(S, TrueV);
Ted Kremenek537f6382008-11-14 19:47:18 +00001783
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001784 // Handle the case where the container has no elements.
1785 SVal FalseV = ValMgr.makeTruthVal(0);
1786 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump11289f42009-09-09 15:08:12 +00001787
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001788 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1789 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1790 // FIXME: The proper thing to do is to really iterate over the
1791 // container. We will do this with dispatch logic to the store.
1792 // For now, just 'conjure' up a symbolic value.
1793 QualType T = R->getValueType(getContext());
1794 assert(Loc::IsLocType(T));
1795 unsigned Count = Builder->getCurrentBlockCount();
1796 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1797 SVal V = ValMgr.makeLoc(Sym);
1798 hasElems = hasElems->bindLoc(ElementV, V);
Mike Stump11289f42009-09-09 15:08:12 +00001799
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001800 // Bind the location to 'nil' on the false branch.
1801 SVal nilV = ValMgr.makeIntVal(0, T);
1802 noElems = noElems->bindLoc(ElementV, nilV);
1803 }
Ted Kremenekdf317922008-11-12 21:12:46 +00001804
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001805 // Create the new nodes.
1806 MakeNode(Dst, S, Pred, hasElems);
1807 MakeNode(Dst, S, Pred, noElems);
1808 }
Ted Kremenek17810802008-11-12 19:24:17 +00001809}
1810
1811//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001812// Transfer function: Objective-C message expressions.
1813//===----------------------------------------------------------------------===//
1814
Zhongxing Xu107f7592009-08-06 12:48:26 +00001815void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
1816 ExplodedNodeSet& Dst){
Mike Stump11289f42009-09-09 15:08:12 +00001817
Ted Kremenek667cacb2008-04-15 23:06:53 +00001818 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1819 Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00001820}
Ted Kremenek667cacb2008-04-15 23:06:53 +00001821
1822void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu4de1c852008-10-31 07:26:14 +00001823 ObjCMessageExpr::arg_iterator AI,
1824 ObjCMessageExpr::arg_iterator AE,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001825 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek667cacb2008-04-15 23:06:53 +00001826 if (AI == AE) {
Mike Stump11289f42009-09-09 15:08:12 +00001827
Ted Kremenek667cacb2008-04-15 23:06:53 +00001828 // Process the receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001829
Ted Kremenek667cacb2008-04-15 23:06:53 +00001830 if (Expr* Receiver = ME->getReceiver()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001831 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001832 Visit(Receiver, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001833
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001834 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE;
1835 ++NI)
Daniel Dunbar8e31e772009-07-23 04:41:06 +00001836 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00001837
Ted Kremenek667cacb2008-04-15 23:06:53 +00001838 return;
1839 }
Mike Stump11289f42009-09-09 15:08:12 +00001840
Daniel Dunbar8e31e772009-07-23 04:41:06 +00001841 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001842 return;
1843 }
Mike Stump11289f42009-09-09 15:08:12 +00001844
Zhongxing Xu107f7592009-08-06 12:48:26 +00001845 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001846 Visit(*AI, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001847
Ted Kremenek667cacb2008-04-15 23:06:53 +00001848 ++AI;
Mike Stump11289f42009-09-09 15:08:12 +00001849
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001850 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremenek667cacb2008-04-15 23:06:53 +00001851 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1852}
1853
1854void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001855 ExplodedNode* Pred,
1856 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001857
1858 // FIXME: More logic for the processing the method call.
1859
Ted Kremenek17d541d2009-02-13 01:45:31 +00001860 const GRState* state = GetState(Pred);
Ted Kremenek7f824732008-05-01 18:33:28 +00001861 bool RaisesException = false;
Mike Stump11289f42009-09-09 15:08:12 +00001862
1863
Ted Kremenek667cacb2008-04-15 23:06:53 +00001864 if (Expr* Receiver = ME->getReceiver()) {
Mike Stump11289f42009-09-09 15:08:12 +00001865
Ted Kremenek7020eae2009-09-11 22:07:28 +00001866 SVal L_untested = state->getSVal(Receiver);
Mike Stump11289f42009-09-09 15:08:12 +00001867
1868 // Check for undefined control-flow.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001869 if (L_untested.isUndef()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001870 ExplodedNode* N = Builder->generateNode(ME, state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001871
Ted Kremenek667cacb2008-04-15 23:06:53 +00001872 if (N) {
1873 N->markAsSink();
1874 UndefReceivers.insert(N);
1875 }
Mike Stump11289f42009-09-09 15:08:12 +00001876
Ted Kremenek667cacb2008-04-15 23:06:53 +00001877 return;
1878 }
Mike Stump11289f42009-09-09 15:08:12 +00001879
1880 // "Assume" that the receiver is not NULL.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001881 DefinedOrUnknownSVal L = cast<DefinedOrUnknownSVal>(L_untested);
1882 const GRState *StNotNull = state->Assume(L, true);
Mike Stump11289f42009-09-09 15:08:12 +00001883
1884 // "Assume" that the receiver is NULL.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001885 const GRState *StNull = state->Assume(L, false);
Mike Stump11289f42009-09-09 15:08:12 +00001886
Ted Kremenekf9906842009-06-18 22:57:13 +00001887 if (StNull) {
Ted Kremenekf9f94202009-04-09 05:45:56 +00001888 QualType RetTy = ME->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001889
Ted Kremenek66d9edc2009-02-19 04:06:22 +00001890 // Check if the receiver was nil and the return value a struct.
Mike Stump11289f42009-09-09 15:08:12 +00001891 if (RetTy->isRecordType()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001892 if (Pred->getParentMap().isConsumedExpr(ME)) {
Ted Kremenek605fee82009-04-08 03:07:17 +00001893 // The [0 ...] expressions will return garbage. Flag either an
1894 // explicit or implicit error. Because of the structure of this
1895 // function we currently do not bifurfacte the state graph at
1896 // this point.
1897 // FIXME: We should bifurcate and fill the returned struct with
Mike Stump11289f42009-09-09 15:08:12 +00001898 // garbage.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001899 if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
Ted Kremenek605fee82009-04-08 03:07:17 +00001900 N->markAsSink();
Ted Kremenekf9906842009-06-18 22:57:13 +00001901 if (StNotNull)
Ted Kremenek605fee82009-04-08 03:07:17 +00001902 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek2f3b0db2009-04-09 06:02:06 +00001903 else
Mike Stump11289f42009-09-09 15:08:12 +00001904 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek605fee82009-04-08 03:07:17 +00001905 }
1906 }
Ted Kremenekd937ed32009-04-09 00:00:02 +00001907 }
Ted Kremenekf9f94202009-04-09 05:45:56 +00001908 else {
Ted Kremenekd937ed32009-04-09 00:00:02 +00001909 ASTContext& Ctx = getContext();
Ted Kremenekf9f94202009-04-09 05:45:56 +00001910 if (RetTy != Ctx.VoidTy) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001911 if (Pred->getParentMap().isConsumedExpr(ME)) {
Ted Kremenekf9f94202009-04-09 05:45:56 +00001912 // sizeof(void *)
1913 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1914 // sizeof(return type)
1915 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenekd937ed32009-04-09 00:00:02 +00001916
Mike Stump11289f42009-09-09 15:08:12 +00001917 if (voidPtrSize < returnTypeSize) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001918 if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
Ted Kremenekf9f94202009-04-09 05:45:56 +00001919 N->markAsSink();
Mike Stump11289f42009-09-09 15:08:12 +00001920 if (StNotNull)
Ted Kremenekf9f94202009-04-09 05:45:56 +00001921 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek2f3b0db2009-04-09 06:02:06 +00001922 else
Mike Stump11289f42009-09-09 15:08:12 +00001923 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekf9f94202009-04-09 05:45:56 +00001924 }
1925 }
Ted Kremenekf9906842009-06-18 22:57:13 +00001926 else if (!StNotNull) {
Ted Kremenekf9f94202009-04-09 05:45:56 +00001927 // Handle the safe cases where the return value is 0 if the
1928 // receiver is nil.
1929 //
1930 // FIXME: For now take the conservative approach that we only
1931 // return null values if we *know* that the receiver is nil.
1932 // This is because we can have surprises like:
1933 //
1934 // ... = [[NSScreens screens] objectAtIndex:0];
1935 //
1936 // What can happen is that [... screens] could return nil, but
1937 // it most likely isn't nil. We should assume the semantics
1938 // of this case unless we have *a lot* more knowledge.
1939 //
Ted Kremenekf8cb51c2009-04-09 16:46:55 +00001940 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001941 MakeNode(Dst, ME, Pred, StNull->BindExpr(ME, V));
Ted Kremenekc3f7c852009-04-09 04:06:51 +00001942 return;
1943 }
Ted Kremenek605fee82009-04-08 03:07:17 +00001944 }
Ted Kremenekd937ed32009-04-09 00:00:02 +00001945 }
Ted Kremenek66d9edc2009-02-19 04:06:22 +00001946 }
Ted Kremenek5451c602009-04-08 18:51:08 +00001947 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek2f3b0db2009-04-09 06:02:06 +00001948 // of this method should assume that the receiver is not nil.
1949 if (!StNotNull)
1950 return;
Mike Stump11289f42009-09-09 15:08:12 +00001951
Ted Kremenek5451c602009-04-08 18:51:08 +00001952 state = StNotNull;
Ted Kremenek66d9edc2009-02-19 04:06:22 +00001953 }
Mike Stump11289f42009-09-09 15:08:12 +00001954
Ted Kremenek7f824732008-05-01 18:33:28 +00001955 // Check if the "raise" message was sent.
1956 if (ME->getSelector() == RaiseSel)
1957 RaisesException = true;
1958 }
1959 else {
Mike Stump11289f42009-09-09 15:08:12 +00001960
Ted Kremenek7f824732008-05-01 18:33:28 +00001961 IdentifierInfo* ClsName = ME->getClassName();
1962 Selector S = ME->getSelector();
Mike Stump11289f42009-09-09 15:08:12 +00001963
Ted Kremenek7f824732008-05-01 18:33:28 +00001964 // Check for special instance methods.
Mike Stump11289f42009-09-09 15:08:12 +00001965
1966 if (!NSExceptionII) {
Ted Kremenek7f824732008-05-01 18:33:28 +00001967 ASTContext& Ctx = getContext();
Mike Stump11289f42009-09-09 15:08:12 +00001968
Ted Kremenek7f824732008-05-01 18:33:28 +00001969 NSExceptionII = &Ctx.Idents.get("NSException");
1970 }
Mike Stump11289f42009-09-09 15:08:12 +00001971
Ted Kremenek7f824732008-05-01 18:33:28 +00001972 if (ClsName == NSExceptionII) {
Mike Stump11289f42009-09-09 15:08:12 +00001973
Ted Kremenek7f824732008-05-01 18:33:28 +00001974 enum { NUM_RAISE_SELECTORS = 2 };
Mike Stump11289f42009-09-09 15:08:12 +00001975
Ted Kremenek7f824732008-05-01 18:33:28 +00001976 // Lazily create a cache of the selectors.
1977
1978 if (!NSExceptionInstanceRaiseSelectors) {
Mike Stump11289f42009-09-09 15:08:12 +00001979
Ted Kremenek7f824732008-05-01 18:33:28 +00001980 ASTContext& Ctx = getContext();
Mike Stump11289f42009-09-09 15:08:12 +00001981
Ted Kremenek7f824732008-05-01 18:33:28 +00001982 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
Mike Stump11289f42009-09-09 15:08:12 +00001983
Ted Kremenek7f824732008-05-01 18:33:28 +00001984 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1985 unsigned idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001986
1987 // raise:format:
Ted Kremenekbb7386a2008-05-02 17:12:56 +00001988 II.push_back(&Ctx.Idents.get("raise"));
Mike Stump11289f42009-09-09 15:08:12 +00001989 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek7f824732008-05-01 18:33:28 +00001990 NSExceptionInstanceRaiseSelectors[idx++] =
Mike Stump11289f42009-09-09 15:08:12 +00001991 Ctx.Selectors.getSelector(II.size(), &II[0]);
1992
1993 // raise:format::arguments:
Ted Kremenekbb7386a2008-05-02 17:12:56 +00001994 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek7f824732008-05-01 18:33:28 +00001995 NSExceptionInstanceRaiseSelectors[idx++] =
1996 Ctx.Selectors.getSelector(II.size(), &II[0]);
1997 }
Mike Stump11289f42009-09-09 15:08:12 +00001998
Ted Kremenek7f824732008-05-01 18:33:28 +00001999 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2000 if (S == NSExceptionInstanceRaiseSelectors[i]) {
2001 RaisesException = true; break;
2002 }
2003 }
Ted Kremenek667cacb2008-04-15 23:06:53 +00002004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
Ted Kremenek667cacb2008-04-15 23:06:53 +00002006 // Check for any arguments that are uninitialized/undefined.
Mike Stump11289f42009-09-09 15:08:12 +00002007
Ted Kremenek667cacb2008-04-15 23:06:53 +00002008 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2009 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002010
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002011 if (state->getSVal(*I).isUndef()) {
Mike Stump11289f42009-09-09 15:08:12 +00002012
Ted Kremenek667cacb2008-04-15 23:06:53 +00002013 // Generate an error node for passing an uninitialized/undefined value
2014 // as an argument to a message expression. This node is a sink.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002015 ExplodedNode* N = Builder->generateNode(ME, state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002016
Ted Kremenek667cacb2008-04-15 23:06:53 +00002017 if (N) {
2018 N->markAsSink();
2019 MsgExprUndefArgs[N] = *I;
2020 }
Mike Stump11289f42009-09-09 15:08:12 +00002021
Ted Kremenek667cacb2008-04-15 23:06:53 +00002022 return;
Mike Stump11289f42009-09-09 15:08:12 +00002023 }
Ted Kremenek7f824732008-05-01 18:33:28 +00002024 }
Mike Stump11289f42009-09-09 15:08:12 +00002025
Ted Kremenek18c7cee2009-11-03 08:03:59 +00002026 // Handle previsits checks.
2027 ExplodedNodeSet Src, DstTmp;
2028 Src.Add(Pred);
2029 CheckerVisit(ME, DstTmp, Src, true);
2030
Ted Kremenek7f824732008-05-01 18:33:28 +00002031 // Check if we raise an exception. For now treat these as sinks. Eventually
2032 // we will want to handle exceptions properly.
Ted Kremenek7f824732008-05-01 18:33:28 +00002033 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek7f824732008-05-01 18:33:28 +00002034 if (RaisesException)
2035 Builder->BuildSinks = true;
Mike Stump11289f42009-09-09 15:08:12 +00002036
Ted Kremenek667cacb2008-04-15 23:06:53 +00002037 // Dispatch to plug-in transfer function.
Ted Kremenek667cacb2008-04-15 23:06:53 +00002038 unsigned size = Dst.size();
Ted Kremenekdd43aee2008-04-23 20:12:28 +00002039 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek18c7cee2009-11-03 08:03:59 +00002040
2041 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
2042 DI!=DE; ++DI)
2043 EvalObjCMessageExpr(Dst, ME, *DI);
Mike Stump11289f42009-09-09 15:08:12 +00002044
Ted Kremenek667cacb2008-04-15 23:06:53 +00002045 // Handle the case where no nodes where generated. Auto-generate that
2046 // contains the updated state if we aren't generating sinks.
Ted Kremenekc072b822008-04-18 20:35:30 +00002047 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek17d541d2009-02-13 01:45:31 +00002048 MakeNode(Dst, ME, Pred, state);
Ted Kremenek667cacb2008-04-15 23:06:53 +00002049}
2050
2051//===----------------------------------------------------------------------===//
2052// Transfer functions: Miscellaneous statements.
2053//===----------------------------------------------------------------------===//
2054
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002055void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred,
2056 ExplodedNodeSet& Dst){
Zhongxing Xu107f7592009-08-06 12:48:26 +00002057 ExplodedNodeSet S1;
Ted Kremenek9fd25312008-02-19 18:52:54 +00002058 QualType T = CastE->getType();
Zhongxing Xudab76fd2008-10-21 06:54:23 +00002059 QualType ExTy = Ex->getType();
Zhongxing Xuc2721522008-10-22 08:02:16 +00002060
Zhongxing Xu4de1c852008-10-31 07:26:14 +00002061 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregore200adc2008-10-27 19:41:14 +00002062 T = ExCast->getTypeAsWritten();
2063
Zhongxing Xuc2721522008-10-22 08:02:16 +00002064 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002065 VisitLValue(Ex, Pred, S1);
Ted Kremenek9f3f8272008-03-04 22:16:08 +00002066 else
2067 Visit(Ex, Pred, S1);
Mike Stump11289f42009-09-09 15:08:12 +00002068
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002069 ExplodedNodeSet S2;
2070 CheckerVisit(CastE, S2, S1, true);
2071
Ted Kremenekeccf3e52008-04-22 21:10:18 +00002072 // Check for casting to "void".
Mike Stump11289f42009-09-09 15:08:12 +00002073 if (T->isVoidType()) {
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002074 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I)
2075 Dst.Add(*I);
Ted Kremenek33d82852008-01-24 02:02:54 +00002076 return;
2077 }
Ted Kremenekac7c7242009-07-21 21:03:30 +00002078
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002079 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2080 ExplodedNode* N = *I;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002081 const GRState* state = GetState(N);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002082 SVal V = state->getSVal(Ex);
Ted Kremenekac7c7242009-07-21 21:03:30 +00002083 const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002084 state = Res.getState()->BindExpr(CastE, Res.getSVal());
Ted Kremenekac7c7242009-07-21 21:03:30 +00002085 MakeNode(Dst, CastE, N, state);
Ted Kremenek33d82852008-01-24 02:02:54 +00002086 }
Ted Kremenek05352742008-01-24 20:55:43 +00002087}
2088
Ted Kremenekbf263682008-10-27 21:54:31 +00002089void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Mike Stump11289f42009-09-09 15:08:12 +00002090 ExplodedNode* Pred,
2091 ExplodedNodeSet& Dst,
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002092 bool asLValue) {
Ted Kremenekbf263682008-10-27 21:54:31 +00002093 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002094 ExplodedNodeSet Tmp;
Ted Kremenekbf263682008-10-27 21:54:31 +00002095 Visit(ILE, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002096
Zhongxing Xu107f7592009-08-06 12:48:26 +00002097 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002098 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002099 SVal ILV = state->getSVal(ILE);
2100 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenekbf263682008-10-27 21:54:31 +00002101
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002102 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002103 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL)));
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002104 else
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002105 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenekbf263682008-10-27 21:54:31 +00002106 }
2107}
2108
Ted Kremenek14536f62009-08-21 22:28:32 +00002109void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +00002110 ExplodedNodeSet& Dst) {
Ted Kremenek3b427152008-04-22 22:25:27 +00002111
Mike Stump11289f42009-09-09 15:08:12 +00002112 // The CFG has one DeclStmt per Decl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002113 Decl* D = *DS->decl_begin();
Mike Stump11289f42009-09-09 15:08:12 +00002114
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002115 if (!D || !isa<VarDecl>(D))
Ted Kremenek3b427152008-04-22 22:25:27 +00002116 return;
Mike Stump11289f42009-09-09 15:08:12 +00002117
2118 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek17810802008-11-12 19:24:17 +00002119 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek3b427152008-04-22 22:25:27 +00002120
2121 // FIXME: static variables may have an initializer, but the second
2122 // time a function is called those values may not be current.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002123 ExplodedNodeSet Tmp;
Ted Kremenek3b427152008-04-22 22:25:27 +00002124
Ted Kremenek17810802008-11-12 19:24:17 +00002125 if (InitEx)
2126 Visit(InitEx, Pred, Tmp);
Ted Kremenekfc311292009-07-17 23:48:26 +00002127 else
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002128 Tmp.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002129
Ted Kremenekae3361d2009-11-07 03:56:57 +00002130 ExplodedNodeSet Tmp2;
2131 CheckerVisit(DS, Tmp2, Tmp, true);
2132
2133 for (ExplodedNodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002134 ExplodedNode *N = *I;
Ted Kremenekae3361d2009-11-07 03:56:57 +00002135 const GRState *state = GetState(N);
Zhongxing Xu27fee832009-11-03 12:13:38 +00002136
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00002137 // Decls without InitExpr are not initialized explicitly.
Zhongxing Xu27fee832009-11-03 12:13:38 +00002138 const LocationContext *LC = N->getLocationContext();
Ted Kremenek14536f62009-08-21 22:28:32 +00002139
Ted Kremenek17810802008-11-12 19:24:17 +00002140 if (InitEx) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002141 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek17810802008-11-12 19:24:17 +00002142 QualType T = VD->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002143
Ted Kremenek17810802008-11-12 19:24:17 +00002144 // Recover some path-sensitivity if a scalar value evaluated to
2145 // UnknownVal.
Mike Stump11289f42009-09-09 15:08:12 +00002146 if (InitVal.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002147 !getConstraintManager().canReasonAbout(InitVal)) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002148 InitVal = ValMgr.getConjuredSymbolVal(NULL, InitEx,
2149 Builder->getCurrentBlockCount());
Mike Stump11289f42009-09-09 15:08:12 +00002150 }
Ted Kremenekb006b822009-11-04 00:09:15 +00002151
Ted Kremenek209e31b2009-11-05 00:42:23 +00002152 EvalBind(Dst, DS, DS, *I, state,
2153 loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true);
Mike Stump11289f42009-09-09 15:08:12 +00002154 }
Ted Kremenek13363532009-02-14 01:54:57 +00002155 else {
Ted Kremenekb006b822009-11-04 00:09:15 +00002156 state = state->bindDeclWithNoInit(state->getRegion(VD, LC));
Ted Kremenek13363532009-02-14 01:54:57 +00002157 MakeNode(Dst, DS, *I, state);
Ted Kremenek8f7afdd2008-12-08 22:47:34 +00002158 }
Ted Kremenek3b427152008-04-22 22:25:27 +00002159 }
Ted Kremenek05352742008-01-24 20:55:43 +00002160}
Ted Kremenek33d82852008-01-24 02:02:54 +00002161
Ted Kremenekf68bf632008-10-30 17:47:32 +00002162namespace {
2163 // This class is used by VisitInitListExpr as an item in a worklist
2164 // for processing the values contained in an InitListExpr.
2165class VISIBILITY_HIDDEN InitListWLItem {
2166public:
2167 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu107f7592009-08-06 12:48:26 +00002168 ExplodedNode* N;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002169 InitListExpr::reverse_iterator Itr;
Mike Stump11289f42009-09-09 15:08:12 +00002170
Zhongxing Xu107f7592009-08-06 12:48:26 +00002171 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
2172 InitListExpr::reverse_iterator itr)
Ted Kremenekf68bf632008-10-30 17:47:32 +00002173 : Vals(vals), N(n), Itr(itr) {}
2174};
2175}
2176
2177
Mike Stump11289f42009-09-09 15:08:12 +00002178void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002179 ExplodedNodeSet& Dst) {
Ted Kremenek828e6df2008-10-30 23:14:36 +00002180
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002181 const GRState* state = GetState(Pred);
Ted Kremenek45698bf2008-11-13 05:05:34 +00002182 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002183 unsigned NumInitElements = E->getNumInits();
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002184
Ted Kremeneka41d9dd2009-07-28 20:46:55 +00002185 if (T->isArrayType() || T->isStructureType() ||
2186 T->isUnionType() || T->isVectorType()) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002187
Ted Kremenek828e6df2008-10-30 23:14:36 +00002188 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump11289f42009-09-09 15:08:12 +00002189
Ted Kremenek828e6df2008-10-30 23:14:36 +00002190 // Handle base case where the initializer has no elements.
2191 // e.g: static int* myArray[] = {};
2192 if (NumInitElements == 0) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002193 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002194 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremenek828e6df2008-10-30 23:14:36 +00002195 return;
Mike Stump11289f42009-09-09 15:08:12 +00002196 }
2197
Ted Kremenek828e6df2008-10-30 23:14:36 +00002198 // Create a worklist to process the initializers.
2199 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump11289f42009-09-09 15:08:12 +00002200 WorkList.reserve(NumInitElements);
2201 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002202 InitListExpr::reverse_iterator ItrEnd = E->rend();
Ted Kremenek30030012009-09-22 21:19:14 +00002203 assert(!(E->rbegin() == E->rend()));
Mike Stump11289f42009-09-09 15:08:12 +00002204
Ted Kremenek828e6df2008-10-30 23:14:36 +00002205 // Process the worklist until it is empty.
Ted Kremenekf68bf632008-10-30 17:47:32 +00002206 while (!WorkList.empty()) {
2207 InitListWLItem X = WorkList.back();
2208 WorkList.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +00002209
Zhongxing Xu107f7592009-08-06 12:48:26 +00002210 ExplodedNodeSet Tmp;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002211 Visit(*X.Itr, X.N, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002212
Ted Kremenekf68bf632008-10-30 17:47:32 +00002213 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002214
Zhongxing Xu107f7592009-08-06 12:48:26 +00002215 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002216 // Get the last initializer value.
2217 state = GetState(*NI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002218 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump11289f42009-09-09 15:08:12 +00002219
Ted Kremenekf68bf632008-10-30 17:47:32 +00002220 // Construct the new list of values by prepending the new value to
2221 // the already constructed list.
2222 llvm::ImmutableList<SVal> NewVals =
2223 getBasicVals().consVals(InitV, X.Vals);
Mike Stump11289f42009-09-09 15:08:12 +00002224
Ted Kremenekf68bf632008-10-30 17:47:32 +00002225 if (NewItr == ItrEnd) {
Zhongxing Xu121a53a2008-10-31 03:01:26 +00002226 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002227 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002228
Ted Kremenekf68bf632008-10-30 17:47:32 +00002229 // Make final state and node.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002230 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002231 }
2232 else {
2233 // Still some initializer values to go. Push them onto the worklist.
2234 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2235 }
2236 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002237 }
Mike Stump11289f42009-09-09 15:08:12 +00002238
Ted Kremenek28f41ba2008-10-30 18:34:31 +00002239 return;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002240 }
2241
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002242 if (Loc::IsLocType(T) || T->isIntegerType()) {
2243 assert (E->getNumInits() == 1);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002244 ExplodedNodeSet Tmp;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002245 Expr* Init = E->getInit(0);
2246 Visit(Init, Pred, Tmp);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002247 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002248 state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002249 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002250 }
2251 return;
2252 }
2253
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002254
2255 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2256 assert(0 && "unprocessed InitListExpr type");
2257}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00002258
Sebastian Redl6f282892008-11-11 17:56:53 +00002259/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2260void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002261 ExplodedNode* Pred,
2262 ExplodedNodeSet& Dst) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002263 QualType T = Ex->getTypeOfArgument();
Mike Stump11289f42009-09-09 15:08:12 +00002264 uint64_t amt;
2265
Ted Kremenekae5b7862008-03-15 03:13:20 +00002266 if (Ex->isSizeOf()) {
Mike Stump11289f42009-09-09 15:08:12 +00002267 if (T == getContext().VoidTy) {
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002268 // sizeof(void) == 1 byte.
2269 amt = 1;
2270 }
2271 else if (!T.getTypePtr()->isConstantSizeType()) {
2272 // FIXME: Add support for VLAs.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002273 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002274 }
2275 else if (T->isObjCInterfaceType()) {
2276 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2277 // the compiler has laid out its representation. Just report Unknown
Mike Stump11289f42009-09-09 15:08:12 +00002278 // for these.
Ted Kremenek99057462008-04-30 21:31:12 +00002279 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002280 }
2281 else {
2282 // All other cases.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002283 amt = getContext().getTypeSize(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002284 }
Ted Kremenekae5b7862008-03-15 03:13:20 +00002285 }
2286 else // Get alignment of the type.
Ted Kremenek88ba7502008-03-15 03:13:55 +00002287 amt = getContext().getTypeAlign(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002288
Ted Kremenek181f7232008-03-21 21:30:14 +00002289 MakeNode(Dst, Ex, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002290 GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenek002bf742008-02-12 19:49:57 +00002291}
2292
Ted Kremenekb597bb92008-02-20 04:02:35 +00002293
Zhongxing Xu107f7592009-08-06 12:48:26 +00002294void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
2295 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002296
Ted Kremenekb597bb92008-02-20 04:02:35 +00002297 switch (U->getOpcode()) {
Mike Stump11289f42009-09-09 15:08:12 +00002298
Ted Kremenekb597bb92008-02-20 04:02:35 +00002299 default:
Ted Kremenekb597bb92008-02-20 04:02:35 +00002300 break;
Mike Stump11289f42009-09-09 15:08:12 +00002301
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002302 case UnaryOperator::Deref: {
Mike Stump11289f42009-09-09 15:08:12 +00002303
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002304 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002305 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002306 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002307
Zhongxing Xu107f7592009-08-06 12:48:26 +00002308 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002309
Ted Kremenek17d541d2009-02-13 01:45:31 +00002310 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002311 SVal location = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002312
Zhongxing Xu232c7922008-10-16 06:09:51 +00002313 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002314 MakeNode(Dst, U, *I, state->BindExpr(U, location),
Ted Kremeneka6e08322009-05-07 18:27:16 +00002315 ProgramPoint::PostLValueKind);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002316 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00002317 EvalLoad(Dst, U, *I, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002318 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002319
2320 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002321 }
Mike Stump11289f42009-09-09 15:08:12 +00002322
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002323 case UnaryOperator::Real: {
Mike Stump11289f42009-09-09 15:08:12 +00002324
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002325 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002326 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002327 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002328
Zhongxing Xu107f7592009-08-06 12:48:26 +00002329 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002330
Zhongxing Xu27f17422008-10-17 05:57:07 +00002331 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002332 if (Ex->getType()->isAnyComplexType()) {
2333 // Just report "Unknown."
2334 Dst.Add(*I);
2335 continue;
2336 }
Mike Stump11289f42009-09-09 15:08:12 +00002337
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002338 // For all other types, UnaryOperator::Real is an identity operation.
2339 assert (U->getType() == Ex->getType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002340 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002341 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump11289f42009-09-09 15:08:12 +00002342 }
2343
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002344 return;
2345 }
Mike Stump11289f42009-09-09 15:08:12 +00002346
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002347 case UnaryOperator::Imag: {
Mike Stump11289f42009-09-09 15:08:12 +00002348
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002349 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002350 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002351 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002352
Zhongxing Xu107f7592009-08-06 12:48:26 +00002353 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu27f17422008-10-17 05:57:07 +00002354 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002355 if (Ex->getType()->isAnyComplexType()) {
2356 // Just report "Unknown."
2357 Dst.Add(*I);
2358 continue;
2359 }
Mike Stump11289f42009-09-09 15:08:12 +00002360
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002361 // For all other types, UnaryOperator::Float returns 0.
2362 assert (Ex->getType()->isIntegerType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002363 const GRState* state = GetState(*I);
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002364 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002365 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002366 }
Mike Stump11289f42009-09-09 15:08:12 +00002367
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002368 return;
2369 }
Mike Stump11289f42009-09-09 15:08:12 +00002370
Ted Kremenek27347132009-09-15 00:40:32 +00002371 case UnaryOperator::OffsetOf: {
Ted Kremenekb6622942009-09-15 04:19:09 +00002372 Expr::EvalResult Res;
2373 if (U->Evaluate(Res, getContext()) && Res.Val.isInt()) {
2374 const APSInt &IV = Res.Val.getInt();
2375 assert(IV.getBitWidth() == getContext().getTypeSize(U->getType()));
2376 assert(U->getType()->isIntegerType());
2377 assert(IV.isSigned() == U->getType()->isSignedIntegerType());
2378 SVal X = ValMgr.makeIntVal(IV);
2379 MakeNode(Dst, U, Pred, GetState(Pred)->BindExpr(U, X));
2380 return;
2381 }
2382 // FIXME: Handle the case where __builtin_offsetof is not a constant.
2383 Dst.Add(Pred);
Ted Kremenekca67cab2008-04-30 21:45:55 +00002384 return;
Ted Kremenek27347132009-09-15 00:40:32 +00002385 }
Mike Stump11289f42009-09-09 15:08:12 +00002386
Zhongxing Xu232c7922008-10-16 06:09:51 +00002387 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002388 case UnaryOperator::Extension: {
Mike Stump11289f42009-09-09 15:08:12 +00002389
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002390 // Unary "+" is a no-op, similar to a parentheses. We still have places
2391 // where it may be a block-level expression, so we need to
2392 // generate an extra node that just propagates the value of the
2393 // subexpression.
2394
2395 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002396 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002397 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002398
2399 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002400 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002401 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002402 }
Mike Stump11289f42009-09-09 15:08:12 +00002403
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002404 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002405 }
Mike Stump11289f42009-09-09 15:08:12 +00002406
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002407 case UnaryOperator::AddrOf: {
Mike Stump11289f42009-09-09 15:08:12 +00002408
Zhongxing Xu232c7922008-10-16 06:09:51 +00002409 assert(!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002410 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002411 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002412 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002413
2414 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002415 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002416 SVal V = state->getSVal(Ex);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002417 state = state->BindExpr(U, V);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002418 MakeNode(Dst, U, *I, state);
Ted Kremenek7f8ebb72008-02-21 19:15:37 +00002419 }
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002420
Mike Stump11289f42009-09-09 15:08:12 +00002421 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002422 }
Mike Stump11289f42009-09-09 15:08:12 +00002423
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002424 case UnaryOperator::LNot:
2425 case UnaryOperator::Minus:
2426 case UnaryOperator::Not: {
Mike Stump11289f42009-09-09 15:08:12 +00002427
Zhongxing Xu232c7922008-10-16 06:09:51 +00002428 assert (!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002429 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002430 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002431 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002432
2433 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002434 const GRState* state = GetState(*I);
Mike Stump11289f42009-09-09 15:08:12 +00002435
Ted Kremenek76bccf62008-09-30 05:32:44 +00002436 // Get the value of the subexpression.
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002437 SVal V = state->getSVal(Ex);
Ted Kremenek76bccf62008-09-30 05:32:44 +00002438
Ted Kremenek1ca33462008-11-15 00:20:05 +00002439 if (V.isUnknownOrUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002440 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremenek1ca33462008-11-15 00:20:05 +00002441 continue;
2442 }
Mike Stump11289f42009-09-09 15:08:12 +00002443
Ted Kremenek44137142008-11-15 04:01:56 +00002444// QualType DstT = getContext().getCanonicalType(U->getType());
2445// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002446//
Ted Kremenek44137142008-11-15 04:01:56 +00002447// if (DstT != SrcT) // Perform promotions.
Mike Stump11289f42009-09-09 15:08:12 +00002448// V = EvalCast(V, DstT);
2449//
Ted Kremenek44137142008-11-15 04:01:56 +00002450// if (V.isUnknownOrUndef()) {
2451// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2452// continue;
2453// }
Mike Stump11289f42009-09-09 15:08:12 +00002454
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002455 switch (U->getOpcode()) {
2456 default:
2457 assert(false && "Invalid Opcode.");
2458 break;
Mike Stump11289f42009-09-09 15:08:12 +00002459
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002460 case UnaryOperator::Not:
Ted Kremenekd331d092008-10-01 00:21:14 +00002461 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002462 state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002463 break;
2464
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002465 case UnaryOperator::Minus:
Ted Kremenekd331d092008-10-01 00:21:14 +00002466 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002467 state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002468 break;
2469
2470 case UnaryOperator::LNot:
2471
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002472 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2473 //
2474 // Note: technically we do "E == 0", but this is the same in the
2475 // transfer functions as "0 == E".
Ted Kremenek1642bda2009-06-26 00:05:51 +00002476 SVal Result;
Mike Stump11289f42009-09-09 15:08:12 +00002477
Zhongxing Xu27f17422008-10-17 05:57:07 +00002478 if (isa<Loc>(V)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002479 Loc X = ValMgr.makeNull();
Ted Kremenek1642bda2009-06-26 00:05:51 +00002480 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2481 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002482 }
2483 else {
Ted Kremenek44137142008-11-15 04:01:56 +00002484 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenek8ec57712009-10-06 01:39:48 +00002485 Result = EvalBinOp(state, BinaryOperator::EQ, cast<NonLoc>(V), X,
Ted Kremenek1642bda2009-06-26 00:05:51 +00002486 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002487 }
Mike Stump11289f42009-09-09 15:08:12 +00002488
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002489 state = state->BindExpr(U, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002490
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002491 break;
2492 }
Mike Stump11289f42009-09-09 15:08:12 +00002493
Ted Kremenek17d541d2009-02-13 01:45:31 +00002494 MakeNode(Dst, U, *I, state);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002495 }
Mike Stump11289f42009-09-09 15:08:12 +00002496
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002497 return;
2498 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002499 }
2500
2501 // Handle ++ and -- (both pre- and post-increment).
2502
2503 assert (U->isIncrementDecrementOp());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002504 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002505 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu232c7922008-10-16 06:09:51 +00002506 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002507
Zhongxing Xu107f7592009-08-06 12:48:26 +00002508 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002509
Ted Kremenek17d541d2009-02-13 01:45:31 +00002510 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002511 SVal V1 = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002512
2513 // Perform a load.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002514 ExplodedNodeSet Tmp2;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002515 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002516
Zhongxing Xu107f7592009-08-06 12:48:26 +00002517 for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
Mike Stump11289f42009-09-09 15:08:12 +00002518
Ted Kremenek17d541d2009-02-13 01:45:31 +00002519 state = GetState(*I2);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002520 SVal V2_untested = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002521
2522 // Propagate unknown and undefined values.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002523 if (V2_untested.isUnknownOrUndef()) {
2524 MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested));
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002525 continue;
Ted Kremenek7020eae2009-09-11 22:07:28 +00002526 }
2527 DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
Mike Stump11289f42009-09-09 15:08:12 +00002528
2529 // Handle all other values.
Ted Kremeneke81734b2008-02-15 22:09:30 +00002530 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2531 : BinaryOperator::Sub;
Ted Kremenek32c41ec2009-03-11 03:54:24 +00002532
Zhongxing Xufe971652009-08-05 02:51:59 +00002533 // If the UnaryOperator has non-location type, use its type to create the
2534 // constant value. If the UnaryOperator has location type, create the
2535 // constant with int type and pointer width.
2536 SVal RHS;
2537
2538 if (U->getType()->isAnyPointerType())
2539 RHS = ValMgr.makeIntValWithPtrWidth(1, false);
2540 else
2541 RHS = ValMgr.makeIntVal(1, U->getType());
2542
Mike Stump11289f42009-09-09 15:08:12 +00002543 SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
2544
Ted Kremenek6b315332009-03-20 20:10:45 +00002545 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002546 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek7020eae2009-09-11 22:07:28 +00002547 DefinedOrUnknownSVal SymVal =
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002548 ValMgr.getConjuredSymbolVal(NULL, Ex,
2549 Builder->getCurrentBlockCount());
Ted Kremenek7020eae2009-09-11 22:07:28 +00002550 Result = SymVal;
Mike Stump11289f42009-09-09 15:08:12 +00002551
Ted Kremenek35f875c2009-04-21 22:38:05 +00002552 // If the value is a location, ++/-- should always preserve
Ted Kremenek1642bda2009-06-26 00:05:51 +00002553 // non-nullness. Check if the original value was non-null, and if so
Mike Stump11289f42009-09-09 15:08:12 +00002554 // propagate that constraint.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002555 if (Loc::IsLocType(U->getType())) {
Ted Kremenek7020eae2009-09-11 22:07:28 +00002556 DefinedOrUnknownSVal Constraint =
2557 SVator.EvalEQ(state, V2, ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002558
Ted Kremenek7020eae2009-09-11 22:07:28 +00002559 if (!state->Assume(Constraint, true)) {
Ted Kremenek35f875c2009-04-21 22:38:05 +00002560 // It isn't feasible for the original value to be null.
2561 // Propagate this constraint.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002562 Constraint = SVator.EvalEQ(state, SymVal,
2563 ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002564
Ted Kremenek7020eae2009-09-11 22:07:28 +00002565
2566 state = state->Assume(Constraint, false);
Ted Kremenekf9906842009-06-18 22:57:13 +00002567 assert(state);
Mike Stump11289f42009-09-09 15:08:12 +00002568 }
2569 }
Ted Kremenek35f875c2009-04-21 22:38:05 +00002570 }
Mike Stump11289f42009-09-09 15:08:12 +00002571
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002572 state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002573
Mike Stump11289f42009-09-09 15:08:12 +00002574 // Perform the store.
Ted Kremenek209e31b2009-11-05 00:42:23 +00002575 EvalStore(Dst, NULL, U, *I2, state, V1, Result);
Ted Kremenek43523e02008-02-07 01:08:27 +00002576 }
Ted Kremenek38213f92008-04-21 23:43:38 +00002577 }
Ted Kremenek43523e02008-02-07 01:08:27 +00002578}
2579
Zhongxing Xu107f7592009-08-06 12:48:26 +00002580void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002581 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00002582}
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002583
2584void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2585 AsmStmt::outputs_iterator I,
2586 AsmStmt::outputs_iterator E,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002587 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002588 if (I == E) {
2589 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2590 return;
2591 }
Mike Stump11289f42009-09-09 15:08:12 +00002592
Zhongxing Xu107f7592009-08-06 12:48:26 +00002593 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002594 VisitLValue(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002595
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002596 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002597
Zhongxing Xu107f7592009-08-06 12:48:26 +00002598 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002599 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2600}
2601
2602void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2603 AsmStmt::inputs_iterator I,
2604 AsmStmt::inputs_iterator E,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002605 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002606 if (I == E) {
Mike Stump11289f42009-09-09 15:08:12 +00002607
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002608 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu27f17422008-10-17 05:57:07 +00002609 // should evaluate to Locs. Nuke all of their values.
Mike Stump11289f42009-09-09 15:08:12 +00002610
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002611 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2612 // which interprets the inline asm and stores proper results in the
2613 // outputs.
Mike Stump11289f42009-09-09 15:08:12 +00002614
Ted Kremenek17d541d2009-02-13 01:45:31 +00002615 const GRState* state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002616
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002617 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2618 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump11289f42009-09-09 15:08:12 +00002619
2620 SVal X = state->getSVal(*OI);
Zhongxing Xu27f17422008-10-17 05:57:07 +00002621 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump11289f42009-09-09 15:08:12 +00002622
Zhongxing Xu27f17422008-10-17 05:57:07 +00002623 if (isa<Loc>(X))
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002624 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002625 }
Mike Stump11289f42009-09-09 15:08:12 +00002626
Ted Kremenek17d541d2009-02-13 01:45:31 +00002627 MakeNode(Dst, A, Pred, state);
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002628 return;
2629 }
Mike Stump11289f42009-09-09 15:08:12 +00002630
Zhongxing Xu107f7592009-08-06 12:48:26 +00002631 ExplodedNodeSet Tmp;
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002632 Visit(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002633
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002634 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002635
Ted Kremenek17a02962009-09-03 03:02:58 +00002636 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002637 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2638}
2639
Ted Kremenekbee01e52009-11-06 02:24:13 +00002640void GRExprEngine::VisitReturnStmt(ReturnStmt *RS, ExplodedNode *Pred,
2641 ExplodedNodeSet &Dst) {
2642
2643 ExplodedNodeSet Src;
2644 if (Expr *RetE = RS->getRetValue()) {
2645 Visit(RetE, Pred, Src);
Ted Kremenekf6467742008-03-31 15:02:58 +00002646 }
Ted Kremenekbee01e52009-11-06 02:24:13 +00002647 else {
2648 Src.Add(Pred);
2649 }
2650
2651 ExplodedNodeSet CheckedSet;
2652 CheckerVisit(RS, CheckedSet, Src, true);
2653
2654 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
2655 I != E; ++I) {
Ted Kremenek9c375152008-04-16 23:05:51 +00002656
Ted Kremenekbee01e52009-11-06 02:24:13 +00002657 assert(Builder && "GRStmtNodeBuilder must be defined.");
2658
2659 Pred = *I;
2660 unsigned size = Dst.size();
2661
2662 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2663 SaveOr OldHasGen(Builder->HasGeneratedNode);
2664
2665 getTF().EvalReturn(Dst, *this, *Builder, RS, Pred);
2666
2667 // Handle the case where no nodes where generated.
2668 if (!Builder->BuildSinks && Dst.size() == size &&
2669 !Builder->HasGeneratedNode)
2670 MakeNode(Dst, RS, Pred, GetState(Pred));
Ted Kremenek0b63f962008-11-21 00:27:44 +00002671 }
Ted Kremenekf6467742008-03-31 15:02:58 +00002672}
Ted Kremenek64100da2008-03-25 00:34:37 +00002673
Ted Kremenek667cacb2008-04-15 23:06:53 +00002674//===----------------------------------------------------------------------===//
2675// Transfer functions: Binary operators.
2676//===----------------------------------------------------------------------===//
2677
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002678void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002679 ExplodedNode* Pred,
Zhongxing Xub9eda672009-10-30 07:19:39 +00002680 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002681
Zhongxing Xu107f7592009-08-06 12:48:26 +00002682 ExplodedNodeSet Tmp1;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002683 Expr* LHS = B->getLHS()->IgnoreParens();
2684 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002685
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002686 // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
2687 if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
Mike Stump11289f42009-09-09 15:08:12 +00002688 Visit(RHS, Pred, Dst);
Ted Kremenek69d78b92008-12-06 02:39:30 +00002689 return;
2690 }
Mike Stump11289f42009-09-09 15:08:12 +00002691
Ted Kremenek43523e02008-02-07 01:08:27 +00002692 if (B->isAssignmentOp())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002693 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek43523e02008-02-07 01:08:27 +00002694 else
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002695 Visit(LHS, Pred, Tmp1);
Ted Kremenekfb553542008-01-16 00:53:15 +00002696
Ted Kremenek17a02962009-09-03 03:02:58 +00002697 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002698 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002699 ExplodedNodeSet Tmp2;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002700 Visit(RHS, *I1, Tmp2);
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002701
2702 ExplodedNodeSet CheckedSet;
2703 CheckerVisit(B, CheckedSet, Tmp2, true);
Mike Stump11289f42009-09-09 15:08:12 +00002704
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002705 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump11289f42009-09-09 15:08:12 +00002706
2707 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002708 I2 != E2; ++I2) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002709
Ted Kremenek7020eae2009-09-11 22:07:28 +00002710 const GRState *state = GetState(*I2);
2711 const GRState *OldSt = state;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002712 SVal RightV = state->getSVal(RHS);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002713
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002714 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00002715
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002716 if (Op == BinaryOperator::Assign) {
2717 // EXPERIMENTAL: "Conjured" symbols.
2718 // FIXME: Handle structs.
2719 QualType T = RHS->getType();
2720
2721 if ((RightV.isUnknown()||!getConstraintManager().canReasonAbout(RightV))
2722 && (Loc::IsLocType(T) || (T->isScalarType()&&T->isIntegerType()))) {
2723 unsigned Count = Builder->getCurrentBlockCount();
2724 RightV = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), Count);
2725 }
Zhongxing Xub9eda672009-10-30 07:19:39 +00002726
Ted Kremenek5c2040b12009-10-30 22:01:29 +00002727 SVal ExprVal = asLValue ? LeftV : RightV;
Zhongxing Xub9eda672009-10-30 07:19:39 +00002728
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002729 // Simulate the effects of a "store": bind the value of the RHS
2730 // to the L-Value represented by the LHS.
Zhongxing Xub9eda672009-10-30 07:19:39 +00002731 EvalStore(Dst, B, LHS, *I2, state->BindExpr(B, ExprVal), LeftV, RightV);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002732 continue;
2733 }
2734
2735 if (!B->isAssignmentOp()) {
2736 // Process non-assignments except commas or short-circuited
2737 // logical expressions (LAnd and LOr).
2738 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
2739
2740 if (Result.isUnknown()) {
2741 if (OldSt != state) {
2742 // Generate a new node if we have already created a new state.
2743 MakeNode(Dst, B, *I2, state);
Ted Kremeneke2f6d6c2008-03-12 21:45:47 +00002744 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002745 else
2746 Dst.Add(*I2);
2747
Ted Kremenek2044a512008-04-16 18:21:25 +00002748 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002749 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002750
2751 state = state->BindExpr(B, Result);
2752
2753 if (Result.isUndef()) {
2754 // The operands were *not* undefined, but the result is undefined.
2755 // This is a special node that should be flagged as an error.
2756 if (ExplodedNode *UndefNode = Builder->generateNode(B, state, *I2)){
2757 UndefNode->markAsSink();
2758 UndefResults.insert(UndefNode);
Ted Kremenek7f8ebb72008-02-21 19:15:37 +00002759 }
Ted Kremenek2044a512008-04-16 18:21:25 +00002760 continue;
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002761 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002762
2763 // Otherwise, create a new node.
2764 MakeNode(Dst, B, *I2, state);
2765 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002766 }
Mike Stump11289f42009-09-09 15:08:12 +00002767
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002768 assert (B->isCompoundAssignmentOp());
2769
Ted Kremenek5d7662c2009-02-07 00:52:24 +00002770 switch (Op) {
2771 default:
2772 assert(0 && "Invalid opcode for compound assignment.");
2773 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2774 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2775 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2776 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2777 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2778 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2779 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2780 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2781 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2782 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek54d399a2008-10-27 23:02:39 +00002783 }
Mike Stump11289f42009-09-09 15:08:12 +00002784
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002785 // Perform a load (the LHS). This performs the checks for
2786 // null dereferences, and so on.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002787 ExplodedNodeSet Tmp3;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002788 SVal location = state->getSVal(LHS);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002789 EvalLoad(Tmp3, LHS, *I2, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002790
2791 for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002792 ++I3) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002793 state = GetState(*I3);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002794 SVal V = state->getSVal(LHS);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002795
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002796 // Get the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002797 QualType CTy =
2798 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek44137142008-11-15 04:01:56 +00002799 CTy = getContext().getCanonicalType(CTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002800
Ted Kremenekac7c7242009-07-21 21:03:30 +00002801 QualType CLHSTy =
2802 cast<CompoundAssignOperator>(B)->getComputationLHSType();
2803 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002804
Ted Kremenek44137142008-11-15 04:01:56 +00002805 QualType LTy = getContext().getCanonicalType(LHS->getType());
2806 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002807
2808 // Promote LHS.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002809 llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002810
Mike Stump11289f42009-09-09 15:08:12 +00002811 // Compute the result of the operation.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002812 SVal Result;
2813 llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
2814 RightV, CTy),
2815 state, B->getType(), CTy);
Mike Stump11289f42009-09-09 15:08:12 +00002816
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002817 if (Result.isUndef()) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002818 // The operands were not undefined, but the result is undefined.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002819 if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I3)) {
Mike Stump11289f42009-09-09 15:08:12 +00002820 UndefNode->markAsSink();
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002821 UndefResults.insert(UndefNode);
2822 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002823 continue;
2824 }
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002825
2826 // EXPERIMENTAL: "Conjured" symbols.
2827 // FIXME: Handle structs.
Mike Stump11289f42009-09-09 15:08:12 +00002828
Ted Kremenek44137142008-11-15 04:01:56 +00002829 SVal LHSVal;
Mike Stump11289f42009-09-09 15:08:12 +00002830
2831 if ((Result.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002832 !getConstraintManager().canReasonAbout(Result))
Mike Stump11289f42009-09-09 15:08:12 +00002833 && (Loc::IsLocType(CTy)
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002834 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Mike Stump11289f42009-09-09 15:08:12 +00002835
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002836 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002837
Ted Kremenek44137142008-11-15 04:01:56 +00002838 // The symbolic value is actually for the type of the left-hand side
2839 // expression, not the computation type, as this is the value the
2840 // LValue on the LHS will bind to.
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002841 LHSVal = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002842
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00002843 // However, we need to convert the symbol to the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002844 llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002845 }
Ted Kremenek44137142008-11-15 04:01:56 +00002846 else {
2847 // The left-hand side may bind to a different value then the
2848 // computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002849 llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
Ted Kremenek44137142008-11-15 04:01:56 +00002850 }
Mike Stump11289f42009-09-09 15:08:12 +00002851
2852 EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, Result),
Zhongxing Xu342950e2009-08-25 06:51:30 +00002853 location, LHSVal);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002854 }
Ted Kremenekfb553542008-01-16 00:53:15 +00002855 }
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002856 }
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002857}
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002858
2859//===----------------------------------------------------------------------===//
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002860// Checker registration/lookup.
2861//===----------------------------------------------------------------------===//
2862
2863Checker *GRExprEngine::lookupChecker(void *tag) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +00002864 CheckerMap::const_iterator I = CheckerM.find(tag);
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002865 return (I == CheckerM.end()) ? NULL : Checkers[I->second].second;
2866}
2867
2868//===----------------------------------------------------------------------===//
Ted Kremenekd3122cb2008-02-14 22:36:46 +00002869// Visualization.
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002870//===----------------------------------------------------------------------===//
2871
Ted Kremenekac886cb2008-01-16 21:46:15 +00002872#ifndef NDEBUG
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002873static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek6947a792008-03-07 20:57:30 +00002874static SourceManager* GraphPrintSourceManager;
Ted Kremenek2531fce2008-01-30 23:24:39 +00002875
Ted Kremenekac886cb2008-01-16 21:46:15 +00002876namespace llvm {
2877template<>
Zhongxing Xu107f7592009-08-06 12:48:26 +00002878struct VISIBILITY_HIDDEN DOTGraphTraits<ExplodedNode*> :
Ted Kremenekac886cb2008-01-16 21:46:15 +00002879 public DefaultDOTGraphTraits {
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +00002880 // FIXME: Since we do not cache error nodes in GRExprEngine now, this does not
2881 // work.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002882 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump11289f42009-09-09 15:08:12 +00002883
Ted Kremenek5b70a222008-02-14 22:54:53 +00002884 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek0f7130a2008-02-19 00:22:37 +00002885 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek93d1fed2008-02-28 09:25:22 +00002886 GraphPrintCheckerState->isUndefDeref(N) ||
2887 GraphPrintCheckerState->isUndefStore(N) ||
2888 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek6f5fca72008-02-29 23:14:48 +00002889 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek51e87ea2008-02-29 23:53:11 +00002890 GraphPrintCheckerState->isBadCall(N) ||
2891 GraphPrintCheckerState->isUndefArg(N))
Ted Kremenek5b70a222008-02-14 22:54:53 +00002892 return "color=\"red\",style=\"filled\"";
Mike Stump11289f42009-09-09 15:08:12 +00002893
Ted Kremeneke0c79382008-02-28 20:32:03 +00002894 if (GraphPrintCheckerState->isNoReturnCall(N))
2895 return "color=\"blue\",style=\"filled\"";
Mike Stump11289f42009-09-09 15:08:12 +00002896
Ted Kremenek5b70a222008-02-14 22:54:53 +00002897 return "";
2898 }
Mike Stump11289f42009-09-09 15:08:12 +00002899
Zhongxing Xu107f7592009-08-06 12:48:26 +00002900 static std::string getNodeLabel(const ExplodedNode* N, void*,bool ShortNames){
Mike Stump11289f42009-09-09 15:08:12 +00002901
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002902 std::string sbuf;
2903 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek930191c2008-01-23 22:30:44 +00002904
2905 // Program Location.
Ted Kremenekac886cb2008-01-16 21:46:15 +00002906 ProgramPoint Loc = N->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002907
Ted Kremenekac886cb2008-01-16 21:46:15 +00002908 switch (Loc.getKind()) {
2909 case ProgramPoint::BlockEntranceKind:
Mike Stump11289f42009-09-09 15:08:12 +00002910 Out << "Block Entrance: B"
Ted Kremenekac886cb2008-01-16 21:46:15 +00002911 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2912 break;
Mike Stump11289f42009-09-09 15:08:12 +00002913
Ted Kremenekac886cb2008-01-16 21:46:15 +00002914 case ProgramPoint::BlockExitKind:
2915 assert (false);
2916 break;
Mike Stump11289f42009-09-09 15:08:12 +00002917
Ted Kremenekac886cb2008-01-16 21:46:15 +00002918 default: {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002919 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
2920 const Stmt* S = L->getStmt();
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002921 SourceLocation SLoc = S->getLocStart();
2922
Mike Stump11289f42009-09-09 15:08:12 +00002923 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattnerc61089a2009-06-30 01:26:17 +00002924 LangOptions LO; // FIXME.
2925 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00002926
2927 if (SLoc.isFileID()) {
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002928 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00002929 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2930 << " col="
2931 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2932 << "\\l";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002933 }
Mike Stump11289f42009-09-09 15:08:12 +00002934
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002935 if (isa<PreStmt>(Loc))
Mike Stump11289f42009-09-09 15:08:12 +00002936 Out << "\\lPreStmt\\l;";
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002937 else if (isa<PostLoad>(Loc))
Ted Kremeneka6e08322009-05-07 18:27:16 +00002938 Out << "\\lPostLoad\\l;";
2939 else if (isa<PostStore>(Loc))
2940 Out << "\\lPostStore\\l";
2941 else if (isa<PostLValue>(Loc))
2942 Out << "\\lPostLValue\\l";
Mike Stump11289f42009-09-09 15:08:12 +00002943
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002944 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2945 Out << "\\|Implicit-Null Dereference.\\l";
2946 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2947 Out << "\\|Explicit-Null Dereference.\\l";
2948 else if (GraphPrintCheckerState->isUndefDeref(N))
2949 Out << "\\|Dereference of undefialied value.\\l";
2950 else if (GraphPrintCheckerState->isUndefStore(N))
2951 Out << "\\|Store to Undefined Loc.";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002952 else if (GraphPrintCheckerState->isUndefResult(N))
2953 Out << "\\|Result of operation is undefined.";
2954 else if (GraphPrintCheckerState->isNoReturnCall(N))
2955 Out << "\\|Call to function marked \"noreturn\".";
2956 else if (GraphPrintCheckerState->isBadCall(N))
2957 Out << "\\|Call to NULL/Undefined.";
2958 else if (GraphPrintCheckerState->isUndefArg(N))
2959 Out << "\\|Argument in call is undefined";
Mike Stump11289f42009-09-09 15:08:12 +00002960
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002961 break;
2962 }
2963
Ted Kremenekac886cb2008-01-16 21:46:15 +00002964 const BlockEdge& E = cast<BlockEdge>(Loc);
2965 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2966 << E.getDst()->getBlockID() << ')';
Mike Stump11289f42009-09-09 15:08:12 +00002967
Ted Kremeneka50d9852008-01-30 23:03:39 +00002968 if (Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump11289f42009-09-09 15:08:12 +00002969
Ted Kremenek6947a792008-03-07 20:57:30 +00002970 SourceLocation SLoc = T->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00002971
Ted Kremeneka50d9852008-01-30 23:03:39 +00002972 Out << "\\|Terminator: ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002973 LangOptions LO; // FIXME.
2974 E.getSrc()->printTerminator(Out, LO);
Mike Stump11289f42009-09-09 15:08:12 +00002975
Ted Kremenek03ab1562008-03-09 03:30:59 +00002976 if (SLoc.isFileID()) {
2977 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00002978 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2979 << " col="
2980 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek03ab1562008-03-09 03:30:59 +00002981 }
Mike Stump11289f42009-09-09 15:08:12 +00002982
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002983 if (isa<SwitchStmt>(T)) {
2984 Stmt* Label = E.getDst()->getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00002985
2986 if (Label) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002987 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2988 Out << "\\lcase ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002989 LangOptions LO; // FIXME.
2990 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00002991
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002992 if (Stmt* RHS = C->getRHS()) {
2993 Out << " .. ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002994 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002995 }
Mike Stump11289f42009-09-09 15:08:12 +00002996
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002997 Out << ":";
2998 }
2999 else {
3000 assert (isa<DefaultStmt>(Label));
3001 Out << "\\ldefault:";
3002 }
3003 }
Mike Stump11289f42009-09-09 15:08:12 +00003004 else
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00003005 Out << "\\l(implicit) default:";
3006 }
3007 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremeneka50d9852008-01-30 23:03:39 +00003008 // FIXME
3009 }
3010 else {
3011 Out << "\\lCondition: ";
3012 if (*E.getSrc()->succ_begin() == E.getDst())
3013 Out << "true";
3014 else
Mike Stump11289f42009-09-09 15:08:12 +00003015 Out << "false";
Ted Kremeneka50d9852008-01-30 23:03:39 +00003016 }
Mike Stump11289f42009-09-09 15:08:12 +00003017
Ted Kremeneka50d9852008-01-30 23:03:39 +00003018 Out << "\\l";
3019 }
Mike Stump11289f42009-09-09 15:08:12 +00003020
Ted Kremenek93d1fed2008-02-28 09:25:22 +00003021 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3022 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek2531fce2008-01-30 23:24:39 +00003023 }
Ted Kremenekac886cb2008-01-16 21:46:15 +00003024 }
3025 }
Mike Stump11289f42009-09-09 15:08:12 +00003026
Ted Kremenek22c62c12008-02-28 10:21:43 +00003027 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenekb54312d2008-02-08 21:10:02 +00003028
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003029 const GRState *state = N->getState();
3030 state->printDOT(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003031
Ted Kremenek930191c2008-01-23 22:30:44 +00003032 Out << "\\l";
Ted Kremenekac886cb2008-01-16 21:46:15 +00003033 return Out.str();
3034 }
3035};
Mike Stump11289f42009-09-09 15:08:12 +00003036} // end llvm namespace
Ted Kremenekac886cb2008-01-16 21:46:15 +00003037#endif
3038
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003039#ifndef NDEBUG
Ted Kremenek576b76a2008-03-12 17:18:20 +00003040template <typename ITERATOR>
Zhongxing Xu107f7592009-08-06 12:48:26 +00003041ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek576b76a2008-03-12 17:18:20 +00003042
Zhongxing Xu107f7592009-08-06 12:48:26 +00003043template <> ExplodedNode*
3044GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3045 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek576b76a2008-03-12 17:18:20 +00003046 return I->first;
3047}
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003048#endif
3049
3050void GRExprEngine::ViewGraph(bool trim) {
Mike Stump11289f42009-09-09 15:08:12 +00003051#ifndef NDEBUG
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003052 if (trim) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00003053 std::vector<ExplodedNode*> Src;
Ted Kremenek95175052009-03-11 01:41:22 +00003054
3055 // Flush any outstanding reports to make sure we cover all the nodes.
3056 // This does not cause them to get displayed.
3057 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3058 const_cast<BugType*>(*I)->FlushReports(BR);
3059
3060 // Iterate through the reports and get their nodes.
3061 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
Ted Kremenek17a02962009-09-03 03:02:58 +00003062 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
Mike Stump11289f42009-09-09 15:08:12 +00003063 I2!=E2; ++I2) {
Ted Kremenek95175052009-03-11 01:41:22 +00003064 const BugReportEquivClass& EQ = *I2;
3065 const BugReport &R = **EQ.begin();
Zhongxing Xu107f7592009-08-06 12:48:26 +00003066 ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
Ted Kremenek95175052009-03-11 01:41:22 +00003067 if (N) Src.push_back(N);
3068 }
3069 }
Mike Stump11289f42009-09-09 15:08:12 +00003070
Ted Kremenek576b76a2008-03-12 17:18:20 +00003071 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003072 }
Ted Kremeneka7178c72008-03-11 18:25:33 +00003073 else {
3074 GraphPrintCheckerState = this;
3075 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek16306102008-08-13 21:24:49 +00003076
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003077 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Mike Stump11289f42009-09-09 15:08:12 +00003078
Ted Kremeneka7178c72008-03-11 18:25:33 +00003079 GraphPrintCheckerState = NULL;
3080 GraphPrintSourceManager = NULL;
3081 }
3082#endif
3083}
3084
Zhongxing Xu107f7592009-08-06 12:48:26 +00003085void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremeneka7178c72008-03-11 18:25:33 +00003086#ifndef NDEBUG
3087 GraphPrintCheckerState = this;
3088 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00003089
Zhongxing Xu107f7592009-08-06 12:48:26 +00003090 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneka7178c72008-03-11 18:25:33 +00003091
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003092 if (!TrimmedG.get())
Benjamin Kramer89b422c2009-08-23 12:08:50 +00003093 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003094 else
Mike Stump11289f42009-09-09 15:08:12 +00003095 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
3096
Ted Kremenek2531fce2008-01-30 23:24:39 +00003097 GraphPrintCheckerState = NULL;
Ted Kremenek6947a792008-03-07 20:57:30 +00003098 GraphPrintSourceManager = NULL;
Ted Kremenekd3122cb2008-02-14 22:36:46 +00003099#endif
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003100}