blob: 35a1bf2d722ac9cc872661cc731d2a15b9a5f2dd [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-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 Kremenek77349cb2008-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 Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek5a5d98b2009-07-22 21:43:51 +000018#include "clang/Analysis/PathSensitive/Checker.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000025#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000027#include "llvm/ADT/ImmutableList.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000028
Ted Kremenek0f5f0592008-02-27 06:07:00 +000029#ifndef NDEBUG
30#include "llvm/Support/GraphWriter.h"
Ted Kremenek0f5f0592008-02-27 06:07:00 +000031#endif
32
Ted Kremenekb387a3f2008-02-14 22:16:04 +000033using namespace clang;
34using llvm::dyn_cast;
35using llvm::cast;
36using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000037
Ted Kremeneke695e1c2008-04-15 23:06:53 +000038//===----------------------------------------------------------------------===//
Ted Kremenek5a5d98b2009-07-22 21:43:51 +000039// Batch auditor. DEPRECATED.
Ted Kremeneke695e1c2008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
41
Ted Kremenekbdb435d2008-07-11 18:37:32 +000042namespace {
43
44class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
45 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
46 typedef llvm::DenseMap<void*,Checks> MapTy;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenekbdb435d2008-07-11 18:37:32 +000048 MapTy M;
49 Checks::Factory F;
Ted Kremenek536aa022009-03-30 17:53:05 +000050 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000051
52public:
Ted Kremenek536aa022009-03-30 17:53:05 +000053 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
54 F(Alloc), AllStmts(F.GetEmptyList()) {}
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekbdb435d2008-07-11 18:37:32 +000056 virtual ~MappedBatchAuditor() {
57 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenekbdb435d2008-07-11 18:37:32 +000059 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
60 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
61
62 GRSimpleAPICheck* check = *I;
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenekbdb435d2008-07-11 18:37:32 +000064 if (AlreadyVisited.count(check))
65 continue;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekbdb435d2008-07-11 18:37:32 +000067 AlreadyVisited.insert(check);
68 delete check;
69 }
70 }
71
Ted Kremenek536aa022009-03-30 17:53:05 +000072 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000073 assert (A && "Check cannot be null.");
74 void* key = reinterpret_cast<void*>((uintptr_t) C);
75 MapTy::iterator I = M.find(key);
76 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Ted Kremenek536aa022009-03-30 17:53:05 +000079 void AddCheck(GRSimpleAPICheck *A) {
80 assert (A && "Check cannot be null.");
Mike Stump1eb44332009-09-09 15:08:12 +000081 AllStmts = F.Concat(A, AllStmts);
Ted Kremenek536aa022009-03-30 17:53:05 +000082 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000083
Zhongxing Xu031ccc02009-08-06 12:48:26 +000084 virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-03-30 17:53:05 +000085 // First handle the auditors that accept all statements.
86 bool isSink = false;
87 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
88 isSink |= (*I)->Audit(N, VMgr);
Mike Stump1eb44332009-09-09 15:08:12 +000089
Ted Kremenek536aa022009-03-30 17:53:05 +000090 // Next handle the auditors that accept only specific statements.
Ted Kremenek5f85e172009-07-22 22:35:28 +000091 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekbdb435d2008-07-11 18:37:32 +000092 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
93 MapTy::iterator MI = M.find(key);
Mike Stump1eb44332009-09-09 15:08:12 +000094 if (MI != M.end()) {
Ted Kremenek536aa022009-03-30 17:53:05 +000095 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
96 isSink |= (*I)->Audit(N, VMgr);
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
99 return isSink;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100 }
101};
102
103} // end anonymous namespace
104
105//===----------------------------------------------------------------------===//
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000106// Checker worklist routines.
107//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000108
109void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst,
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000110 ExplodedNodeSet &Src, bool isPrevisit) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000112 if (Checkers.empty()) {
113 Dst = Src;
114 return;
115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000117 ExplodedNodeSet Tmp;
118 ExplodedNodeSet *PrevSet = &Src;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000120 for (std::vector<Checker*>::iterator I = Checkers.begin(), E = Checkers.end();
121 I != E; ++I) {
122
Mike Stump1eb44332009-09-09 15:08:12 +0000123 ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000124 : (PrevSet == &Tmp) ? &Src : &Tmp;
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000125 CurrSet->clear();
126 Checker *checker = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000128 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000129 NI != NE; ++NI)
130 checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, isPrevisit);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000132 // Update which NodeSet is the current one.
133 PrevSet = CurrSet;
134 }
135
136 // Don't autotransition. The CheckerContext objects should do this
137 // automatically.
138}
139
140//===----------------------------------------------------------------------===//
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000141// Engine construction and deletion.
142//===----------------------------------------------------------------------===//
143
Ted Kremeneke448ab42008-05-01 18:33:28 +0000144static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
145 IdentifierInfo* II = &Ctx.Idents.get(name);
146 return Ctx.Selectors.getSelector(0, &II);
147}
148
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000149
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000150GRExprEngine::GRExprEngine(AnalysisManager &mgr)
Zhongxing Xu25e695b2009-08-15 03:17:38 +0000151 : AMgr(mgr),
Mike Stump1eb44332009-09-09 15:08:12 +0000152 CoreEngine(mgr.getASTContext(), *this),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000153 G(CoreEngine.getGraph()),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000154 Builder(NULL),
Mike Stump1eb44332009-09-09 15:08:12 +0000155 StateMgr(G.getContext(), mgr.getStoreManagerCreator(),
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000156 mgr.getConstraintManagerCreator(), G.getAllocator()),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000157 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000158 ValMgr(StateMgr.getValueManager()),
Ted Kremenek06669c82009-07-16 01:32:00 +0000159 SVator(ValMgr.getSValuator()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000160 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000161 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Mike Stump1eb44332009-09-09 15:08:12 +0000162 RaiseSel(GetNullarySelector("raise", G.getContext())),
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000163 BR(mgr, *this) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000164
Mike Stump1eb44332009-09-09 15:08:12 +0000165GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000166 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000167 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek5a5d98b2009-07-22 21:43:51 +0000168 for (std::vector<Checker*>::iterator I=Checkers.begin(), E=Checkers.end();
169 I!=E; ++I)
170 delete *I;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000171}
172
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000173//===----------------------------------------------------------------------===//
174// Utility methods.
175//===----------------------------------------------------------------------===//
176
Ted Kremenek186350f2008-04-23 20:12:28 +0000177
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000178void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000179 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000180 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000182}
183
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000184void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
185 if (!BatchAuditor)
186 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000188 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000189}
190
Ted Kremenek536aa022009-03-30 17:53:05 +0000191void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
192 if (!BatchAuditor)
193 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
194
195 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
196}
197
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000198const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) {
199 const GRState *state = StateMgr.getInitialState(InitLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000201 // Preconditions.
202
Ted Kremenek52e56022009-04-10 00:59:50 +0000203 // FIXME: It would be nice if we had a more general mechanism to add
204 // such preconditions. Some day.
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000205 const Decl *D = InitLoc->getDecl();
206
207 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
208 // Precondition: the first argument of 'main' is an integer guaranteed
209 // to be > 0.
Ted Kremenek52e56022009-04-10 00:59:50 +0000210 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
211 FD->getNumParams() > 0) {
212 const ParmVarDecl *PD = FD->getParamDecl(0);
213 QualType T = PD->getType();
214 if (T->isIntegerType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000215 if (const MemRegion *R = state->getRegion(PD, InitLoc)) {
Ted Kremenek3ff12592009-06-19 17:10:32 +0000216 SVal V = state->getSVal(loc::MemRegionVal(R));
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000217 SVal Constraint_untested = EvalBinOp(state, BinaryOperator::GT, V,
218 ValMgr.makeZeroVal(T),
219 getContext().IntTy);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000220
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000221 if (DefinedOrUnknownSVal *Constraint =
222 dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested)) {
223 if (const GRState *newState = state->Assume(*Constraint, true))
224 state = newState;
225 }
Ted Kremenek52e56022009-04-10 00:59:50 +0000226 }
227 }
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000228 }
229 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
230 // Precondition: 'self' is always non-null upon entry to an Objective-C
231 // method.
232 const ImplicitParamDecl *SelfD = MD->getSelfDecl();
233 const MemRegion *R = state->getRegion(SelfD, InitLoc);
234 SVal V = state->getSVal(loc::MemRegionVal(R));
235
236 if (const Loc *LV = dyn_cast<Loc>(&V)) {
237 // Assume that the pointer value in 'self' is non-null.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000238 state = state->Assume(*LV, true);
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000239 assert(state && "'self' cannot be null");
240 }
241 }
242
Ted Kremenek52e56022009-04-10 00:59:50 +0000243 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000244}
245
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000246//===----------------------------------------------------------------------===//
247// Top-level transfer function logic (Dispatcher).
248//===----------------------------------------------------------------------===//
249
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000250void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) {
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000252 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
253 S->getLocStart(),
254 "Error evaluating statement");
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000256 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000257 EntryNode = builder.getLastNode();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000259 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000260 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000261 StateMgr.CurrentStmt = S;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000263 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000264 if (BatchAuditor)
265 Builder->setAuditor(BatchAuditor.get());
Mike Stump1eb44332009-09-09 15:08:12 +0000266
267 // Create the cleaned state.
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000268 SymbolReaper SymReaper(Builder->getBasePredecessor()->getLiveVariables(),
269 SymMgr);
Zhongxing Xuc9994962009-08-27 06:55:26 +0000270 CleanedState = AMgr.shouldPurgeDead()
271 ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
272 : EntryNode->getState();
Ted Kremenek241677a2009-01-21 22:26:05 +0000273
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000274 // Process any special transfer function for dead symbols.
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000275 ExplodedNodeSet Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenek241677a2009-01-21 22:26:05 +0000277 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000278 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000279 else {
280 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000281 SaveOr OldHasGen(Builder->HasGeneratedNode);
282
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000283 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
284 Builder->PurgingDeadSymbols = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000287 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000288
289 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
290 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek846d4e92008-04-24 23:35:58 +0000293 bool HasAutoGenerated = false;
294
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000295 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000296
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000297 ExplodedNodeSet Dst;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
299 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000300 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
Mike Stump1eb44332009-09-09 15:08:12 +0000301
302 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000303 Visit(S, *I, Dst);
304
305 // Do we need to auto-generate a node? We only need to do this to generate
306 // a node with a "cleaned" state; GRCoreEngine will actually handle
Mike Stump1eb44332009-09-09 15:08:12 +0000307 // auto-transitions for other cases.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000308 if (Dst.size() == 1 && *Dst.begin() == EntryNode
309 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
310 HasAutoGenerated = true;
311 builder.generateNode(S, GetState(EntryNode), *I);
312 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000315 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000316 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000317 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000318
319 // FIXME: Consolidate.
320 StateMgr.CurrentStmt = 0;
321 CurrentStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek846d4e92008-04-24 23:35:58 +0000323 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324}
325
Mike Stump1eb44332009-09-09 15:08:12 +0000326void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000327 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
328 S->getLocStart(),
329 "Error evaluating statement");
330
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000331 // FIXME: add metadata to the CFG so that we can disable
332 // this check when we KNOW that there is no block-level subexpression.
333 // The motivation is that this check requires a hashtable lookup.
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Zhongxing Xucc025532009-08-25 03:33:41 +0000335 if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000336 Dst.Add(Pred);
337 return;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000340 switch (S->getStmtClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000342 default:
343 // Cases we intentionally have "default" handle:
344 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000346 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
347 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek540cbe22008-04-22 04:56:29 +0000349 case Stmt::ArraySubscriptExprClass:
350 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
351 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000353 case Stmt::AsmStmtClass:
354 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
355 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000357 case Stmt::BinaryOperatorClass: {
358 BinaryOperator* B = cast<BinaryOperator>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 if (B->isLogicalOp()) {
361 VisitLogicalExpr(B, Pred, Dst);
362 break;
363 }
364 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000365 const GRState* state = GetState(Pred);
Ted Kremenek8e029342009-08-27 22:17:37 +0000366 MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367 break;
368 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000369
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000370 if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000371 ExplodedNodeSet Tmp;
Ted Kremenek48af2a92009-02-25 22:32:02 +0000372 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000373 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000374 }
375 else
376 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
377
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000378 break;
379 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000380
Douglas Gregorb4609802008-11-14 16:09:21 +0000381 case Stmt::CallExprClass:
382 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000383 CallExpr* C = cast<CallExpr>(S);
384 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000385 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000386 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000387
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000388 // FIXME: ChooseExpr is really a constant. We need to fix
389 // the CFG do not model them as explicit control-flow.
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000391 case Stmt::ChooseExprClass: { // __builtin_choose_expr
392 ChooseExpr* C = cast<ChooseExpr>(S);
393 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
394 break;
395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000397 case Stmt::CompoundAssignOperatorClass:
398 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
399 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000400
401 case Stmt::CompoundLiteralExprClass:
402 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
403 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 case Stmt::ConditionalOperatorClass: { // '?' operator
406 ConditionalOperator* C = cast<ConditionalOperator>(S);
407 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
408 break;
409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000412 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000413 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000414 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000416 case Stmt::DeclStmtClass:
417 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
418 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000420 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000421 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000422 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000423 VisitCast(C, C->getSubExpr(), Pred, Dst);
424 break;
425 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000426
427 case Stmt::InitListExprClass:
428 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
429 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000431 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000432 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
433 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000435 case Stmt::ObjCIvarRefExprClass:
436 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
437 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000438
439 case Stmt::ObjCForCollectionStmtClass:
440 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
441 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000443 case Stmt::ObjCMessageExprClass: {
444 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
445 break;
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000448 case Stmt::ObjCAtThrowStmtClass: {
449 // FIXME: This is not complete. We basically treat @throw as
450 // an abort.
451 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
452 Builder->BuildSinks = true;
453 MakeNode(Dst, S, Pred, GetState(Pred));
454 break;
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000457 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000458 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000459 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000461 case Stmt::ReturnStmtClass:
462 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
463 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Sebastian Redl05189992008-11-11 17:56:53 +0000465 case Stmt::SizeOfAlignOfExprClass:
466 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000467 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000469 case Stmt::StmtExprClass: {
470 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000471
472 if (SE->getSubStmt()->body_empty()) {
473 // Empty statement expression.
474 assert(SE->getType() == getContext().VoidTy
475 && "Empty statement expression must have void type.");
476 Dst.Add(Pred);
477 break;
478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000480 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
481 const GRState* state = GetState(Pred);
Ted Kremenek8e029342009-08-27 22:17:37 +0000482 MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000483 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000484 else
485 Dst.Add(Pred);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000487 break;
488 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000489
490 case Stmt::StringLiteralClass:
491 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
492 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek72374592009-03-18 23:49:26 +0000494 case Stmt::UnaryOperatorClass: {
495 UnaryOperator *U = cast<UnaryOperator>(S);
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000496 if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000497 ExplodedNodeSet Tmp;
Ted Kremenek72374592009-03-18 23:49:26 +0000498 VisitUnaryOperator(U, Pred, Tmp, false);
499 EvalEagerlyAssume(Dst, Tmp, U);
500 }
501 else
502 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000503 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000504 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000505 }
506}
507
Mike Stump1eb44332009-09-09 15:08:12 +0000508void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred,
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000509 ExplodedNodeSet& Dst) {
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000511 Ex = Ex->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Zhongxing Xucc025532009-08-25 03:33:41 +0000513 if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000514 Dst.Add(Pred);
515 return;
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000518 switch (Ex->getStmtClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000520 case Stmt::ArraySubscriptExprClass:
521 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
522 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000524 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000525 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000526 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
527 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000529 case Stmt::ObjCIvarRefExprClass:
530 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
531 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000533 case Stmt::UnaryOperatorClass:
534 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
535 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000537 case Stmt::MemberExprClass:
538 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
539 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek4f090272008-10-27 21:54:31 +0000541 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000542 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000543 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000545 case Stmt::ObjCPropertyRefExprClass:
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000546 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000547 // FIXME: Property assignments are lvalues, but not really "locations".
548 // e.g.: self.x = something;
549 // Here the "self.x" really can translate to a method call (setter) when
550 // the assignment is made. Moreover, the entire assignment expression
551 // evaluate to whatever "something" is, not calling the "getter" for
552 // the property (which would make sense since it can have side effects).
553 // We'll probably treat this as a location, but not one that we can
554 // take the address of. Perhaps we need a new SVal class for cases
555 // like thsis?
556 // Note that we have a similar problem for bitfields, since they don't
557 // have "locations" in the sense that we can take their address.
558 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000559 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000560
561 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 const GRState* state = GetState(Pred);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000563 SVal V = state->getLValue(cast<StringLiteral>(Ex));
Ted Kremenek8e029342009-08-27 22:17:37 +0000564 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000565 return;
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000568 default:
569 // Arbitrary subexpressions can return aggregate temporaries that
570 // can be used in a lvalue context. We need to enhance our support
571 // of such temporaries in both the environment and the store, so right
572 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000573 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000574 "Other kinds of expressions with non-aggregate/union types do"
575 " not have lvalues.");
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000577 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000578 }
579}
580
581//===----------------------------------------------------------------------===//
582// Block entrance. (Update counters).
583//===----------------------------------------------------------------------===//
584
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000585bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000586 GRBlockCounter BC) {
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000588 return BC.getNumVisited(B->getBlockID()) < 3;
589}
590
591//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +0000592// Generic node creation.
593//===----------------------------------------------------------------------===//
594
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000595ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
596 ExplodedNode* Pred, const GRState* St,
597 ProgramPoint::Kind K, const void *tag) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000598 assert (Builder && "GRStmtNodeBuilder not present.");
599 SaveAndRestore<const void*> OldTag(Builder->Tag);
600 Builder->Tag = tag;
601 return Builder->MakeNode(Dst, S, Pred, St, K);
602}
603
604//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000605// Branch processing.
606//===----------------------------------------------------------------------===//
607
Ted Kremeneka8538d92009-02-13 01:45:31 +0000608const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000609 Stmt* Terminator,
610 bool branchTaken) {
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek05a23782008-02-26 19:05:15 +0000612 switch (Terminator->getStmtClass()) {
613 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000614 return state;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Ted Kremenek05a23782008-02-26 19:05:15 +0000616 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenek05a23782008-02-26 19:05:15 +0000618 BinaryOperator* B = cast<BinaryOperator>(Terminator);
619 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Ted Kremenek05a23782008-02-26 19:05:15 +0000621 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek05a23782008-02-26 19:05:15 +0000623 // For &&, if we take the true branch, then the value of the whole
624 // expression is that of the RHS expression.
625 //
626 // For ||, if we take the false branch, then the value of the whole
627 // expression is that of the RHS expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek05a23782008-02-26 19:05:15 +0000629 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000630 (Op == BinaryOperator::LOr && !branchTaken)
Ted Kremenek05a23782008-02-26 19:05:15 +0000631 ? B->getRHS() : B->getLHS();
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek8e029342009-08-27 22:17:37 +0000633 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek05a23782008-02-26 19:05:15 +0000636 case Stmt::ConditionalOperatorClass: { // ?:
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Ted Kremenek05a23782008-02-26 19:05:15 +0000638 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek05a23782008-02-26 19:05:15 +0000640 // For ?, if branchTaken == true then the value is either the LHS or
641 // the condition itself. (GNU extension).
Mike Stump1eb44332009-09-09 15:08:12 +0000642
643 Expr* Ex;
644
Ted Kremenek05a23782008-02-26 19:05:15 +0000645 if (branchTaken)
Mike Stump1eb44332009-09-09 15:08:12 +0000646 Ex = C->getLHS() ? C->getLHS() : C->getCond();
Ted Kremenek05a23782008-02-26 19:05:15 +0000647 else
648 Ex = C->getRHS();
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek8e029342009-08-27 22:17:37 +0000650 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek05a23782008-02-26 19:05:15 +0000653 case Stmt::ChooseExprClass: { // ?:
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Ted Kremenek05a23782008-02-26 19:05:15 +0000655 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
657 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek8e029342009-08-27 22:17:37 +0000658 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000659 }
660 }
661}
662
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000663/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
664/// to try to recover some path-sensitivity for casts of symbolic
665/// integers that promote their values (which are currently not tracked well).
666/// This function returns the SVal bound to Condition->IgnoreCasts if all the
667// cast(s) did was sign-extend the original value.
668static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
669 Stmt* Condition, ASTContext& Ctx) {
670
671 Expr *Ex = dyn_cast<Expr>(Condition);
672 if (!Ex)
673 return UnknownVal();
674
675 uint64_t bits = 0;
676 bool bitsInit = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000678 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
679 QualType T = CE->getType();
680
681 if (!T->isIntegerType())
682 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000684 uint64_t newBits = Ctx.getTypeSize(T);
685 if (!bitsInit || newBits < bits) {
686 bitsInit = true;
687 bits = newBits;
688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000690 Ex = CE->getSubExpr();
691 }
692
693 // We reached a non-cast. Is it a symbolic value?
694 QualType T = Ex->getType();
695
696 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
697 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000699 return state->getSVal(Ex);
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000700}
701
Ted Kremenekaf337412008-11-12 19:24:17 +0000702void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000703 GRBranchNodeBuilder& builder) {
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenekb2331832008-02-15 22:29:00 +0000705 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump1eb44332009-09-09 15:08:12 +0000706 if (!Condition) {
Ted Kremenekb2331832008-02-15 22:29:00 +0000707 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000708 return;
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenek21028dd2009-03-11 03:54:24 +0000711 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
712 Condition->getLocStart(),
713 "Error evaluating branch");
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000714
Mike Stump1eb44332009-09-09 15:08:12 +0000715 const GRState* PrevState = builder.getState();
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000716 SVal X = PrevState->getSVal(Condition);
717 DefinedSVal *V = NULL;
718
719 while (true) {
720 V = dyn_cast<DefinedSVal>(&X);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000722 if (!V) {
723 if (X.isUnknown()) {
724 if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000725 if (Ex->getType()->isIntegerType()) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000726 // Try to recover some path-sensitivity. Right now casts of symbolic
727 // integers that promote their values are currently not tracked well.
728 // If 'Condition' is such an expression, try and recover the
729 // underlying value and use that instead.
730 SVal recovered = RecoverCastedSymbol(getStateManager(),
731 builder.getState(), Condition,
732 getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000734 if (!recovered.isUnknown()) {
735 X = recovered;
736 continue;
737 }
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000738 }
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000739 }
740
741 builder.generateNode(MarkBranch(PrevState, Term, true), true);
742 builder.generateNode(MarkBranch(PrevState, Term, false), false);
743 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000746 assert(X.isUndef());
747 ExplodedNode *N = builder.generateNode(PrevState, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000748
749 if (N) {
750 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000751 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenekb38911f2008-01-30 23:03:39 +0000754 builder.markInfeasible(false);
755 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000756 }
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000757
758 break;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000761 // Process the true branch.
Ted Kremenek52003542009-07-20 18:44:36 +0000762 if (builder.isFeasible(true)) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000763 if (const GRState *state = PrevState->Assume(*V, true))
Ted Kremenek52003542009-07-20 18:44:36 +0000764 builder.generateNode(MarkBranch(state, Term, true), true);
765 else
766 builder.markInfeasible(true);
767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
769 // Process the false branch.
Ted Kremenek52003542009-07-20 18:44:36 +0000770 if (builder.isFeasible(false)) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000771 if (const GRState *state = PrevState->Assume(*V, false))
Ted Kremenek52003542009-07-20 18:44:36 +0000772 builder.generateNode(MarkBranch(state, Term, false), false);
773 else
774 builder.markInfeasible(false);
775 }
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000776}
777
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000778/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000779/// nodes by processing the 'effects' of a computed goto jump.
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000780void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000781
Mike Stump1eb44332009-09-09 15:08:12 +0000782 const GRState *state = builder.getState();
Ted Kremenek3ff12592009-06-19 17:10:32 +0000783 SVal V = state->getSVal(builder.getTarget());
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek754607e2008-02-13 00:24:44 +0000785 // Three possibilities:
786 //
787 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000788 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000789 // (3) We have no clue about the label. Dispatch to all targets.
790 //
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000792 typedef GRIndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek754607e2008-02-13 00:24:44 +0000793
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000794 if (isa<loc::GotoLabel>(V)) {
795 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek754607e2008-02-13 00:24:44 +0000797 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000798 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000799 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000800 return;
801 }
802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek754607e2008-02-13 00:24:44 +0000804 assert (false && "No block with label.");
805 return;
806 }
807
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000808 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000809 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000810 ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000811 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000812 return;
813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek754607e2008-02-13 00:24:44 +0000815 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000816 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremenek754607e2008-02-13 00:24:44 +0000818 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000819 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000820}
Ted Kremenekf233d482008-02-05 00:26:40 +0000821
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000822
823void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000824 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Zhongxing Xucc025532009-08-25 03:33:41 +0000826 assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremeneka8538d92009-02-13 01:45:31 +0000828 const GRState* state = GetState(Pred);
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000829 SVal X = state->getSVal(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000831 assert (X.isUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek3ff12592009-06-19 17:10:32 +0000833 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump1eb44332009-09-09 15:08:12 +0000834 assert(SE);
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000835 X = state->getSVal(SE);
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000837 // Make sure that we invalidate the previous binding.
Ted Kremenek8e029342009-08-27 22:17:37 +0000838 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000839}
840
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000841/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
842/// nodes by processing the 'effects' of a switch statement.
Mike Stump1eb44332009-09-09 15:08:12 +0000843void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
844 typedef GRSwitchNodeBuilder::iterator iterator;
845 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000846 Expr* CondE = builder.getCondition();
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000847 SVal CondV_untested = state->getSVal(CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000848
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000849 if (CondV_untested.isUndef()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000850 ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000851 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000852 return;
853 }
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000854 DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
Ted Kremenek692416c2008-02-18 22:57:02 +0000855
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000856 const GRState *DefaultSt = state;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000857 bool defaultIsFeasible = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000859 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000860 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000861
862 // Evaluate the LHS of the case value.
863 Expr::EvalResult V1;
Mike Stump1eb44332009-09-09 15:08:12 +0000864 bool b = Case->getLHS()->Evaluate(V1, getContext());
865
Ted Kremenek72afb372009-01-17 01:54:16 +0000866 // Sanity checks. These go away in Release builds.
Mike Stump1eb44332009-09-09 15:08:12 +0000867 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek72afb372009-01-17 01:54:16 +0000868 && "Case condition must evaluate to an integer constant.");
Mike Stump1eb44332009-09-09 15:08:12 +0000869 b = b; // silence unused variable warning
870 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek72afb372009-01-17 01:54:16 +0000871 getContext().getTypeSize(CondE->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000873 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000874 Expr::EvalResult V2;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000876 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000877 b = E->Evaluate(V2, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000878 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek72afb372009-01-17 01:54:16 +0000879 && "Case condition must evaluate to an integer constant.");
880 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000881 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000882 else
883 V2 = V1;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000885 // FIXME: Eventually we should replace the logic below with a range
886 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000887 // This should be easy once we have "ranges" for NonLVals.
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek14a11402008-03-17 22:17:56 +0000889 do {
Mike Stump1eb44332009-09-09 15:08:12 +0000890 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000891 DefinedOrUnknownSVal Res = SVator.EvalEQ(DefaultSt, CondV, CaseVal);
892
Mike Stump1eb44332009-09-09 15:08:12 +0000893 // Now "assume" that the case matches.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000894 if (const GRState* stateNew = state->Assume(Res, true)) {
Ted Kremeneka591bc02009-06-18 22:57:13 +0000895 builder.generateCaseStmtNode(I, stateNew);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000897 // If CondV evaluates to a constant, then we know that this
898 // is the *only* case that we can take, so stop evaluating the
899 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000900 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000901 return;
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000904 // Now "assume" that the case doesn't match. Add this state
905 // to the default state (if it is feasible).
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000906 if (const GRState *stateNew = DefaultSt->Assume(Res, false)) {
Ted Kremeneka591bc02009-06-18 22:57:13 +0000907 defaultIsFeasible = true;
908 DefaultSt = stateNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000909 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000910
Ted Kremenek14a11402008-03-17 22:17:56 +0000911 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000912 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000913 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek72afb372009-01-17 01:54:16 +0000915 ++V1.Val.getInt();
916 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenek14a11402008-03-17 22:17:56 +0000918 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000921 // If we reach here, than we know that the default branch is
Mike Stump1eb44332009-09-09 15:08:12 +0000922 // possible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000923 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000924}
925
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000926//===----------------------------------------------------------------------===//
927// Transfer functions: logical operations ('&&', '||').
928//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000929
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000930void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
931 ExplodedNodeSet& Dst) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Ted Kremenek3ff12592009-06-19 17:10:32 +0000933 assert(B->getOpcode() == BinaryOperator::LAnd ||
934 B->getOpcode() == BinaryOperator::LOr);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Zhongxing Xucc025532009-08-25 03:33:41 +0000936 assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremeneka8538d92009-02-13 01:45:31 +0000938 const GRState* state = GetState(Pred);
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000939 SVal X = state->getSVal(B);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000940 assert(X.isUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000942 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek3ff12592009-06-19 17:10:32 +0000943 assert(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremenek05a23782008-02-26 19:05:15 +0000945 if (Ex == B->getRHS()) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000946 X = state->getSVal(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000948 // Handle undefined values.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000949 if (X.isUndef()) {
Ted Kremenek8e029342009-08-27 22:17:37 +0000950 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000951 return;
952 }
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000953
954 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek05a23782008-02-26 19:05:15 +0000956 // We took the RHS. Because the value of the '&&' or '||' expression must
957 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
958 // or 1. Alternatively, we could take a lazy approach, and calculate this
959 // value later when necessary. We don't have the machinery in place for
960 // this right now, and since most logical expressions are used for branches,
Mike Stump1eb44332009-09-09 15:08:12 +0000961 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000962 if (const GRState *newState = state->Assume(XD, true))
Mike Stump1eb44332009-09-09 15:08:12 +0000963 MakeNode(Dst, B, Pred,
Ted Kremenek8e029342009-08-27 22:17:37 +0000964 newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000966 if (const GRState *newState = state->Assume(XD, false))
Mike Stump1eb44332009-09-09 15:08:12 +0000967 MakeNode(Dst, B, Pred,
Ted Kremenek8e029342009-08-27 22:17:37 +0000968 newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenekf233d482008-02-05 00:26:40 +0000969 }
970 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000971 // We took the LHS expression. Depending on whether we are '&&' or
972 // '||' we know what the value of the expression is via properties of
973 // the short-circuiting.
Mike Stump1eb44332009-09-09 15:08:12 +0000974 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
Zhongxing Xud91ee272009-06-23 09:02:15 +0000975 B->getType());
Ted Kremenek8e029342009-08-27 22:17:37 +0000976 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000977 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000978}
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000980//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000981// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000982//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000983
Mike Stump1eb44332009-09-09 15:08:12 +0000984void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000985 ExplodedNodeSet &Dst, bool asLValue) {
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000987 const GRState *state = GetState(Pred);
988 const NamedDecl *D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000989
990 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
991
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000992 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xua7581732008-10-17 02:20:14 +0000993
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000994 if (asLValue)
Ted Kremenek8e029342009-08-27 22:17:37 +0000995 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremenek7090d542009-05-07 18:27:16 +0000996 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000997 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000998 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000999 return;
1000
1001 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
1002 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
1003
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001004 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek8e029342009-08-27 22:17:37 +00001005 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001006 return;
1007
1008 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek657406d2009-09-23 01:30:01 +00001009 // This code is valid regardless of the value of 'isLValue'.
Zhongxing Xu369f4472009-04-20 05:24:46 +00001010 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek8e029342009-08-27 22:17:37 +00001011 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremenek7090d542009-05-07 18:27:16 +00001012 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001013 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001014 }
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001016 assert (false &&
1017 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001018}
1019
Ted Kremenek540cbe22008-04-22 04:56:29 +00001020/// VisitArraySubscriptExpr - Transfer function for array accesses
Mike Stump1eb44332009-09-09 15:08:12 +00001021void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001022 ExplodedNode* Pred,
1023 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek540cbe22008-04-22 04:56:29 +00001025 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001026 Expr* Idx = A->getIdx()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001027 ExplodedNodeSet Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek265a3052009-02-24 02:23:11 +00001029 if (Base->getType()->isVectorType()) {
1030 // For vector types get its lvalue.
1031 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1032 // In fact, I think this is just a hack. We need to get the right
1033 // semantics.
1034 VisitLValue(Base, Pred, Tmp);
1035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036 else
Ted Kremenek265a3052009-02-24 02:23:11 +00001037 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001039 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1040 ExplodedNodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001041 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001043 for (ExplodedNodeSet::iterator I2=Tmp2.begin(),E2=Tmp2.end();I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001044 const GRState* state = GetState(*I2);
Zhongxing Xud0f8bb12009-10-14 03:33:08 +00001045 SVal V = state->getLValue(A->getType(), state->getSVal(Idx),
1046 state->getSVal(Base));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001047
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001048 if (asLValue)
Ted Kremenek8e029342009-08-27 22:17:37 +00001049 MakeNode(Dst, A, *I2, state->BindExpr(A, V),
Ted Kremenek7090d542009-05-07 18:27:16 +00001050 ProgramPoint::PostLValueKind);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001051 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001052 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001053 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001054 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001055}
1056
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001057/// VisitMemberExpr - Transfer function for member expressions.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001058void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
1059 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001061 Expr* Base = M->getBase()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001062 ExplodedNodeSet Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +00001063
1064 if (M->isArrow())
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001065 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1066 else
1067 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregor86f19402008-12-20 23:49:58 +00001069 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1070 if (!Field) // FIXME: skipping member expressions for non-fields
1071 return;
1072
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001073 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001074 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001075 // FIXME: Should we insert some assumption logic in here to determine
1076 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001077 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xud0f8bb12009-10-14 03:33:08 +00001078 SVal L = state->getLValue(Field, state->getSVal(Base));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001079
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001080 if (asLValue)
Ted Kremenek8e029342009-08-27 22:17:37 +00001081 MakeNode(Dst, M, *I, state->BindExpr(M, L),
Ted Kremenek7090d542009-05-07 18:27:16 +00001082 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001083 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001084 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001085 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001086}
1087
Ted Kremeneka8538d92009-02-13 01:45:31 +00001088/// EvalBind - Handle the semantics of binding a value to a specific location.
1089/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001090void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001091 const GRState* state, SVal location, SVal Val) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001092
Ted Kremenek41573eb2009-02-14 01:43:44 +00001093 const GRState* newState = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremenek41573eb2009-02-14 01:43:44 +00001095 if (location.isUnknown()) {
1096 // We know that the new state will be the same as the old state since
1097 // the location of the binding is "unknown". Consequently, there
1098 // is no reason to just create a new node.
1099 newState = state;
1100 }
1101 else {
1102 // We are binding to a value other than 'unknown'. Perform the binding
1103 // using the StoreManager.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001104 newState = state->bindLoc(cast<Loc>(location), Val);
Ted Kremenek41573eb2009-02-14 01:43:44 +00001105 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001106
Ted Kremenek41573eb2009-02-14 01:43:44 +00001107 // The next thing to do is check if the GRTransferFuncs object wants to
1108 // update the state based on the new binding. If the GRTransferFunc object
1109 // doesn't do anything, just auto-propagate the current state.
1110 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1111 newState != state);
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Ted Kremenek41573eb2009-02-14 01:43:44 +00001113 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001114}
1115
1116/// EvalStore - Handle the semantics of a store via an assignment.
1117/// @param Dst The node set to store generated state nodes
1118/// @param Ex The expression representing the location of the store
1119/// @param state The current simulation state
1120/// @param location The location to store the value
1121/// @param Val The value to be stored
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001122void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001123 const GRState* state, SVal location, SVal Val,
1124 const void *tag) {
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001126 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001128 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001129 Pred = EvalLocation(Ex, Pred, state, location, tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenek8c354752008-12-16 22:02:27 +00001131 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001132 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001133
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001134 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001135 state = GetState(Pred);
1136
Mike Stump1eb44332009-09-09 15:08:12 +00001137 // Proceed with the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001138 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek1670e402009-04-11 00:11:10 +00001139 SaveAndRestore<const void*> OldTag(Builder->Tag);
1140 Builder->PointKind = ProgramPoint::PostStoreKind;
1141 Builder->Tag = tag;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001142 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001143}
1144
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001145void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001146 const GRState* state, SVal location,
1147 const void *tag) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001148
Mike Stump1eb44332009-09-09 15:08:12 +00001149 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001150 Pred = EvalLocation(Ex, Pred, state, location, tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenek8c354752008-12-16 22:02:27 +00001152 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001153 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremeneka8538d92009-02-13 01:45:31 +00001155 state = GetState(Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001157 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001158 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001159
1160 // FIXME: Currently symbolic analysis "generates" new symbols
1161 // for the contents of values. We need a better approach.
1162
Ted Kremenek8c354752008-12-16 22:02:27 +00001163 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001164 // This is important. We must nuke the old binding.
Ted Kremenek8e029342009-08-27 22:17:37 +00001165 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, UnknownVal()),
Zhongxing Xu5032ffe2009-08-25 06:51:30 +00001166 K, tag);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001167 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001168 else {
Ted Kremenek3ff12592009-06-19 17:10:32 +00001169 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek43d74a52009-07-11 04:38:49 +00001171 // Casts can create weird scenarios where a location must be implicitly
1172 // converted to something else. For example:
1173 //
1174 // void *x;
1175 // int *y = (int*) &x; // void** -> int* cast.
1176 // invalidate(y); // 'x' now binds to a symbolic region
1177 // int z = *y;
Mike Stump1eb44332009-09-09 15:08:12 +00001178 //
Zhongxing Xu82037252009-07-14 01:12:46 +00001179 //if (isa<Loc>(V) && !Loc::IsLocType(Ex->getType())) {
1180 // V = EvalCast(V, Ex->getType());
1181 //}
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Ted Kremenek8e029342009-08-27 22:17:37 +00001183 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), K, tag);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001184 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001185}
1186
Mike Stump1eb44332009-09-09 15:08:12 +00001187void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, Expr* StoreE,
1188 ExplodedNode* Pred, const GRState* state,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001189 SVal location, SVal Val, const void *tag) {
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001191 ExplodedNodeSet TmpDst;
Ted Kremenek1670e402009-04-11 00:11:10 +00001192 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001193
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001194 for (ExplodedNodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek1670e402009-04-11 00:11:10 +00001195 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001196}
1197
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001198ExplodedNode* GRExprEngine::EvalLocation(Stmt* Ex, ExplodedNode* Pred,
1199 const GRState* state, SVal location,
1200 const void *tag) {
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Ted Kremenek1670e402009-04-11 00:11:10 +00001202 SaveAndRestore<const void*> OldTag(Builder->Tag);
1203 Builder->Tag = tag;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
1205 // Check for loads/stores from/to undefined values.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001206 if (location.isUndef()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001207 ExplodedNode* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001208 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001209 ProgramPoint::PostUndefLocationCheckFailedKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenek8c354752008-12-16 22:02:27 +00001211 if (N) {
1212 N->markAsSink();
1213 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Ted Kremenek8c354752008-12-16 22:02:27 +00001216 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001219 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1220 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001221 return Pred;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001223 // During a load, one of two possible situations arise:
1224 // (1) A crash, because the location (pointer) was NULL.
1225 // (2) The location (pointer) is not NULL, and the dereference works.
Mike Stump1eb44332009-09-09 15:08:12 +00001226 //
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001227 // We add these assumptions.
Mike Stump1eb44332009-09-09 15:08:12 +00001228
1229 Loc LV = cast<Loc>(location);
1230
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001231 // "Assume" that the pointer is not NULL.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001232 const GRState *StNotNull = state->Assume(LV, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001234 // "Assume" that the pointer is NULL.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001235 const GRState *StNull = state->Assume(LV, false);
Zhongxing Xua1718c72009-04-03 07:33:13 +00001236
Mike Stump1eb44332009-09-09 15:08:12 +00001237 if (StNull) {
Ted Kremenek7360fda2008-09-18 23:09:54 +00001238 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001239 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekb65be702009-06-18 01:23:53 +00001240 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001242 // We don't use "MakeNode" here because the node will be a sink
1243 // and we have no intention of processing it later.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001244 ExplodedNode* NullNode =
Mike Stump1eb44332009-09-09 15:08:12 +00001245 Builder->generateNode(Ex, StNull, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001246 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001247
Mike Stump1eb44332009-09-09 15:08:12 +00001248 if (NullNode) {
1249 NullNode->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001250 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001251 else ExplicitNullDeref.insert(NullNode);
1252 }
1253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Ted Kremeneka591bc02009-06-18 22:57:13 +00001255 if (!StNotNull)
1256 return NULL;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001257
Ted Kremenek39abcdf2009-08-01 05:59:39 +00001258 // FIXME: Temporarily disable out-of-bounds checking until we make
1259 // the logic reflect recent changes to CastRegion and friends.
1260#if 0
Zhongxing Xu60156f02008-11-08 03:45:42 +00001261 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001262 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001263 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1264 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1265 // Get the index of the accessed element.
1266 SVal Idx = ER->getIndex();
1267 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001268 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1269 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001270
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001271 const GRState * StInBound = StNotNull->AssumeInBound(Idx, NumElements,
Ted Kremeneka591bc02009-06-18 22:57:13 +00001272 true);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001273 const GRState* StOutBound = StNotNull->AssumeInBound(Idx, NumElements,
Ted Kremeneka591bc02009-06-18 22:57:13 +00001274 false);
Zhongxing Xu60156f02008-11-08 03:45:42 +00001275
Ted Kremeneka591bc02009-06-18 22:57:13 +00001276 if (StOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001277 // Report warning. Make sink node manually.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001278 ExplodedNode* OOBNode =
Ted Kremenek8c354752008-12-16 22:02:27 +00001279 Builder->generateNode(Ex, StOutBound, Pred,
1280 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001281
1282 if (OOBNode) {
1283 OOBNode->markAsSink();
1284
Ted Kremeneka591bc02009-06-18 22:57:13 +00001285 if (StInBound)
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001286 ImplicitOOBMemAccesses.insert(OOBNode);
1287 else
1288 ExplicitOOBMemAccesses.insert(OOBNode);
1289 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001290 }
1291
Ted Kremeneka591bc02009-06-18 22:57:13 +00001292 if (!StInBound)
1293 return NULL;
1294
Ted Kremenek8c354752008-12-16 22:02:27 +00001295 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001296 }
1297 }
Ted Kremenek39abcdf2009-08-01 05:59:39 +00001298#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Ted Kremenek8c354752008-12-16 22:02:27 +00001300 // Generate a new node indicating the checks succeed.
1301 return Builder->generateNode(Ex, StNotNull, Pred,
1302 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001303}
1304
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001305//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001306// Transfer function: OSAtomics.
1307//
1308// FIXME: Eventually refactor into a more "plugin" infrastructure.
1309//===----------------------------------------------------------------------===//
1310
1311// Mac OS X:
1312// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1313// atomic.3.html
1314//
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001315static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst,
Ted Kremenek1670e402009-04-11 00:11:10 +00001316 GRExprEngine& Engine,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001317 GRStmtNodeBuilder& Builder,
Mike Stump1eb44332009-09-09 15:08:12 +00001318 CallExpr* CE, SVal L,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001319 ExplodedNode* Pred) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001320
1321 // Not enough arguments to match OSAtomicCompareAndSwap?
1322 if (CE->getNumArgs() != 3)
1323 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Ted Kremenek1670e402009-04-11 00:11:10 +00001325 ASTContext &C = Engine.getContext();
1326 Expr *oldValueExpr = CE->getArg(0);
1327 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1328
1329 Expr *newValueExpr = CE->getArg(1);
1330 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Ted Kremenek1670e402009-04-11 00:11:10 +00001332 // Do the types of 'oldValue' and 'newValue' match?
1333 if (oldValueType != newValueType)
1334 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Ted Kremenek1670e402009-04-11 00:11:10 +00001336 Expr *theValueExpr = CE->getArg(2);
Ted Kremenek39abcdf2009-08-01 05:59:39 +00001337 const PointerType *theValueType =
1338 theValueExpr->getType()->getAs<PointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Ted Kremenek1670e402009-04-11 00:11:10 +00001340 // theValueType not a pointer?
1341 if (!theValueType)
1342 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Ted Kremenek1670e402009-04-11 00:11:10 +00001344 QualType theValueTypePointee =
1345 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Ted Kremenek1670e402009-04-11 00:11:10 +00001347 // The pointee must match newValueType and oldValueType.
1348 if (theValueTypePointee != newValueType)
1349 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Ted Kremenek1670e402009-04-11 00:11:10 +00001351 static unsigned magic_load = 0;
1352 static unsigned magic_store = 0;
1353
1354 const void *OSAtomicLoadTag = &magic_load;
1355 const void *OSAtomicStoreTag = &magic_store;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Ted Kremenek1670e402009-04-11 00:11:10 +00001357 // Load 'theValue'.
Ted Kremenek1670e402009-04-11 00:11:10 +00001358 const GRState *state = Pred->getState();
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001359 ExplodedNodeSet Tmp;
Ted Kremenek23ec48c2009-06-18 23:58:37 +00001360 SVal location = state->getSVal(theValueExpr);
Ted Kremenek1670e402009-04-11 00:11:10 +00001361 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1362
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001363 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
Ted Kremenek1670e402009-04-11 00:11:10 +00001364 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001366 ExplodedNode *N = *I;
Ted Kremenek1670e402009-04-11 00:11:10 +00001367 const GRState *stateLoad = N->getState();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001368 SVal theValueVal_untested = stateLoad->getSVal(theValueExpr);
1369 SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Ted Kremenek1ff83392009-07-20 20:38:59 +00001371 // FIXME: Issue an error.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001372 if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001373 return false;
Ted Kremenek1ff83392009-07-20 20:38:59 +00001374 }
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001375
1376 DefinedOrUnknownSVal theValueVal =
1377 cast<DefinedOrUnknownSVal>(theValueVal_untested);
1378 DefinedOrUnknownSVal oldValueVal =
1379 cast<DefinedOrUnknownSVal>(oldValueVal_untested);
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenekff4264d2009-08-25 18:44:25 +00001381 SValuator &SVator = Engine.getSValuator();
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Ted Kremenek1670e402009-04-11 00:11:10 +00001383 // Perform the comparison.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001384 DefinedOrUnknownSVal Cmp = SVator.EvalEQ(stateLoad, theValueVal,
1385 oldValueVal);
Ted Kremeneka591bc02009-06-18 22:57:13 +00001386
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001387 const GRState *stateEqual = stateLoad->Assume(Cmp, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Ted Kremenek1670e402009-04-11 00:11:10 +00001389 // Were they equal?
Ted Kremeneka591bc02009-06-18 22:57:13 +00001390 if (stateEqual) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001391 // Perform the store.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001392 ExplodedNodeSet TmpStore;
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001393 SVal val = stateEqual->getSVal(newValueExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001395 // Handle implicit value casts.
1396 if (const TypedRegion *R =
1397 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
Ted Kremenekff4264d2009-08-25 18:44:25 +00001398 llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C),
1399 newValueExpr->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001400 }
1401
1402 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001403 val, OSAtomicStoreTag);
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Ted Kremenek1670e402009-04-11 00:11:10 +00001405 // Now bind the result of the comparison.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001406 for (ExplodedNodeSet::iterator I2 = TmpStore.begin(),
Ted Kremenek1670e402009-04-11 00:11:10 +00001407 E2 = TmpStore.end(); I2 != E2; ++I2) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001408 ExplodedNode *predNew = *I2;
Ted Kremenek1670e402009-04-11 00:11:10 +00001409 const GRState *stateNew = predNew->getState();
1410 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek8e029342009-08-27 22:17:37 +00001411 Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res));
Ted Kremenek1670e402009-04-11 00:11:10 +00001412 }
1413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Ted Kremenek1670e402009-04-11 00:11:10 +00001415 // Were they not equal?
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001416 if (const GRState *stateNotEqual = stateLoad->Assume(Cmp, false)) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001417 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek8e029342009-08-27 22:17:37 +00001418 Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res));
Ted Kremenek1670e402009-04-11 00:11:10 +00001419 }
1420 }
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Ted Kremenek1670e402009-04-11 00:11:10 +00001422 return true;
1423}
1424
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001425static bool EvalOSAtomic(ExplodedNodeSet& Dst,
Ted Kremenek1670e402009-04-11 00:11:10 +00001426 GRExprEngine& Engine,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001427 GRStmtNodeBuilder& Builder,
Ted Kremenek1670e402009-04-11 00:11:10 +00001428 CallExpr* CE, SVal L,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001429 ExplodedNode* Pred) {
Zhongxing Xu369f4472009-04-20 05:24:46 +00001430 const FunctionDecl* FD = L.getAsFunctionDecl();
1431 if (!FD)
Ted Kremenek1670e402009-04-11 00:11:10 +00001432 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +00001433
Ted Kremenek1670e402009-04-11 00:11:10 +00001434 const char *FName = FD->getNameAsCString();
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Ted Kremenek1670e402009-04-11 00:11:10 +00001436 // Check for compare and swap.
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001437 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1438 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek1670e402009-04-11 00:11:10 +00001439 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001440
Ted Kremenek1670e402009-04-11 00:11:10 +00001441 // FIXME: Other atomics.
1442 return false;
1443}
1444
1445//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001446// Transfer function: Function calls.
1447//===----------------------------------------------------------------------===//
Zhongxing Xu326d6172009-09-04 02:13:36 +00001448static void MarkNoReturnFunction(const FunctionDecl *FD, CallExpr *CE,
Mike Stump1eb44332009-09-09 15:08:12 +00001449 const GRState *state,
Zhongxing Xu326d6172009-09-04 02:13:36 +00001450 GRStmtNodeBuilder *Builder) {
Zhongxing Xu4e71ddb2009-09-04 02:17:35 +00001451 if (!FD)
1452 return;
1453
Mike Stump1eb44332009-09-09 15:08:12 +00001454 if (FD->getAttr<NoReturnAttr>() ||
Zhongxing Xu326d6172009-09-04 02:13:36 +00001455 FD->getAttr<AnalyzerNoReturnAttr>())
1456 Builder->BuildSinks = true;
1457 else {
1458 // HACK: Some functions are not marked noreturn, and don't return.
1459 // Here are a few hardwired ones. If this takes too long, we can
1460 // potentially cache these results.
1461 const char* s = FD->getIdentifier()->getName();
1462 unsigned n = strlen(s);
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Zhongxing Xu326d6172009-09-04 02:13:36 +00001464 switch (n) {
1465 default:
1466 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Zhongxing Xu326d6172009-09-04 02:13:36 +00001468 case 4:
1469 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1470 break;
1471
1472 case 5:
1473 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1474 else if (!memcmp(s, "error", 5)) {
1475 if (CE->getNumArgs() > 0) {
1476 SVal X = state->getSVal(*CE->arg_begin());
1477 // FIXME: use Assume to inspect the possible symbolic value of
1478 // X. Also check the specific signature of error().
1479 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
1480 if (CI && CI->getValue() != 0)
1481 Builder->BuildSinks = true;
1482 }
1483 }
1484 break;
1485
1486 case 6:
1487 if (!memcmp(s, "Assert", 6)) {
1488 Builder->BuildSinks = true;
1489 break;
1490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Zhongxing Xu326d6172009-09-04 02:13:36 +00001492 // FIXME: This is just a wrapper around throwing an exception.
1493 // Eventually inter-procedural analysis should handle this easily.
1494 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1495
1496 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Zhongxing Xu326d6172009-09-04 02:13:36 +00001498 case 7:
1499 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1500 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Zhongxing Xu326d6172009-09-04 02:13:36 +00001502 case 8:
Mike Stump1eb44332009-09-09 15:08:12 +00001503 if (!memcmp(s ,"db_error", 8) ||
Zhongxing Xu326d6172009-09-04 02:13:36 +00001504 !memcmp(s, "__assert", 8))
1505 Builder->BuildSinks = true;
1506 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Zhongxing Xu326d6172009-09-04 02:13:36 +00001508 case 12:
1509 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1510 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Zhongxing Xu326d6172009-09-04 02:13:36 +00001512 case 13:
1513 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1514 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Zhongxing Xu326d6172009-09-04 02:13:36 +00001516 case 14:
1517 if (!memcmp(s, "dtrace_assfail", 14) ||
1518 !memcmp(s, "yy_fatal_error", 14))
1519 Builder->BuildSinks = true;
1520 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Zhongxing Xu326d6172009-09-04 02:13:36 +00001522 case 26:
1523 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1524 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1525 !memcmp(s, "_TSAssertionFailureHandler", 26))
1526 Builder->BuildSinks = true;
1527
1528 break;
1529 }
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Zhongxing Xu326d6172009-09-04 02:13:36 +00001531 }
1532}
Ted Kremenek1670e402009-04-11 00:11:10 +00001533
Zhongxing Xu248072a2009-09-05 05:00:57 +00001534bool GRExprEngine::EvalBuiltinFunction(const FunctionDecl *FD, CallExpr *CE,
1535 ExplodedNode *Pred,
1536 ExplodedNodeSet &Dst) {
1537 if (!FD)
1538 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001540 unsigned id = FD->getBuiltinID();
Zhongxing Xu248072a2009-09-05 05:00:57 +00001541 if (!id)
1542 return false;
1543
1544 const GRState *state = Pred->getState();
1545
1546 switch (id) {
1547 case Builtin::BI__builtin_expect: {
1548 // For __builtin_expect, just return the value of the subexpression.
Mike Stump1eb44332009-09-09 15:08:12 +00001549 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu248072a2009-09-05 05:00:57 +00001550 SVal X = state->getSVal(*(CE->arg_begin()));
1551 MakeNode(Dst, CE, Pred, state->BindExpr(CE, X));
1552 return true;
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Zhongxing Xu248072a2009-09-05 05:00:57 +00001555 case Builtin::BI__builtin_alloca: {
1556 // FIXME: Refactor into StoreManager itself?
1557 MemRegionManager& RM = getStateManager().getRegionManager();
1558 const MemRegion* R =
1559 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Zhongxing Xu248072a2009-09-05 05:00:57 +00001561 // Set the extent of the region in bytes. This enables us to use the
1562 // SVal of the argument directly. If we save the extent in bits, we
1563 // cannot represent values like symbol*8.
1564 SVal Extent = state->getSVal(*(CE->arg_begin()));
1565 state = getStoreManager().setExtent(state, R, Extent);
1566 MakeNode(Dst, CE, Pred, state->BindExpr(CE, loc::MemRegionVal(R)));
1567 return true;
1568 }
1569 }
1570
1571 return false;
1572}
1573
Mike Stump1eb44332009-09-09 15:08:12 +00001574void GRExprEngine::EvalCall(ExplodedNodeSet& Dst, CallExpr* CE, SVal L,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001575 ExplodedNode* Pred) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001576 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenek1670e402009-04-11 00:11:10 +00001578 // FIXME: Allow us to chain together transfer functions.
1579 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1580 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremenek1670e402009-04-11 00:11:10 +00001582 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1583}
1584
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001585void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001586 CallExpr::arg_iterator AI,
1587 CallExpr::arg_iterator AE,
Mike Stump1eb44332009-09-09 15:08:12 +00001588 ExplodedNodeSet& Dst) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001589 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001590 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001591 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001592 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
John McCall183700f2009-09-21 23:43:11 +00001593 Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001594
1595 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1596}
1597
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001598void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001599 CallExpr::arg_iterator AI,
1600 CallExpr::arg_iterator AE,
Mike Stump1eb44332009-09-09 15:08:12 +00001601 ExplodedNodeSet& Dst,
1602 const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001603 unsigned ParamIdx) {
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001605 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001606 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001607 // If the call argument is being bound to a reference parameter,
1608 // visit it as an lvalue, not an rvalue.
1609 bool VisitAsLvalue = false;
1610 if (Proto && ParamIdx < Proto->getNumArgs())
1611 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1612
Mike Stump1eb44332009-09-09 15:08:12 +00001613 ExplodedNodeSet DstTmp;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001614 if (VisitAsLvalue)
Mike Stump1eb44332009-09-09 15:08:12 +00001615 VisitLValue(*AI, Pred, DstTmp);
Douglas Gregor9d293df2008-10-28 00:22:11 +00001616 else
Mike Stump1eb44332009-09-09 15:08:12 +00001617 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001618 ++AI;
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001620 for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE;
1621 ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001622 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenekde434242008-02-19 01:44:53 +00001624 return;
1625 }
1626
1627 // If we reach here we have processed all of the arguments. Evaluate
1628 // the callee expression.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001629 ExplodedNodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001630 Expr* Callee = CE->getCallee()->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek5a5d98b2009-07-22 21:43:51 +00001632 { // Enter new scope to make the lifetime of 'DstTmp2' bounded.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001633 ExplodedNodeSet DstTmp2;
Ted Kremenek5a5d98b2009-07-22 21:43:51 +00001634 Visit(Callee, Pred, DstTmp2);
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Ted Kremenek5a5d98b2009-07-22 21:43:51 +00001636 // Perform the previsit of the CallExpr, storing the results in DstTmp.
1637 CheckerVisit(CE, DstTmp, DstTmp2, true);
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Ted Kremenekde434242008-02-19 01:44:53 +00001640 // Finally, evaluate the function call.
Mike Stump1eb44332009-09-09 15:08:12 +00001641 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu248072a2009-09-05 05:00:57 +00001642 DI != DE; ++DI) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001643
Ted Kremeneka8538d92009-02-13 01:45:31 +00001644 const GRState* state = GetState(*DI);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001645 SVal L = state->getSVal(Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001646
Ted Kremeneka1354a52008-03-03 16:47:31 +00001647 // FIXME: Add support for symbolic function calls (calls involving
1648 // function pointer values that are symbolic).
Zhongxing Xud99f3612009-09-02 08:10:35 +00001649
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001650 // Check for the "noreturn" attribute.
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001652 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xu369f4472009-04-20 05:24:46 +00001653 const FunctionDecl* FD = L.getAsFunctionDecl();
Zhongxing Xu4e71ddb2009-09-04 02:17:35 +00001654
1655 MarkNoReturnFunction(FD, CE, state, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001657 // Evaluate the call.
Zhongxing Xuaeba2842009-09-05 06:46:12 +00001658 if (EvalBuiltinFunction(FD, CE, *DI, Dst))
Zhongxing Xu248072a2009-09-05 05:00:57 +00001659 continue;
Ted Kremenek186350f2008-04-23 20:12:28 +00001660
Mike Stump1eb44332009-09-09 15:08:12 +00001661 // Dispatch to the plug-in transfer function.
1662
Ted Kremenek186350f2008-04-23 20:12:28 +00001663 unsigned size = Dst.size();
1664 SaveOr OldHasGen(Builder->HasGeneratedNode);
1665 EvalCall(Dst, CE, L, *DI);
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Ted Kremenek186350f2008-04-23 20:12:28 +00001667 // Handle the case where no nodes where generated. Auto-generate that
1668 // contains the updated state if we aren't generating sinks.
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Ted Kremenek186350f2008-04-23 20:12:28 +00001670 if (!Builder->BuildSinks && Dst.size() == size &&
1671 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001672 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001673 }
1674}
1675
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001676//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001677// Transfer function: Objective-C ivar references.
1678//===----------------------------------------------------------------------===//
1679
Ted Kremenekf5cae632009-02-28 20:50:43 +00001680static std::pair<const void*,const void*> EagerlyAssumeTag
1681 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1682
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001683void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
1684 Expr *Ex) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001685 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1686 ExplodedNode *Pred = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Ted Kremenekb2939022009-02-25 23:32:10 +00001688 // Test if the previous node was as the same expression. This can happen
1689 // when the expression fails to evaluate to anything meaningful and
1690 // (as an optimization) we don't generate a node.
Mike Stump1eb44332009-09-09 15:08:12 +00001691 ProgramPoint P = Pred->getLocation();
Ted Kremenekb2939022009-02-25 23:32:10 +00001692 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump1eb44332009-09-09 15:08:12 +00001693 Dst.Add(Pred);
Ted Kremenekb2939022009-02-25 23:32:10 +00001694 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001695 }
Ted Kremenekb2939022009-02-25 23:32:10 +00001696
Mike Stump1eb44332009-09-09 15:08:12 +00001697 const GRState* state = Pred->getState();
1698 SVal V = state->getSVal(Ex);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001699 if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001700 // First assume that the condition is true.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001701 if (const GRState *stateTrue = state->Assume(*SEV, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001702 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek0fb0bc42009-08-27 01:39:13 +00001703 ValMgr.makeIntVal(1U, Ex->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001704 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xu25e695b2009-08-15 03:17:38 +00001705 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001706 stateTrue, Pred));
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Ted Kremenek48af2a92009-02-25 22:32:02 +00001709 // Next, assume that the condition is false.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001710 if (const GRState *stateFalse = state->Assume(*SEV, false)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001711 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek0fb0bc42009-08-27 01:39:13 +00001712 ValMgr.makeIntVal(0U, Ex->getType()));
Zhongxing Xu25e695b2009-08-15 03:17:38 +00001713 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
1714 Pred->getLocationContext()),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001715 stateFalse, Pred));
1716 }
1717 }
1718 else
1719 Dst.Add(Pred);
1720 }
1721}
1722
1723//===----------------------------------------------------------------------===//
1724// Transfer function: Objective-C ivar references.
1725//===----------------------------------------------------------------------===//
1726
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001727void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, ExplodedNode* Pred,
1728 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001730 Expr* Base = cast<Expr>(Ex->getBase());
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001731 ExplodedNodeSet Tmp;
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001732 Visit(Base, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001734 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001735 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001736 SVal BaseVal = state->getSVal(Base);
1737 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001739 if (asLValue)
Ted Kremenek8e029342009-08-27 22:17:37 +00001740 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001741 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001742 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001743 }
1744}
1745
1746//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001747// Transfer function: Objective-C fast enumeration 'for' statements.
1748//===----------------------------------------------------------------------===//
1749
1750void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001751 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Ted Kremenekaf337412008-11-12 19:24:17 +00001753 // ObjCForCollectionStmts are processed in two places. This method
1754 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1755 // statements within a basic block. This transfer function does two things:
1756 //
1757 // (1) binds the next container value to 'element'. This creates a new
1758 // node in the ExplodedGraph.
1759 //
1760 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1761 // whether or not the container has any more elements. This value
1762 // will be tested in ProcessBranch. We need to explicitly bind
1763 // this value because a container can contain nil elements.
Mike Stump1eb44332009-09-09 15:08:12 +00001764 //
Ted Kremenekaf337412008-11-12 19:24:17 +00001765 // FIXME: Eventually this logic should actually do dispatches to
1766 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1767 // This will require simulating a temporary NSFastEnumerationState, either
1768 // through an SVal or through the use of MemRegions. This value can
1769 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1770 // terminates we reclaim the temporary (it goes out of scope) and we
1771 // we can test if the SVal is 0 or if the MemRegion is null (depending
1772 // on what approach we take).
1773 //
1774 // For now: simulate (1) by assigning either a symbol or nil if the
1775 // container is empty. Thus this transfer function will by default
1776 // result in state splitting.
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001778 Stmt* elem = S->getElement();
1779 SVal ElementV;
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Ted Kremenekaf337412008-11-12 19:24:17 +00001781 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001782 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001783 assert (ElemD->getInit() == 0);
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001784 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001785 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1786 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001787 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001788
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001789 ExplodedNodeSet Tmp;
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001790 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001792 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001793 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001794 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001795 }
1796}
1797
1798void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001799 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001800 SVal ElementV) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001801
Mike Stump1eb44332009-09-09 15:08:12 +00001802
1803
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001804 // Get the current state. Use 'EvalLocation' to determine if it is a null
1805 // pointer, etc.
1806 Stmt* elem = S->getElement();
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremenek8c354752008-12-16 22:02:27 +00001808 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1809 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001810 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenekb65be702009-06-18 01:23:53 +00001812 const GRState *state = GetState(Pred);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001813
Ted Kremenekaf337412008-11-12 19:24:17 +00001814 // Handle the case where the container still has elements.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001815 SVal TrueV = ValMgr.makeTruthVal(1);
Ted Kremenek8e029342009-08-27 22:17:37 +00001816 const GRState *hasElems = state->BindExpr(S, TrueV);
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Ted Kremenekaf337412008-11-12 19:24:17 +00001818 // Handle the case where the container has no elements.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001819 SVal FalseV = ValMgr.makeTruthVal(0);
Ted Kremenek8e029342009-08-27 22:17:37 +00001820 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001822 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1823 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1824 // FIXME: The proper thing to do is to really iterate over the
1825 // container. We will do this with dispatch logic to the store.
1826 // For now, just 'conjure' up a symbolic value.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001827 QualType T = R->getValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001828 assert (Loc::IsLocType(T));
1829 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001830 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
Zhongxing Xud91ee272009-06-23 09:02:15 +00001831 SVal V = ValMgr.makeLoc(Sym);
Ted Kremenekb65be702009-06-18 01:23:53 +00001832 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001833
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001834 // Bind the location to 'nil' on the false branch.
Mike Stump1eb44332009-09-09 15:08:12 +00001835 SVal nilV = ValMgr.makeIntVal(0, T);
1836 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001839 // Create the new nodes.
1840 MakeNode(Dst, S, Pred, hasElems);
1841 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001842}
1843
1844//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001845// Transfer function: Objective-C message expressions.
1846//===----------------------------------------------------------------------===//
1847
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001848void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
1849 ExplodedNodeSet& Dst){
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001851 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1852 Pred, Dst);
Mike Stump1eb44332009-09-09 15:08:12 +00001853}
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001854
1855void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001856 ObjCMessageExpr::arg_iterator AI,
1857 ObjCMessageExpr::arg_iterator AE,
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001858 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001859 if (AI == AE) {
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001861 // Process the receiver.
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001863 if (Expr* Receiver = ME->getReceiver()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001864 ExplodedNodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001865 Visit(Receiver, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001867 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE;
1868 ++NI)
Daniel Dunbar36292552009-07-23 04:41:06 +00001869 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001871 return;
1872 }
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Daniel Dunbar36292552009-07-23 04:41:06 +00001874 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001875 return;
1876 }
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001878 ExplodedNodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001879 Visit(*AI, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001881 ++AI;
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001883 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001884 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1885}
1886
1887void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001888 ExplodedNode* Pred,
1889 ExplodedNodeSet& Dst) {
Mike Stump1eb44332009-09-09 15:08:12 +00001890
1891 // FIXME: More logic for the processing the method call.
1892
Ted Kremeneka8538d92009-02-13 01:45:31 +00001893 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001894 bool RaisesException = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
1896
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001897 if (Expr* Receiver = ME->getReceiver()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001899 SVal L_untested = state->getSVal(Receiver);
Mike Stump1eb44332009-09-09 15:08:12 +00001900
1901 // Check for undefined control-flow.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001902 if (L_untested.isUndef()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001903 ExplodedNode* N = Builder->generateNode(ME, state, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001905 if (N) {
1906 N->markAsSink();
1907 UndefReceivers.insert(N);
1908 }
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001910 return;
1911 }
Mike Stump1eb44332009-09-09 15:08:12 +00001912
1913 // "Assume" that the receiver is not NULL.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001914 DefinedOrUnknownSVal L = cast<DefinedOrUnknownSVal>(L_untested);
1915 const GRState *StNotNull = state->Assume(L, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001916
1917 // "Assume" that the receiver is NULL.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001918 const GRState *StNull = state->Assume(L, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Ted Kremeneka591bc02009-06-18 22:57:13 +00001920 if (StNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001921 QualType RetTy = ME->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Ted Kremenek21fe8372009-02-19 04:06:22 +00001923 // Check if the receiver was nil and the return value a struct.
Mike Stump1eb44332009-09-09 15:08:12 +00001924 if (RetTy->isRecordType()) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001925 if (Pred->getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001926 // The [0 ...] expressions will return garbage. Flag either an
1927 // explicit or implicit error. Because of the structure of this
1928 // function we currently do not bifurfacte the state graph at
1929 // this point.
1930 // FIXME: We should bifurcate and fill the returned struct with
Mike Stump1eb44332009-09-09 15:08:12 +00001931 // garbage.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001932 if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001933 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001934 if (StNotNull)
Ted Kremenek899b3de2009-04-08 03:07:17 +00001935 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001936 else
Mike Stump1eb44332009-09-09 15:08:12 +00001937 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001938 }
1939 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001940 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001941 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001942 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001943 if (RetTy != Ctx.VoidTy) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +00001944 if (Pred->getParentMap().isConsumedExpr(ME)) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001945 // sizeof(void *)
1946 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1947 // sizeof(return type)
1948 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001949
Mike Stump1eb44332009-09-09 15:08:12 +00001950 if (voidPtrSize < returnTypeSize) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001951 if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001952 N->markAsSink();
Mike Stump1eb44332009-09-09 15:08:12 +00001953 if (StNotNull)
Ted Kremenekfe630b92009-04-09 05:45:56 +00001954 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001955 else
Mike Stump1eb44332009-09-09 15:08:12 +00001956 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001957 }
1958 }
Ted Kremeneka591bc02009-06-18 22:57:13 +00001959 else if (!StNotNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001960 // Handle the safe cases where the return value is 0 if the
1961 // receiver is nil.
1962 //
1963 // FIXME: For now take the conservative approach that we only
1964 // return null values if we *know* that the receiver is nil.
1965 // This is because we can have surprises like:
1966 //
1967 // ... = [[NSScreens screens] objectAtIndex:0];
1968 //
1969 // What can happen is that [... screens] could return nil, but
1970 // it most likely isn't nil. We should assume the semantics
1971 // of this case unless we have *a lot* more knowledge.
1972 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001973 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenek8e029342009-08-27 22:17:37 +00001974 MakeNode(Dst, ME, Pred, StNull->BindExpr(ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001975 return;
1976 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001977 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001978 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001979 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001980 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001981 // of this method should assume that the receiver is not nil.
1982 if (!StNotNull)
1983 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Ted Kremenekda9ae602009-04-08 18:51:08 +00001985 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001986 }
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Ted Kremeneke448ab42008-05-01 18:33:28 +00001988 // Check if the "raise" message was sent.
1989 if (ME->getSelector() == RaiseSel)
1990 RaisesException = true;
1991 }
1992 else {
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Ted Kremeneke448ab42008-05-01 18:33:28 +00001994 IdentifierInfo* ClsName = ME->getClassName();
1995 Selector S = ME->getSelector();
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Ted Kremeneke448ab42008-05-01 18:33:28 +00001997 // Check for special instance methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001998
1999 if (!NSExceptionII) {
Ted Kremeneke448ab42008-05-01 18:33:28 +00002000 ASTContext& Ctx = getContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Ted Kremeneke448ab42008-05-01 18:33:28 +00002002 NSExceptionII = &Ctx.Idents.get("NSException");
2003 }
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Ted Kremeneke448ab42008-05-01 18:33:28 +00002005 if (ClsName == NSExceptionII) {
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Ted Kremeneke448ab42008-05-01 18:33:28 +00002007 enum { NUM_RAISE_SELECTORS = 2 };
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Ted Kremeneke448ab42008-05-01 18:33:28 +00002009 // Lazily create a cache of the selectors.
2010
2011 if (!NSExceptionInstanceRaiseSelectors) {
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Ted Kremeneke448ab42008-05-01 18:33:28 +00002013 ASTContext& Ctx = getContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Ted Kremeneke448ab42008-05-01 18:33:28 +00002015 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Ted Kremeneke448ab42008-05-01 18:33:28 +00002017 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
2018 unsigned idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
2020 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00002021 II.push_back(&Ctx.Idents.get("raise"));
Mike Stump1eb44332009-09-09 15:08:12 +00002022 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00002023 NSExceptionInstanceRaiseSelectors[idx++] =
Mike Stump1eb44332009-09-09 15:08:12 +00002024 Ctx.Selectors.getSelector(II.size(), &II[0]);
2025
2026 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00002027 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00002028 NSExceptionInstanceRaiseSelectors[idx++] =
2029 Ctx.Selectors.getSelector(II.size(), &II[0]);
2030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Ted Kremeneke448ab42008-05-01 18:33:28 +00002032 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2033 if (S == NSExceptionInstanceRaiseSelectors[i]) {
2034 RaisesException = true; break;
2035 }
2036 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002037 }
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002039 // Check for any arguments that are uninitialized/undefined.
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002041 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2042 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Ted Kremenek3ff12592009-06-19 17:10:32 +00002044 if (state->getSVal(*I).isUndef()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002046 // Generate an error node for passing an uninitialized/undefined value
2047 // as an argument to a message expression. This node is a sink.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002048 ExplodedNode* N = Builder->generateNode(ME, state, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002050 if (N) {
2051 N->markAsSink();
2052 MsgExprUndefArgs[N] = *I;
2053 }
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002055 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002056 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00002057 }
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Ted Kremeneke448ab42008-05-01 18:33:28 +00002059 // Check if we raise an exception. For now treat these as sinks. Eventually
2060 // we will want to handle exceptions properly.
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Ted Kremeneke448ab42008-05-01 18:33:28 +00002062 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2063
2064 if (RaisesException)
2065 Builder->BuildSinks = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002067 // Dispatch to plug-in transfer function.
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002069 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00002070 SaveOr OldHasGen(Builder->HasGeneratedNode);
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002072 EvalObjCMessageExpr(Dst, ME, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002074 // Handle the case where no nodes where generated. Auto-generate that
2075 // contains the updated state if we aren't generating sinks.
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Ted Kremenekb0533962008-04-18 20:35:30 +00002077 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002078 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002079}
2080
2081//===----------------------------------------------------------------------===//
2082// Transfer functions: Miscellaneous statements.
2083//===----------------------------------------------------------------------===//
2084
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002085void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred, ExplodedNodeSet& Dst){
2086 ExplodedNodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002087 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002088 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002089
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002090 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002091 T = ExCast->getTypeAsWritten();
2092
Zhongxing Xued340f72008-10-22 08:02:16 +00002093 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002094 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002095 else
2096 Visit(Ex, Pred, S1);
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002098 // Check for casting to "void".
Mike Stump1eb44332009-09-09 15:08:12 +00002099 if (T->isVoidType()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002100 for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002101 Dst.Add(*I1);
2102
Ted Kremenek874d63f2008-01-24 02:02:54 +00002103 return;
2104 }
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002105
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002106 for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
2107 ExplodedNode* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002108 const GRState* state = GetState(N);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002109 SVal V = state->getSVal(Ex);
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002110 const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
Ted Kremenek8e029342009-08-27 22:17:37 +00002111 state = Res.getState()->BindExpr(CastE, Res.getSVal());
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002112 MakeNode(Dst, CastE, N, state);
Ted Kremenek874d63f2008-01-24 02:02:54 +00002113 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002114}
2115
Ted Kremenek4f090272008-10-27 21:54:31 +00002116void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Mike Stump1eb44332009-09-09 15:08:12 +00002117 ExplodedNode* Pred,
2118 ExplodedNodeSet& Dst,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002119 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002120 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002121 ExplodedNodeSet Tmp;
Ted Kremenek4f090272008-10-27 21:54:31 +00002122 Visit(ILE, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002124 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002125 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002126 SVal ILV = state->getSVal(ILE);
2127 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002128
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002129 if (asLValue)
Ted Kremenek8e029342009-08-27 22:17:37 +00002130 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002131 else
Ted Kremenek8e029342009-08-27 22:17:37 +00002132 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002133 }
2134}
2135
Ted Kremenekd17da2b2009-08-21 22:28:32 +00002136void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
Mike Stump1eb44332009-09-09 15:08:12 +00002137 ExplodedNodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002138
Mike Stump1eb44332009-09-09 15:08:12 +00002139 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002140 Decl* D = *DS->decl_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002142 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002143 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002144
2145 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002146 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002147
2148 // FIXME: static variables may have an initializer, but the second
2149 // time a function is called those values may not be current.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002150 ExplodedNodeSet Tmp;
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002151
Ted Kremenekaf337412008-11-12 19:24:17 +00002152 if (InitEx)
2153 Visit(InitEx, Pred, Tmp);
Ted Kremenek1152fc02009-07-17 23:48:26 +00002154 else
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002155 Tmp.Add(Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002157 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002158 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002159 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002160
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002161 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2162 QualType T = getContext().getCanonicalType(VD->getType());
2163 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2164 // FIXME: Handle multi-dimensional VLAs.
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002166 Expr* SE = VLA->getSizeExpr();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002167 SVal Size_untested = state->getSVal(SE);
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002169 if (Size_untested.isUndef()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002170 if (ExplodedNode* N = Builder->generateNode(DS, state, Pred)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002171 N->markAsSink();
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002172 ExplicitBadSizedVLA.insert(N);
2173 }
2174 continue;
2175 }
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002177 DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Size_untested);
2178 const GRState *zeroState = state->Assume(Size, false);
2179 state = state->Assume(Size, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Ted Kremeneka591bc02009-06-18 22:57:13 +00002181 if (zeroState) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002182 if (ExplodedNode* N = Builder->generateNode(DS, zeroState, Pred)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002183 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00002184 if (state)
2185 ImplicitBadSizedVLA.insert(N);
2186 else
2187 ExplicitBadSizedVLA.insert(N);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002188 }
2189 }
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Ted Kremeneka591bc02009-06-18 22:57:13 +00002191 if (!state)
Mike Stump1eb44332009-09-09 15:08:12 +00002192 continue;
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002193 }
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002195 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekd17da2b2009-08-21 22:28:32 +00002196 const LocationContext *LC = (*I)->getLocationContext();
2197
Ted Kremenekaf337412008-11-12 19:24:17 +00002198 if (InitEx) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002199 SVal InitVal = state->getSVal(InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002200 QualType T = VD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Ted Kremenekaf337412008-11-12 19:24:17 +00002202 // Recover some path-sensitivity if a scalar value evaluated to
2203 // UnknownVal.
Mike Stump1eb44332009-09-09 15:08:12 +00002204 if (InitVal.isUnknown() ||
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002205 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek87806792009-09-27 20:45:21 +00002206 InitVal = ValMgr.getConjuredSymbolVal(NULL, InitEx, Count);
Mike Stump1eb44332009-09-09 15:08:12 +00002207 }
2208
Ted Kremenekd17da2b2009-08-21 22:28:32 +00002209 state = state->bindDecl(VD, LC, InitVal);
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002211 // The next thing to do is check if the GRTransferFuncs object wants to
2212 // update the state based on the new binding. If the GRTransferFunc
2213 // object doesn't do anything, just auto-propagate the current state.
2214 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
Ted Kremenekd17da2b2009-08-21 22:28:32 +00002215 getTF().EvalBind(BuilderRef, loc::MemRegionVal(state->getRegion(VD, LC)),
Mike Stump1eb44332009-09-09 15:08:12 +00002216 InitVal);
2217 }
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002218 else {
Ted Kremenekd17da2b2009-08-21 22:28:32 +00002219 state = state->bindDeclWithNoInit(VD, LC);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002220 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002221 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002222 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002223}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002224
Ted Kremenekf75b1862008-10-30 17:47:32 +00002225namespace {
2226 // This class is used by VisitInitListExpr as an item in a worklist
2227 // for processing the values contained in an InitListExpr.
2228class VISIBILITY_HIDDEN InitListWLItem {
2229public:
2230 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002231 ExplodedNode* N;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002232 InitListExpr::reverse_iterator Itr;
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002234 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
2235 InitListExpr::reverse_iterator itr)
Ted Kremenekf75b1862008-10-30 17:47:32 +00002236 : Vals(vals), N(n), Itr(itr) {}
2237};
2238}
2239
2240
Mike Stump1eb44332009-09-09 15:08:12 +00002241void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002242 ExplodedNodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002243
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002244 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002245 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002246 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002247
Ted Kremenekf3bfa212009-07-28 20:46:55 +00002248 if (T->isArrayType() || T->isStructureType() ||
2249 T->isUnionType() || T->isVectorType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002250
Ted Kremeneka49e3672008-10-30 23:14:36 +00002251 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Ted Kremeneka49e3672008-10-30 23:14:36 +00002253 // Handle base case where the initializer has no elements.
2254 // e.g: static int* myArray[] = {};
2255 if (NumInitElements == 0) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00002256 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek8e029342009-08-27 22:17:37 +00002257 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremeneka49e3672008-10-30 23:14:36 +00002258 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002259 }
2260
Ted Kremeneka49e3672008-10-30 23:14:36 +00002261 // Create a worklist to process the initializers.
2262 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump1eb44332009-09-09 15:08:12 +00002263 WorkList.reserve(NumInitElements);
2264 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002265 InitListExpr::reverse_iterator ItrEnd = E->rend();
Ted Kremenekcf549592009-09-22 21:19:14 +00002266 assert(!(E->rbegin() == E->rend()));
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Ted Kremeneka49e3672008-10-30 23:14:36 +00002268 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002269 while (!WorkList.empty()) {
2270 InitListWLItem X = WorkList.back();
2271 WorkList.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00002272
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002273 ExplodedNodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002274 Visit(*X.Itr, X.N, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Ted Kremenekf75b1862008-10-30 17:47:32 +00002276 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002277
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002278 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002279 // Get the last initializer value.
2280 state = GetState(*NI);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002281 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Ted Kremenekf75b1862008-10-30 17:47:32 +00002283 // Construct the new list of values by prepending the new value to
2284 // the already constructed list.
2285 llvm::ImmutableList<SVal> NewVals =
2286 getBasicVals().consVals(InitV, X.Vals);
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Ted Kremenekf75b1862008-10-30 17:47:32 +00002288 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002289 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xud91ee272009-06-23 09:02:15 +00002290 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002291
Ted Kremenekf75b1862008-10-30 17:47:32 +00002292 // Make final state and node.
Ted Kremenek8e029342009-08-27 22:17:37 +00002293 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002294 }
2295 else {
2296 // Still some initializer values to go. Push them onto the worklist.
2297 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2298 }
2299 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002300 }
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Ted Kremenek87903072008-10-30 18:34:31 +00002302 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002303 }
2304
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002305 if (Loc::IsLocType(T) || T->isIntegerType()) {
2306 assert (E->getNumInits() == 1);
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002307 ExplodedNodeSet Tmp;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002308 Expr* Init = E->getInit(0);
2309 Visit(Init, Pred, Tmp);
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002310 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002311 state = GetState(*I);
Ted Kremenek8e029342009-08-27 22:17:37 +00002312 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002313 }
2314 return;
2315 }
2316
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002317
2318 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2319 assert(0 && "unprocessed InitListExpr type");
2320}
Ted Kremenekf233d482008-02-05 00:26:40 +00002321
Sebastian Redl05189992008-11-11 17:56:53 +00002322/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2323void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002324 ExplodedNode* Pred,
2325 ExplodedNodeSet& Dst) {
Sebastian Redl05189992008-11-11 17:56:53 +00002326 QualType T = Ex->getTypeOfArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00002327 uint64_t amt;
2328
Ted Kremenek87e80342008-03-15 03:13:20 +00002329 if (Ex->isSizeOf()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002330 if (T == getContext().VoidTy) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002331 // sizeof(void) == 1 byte.
2332 amt = 1;
2333 }
2334 else if (!T.getTypePtr()->isConstantSizeType()) {
2335 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002336 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002337 }
2338 else if (T->isObjCInterfaceType()) {
2339 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2340 // the compiler has laid out its representation. Just report Unknown
Mike Stump1eb44332009-09-09 15:08:12 +00002341 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002342 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002343 }
2344 else {
2345 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002346 amt = getContext().getTypeSize(T) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +00002347 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002348 }
2349 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002350 amt = getContext().getTypeAlign(T) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Ted Kremenek0e561a32008-03-21 21:30:14 +00002352 MakeNode(Dst, Ex, Pred,
Ted Kremenek8e029342009-08-27 22:17:37 +00002353 GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002354}
2355
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002356
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002357void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
2358 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002359
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002360 switch (U->getOpcode()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002362 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002363 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002365 case UnaryOperator::Deref: {
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002367 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002368 ExplodedNodeSet Tmp;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002369 Visit(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002371 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Ted Kremeneka8538d92009-02-13 01:45:31 +00002373 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002374 SVal location = state->getSVal(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002376 if (asLValue)
Ted Kremenek8e029342009-08-27 22:17:37 +00002377 MakeNode(Dst, U, *I, state->BindExpr(U, location),
Ted Kremenek7090d542009-05-07 18:27:16 +00002378 ProgramPoint::PostLValueKind);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002379 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002380 EvalLoad(Dst, U, *I, state, location);
Mike Stump1eb44332009-09-09 15:08:12 +00002381 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002382
2383 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002384 }
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002386 case UnaryOperator::Real: {
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002388 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002389 ExplodedNodeSet Tmp;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002390 Visit(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002392 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002394 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002395 if (Ex->getType()->isAnyComplexType()) {
2396 // Just report "Unknown."
2397 Dst.Add(*I);
2398 continue;
2399 }
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002401 // For all other types, UnaryOperator::Real is an identity operation.
2402 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002403 const GRState* state = GetState(*I);
Ted Kremenek8e029342009-08-27 22:17:37 +00002404 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump1eb44332009-09-09 15:08:12 +00002405 }
2406
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002407 return;
2408 }
Mike Stump1eb44332009-09-09 15:08:12 +00002409
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002410 case UnaryOperator::Imag: {
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002412 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002413 ExplodedNodeSet Tmp;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002414 Visit(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002416 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002417 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002418 if (Ex->getType()->isAnyComplexType()) {
2419 // Just report "Unknown."
2420 Dst.Add(*I);
2421 continue;
2422 }
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002424 // For all other types, UnaryOperator::Float returns 0.
2425 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002426 const GRState* state = GetState(*I);
Zhongxing Xud91ee272009-06-23 09:02:15 +00002427 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek8e029342009-08-27 22:17:37 +00002428 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002429 }
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002431 return;
2432 }
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Ted Kremeneke2b57442009-09-15 00:40:32 +00002434 case UnaryOperator::OffsetOf: {
Ted Kremenekaab7efe2009-09-15 04:19:09 +00002435 Expr::EvalResult Res;
2436 if (U->Evaluate(Res, getContext()) && Res.Val.isInt()) {
2437 const APSInt &IV = Res.Val.getInt();
2438 assert(IV.getBitWidth() == getContext().getTypeSize(U->getType()));
2439 assert(U->getType()->isIntegerType());
2440 assert(IV.isSigned() == U->getType()->isSignedIntegerType());
2441 SVal X = ValMgr.makeIntVal(IV);
2442 MakeNode(Dst, U, Pred, GetState(Pred)->BindExpr(U, X));
2443 return;
2444 }
2445 // FIXME: Handle the case where __builtin_offsetof is not a constant.
2446 Dst.Add(Pred);
Ted Kremeneka084bb62008-04-30 21:45:55 +00002447 return;
Ted Kremeneke2b57442009-09-15 00:40:32 +00002448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002450 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002451 case UnaryOperator::Extension: {
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002453 // Unary "+" is a no-op, similar to a parentheses. We still have places
2454 // where it may be a block-level expression, so we need to
2455 // generate an extra node that just propagates the value of the
2456 // subexpression.
2457
2458 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002459 ExplodedNodeSet Tmp;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002460 Visit(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002461
2462 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002463 const GRState* state = GetState(*I);
Ted Kremenek8e029342009-08-27 22:17:37 +00002464 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002465 }
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002467 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002468 }
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002470 case UnaryOperator::AddrOf: {
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002472 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002473 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002474 ExplodedNodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002475 VisitLValue(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002476
2477 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002478 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002479 SVal V = state->getSVal(Ex);
Ted Kremenek8e029342009-08-27 22:17:37 +00002480 state = state->BindExpr(U, V);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002481 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002482 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002483
Mike Stump1eb44332009-09-09 15:08:12 +00002484 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002485 }
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002487 case UnaryOperator::LNot:
2488 case UnaryOperator::Minus:
2489 case UnaryOperator::Not: {
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002491 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002492 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002493 ExplodedNodeSet Tmp;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002494 Visit(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002495
2496 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002497 const GRState* state = GetState(*I);
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Ted Kremenek855cd902008-09-30 05:32:44 +00002499 // Get the value of the subexpression.
Ted Kremenek3ff12592009-06-19 17:10:32 +00002500 SVal V = state->getSVal(Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002501
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002502 if (V.isUnknownOrUndef()) {
Ted Kremenek8e029342009-08-27 22:17:37 +00002503 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002504 continue;
2505 }
Mike Stump1eb44332009-09-09 15:08:12 +00002506
Ted Kremenek60595da2008-11-15 04:01:56 +00002507// QualType DstT = getContext().getCanonicalType(U->getType());
2508// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002509//
Ted Kremenek60595da2008-11-15 04:01:56 +00002510// if (DstT != SrcT) // Perform promotions.
Mike Stump1eb44332009-09-09 15:08:12 +00002511// V = EvalCast(V, DstT);
2512//
Ted Kremenek60595da2008-11-15 04:01:56 +00002513// if (V.isUnknownOrUndef()) {
2514// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2515// continue;
2516// }
Mike Stump1eb44332009-09-09 15:08:12 +00002517
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002518 switch (U->getOpcode()) {
2519 default:
2520 assert(false && "Invalid Opcode.");
2521 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002523 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002524 // FIXME: Do we need to handle promotions?
Ted Kremenek8e029342009-08-27 22:17:37 +00002525 state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
Mike Stump1eb44332009-09-09 15:08:12 +00002526 break;
2527
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002528 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002529 // FIXME: Do we need to handle promotions?
Ted Kremenek8e029342009-08-27 22:17:37 +00002530 state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
Mike Stump1eb44332009-09-09 15:08:12 +00002531 break;
2532
2533 case UnaryOperator::LNot:
2534
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002535 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2536 //
2537 // Note: technically we do "E == 0", but this is the same in the
2538 // transfer functions as "0 == E".
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00002539 SVal Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002540
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002541 if (isa<Loc>(V)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00002542 Loc X = ValMgr.makeNull();
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00002543 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2544 U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002545 }
2546 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002547 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +00002548 Result = EvalBinOp(state, BinaryOperator::EQ, cast<NonLoc>(V), X,
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00002549 U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002550 }
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Ted Kremenek8e029342009-08-27 22:17:37 +00002552 state = state->BindExpr(U, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002554 break;
2555 }
Mike Stump1eb44332009-09-09 15:08:12 +00002556
Ted Kremeneka8538d92009-02-13 01:45:31 +00002557 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002558 }
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002560 return;
2561 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002562 }
2563
2564 // Handle ++ and -- (both pre- and post-increment).
2565
2566 assert (U->isIncrementDecrementOp());
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002567 ExplodedNodeSet Tmp;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002568 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002569 VisitLValue(Ex, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002570
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002571 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00002572
Ted Kremeneka8538d92009-02-13 01:45:31 +00002573 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002574 SVal V1 = state->getSVal(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +00002575
2576 // Perform a load.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002577 ExplodedNodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002578 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002579
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002580 for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Ted Kremeneka8538d92009-02-13 01:45:31 +00002582 state = GetState(*I2);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002583 SVal V2_untested = state->getSVal(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +00002584
2585 // Propagate unknown and undefined values.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002586 if (V2_untested.isUnknownOrUndef()) {
2587 MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002588 continue;
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002589 }
2590 DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
Mike Stump1eb44332009-09-09 15:08:12 +00002591
2592 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002593 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2594 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002595
Zhongxing Xua52ad4e2009-08-05 02:51:59 +00002596 // If the UnaryOperator has non-location type, use its type to create the
2597 // constant value. If the UnaryOperator has location type, create the
2598 // constant with int type and pointer width.
2599 SVal RHS;
2600
2601 if (U->getType()->isAnyPointerType())
2602 RHS = ValMgr.makeIntValWithPtrWidth(1, false);
2603 else
2604 RHS = ValMgr.makeIntVal(1, U->getType());
2605
Mike Stump1eb44332009-09-09 15:08:12 +00002606 SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
2607
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002608 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002609 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002610 DefinedOrUnknownSVal SymVal =
Ted Kremenek87806792009-09-27 20:45:21 +00002611 ValMgr.getConjuredSymbolVal(NULL, Ex,
2612 Builder->getCurrentBlockCount());
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002613 Result = SymVal;
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002615 // If the value is a location, ++/-- should always preserve
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00002616 // non-nullness. Check if the original value was non-null, and if so
Mike Stump1eb44332009-09-09 15:08:12 +00002617 // propagate that constraint.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002618 if (Loc::IsLocType(U->getType())) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002619 DefinedOrUnknownSVal Constraint =
2620 SVator.EvalEQ(state, V2, ValMgr.makeZeroVal(U->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002621
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002622 if (!state->Assume(Constraint, true)) {
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002623 // It isn't feasible for the original value to be null.
2624 // Propagate this constraint.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002625 Constraint = SVator.EvalEQ(state, SymVal,
2626 ValMgr.makeZeroVal(U->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002628
2629 state = state->Assume(Constraint, false);
Ted Kremeneka591bc02009-06-18 22:57:13 +00002630 assert(state);
Mike Stump1eb44332009-09-09 15:08:12 +00002631 }
2632 }
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002633 }
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Ted Kremenek8e029342009-08-27 22:17:37 +00002635 state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002636
Mike Stump1eb44332009-09-09 15:08:12 +00002637 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002638 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002639 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002640 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002641}
2642
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002643void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002644 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump1eb44332009-09-09 15:08:12 +00002645}
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002646
2647void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2648 AsmStmt::outputs_iterator I,
2649 AsmStmt::outputs_iterator E,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002650 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002651 if (I == E) {
2652 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2653 return;
2654 }
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002656 ExplodedNodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002657 VisitLValue(*I, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002659 ++I;
Mike Stump1eb44332009-09-09 15:08:12 +00002660
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002661 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002662 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2663}
2664
2665void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2666 AsmStmt::inputs_iterator I,
2667 AsmStmt::inputs_iterator E,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002668 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002669 if (I == E) {
Mike Stump1eb44332009-09-09 15:08:12 +00002670
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002671 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002672 // should evaluate to Locs. Nuke all of their values.
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002674 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2675 // which interprets the inline asm and stores proper results in the
2676 // outputs.
Mike Stump1eb44332009-09-09 15:08:12 +00002677
Ted Kremeneka8538d92009-02-13 01:45:31 +00002678 const GRState* state = GetState(Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002680 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2681 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump1eb44332009-09-09 15:08:12 +00002682
2683 SVal X = state->getSVal(*OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002684 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002686 if (isa<Loc>(X))
Ted Kremenek3ff12592009-06-19 17:10:32 +00002687 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002688 }
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Ted Kremeneka8538d92009-02-13 01:45:31 +00002690 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002691 return;
2692 }
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002694 ExplodedNodeSet Tmp;
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002695 Visit(*I, Pred, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002697 ++I;
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Ted Kremeneke1ccccf2009-09-03 03:02:58 +00002699 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002700 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2701}
2702
Ted Kremeneke1ccccf2009-09-03 03:02:58 +00002703void GRExprEngine::EvalReturn(ExplodedNodeSet& Dst, ReturnStmt* S,
2704 ExplodedNode* Pred) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002705 assert (Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump1eb44332009-09-09 15:08:12 +00002706
2707 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002708
Ted Kremenek186350f2008-04-23 20:12:28 +00002709 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2710 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002711
Ted Kremenek729a9a22008-07-17 23:15:45 +00002712 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Ted Kremenekb0533962008-04-18 20:35:30 +00002714 // Handle the case where no nodes where generated.
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Ted Kremenekb0533962008-04-18 20:35:30 +00002716 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002717 MakeNode(Dst, S, Pred, GetState(Pred));
2718}
2719
Ted Kremeneke1ccccf2009-09-03 03:02:58 +00002720void GRExprEngine::VisitReturnStmt(ReturnStmt* S, ExplodedNode* Pred,
2721 ExplodedNodeSet& Dst) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002722
2723 Expr* R = S->getRetValue();
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Ted Kremenek02737ed2008-03-31 15:02:58 +00002725 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002726 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002727 return;
2728 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002729
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002730 ExplodedNodeSet Tmp;
Ted Kremenek5917d782008-11-21 00:27:44 +00002731 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002732
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002733 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002734 SVal X = (*I)->getState()->getSVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Ted Kremenek5917d782008-11-21 00:27:44 +00002736 // Check if we return the address of a stack variable.
2737 if (isa<loc::MemRegionVal>(X)) {
2738 // Determine if the value is on the stack.
2739 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00002740
Ted Kremenekea20cd72009-06-23 18:05:21 +00002741 if (R && R->hasStackStorage()) {
Ted Kremenek5917d782008-11-21 00:27:44 +00002742 // Create a special node representing the error.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002743 if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) {
Ted Kremenek5917d782008-11-21 00:27:44 +00002744 N->markAsSink();
2745 RetsStackAddr.insert(N);
2746 }
2747 continue;
2748 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002749 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002750 // Check if we return an undefined value.
2751 else if (X.isUndef()) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002752 if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) {
Ted Kremenek5917d782008-11-21 00:27:44 +00002753 N->markAsSink();
2754 RetsUndef.insert(N);
2755 }
2756 continue;
2757 }
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002759 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002760 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002761}
Ted Kremenek55deb972008-03-25 00:34:37 +00002762
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002763//===----------------------------------------------------------------------===//
2764// Transfer functions: Binary operators.
2765//===----------------------------------------------------------------------===//
2766
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002767void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002768 ExplodedNode* Pred,
2769 ExplodedNodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002770
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002771 ExplodedNodeSet Tmp1;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002772 Expr* LHS = B->getLHS()->IgnoreParens();
2773 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002775 // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
2776 if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002777 Visit(RHS, Pred, Dst);
Ted Kremenek759623e2008-12-06 02:39:30 +00002778 return;
2779 }
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002781 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002782 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002783 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002784 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002785
Ted Kremeneke1ccccf2009-09-03 03:02:58 +00002786 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002787 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002788 ExplodedNodeSet Tmp2;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002789 Visit(RHS, *I1, Tmp2);
Zhongxing Xu6403b572009-09-02 13:26:26 +00002790
2791 ExplodedNodeSet CheckedSet;
2792 CheckerVisit(B, CheckedSet, Tmp2, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002794 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump1eb44332009-09-09 15:08:12 +00002795
2796 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xu6403b572009-09-02 13:26:26 +00002797 I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002798
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002799 const GRState *state = GetState(*I2);
2800 const GRState *OldSt = state;
Ted Kremenek3ff12592009-06-19 17:10:32 +00002801 SVal RightV = state->getSVal(RHS);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002802
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002803 BinaryOperator::Opcode Op = B->getOpcode();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002804 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002805 case BinaryOperator::Assign: {
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002807 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002808 // FIXME: Handle structs.
2809 QualType T = RHS->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002810
2811 if ((RightV.isUnknown() ||
2812 !getConstraintManager().canReasonAbout(RightV))
2813 && (Loc::IsLocType(T) ||
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002814 (T->isScalarType() && T->isIntegerType()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00002815 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek87806792009-09-27 20:45:21 +00002816 RightV = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002817 }
Mike Stump1eb44332009-09-09 15:08:12 +00002818
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002819 // Simulate the effects of a "store": bind the value of the RHS
Mike Stump1eb44332009-09-09 15:08:12 +00002820 // to the L-Value represented by the LHS.
2821 EvalStore(Dst, B, LHS, *I2, state->BindExpr(B, RightV),
Zhongxing Xu5032ffe2009-08-25 06:51:30 +00002822 LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002823 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002824 }
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002826 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002827
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002828 default: {
Mike Stump1eb44332009-09-09 15:08:12 +00002829
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002830 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002831 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002832
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00002833 // Process non-assignments except commas or short-circuited
Mike Stump1eb44332009-09-09 15:08:12 +00002834 // logical expressions (LAnd and LOr).
Zhongxing Xu262fd032009-05-20 09:00:16 +00002835 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002836
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002837 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002838 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002839 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002840 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002841 }
2842 else
2843 Dst.Add(*I2);
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Ted Kremenek89063af2008-02-21 19:15:37 +00002845 continue;
2846 }
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002848 state = state->BindExpr(B, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002850 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002851 // The operands were *not* undefined, but the result is undefined.
2852 // This is a special node that should be flagged as an error.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002853 if (ExplodedNode *UndefNode = Builder->generateNode(B, state, *I2)){
Mike Stump1eb44332009-09-09 15:08:12 +00002854 UndefNode->markAsSink();
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002855 UndefResults.insert(UndefNode);
2856 }
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002857 continue;
2858 }
Mike Stump1eb44332009-09-09 15:08:12 +00002859
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002860 // Otherwise, create a new node.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00002861 MakeNode(Dst, B, *I2, state);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002862 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002863 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002864 }
Mike Stump1eb44332009-09-09 15:08:12 +00002865
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002866 assert (B->isCompoundAssignmentOp());
2867
Ted Kremenekcad29962009-02-07 00:52:24 +00002868 switch (Op) {
2869 default:
2870 assert(0 && "Invalid opcode for compound assignment.");
2871 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2872 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2873 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2874 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2875 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2876 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2877 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2878 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2879 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2880 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002881 }
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002883 // Perform a load (the LHS). This performs the checks for
2884 // null dereferences, and so on.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002885 ExplodedNodeSet Tmp3;
Ted Kremenek3ff12592009-06-19 17:10:32 +00002886 SVal location = state->getSVal(LHS);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002887 EvalLoad(Tmp3, LHS, *I2, state, location);
Mike Stump1eb44332009-09-09 15:08:12 +00002888
2889 for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
Zhongxing Xu6403b572009-09-02 13:26:26 +00002890 ++I3) {
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Ted Kremeneka8538d92009-02-13 01:45:31 +00002892 state = GetState(*I3);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002893 SVal V = state->getSVal(LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002894
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002895 // Get the computation type.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002896 QualType CTy =
2897 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002898 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002899
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002900 QualType CLHSTy =
2901 cast<CompoundAssignOperator>(B)->getComputationLHSType();
2902 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002903
Ted Kremenek60595da2008-11-15 04:01:56 +00002904 QualType LTy = getContext().getCanonicalType(LHS->getType());
2905 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00002906
2907 // Promote LHS.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002908 llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002909
Mike Stump1eb44332009-09-09 15:08:12 +00002910 // Compute the result of the operation.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002911 SVal Result;
2912 llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
2913 RightV, CTy),
2914 state, B->getType(), CTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002916 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002917 // The operands were not undefined, but the result is undefined.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002918 if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I3)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002919 UndefNode->markAsSink();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002920 UndefResults.insert(UndefNode);
2921 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002922 continue;
2923 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002924
2925 // EXPERIMENTAL: "Conjured" symbols.
2926 // FIXME: Handle structs.
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Ted Kremenek60595da2008-11-15 04:01:56 +00002928 SVal LHSVal;
Mike Stump1eb44332009-09-09 15:08:12 +00002929
2930 if ((Result.isUnknown() ||
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002931 !getConstraintManager().canReasonAbout(Result))
Mike Stump1eb44332009-09-09 15:08:12 +00002932 && (Loc::IsLocType(CTy)
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002933 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00002934
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002935 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Ted Kremenek60595da2008-11-15 04:01:56 +00002937 // The symbolic value is actually for the type of the left-hand side
2938 // expression, not the computation type, as this is the value the
2939 // LValue on the LHS will bind to.
Ted Kremenek87806792009-09-27 20:45:21 +00002940 LHSVal = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count);
Mike Stump1eb44332009-09-09 15:08:12 +00002941
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002942 // However, we need to convert the symbol to the computation type.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002943 llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002944 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002945 else {
2946 // The left-hand side may bind to a different value then the
2947 // computation type.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002948 llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
Ted Kremenek60595da2008-11-15 04:01:56 +00002949 }
Mike Stump1eb44332009-09-09 15:08:12 +00002950
2951 EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, Result),
Zhongxing Xu5032ffe2009-08-25 06:51:30 +00002952 location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002953 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002954 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002955 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002956}
Ted Kremenekee985462008-01-16 18:18:48 +00002957
2958//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002959// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002960//===----------------------------------------------------------------------===//
2961
Ted Kremenekaa66a322008-01-16 21:46:15 +00002962#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002963static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002964static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002965
Ted Kremenekaa66a322008-01-16 21:46:15 +00002966namespace llvm {
2967template<>
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002968struct VISIBILITY_HIDDEN DOTGraphTraits<ExplodedNode*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002969 public DefaultDOTGraphTraits {
Mike Stump1eb44332009-09-09 15:08:12 +00002970
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002971 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump1eb44332009-09-09 15:08:12 +00002972
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002973 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002974 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002975 GraphPrintCheckerState->isUndefDeref(N) ||
2976 GraphPrintCheckerState->isUndefStore(N) ||
2977 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002978 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002979 GraphPrintCheckerState->isBadCall(N) ||
2980 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002981 return "color=\"red\",style=\"filled\"";
Mike Stump1eb44332009-09-09 15:08:12 +00002982
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002983 if (GraphPrintCheckerState->isNoReturnCall(N))
2984 return "color=\"blue\",style=\"filled\"";
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002986 return "";
2987 }
Mike Stump1eb44332009-09-09 15:08:12 +00002988
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002989 static std::string getNodeLabel(const ExplodedNode* N, void*,bool ShortNames){
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Ted Kremenek53ba0b62009-06-24 23:06:47 +00002991 std::string sbuf;
2992 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002993
2994 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002995 ProgramPoint Loc = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002996
Ted Kremenekaa66a322008-01-16 21:46:15 +00002997 switch (Loc.getKind()) {
2998 case ProgramPoint::BlockEntranceKind:
Mike Stump1eb44332009-09-09 15:08:12 +00002999 Out << "Block Entrance: B"
Ted Kremenekaa66a322008-01-16 21:46:15 +00003000 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3001 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Ted Kremenekaa66a322008-01-16 21:46:15 +00003003 case ProgramPoint::BlockExitKind:
3004 assert (false);
3005 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Ted Kremenekaa66a322008-01-16 21:46:15 +00003007 default: {
Ted Kremenek5f85e172009-07-22 22:35:28 +00003008 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
3009 const Stmt* S = L->getStmt();
Ted Kremenek8c354752008-12-16 22:02:27 +00003010 SourceLocation SLoc = S->getLocStart();
3011
Mike Stump1eb44332009-09-09 15:08:12 +00003012 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattnere4f21422009-06-30 01:26:17 +00003013 LangOptions LO; // FIXME.
3014 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump1eb44332009-09-09 15:08:12 +00003015
3016 if (SLoc.isFileID()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00003017 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003018 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3019 << " col="
3020 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3021 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003022 }
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Ted Kremenek5f85e172009-07-22 22:35:28 +00003024 if (isa<PreStmt>(Loc))
Mike Stump1eb44332009-09-09 15:08:12 +00003025 Out << "\\lPreStmt\\l;";
Ted Kremenek5f85e172009-07-22 22:35:28 +00003026 else if (isa<PostLoad>(Loc))
Ted Kremenek7090d542009-05-07 18:27:16 +00003027 Out << "\\lPostLoad\\l;";
3028 else if (isa<PostStore>(Loc))
3029 Out << "\\lPostStore\\l";
3030 else if (isa<PostLValue>(Loc))
3031 Out << "\\lPostLValue\\l";
3032 else if (isa<PostLocationChecksSucceed>(Loc))
3033 Out << "\\lPostLocationChecksSucceed\\l";
3034 else if (isa<PostNullCheckFailed>(Loc))
3035 Out << "\\lPostNullCheckFailed\\l";
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Ted Kremenek8c354752008-12-16 22:02:27 +00003037 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3038 Out << "\\|Implicit-Null Dereference.\\l";
3039 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3040 Out << "\\|Explicit-Null Dereference.\\l";
3041 else if (GraphPrintCheckerState->isUndefDeref(N))
3042 Out << "\\|Dereference of undefialied value.\\l";
3043 else if (GraphPrintCheckerState->isUndefStore(N))
3044 Out << "\\|Store to Undefined Loc.";
Ted Kremenek8c354752008-12-16 22:02:27 +00003045 else if (GraphPrintCheckerState->isUndefResult(N))
3046 Out << "\\|Result of operation is undefined.";
3047 else if (GraphPrintCheckerState->isNoReturnCall(N))
3048 Out << "\\|Call to function marked \"noreturn\".";
3049 else if (GraphPrintCheckerState->isBadCall(N))
3050 Out << "\\|Call to NULL/Undefined.";
3051 else if (GraphPrintCheckerState->isUndefArg(N))
3052 Out << "\\|Argument in call is undefined";
Mike Stump1eb44332009-09-09 15:08:12 +00003053
Ted Kremenek8c354752008-12-16 22:02:27 +00003054 break;
3055 }
3056
Ted Kremenekaa66a322008-01-16 21:46:15 +00003057 const BlockEdge& E = cast<BlockEdge>(Loc);
3058 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3059 << E.getDst()->getBlockID() << ')';
Mike Stump1eb44332009-09-09 15:08:12 +00003060
Ted Kremenekb38911f2008-01-30 23:03:39 +00003061 if (Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003062
Ted Kremeneke97ca062008-03-07 20:57:30 +00003063 SourceLocation SLoc = T->getLocStart();
Mike Stump1eb44332009-09-09 15:08:12 +00003064
Ted Kremenekb38911f2008-01-30 23:03:39 +00003065 Out << "\\|Terminator: ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003066 LangOptions LO; // FIXME.
3067 E.getSrc()->printTerminator(Out, LO);
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003069 if (SLoc.isFileID()) {
3070 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003071 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3072 << " col="
3073 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003074 }
Mike Stump1eb44332009-09-09 15:08:12 +00003075
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003076 if (isa<SwitchStmt>(T)) {
3077 Stmt* Label = E.getDst()->getLabel();
Mike Stump1eb44332009-09-09 15:08:12 +00003078
3079 if (Label) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003080 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3081 Out << "\\lcase ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003082 LangOptions LO; // FIXME.
3083 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump1eb44332009-09-09 15:08:12 +00003084
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003085 if (Stmt* RHS = C->getRHS()) {
3086 Out << " .. ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003087 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003088 }
Mike Stump1eb44332009-09-09 15:08:12 +00003089
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003090 Out << ":";
3091 }
3092 else {
3093 assert (isa<DefaultStmt>(Label));
3094 Out << "\\ldefault:";
3095 }
3096 }
Mike Stump1eb44332009-09-09 15:08:12 +00003097 else
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003098 Out << "\\l(implicit) default:";
3099 }
3100 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003101 // FIXME
3102 }
3103 else {
3104 Out << "\\lCondition: ";
3105 if (*E.getSrc()->succ_begin() == E.getDst())
3106 Out << "true";
3107 else
Mike Stump1eb44332009-09-09 15:08:12 +00003108 Out << "false";
Ted Kremenekb38911f2008-01-30 23:03:39 +00003109 }
Mike Stump1eb44332009-09-09 15:08:12 +00003110
Ted Kremenekb38911f2008-01-30 23:03:39 +00003111 Out << "\\l";
3112 }
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003114 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3115 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003116 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003117 }
3118 }
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003120 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003121
Ted Kremenekb65be702009-06-18 01:23:53 +00003122 const GRState *state = N->getState();
3123 state->printDOT(Out);
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003125 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003126 return Out.str();
3127 }
3128};
Mike Stump1eb44332009-09-09 15:08:12 +00003129} // end llvm namespace
Ted Kremenekaa66a322008-01-16 21:46:15 +00003130#endif
3131
Ted Kremenekffe0f432008-03-07 22:58:01 +00003132#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003133template <typename ITERATOR>
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003134ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003135
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003136template <> ExplodedNode*
3137GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3138 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003139 return I->first;
3140}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003141#endif
3142
3143void GRExprEngine::ViewGraph(bool trim) {
Mike Stump1eb44332009-09-09 15:08:12 +00003144#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003145 if (trim) {
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003146 std::vector<ExplodedNode*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003147
3148 // Flush any outstanding reports to make sure we cover all the nodes.
3149 // This does not cause them to get displayed.
3150 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3151 const_cast<BugType*>(*I)->FlushReports(BR);
3152
3153 // Iterate through the reports and get their nodes.
3154 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
Ted Kremeneke1ccccf2009-09-03 03:02:58 +00003155 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
Mike Stump1eb44332009-09-09 15:08:12 +00003156 I2!=E2; ++I2) {
Ted Kremenek940abcc2009-03-11 01:41:22 +00003157 const BugReportEquivClass& EQ = *I2;
3158 const BugReport &R = **EQ.begin();
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003159 ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
Ted Kremenek940abcc2009-03-11 01:41:22 +00003160 if (N) Src.push_back(N);
3161 }
3162 }
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003164 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003165 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003166 else {
3167 GraphPrintCheckerState = this;
3168 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003169
Ted Kremenekffe0f432008-03-07 22:58:01 +00003170 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Mike Stump1eb44332009-09-09 15:08:12 +00003171
Ted Kremenek493d7a22008-03-11 18:25:33 +00003172 GraphPrintCheckerState = NULL;
3173 GraphPrintSourceManager = NULL;
3174 }
3175#endif
3176}
3177
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003178void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003179#ifndef NDEBUG
3180 GraphPrintCheckerState = this;
3181 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00003182
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003183 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003184
Ted Kremenekcf118d42009-02-04 23:49:09 +00003185 if (!TrimmedG.get())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00003186 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003187 else
Mike Stump1eb44332009-09-09 15:08:12 +00003188 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
3189
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003190 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003191 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003192#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003193}