blob: ea88e70afa2da59ac09f617558d8b8813920bd9e [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 Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000020#include "llvm/ADT/ImmutableList.h"
21#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000118 LiveVariables& L,
119 GRStateManager::StoreManagerCreator SMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000120 : CoreEngine(cfg, CD, Ctx, *this),
121 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000122 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000123 Builder(NULL),
Ted Kremenek95c7b002008-10-24 01:04:59 +0000124 StateMgr(G.getContext(), SMC,
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000125 CreateBasicConstraintManager, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000127 CurrentStmt(NULL),
128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000129 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000130
Ted Kremenek1a654b62008-06-20 21:45:25 +0000131GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
133 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000134
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135
136 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000137}
138
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000139//===----------------------------------------------------------------------===//
140// Utility methods.
141//===----------------------------------------------------------------------===//
142
143// SaveAndRestore - A utility class that uses RIIA to save and restore
144// the value of a variable.
145template<typename T>
146struct VISIBILITY_HIDDEN SaveAndRestore {
147 SaveAndRestore(T& x) : X(x), old_value(x) {}
148 ~SaveAndRestore() { X = old_value; }
149 T get() { return old_value; }
150
151 T& X;
152 T old_value;
153};
154
Ted Kremenek186350f2008-04-23 20:12:28 +0000155// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
156// value of a variable is saved, and during the dstor the old value is
157// or'ed with the new value.
158struct VISIBILITY_HIDDEN SaveOr {
159 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
160 ~SaveOr() { X |= old_value; }
161
162 bool& X;
163 bool old_value;
164};
165
166
Ted Kremenekc0959972008-07-02 21:24:01 +0000167void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000168 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000169 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000170 (*I)->EmitWarnings(BR);
171 }
172
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000173 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000174 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000175 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000176 }
177}
178
179void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000180 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181 tf->RegisterChecks(*this);
182 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000183}
184
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000185void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
186 if (!BatchAuditor)
187 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
188
189 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000190}
191
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000193 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194}
195
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196//===----------------------------------------------------------------------===//
197// Top-level transfer function logic (Dispatcher).
198//===----------------------------------------------------------------------===//
199
200void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
201
202 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000203 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000204
205 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000206 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000207 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000208
209 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000210 if (BatchAuditor)
211 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000212
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000213 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000214 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
215 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000216
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 else {
223 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 SaveOr OldHasGen(Builder->HasGeneratedNode);
225
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000226 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
227 Builder->PurgingDeadSymbols = true;
228
Ted Kremenek729a9a22008-07-17 23:15:45 +0000229 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000230 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000231
232 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
233 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000234 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 bool HasAutoGenerated = false;
237
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 NodeSet Dst;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Visit(S, *I, Dst);
247
248 // Do we need to auto-generate a node? We only need to do this to generate
249 // a node with a "cleaned" state; GRCoreEngine will actually handle
250 // auto-transitions for other cases.
251 if (Dst.size() == 1 && *Dst.begin() == EntryNode
252 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
253 HasAutoGenerated = true;
254 builder.generateNode(S, GetState(EntryNode), *I);
255 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000261
262 // FIXME: Consolidate.
263 StateMgr.CurrentStmt = 0;
264 CurrentStmt = 0;
265
Ted Kremenek846d4e92008-04-24 23:35:58 +0000266 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000267}
268
269void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
270
271 // FIXME: add metadata to the CFG so that we can disable
272 // this check when we KNOW that there is no block-level subexpression.
273 // The motivation is that this check requires a hashtable lookup.
274
275 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
276 Dst.Add(Pred);
277 return;
278 }
279
280 switch (S->getStmtClass()) {
281
282 default:
283 // Cases we intentionally have "default" handle:
284 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
285
286 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
287 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000288
289 case Stmt::ArraySubscriptExprClass:
290 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
291 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000292
293 case Stmt::AsmStmtClass:
294 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
295 break;
296
297 case Stmt::BinaryOperatorClass: {
298 BinaryOperator* B = cast<BinaryOperator>(S);
299
300 if (B->isLogicalOp()) {
301 VisitLogicalExpr(B, Pred, Dst);
302 break;
303 }
304 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000305 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000306 MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000307 break;
308 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000309
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000310 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
311 break;
312 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000313
Douglas Gregorb4609802008-11-14 16:09:21 +0000314 case Stmt::CallExprClass:
315 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000316 CallExpr* C = cast<CallExpr>(S);
317 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000318 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000319 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000320
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000321 // FIXME: ChooseExpr is really a constant. We need to fix
322 // the CFG do not model them as explicit control-flow.
323
324 case Stmt::ChooseExprClass: { // __builtin_choose_expr
325 ChooseExpr* C = cast<ChooseExpr>(S);
326 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
327 break;
328 }
329
330 case Stmt::CompoundAssignOperatorClass:
331 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
332 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000333
334 case Stmt::CompoundLiteralExprClass:
335 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
336 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000337
338 case Stmt::ConditionalOperatorClass: { // '?' operator
339 ConditionalOperator* C = cast<ConditionalOperator>(S);
340 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
341 break;
342 }
343
344 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000345 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000346 break;
347
348 case Stmt::DeclStmtClass:
349 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
350 break;
351
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000352 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000353 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000354 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000355 VisitCast(C, C->getSubExpr(), Pred, Dst);
356 break;
357 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000358
359 case Stmt::InitListExprClass:
360 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
361 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000362
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000363 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000364 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
365 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000366
367 case Stmt::ObjCIvarRefExprClass:
368 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
369 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000370
371 case Stmt::ObjCForCollectionStmtClass:
372 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
373 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000374
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000375 case Stmt::ObjCMessageExprClass: {
376 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
377 break;
378 }
379
380 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000381 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000382 break;
383
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000384 case Stmt::ReturnStmtClass:
385 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
386 break;
387
Sebastian Redl05189992008-11-11 17:56:53 +0000388 case Stmt::SizeOfAlignOfExprClass:
389 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000390 break;
391
392 case Stmt::StmtExprClass: {
393 StmtExpr* SE = cast<StmtExpr>(S);
394
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000395 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396
397 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
398 // probably just remove these from the CFG.
399 assert (!SE->getSubStmt()->body_empty());
400
401 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000402 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 else
404 Dst.Add(Pred);
405
406 break;
407 }
408
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000409 case Stmt::UnaryOperatorClass:
410 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000412 }
413}
414
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000415void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000416
417 Ex = Ex->IgnoreParens();
418
419 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
420 Dst.Add(Pred);
421 return;
422 }
423
424 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000425
426 case Stmt::ArraySubscriptExprClass:
427 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
428 return;
429
430 case Stmt::DeclRefExprClass:
431 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
432 return;
433
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000434 case Stmt::ObjCIvarRefExprClass:
435 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
436 return;
437
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000438 case Stmt::UnaryOperatorClass:
439 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
440 return;
441
442 case Stmt::MemberExprClass:
443 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
444 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000445
Ted Kremenek4f090272008-10-27 21:54:31 +0000446 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000447 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000448 return;
449
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000450 case Stmt::ObjCPropertyRefExprClass:
451 // FIXME: Property assignments are lvalues, but not really "locations".
452 // e.g.: self.x = something;
453 // Here the "self.x" really can translate to a method call (setter) when
454 // the assignment is made. Moreover, the entire assignment expression
455 // evaluate to whatever "something" is, not calling the "getter" for
456 // the property (which would make sense since it can have side effects).
457 // We'll probably treat this as a location, but not one that we can
458 // take the address of. Perhaps we need a new SVal class for cases
459 // like thsis?
460 // Note that we have a similar problem for bitfields, since they don't
461 // have "locations" in the sense that we can take their address.
462 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000463 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000464
465 case Stmt::StringLiteralClass: {
466 const GRState* St = GetState(Pred);
467 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000468 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000469 return;
470 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000471
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000472 default:
473 // Arbitrary subexpressions can return aggregate temporaries that
474 // can be used in a lvalue context. We need to enhance our support
475 // of such temporaries in both the environment and the store, so right
476 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000477 assert ((Ex->getType()->isAggregateType() ||
478 Ex->getType()->isUnionType()) &&
479 "Other kinds of expressions with non-aggregate/union types do"
480 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000481
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000482 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000483 }
484}
485
486//===----------------------------------------------------------------------===//
487// Block entrance. (Update counters).
488//===----------------------------------------------------------------------===//
489
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000490bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000491 GRBlockCounter BC) {
492
493 return BC.getNumVisited(B->getBlockID()) < 3;
494}
495
496//===----------------------------------------------------------------------===//
497// Branch processing.
498//===----------------------------------------------------------------------===//
499
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000500const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000501 Stmt* Terminator,
502 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000503
504 switch (Terminator->getStmtClass()) {
505 default:
506 return St;
507
508 case Stmt::BinaryOperatorClass: { // '&&' and '||'
509
510 BinaryOperator* B = cast<BinaryOperator>(Terminator);
511 BinaryOperator::Opcode Op = B->getOpcode();
512
513 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
514
515 // For &&, if we take the true branch, then the value of the whole
516 // expression is that of the RHS expression.
517 //
518 // For ||, if we take the false branch, then the value of the whole
519 // expression is that of the RHS expression.
520
521 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
522 (Op == BinaryOperator::LOr && !branchTaken)
523 ? B->getRHS() : B->getLHS();
524
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000525 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000526 }
527
528 case Stmt::ConditionalOperatorClass: { // ?:
529
530 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
531
532 // For ?, if branchTaken == true then the value is either the LHS or
533 // the condition itself. (GNU extension).
534
535 Expr* Ex;
536
537 if (branchTaken)
538 Ex = C->getLHS() ? C->getLHS() : C->getCond();
539 else
540 Ex = C->getRHS();
541
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000542 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000543 }
544
545 case Stmt::ChooseExprClass: { // ?:
546
547 ChooseExpr* C = cast<ChooseExpr>(Terminator);
548
549 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000550 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000551 }
552 }
553}
554
Ted Kremenekaf337412008-11-12 19:24:17 +0000555void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000556 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000557
Ted Kremeneke7d22112008-02-11 19:21:59 +0000558 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000559 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000560 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000561
Ted Kremenekb2331832008-02-15 22:29:00 +0000562 // Check for NULL conditions; e.g. "for(;;)"
563 if (!Condition) {
564 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000565 return;
566 }
567
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000568 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000569
570 switch (V.getBaseKind()) {
571 default:
572 break;
573
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000574 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000575 builder.generateNode(MarkBranch(PrevState, Term, true), true);
576 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000577 return;
578
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000579 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000580 NodeTy* N = builder.generateNode(PrevState, true);
581
582 if (N) {
583 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000584 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000585 }
586
587 builder.markInfeasible(false);
588 return;
589 }
590 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000591
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000592 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000593
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000594 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000595 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000596
597 if (isFeasible)
598 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000599 else
600 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000601
602 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000603
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000604 isFeasible = false;
605 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000606
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000607 if (isFeasible)
608 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000609 else
610 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000611}
612
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000613/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000614/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000615void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000616
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000617 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000618 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000619
620 // Three possibilities:
621 //
622 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000623 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000624 // (3) We have no clue about the label. Dispatch to all targets.
625 //
626
627 typedef IndirectGotoNodeBuilder::iterator iterator;
628
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000629 if (isa<loc::GotoLabel>(V)) {
630 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000631
632 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000633 if (I.getLabel() == L) {
634 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000635 return;
636 }
637 }
638
639 assert (false && "No block with label.");
640 return;
641 }
642
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000643 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000644 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000645 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000646 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000647 return;
648 }
649
650 // This is really a catch-all. We don't support symbolics yet.
651
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000652 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000653
654 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000655 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000656}
Ted Kremenekf233d482008-02-05 00:26:40 +0000657
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000658
659void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
660 NodeTy* Pred, NodeSet& Dst) {
661
662 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
663
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000664 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000665 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000666
667 assert (X.isUndef());
668
669 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
670
671 assert (SE);
672
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000673 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000674
675 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000676 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000677}
678
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
680/// nodes by processing the 'effects' of a switch statement.
681void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
682
683 typedef SwitchNodeBuilder::iterator iterator;
684
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000685 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000686 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000687 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000688
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000689 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000690 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000691 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000692 return;
693 }
694
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000695 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000696
697 // While most of this can be assumed (such as the signedness), having it
698 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000699
Chris Lattner98be4942008-03-05 18:54:05 +0000700 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000701
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000702 APSInt V1(bits, false);
703 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000704 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000705
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000706 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000707
708 CaseStmt* Case = cast<CaseStmt>(I.getCase());
709
710 // Evaluate the case.
711 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
712 assert (false && "Case condition must evaluate to an integer constant.");
713 return;
714 }
715
716 // Get the RHS of the case, if it exists.
717
718 if (Expr* E = Case->getRHS()) {
719 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
720 assert (false &&
721 "Case condition (RHS) must evaluate to an integer constant.");
722 return ;
723 }
724
725 assert (V1 <= V2);
726 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000727 else
728 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000729
730 // FIXME: Eventually we should replace the logic below with a range
731 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000732 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000733
Ted Kremenek14a11402008-03-17 22:17:56 +0000734 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000735 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000736
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000737 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738
739 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000740
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000741 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000742 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000743
744 if (isFeasible) {
745 builder.generateCaseStmtNode(I, StNew);
746
747 // If CondV evaluates to a constant, then we know that this
748 // is the *only* case that we can take, so stop evaluating the
749 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000750 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751 return;
752 }
753
754 // Now "assume" that the case doesn't match. Add this state
755 // to the default state (if it is feasible).
756
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000757 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000758 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759
Ted Kremenek5014ab12008-04-23 05:03:18 +0000760 if (isFeasible) {
761 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000762 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000763 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000764
Ted Kremenek14a11402008-03-17 22:17:56 +0000765 // Concretize the next value in the range.
766 if (V1 == V2)
767 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000768
Ted Kremenek14a11402008-03-17 22:17:56 +0000769 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000770 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000771
772 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000773 }
774
775 // If we reach here, than we know that the default branch is
776 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000777 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000778}
779
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000780//===----------------------------------------------------------------------===//
781// Transfer functions: logical operations ('&&', '||').
782//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000783
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000784void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000785 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000786
Ted Kremenek05a23782008-02-26 19:05:15 +0000787 assert (B->getOpcode() == BinaryOperator::LAnd ||
788 B->getOpcode() == BinaryOperator::LOr);
789
790 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
791
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000792 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000793 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000794
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000795 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000796
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000797 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000798
799 assert (Ex);
800
801 if (Ex == B->getRHS()) {
802
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000803 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000804
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000805 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000806
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000807 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000808 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000809 return;
810 }
811
Ted Kremenek05a23782008-02-26 19:05:15 +0000812 // We took the RHS. Because the value of the '&&' or '||' expression must
813 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
814 // or 1. Alternatively, we could take a lazy approach, and calculate this
815 // value later when necessary. We don't have the machinery in place for
816 // this right now, and since most logical expressions are used for branches,
817 // the payoff is not likely to be large. Instead, we do eager evaluation.
818
819 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000820 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000821
822 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000823 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000824 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000825
826 isFeasible = false;
827 NewState = Assume(St, X, false, isFeasible);
828
829 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000830 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000831 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000832 }
833 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000834 // We took the LHS expression. Depending on whether we are '&&' or
835 // '||' we know what the value of the expression is via properties of
836 // the short-circuiting.
837
838 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000839 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000840 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000841}
Ted Kremenek05a23782008-02-26 19:05:15 +0000842
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000843//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000844// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000845//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000846
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000847void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
848 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000849
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000850 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000851
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000852 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000853
854 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
855
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000856 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000857
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000858 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000859 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000860 else
861 EvalLoad(Dst, Ex, Pred, St, V);
862 return;
863
864 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
865 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
866
867 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000868 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000869 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000870 return;
871
872 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000873 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000874 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000875 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000876 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000877 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000878
879 assert (false &&
880 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000881}
882
Ted Kremenek540cbe22008-04-22 04:56:29 +0000883/// VisitArraySubscriptExpr - Transfer function for array accesses
884void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000885 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000886
887 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000888 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000889 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000890 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000891
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000892 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000893 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000894 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000895
896 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000897 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000898 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000899
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000900 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000901 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000902 else
903 EvalLoad(Dst, A, *I2, St, V);
904 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000905 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000906}
907
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000908/// VisitMemberExpr - Transfer function for member expressions.
909void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000910 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000911
912 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000913 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000914
915 if (M->isArrow())
916 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
917 else
918 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
919
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000920 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000921 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000922 // FIXME: Should we insert some assumption logic in here to determine
923 // if "Base" is a valid piece of memory? Before we put this assumption
924 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000925 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000926
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000927 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000928 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000929 else
930 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000931 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000932}
933
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000934void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000935 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000936
937 assert (Builder && "GRStmtNodeBuilder must be defined.");
938
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000939 // Evaluate the location (checks for bad dereferences).
940 St = EvalLocation(Ex, Pred, St, location);
941
942 if (!St)
943 return;
944
945 // Proceed with the store.
946
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000947 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000948
Ted Kremenek186350f2008-04-23 20:12:28 +0000949 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000950 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000951 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000952
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000954 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000955
Ted Kremenek729a9a22008-07-17 23:15:45 +0000956 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000957
958 // Handle the case where no nodes where generated. Auto-generate that
959 // contains the updated state if we aren't generating sinks.
960
Ted Kremenekb0533962008-04-18 20:35:30 +0000961 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000962 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000963 location, Val);
964}
965
966void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000967 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000968 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969
970 // Evaluate the location (checks for bad dereferences).
971
972 St = EvalLocation(Ex, Pred, St, location, true);
973
974 if (!St)
975 return;
976
977 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000978 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000979
980 // FIXME: Currently symbolic analysis "generates" new symbols
981 // for the contents of values. We need a better approach.
982
983 // FIXME: The "CheckOnly" option exists only because Array and Field
984 // loads aren't fully implemented. Eventually this option will go away.
Ted Kremenek982e6742008-08-28 18:43:46 +0000985
Ted Kremenek436f2b92008-04-30 04:23:07 +0000986 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000987 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000988 else if (location.isUnknown()) {
989 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000990 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000991 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000992 else
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000993 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, GetSVal(St, cast<Loc>(location),
994 Ex->getType())), K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000995}
996
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000997void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000998 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000999
1000 NodeSet TmpDst;
1001 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1002
1003 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1004 MakeNode(Dst, Ex, *I, (*I)->getState());
1005}
1006
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001007const GRState* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001008 const GRState* St,
1009 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001010
1011 // Check for loads/stores from/to undefined values.
1012 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001013 ProgramPoint::Kind K =
1014 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1015
1016 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001017 Succ->markAsSink();
1018 UndefDeref.insert(Succ);
1019 }
1020
1021 return NULL;
1022 }
1023
1024 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1025 if (location.isUnknown())
1026 return St;
1027
1028 // During a load, one of two possible situations arise:
1029 // (1) A crash, because the location (pointer) was NULL.
1030 // (2) The location (pointer) is not NULL, and the dereference works.
1031 //
1032 // We add these assumptions.
1033
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001034 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001035
1036 // "Assume" that the pointer is not NULL.
1037
1038 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001039 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001040
1041 // "Assume" that the pointer is NULL.
1042
1043 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001044 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1045 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046
1047 if (isFeasibleNull) {
1048
Ted Kremenek7360fda2008-09-18 23:09:54 +00001049 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001050 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001051 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1052
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053 // We don't use "MakeNode" here because the node will be a sink
1054 // and we have no intention of processing it later.
1055
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001056 ProgramPoint::Kind K =
1057 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1058
1059 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001060
1061 if (NullNode) {
1062
1063 NullNode->markAsSink();
1064
1065 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1066 else ExplicitNullDeref.insert(NullNode);
1067 }
1068 }
Zhongxing Xu60156f02008-11-08 03:45:42 +00001069
1070 // Check for out-of-bound array access.
1071 if (isFeasibleNotNull && isa<loc::MemRegionVal>(LV)) {
1072 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1073 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1074 // Get the index of the accessed element.
1075 SVal Idx = ER->getIndex();
1076 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001077 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1078 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001079
1080 bool isFeasibleInBound = false;
1081 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1082 true, isFeasibleInBound);
1083
1084 bool isFeasibleOutBound = false;
1085 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1086 false, isFeasibleOutBound);
1087
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001088 if (isFeasibleOutBound) {
1089 // Report warning.
1090
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001091 // Make sink node manually.
1092 ProgramPoint::Kind K = isLoad ? ProgramPoint::PostLoadKind
1093 : ProgramPoint::PostStoreKind;
1094
1095 NodeTy* OOBNode = Builder->generateNode(Ex, StOutBound, Pred, K);
1096
1097 if (OOBNode) {
1098 OOBNode->markAsSink();
1099
1100 if (isFeasibleInBound)
1101 ImplicitOOBMemAccesses.insert(OOBNode);
1102 else
1103 ExplicitOOBMemAccesses.insert(OOBNode);
1104 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001105 }
1106
1107 return isFeasibleInBound ? StInBound : NULL;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001108 }
1109 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001110
1111 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001112}
1113
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001114//===----------------------------------------------------------------------===//
1115// Transfer function: Function calls.
1116//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001117void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001118 CallExpr::arg_iterator AI,
1119 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001120 NodeSet& Dst)
1121{
1122 // Determine the type of function we're calling (if available).
1123 const FunctionTypeProto *Proto = NULL;
1124 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1125 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1126 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1127
1128 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1129}
1130
1131void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1132 CallExpr::arg_iterator AI,
1133 CallExpr::arg_iterator AE,
1134 NodeSet& Dst, const FunctionTypeProto *Proto,
1135 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001136
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001137 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001138 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001139 // If the call argument is being bound to a reference parameter,
1140 // visit it as an lvalue, not an rvalue.
1141 bool VisitAsLvalue = false;
1142 if (Proto && ParamIdx < Proto->getNumArgs())
1143 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1144
1145 NodeSet DstTmp;
1146 if (VisitAsLvalue)
1147 VisitLValue(*AI, Pred, DstTmp);
1148 else
1149 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001150 ++AI;
1151
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001152 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001153 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001154
1155 return;
1156 }
1157
1158 // If we reach here we have processed all of the arguments. Evaluate
1159 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001160
Ted Kremenek994a09b2008-02-25 21:16:03 +00001161 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001162 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001163
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001164 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001165
Ted Kremenekde434242008-02-19 01:44:53 +00001166 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001167 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1168
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001169 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001170 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001171
Ted Kremeneka1354a52008-03-03 16:47:31 +00001172 // FIXME: Add support for symbolic function calls (calls involving
1173 // function pointer values that are symbolic).
1174
1175 // Check for undefined control-flow or calls to NULL.
1176
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001177 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001178 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001179
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001180 if (N) {
1181 N->markAsSink();
1182 BadCalls.insert(N);
1183 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001184
Ted Kremenekde434242008-02-19 01:44:53 +00001185 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001186 }
1187
1188 // Check for the "noreturn" attribute.
1189
1190 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1191
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001192 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001193
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001194 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001195
1196 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001197 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001198 else {
1199 // HACK: Some functions are not marked noreturn, and don't return.
1200 // Here are a few hardwired ones. If this takes too long, we can
1201 // potentially cache these results.
1202 const char* s = FD->getIdentifier()->getName();
1203 unsigned n = strlen(s);
1204
1205 switch (n) {
1206 default:
1207 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001208
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001209 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001210 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1211 break;
1212
1213 case 5:
1214 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001215 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001216 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001217 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001218 // FIXME: use Assume to inspect the possible symbolic value of
1219 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001220 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001221 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001222 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001223 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001224 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001225 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001226
1227 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001228 if (!memcmp(s, "Assert", 6)) {
1229 Builder->BuildSinks = true;
1230 break;
1231 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001232
1233 // FIXME: This is just a wrapper around throwing an exception.
1234 // Eventually inter-procedural analysis should handle this easily.
1235 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1236
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001237 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001238
1239 case 7:
1240 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1241 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001242
Ted Kremenekf47bb782008-04-30 17:54:04 +00001243 case 8:
1244 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1245 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001246
1247 case 12:
1248 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1249 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001250
Ted Kremenekf9683082008-09-19 02:30:47 +00001251 case 13:
1252 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1253 break;
1254
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001255 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001256 if (!memcmp(s, "dtrace_assfail", 14) ||
1257 !memcmp(s, "yy_fatal_error", 14))
1258 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001259 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001260
1261 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001262 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1263 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001264 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001265
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001266 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001267 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001268
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001269 }
1270 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001271
1272 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001273
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001274 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001275
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001276 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001277
Ted Kremenek186350f2008-04-23 20:12:28 +00001278 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001279 switch (id) {
1280 case Builtin::BI__builtin_expect: {
1281 // For __builtin_expect, just return the value of the subexpression.
1282 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001283 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001284 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001285 continue;
1286 }
1287
Ted Kremenekb3021332008-11-02 00:35:01 +00001288 case Builtin::BI__builtin_alloca: {
1289 // FIXME: Handle size.
1290 // FIXME: Refactor into StoreManager itself?
1291 MemRegionManager& RM = getStateManager().getRegionManager();
1292 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001293 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001294
1295 // Set the extent of the region in bytes. This enables us to use the
1296 // SVal of the argument directly. If we save the extent in bits, we
1297 // cannot represent values like symbol*8.
1298 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1299 St = getStoreManager().setExtent(St, R, Extent);
1300
Ted Kremenekb3021332008-11-02 00:35:01 +00001301 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1302 continue;
1303 }
1304
Ted Kremenek55aea312008-03-05 22:59:42 +00001305 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001306 break;
1307 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001308 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001309
Ted Kremenek186350f2008-04-23 20:12:28 +00001310 // Check any arguments passed-by-value against being undefined.
1311
1312 bool badArg = false;
1313
1314 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1315 I != E; ++I) {
1316
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001317 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001318 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001319
Ted Kremenek186350f2008-04-23 20:12:28 +00001320 if (N) {
1321 N->markAsSink();
1322 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001323 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001324
Ted Kremenek186350f2008-04-23 20:12:28 +00001325 badArg = true;
1326 break;
1327 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001328 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001329
1330 if (badArg)
1331 continue;
1332
1333 // Dispatch to the plug-in transfer function.
1334
1335 unsigned size = Dst.size();
1336 SaveOr OldHasGen(Builder->HasGeneratedNode);
1337 EvalCall(Dst, CE, L, *DI);
1338
1339 // Handle the case where no nodes where generated. Auto-generate that
1340 // contains the updated state if we aren't generating sinks.
1341
1342 if (!Builder->BuildSinks && Dst.size() == size &&
1343 !Builder->HasGeneratedNode)
1344 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001345 }
1346}
1347
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001348//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001349// Transfer function: Objective-C ivar references.
1350//===----------------------------------------------------------------------===//
1351
1352void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1353 NodeTy* Pred, NodeSet& Dst,
1354 bool asLValue) {
1355
1356 Expr* Base = cast<Expr>(Ex->getBase());
1357 NodeSet Tmp;
1358 Visit(Base, Pred, Tmp);
1359
1360 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1361 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001362 SVal BaseVal = GetSVal(St, Base);
1363 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001364
1365 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001366 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001367 else
1368 EvalLoad(Dst, Ex, *I, St, location);
1369 }
1370}
1371
1372//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001373// Transfer function: Objective-C fast enumeration 'for' statements.
1374//===----------------------------------------------------------------------===//
1375
1376void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1377 NodeTy* Pred, NodeSet& Dst) {
1378
1379 // ObjCForCollectionStmts are processed in two places. This method
1380 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1381 // statements within a basic block. This transfer function does two things:
1382 //
1383 // (1) binds the next container value to 'element'. This creates a new
1384 // node in the ExplodedGraph.
1385 //
1386 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1387 // whether or not the container has any more elements. This value
1388 // will be tested in ProcessBranch. We need to explicitly bind
1389 // this value because a container can contain nil elements.
1390 //
1391 // FIXME: Eventually this logic should actually do dispatches to
1392 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1393 // This will require simulating a temporary NSFastEnumerationState, either
1394 // through an SVal or through the use of MemRegions. This value can
1395 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1396 // terminates we reclaim the temporary (it goes out of scope) and we
1397 // we can test if the SVal is 0 or if the MemRegion is null (depending
1398 // on what approach we take).
1399 //
1400 // For now: simulate (1) by assigning either a symbol or nil if the
1401 // container is empty. Thus this transfer function will by default
1402 // result in state splitting.
1403
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001404 Stmt* elem = S->getElement();
1405 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001406
1407 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001408 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001409 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001410 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1411 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1412 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001413 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001414
1415 NodeSet Tmp;
1416 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001417
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001418 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1419 const GRState* state = GetState(*I);
1420 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1421 }
1422}
1423
1424void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1425 NodeTy* Pred, NodeSet& Dst,
1426 SVal ElementV) {
1427
1428
Ted Kremenekaf337412008-11-12 19:24:17 +00001429
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001430 // Get the current state. Use 'EvalLocation' to determine if it is a null
1431 // pointer, etc.
1432 Stmt* elem = S->getElement();
1433 GRStateRef state = GRStateRef(EvalLocation(elem, Pred, GetState(Pred),
1434 ElementV, false),
1435 getStateManager());
Ted Kremenekaf337412008-11-12 19:24:17 +00001436
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001437 if (!state)
1438 return;
1439
Ted Kremenekaf337412008-11-12 19:24:17 +00001440 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001441 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001442 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1443 GRStateRef hasElems = state.BindExpr(S, TrueV);
1444
Ted Kremenekaf337412008-11-12 19:24:17 +00001445 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001446 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1447 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001448
1449 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1450 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1451 // FIXME: The proper thing to do is to really iterate over the
1452 // container. We will do this with dispatch logic to the store.
1453 // For now, just 'conjure' up a symbolic value.
1454 QualType T = R->getType(getContext());
1455 assert (Loc::IsLocType(T));
1456 unsigned Count = Builder->getCurrentBlockCount();
1457 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1458 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001459
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001460 // Bind the location to 'nil' on the false branch.
1461 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1462 noElems = noElems.BindLoc(ElementV, nilV);
1463 }
1464
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001465 // Create the new nodes.
1466 MakeNode(Dst, S, Pred, hasElems);
1467 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001468}
1469
1470//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001471// Transfer function: Objective-C message expressions.
1472//===----------------------------------------------------------------------===//
1473
1474void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1475 NodeSet& Dst){
1476
1477 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1478 Pred, Dst);
1479}
1480
1481void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001482 ObjCMessageExpr::arg_iterator AI,
1483 ObjCMessageExpr::arg_iterator AE,
1484 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001485 if (AI == AE) {
1486
1487 // Process the receiver.
1488
1489 if (Expr* Receiver = ME->getReceiver()) {
1490 NodeSet Tmp;
1491 Visit(Receiver, Pred, Tmp);
1492
1493 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1494 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1495
1496 return;
1497 }
1498
1499 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1500 return;
1501 }
1502
1503 NodeSet Tmp;
1504 Visit(*AI, Pred, Tmp);
1505
1506 ++AI;
1507
1508 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1509 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1510}
1511
1512void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1513 NodeTy* Pred,
1514 NodeSet& Dst) {
1515
1516 // FIXME: More logic for the processing the method call.
1517
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001518 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001519 bool RaisesException = false;
1520
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001521
1522 if (Expr* Receiver = ME->getReceiver()) {
1523
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001524 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001525
1526 // Check for undefined control-flow or calls to NULL.
1527
1528 if (L.isUndef()) {
1529 NodeTy* N = Builder->generateNode(ME, St, Pred);
1530
1531 if (N) {
1532 N->markAsSink();
1533 UndefReceivers.insert(N);
1534 }
1535
1536 return;
1537 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001538
1539 // Check if the "raise" message was sent.
1540 if (ME->getSelector() == RaiseSel)
1541 RaisesException = true;
1542 }
1543 else {
1544
1545 IdentifierInfo* ClsName = ME->getClassName();
1546 Selector S = ME->getSelector();
1547
1548 // Check for special instance methods.
1549
1550 if (!NSExceptionII) {
1551 ASTContext& Ctx = getContext();
1552
1553 NSExceptionII = &Ctx.Idents.get("NSException");
1554 }
1555
1556 if (ClsName == NSExceptionII) {
1557
1558 enum { NUM_RAISE_SELECTORS = 2 };
1559
1560 // Lazily create a cache of the selectors.
1561
1562 if (!NSExceptionInstanceRaiseSelectors) {
1563
1564 ASTContext& Ctx = getContext();
1565
1566 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1567
1568 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1569 unsigned idx = 0;
1570
1571 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001572 II.push_back(&Ctx.Idents.get("raise"));
1573 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001574 NSExceptionInstanceRaiseSelectors[idx++] =
1575 Ctx.Selectors.getSelector(II.size(), &II[0]);
1576
1577 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001578 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001579 NSExceptionInstanceRaiseSelectors[idx++] =
1580 Ctx.Selectors.getSelector(II.size(), &II[0]);
1581 }
1582
1583 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1584 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1585 RaisesException = true; break;
1586 }
1587 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001588 }
1589
1590 // Check for any arguments that are uninitialized/undefined.
1591
1592 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1593 I != E; ++I) {
1594
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001595 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001596
1597 // Generate an error node for passing an uninitialized/undefined value
1598 // as an argument to a message expression. This node is a sink.
1599 NodeTy* N = Builder->generateNode(ME, St, Pred);
1600
1601 if (N) {
1602 N->markAsSink();
1603 MsgExprUndefArgs[N] = *I;
1604 }
1605
1606 return;
1607 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001608 }
1609
1610 // Check if we raise an exception. For now treat these as sinks. Eventually
1611 // we will want to handle exceptions properly.
1612
1613 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1614
1615 if (RaisesException)
1616 Builder->BuildSinks = true;
1617
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001618 // Dispatch to plug-in transfer function.
1619
1620 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001621 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001622
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001623 EvalObjCMessageExpr(Dst, ME, Pred);
1624
1625 // Handle the case where no nodes where generated. Auto-generate that
1626 // contains the updated state if we aren't generating sinks.
1627
Ted Kremenekb0533962008-04-18 20:35:30 +00001628 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001629 MakeNode(Dst, ME, Pred, St);
1630}
1631
1632//===----------------------------------------------------------------------===//
1633// Transfer functions: Miscellaneous statements.
1634//===----------------------------------------------------------------------===//
1635
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001636void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001637 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001638 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001639 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001640
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001641 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001642 T = ExCast->getTypeAsWritten();
1643
Zhongxing Xued340f72008-10-22 08:02:16 +00001644 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001645 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001646 else
1647 Visit(Ex, Pred, S1);
1648
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001649 // Check for casting to "void".
1650 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001651
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001652 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001653 Dst.Add(*I1);
1654
Ted Kremenek874d63f2008-01-24 02:02:54 +00001655 return;
1656 }
1657
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001658 // FIXME: The rest of this should probably just go into EvalCall, and
1659 // let the transfer function object be responsible for constructing
1660 // nodes.
1661
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001662 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001663 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001664 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001665 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001666
1667 // Unknown?
1668
1669 if (V.isUnknown()) {
1670 Dst.Add(N);
1671 continue;
1672 }
1673
1674 // Undefined?
1675
1676 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001677 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001678 continue;
1679 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001680
1681 // For const casts, just propagate the value.
1682 ASTContext& C = getContext();
1683
1684 if (C.getCanonicalType(T).getUnqualifiedType() ==
1685 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001686 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001687 continue;
1688 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001689
1690 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001691 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001692 unsigned bits = getContext().getTypeSize(ExTy);
1693
1694 // FIXME: Determine if the number of bits of the target type is
1695 // equal or exceeds the number of bits to store the pointer value.
1696 // If not, flag an error.
1697
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001698 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001699 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001700 continue;
1701 }
1702
1703 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001704 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1705 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001706 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001707 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001708 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001709 continue;
1710 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001711
Zhongxing Xu37d682a2008-11-14 09:23:38 +00001712 // Check for casts from array type to pointer type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001713 if (ExTy->isArrayType()) {
Ted Kremenek0fb7c612008-11-15 05:00:27 +00001714 assert(T->isPointerType());
Zhongxing Xue1911af2008-10-23 03:10:39 +00001715 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001716 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001717 continue;
1718 }
1719
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001720 // Check for casts from AllocaRegion pointer to typed pointer.
1721 if (isa<loc::MemRegionVal>(V)) {
1722 assert(Loc::IsLocType(T));
1723 assert(Loc::IsLocType(ExTy));
1724
1725 // Delegate to store manager.
Zhongxing Xucb529b52008-11-16 07:06:26 +00001726 std::pair<const GRState*, SVal> Res =
1727 getStoreManager().CastRegion(St, V, T, CastE);
1728
1729 const GRState* NewSt = Res.first;
1730 SVal NewPtr = Res.second;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001731
1732 // If no new region is created, fall through to the default case.
1733 if (NewSt != St) {
Zhongxing Xucb529b52008-11-16 07:06:26 +00001734 MakeNode(Dst, CastE, N, BindExpr(NewSt, CastE, NewPtr));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001735 continue;
1736 }
1737 }
1738
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001739 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001740 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001741 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001742}
1743
Ted Kremenek4f090272008-10-27 21:54:31 +00001744void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001745 NodeTy* Pred, NodeSet& Dst,
1746 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001747 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1748 NodeSet Tmp;
1749 Visit(ILE, Pred, Tmp);
1750
1751 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001752 const GRState* St = GetState(*I);
1753 SVal ILV = GetSVal(St, ILE);
1754 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001755
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001756 if (asLValue)
1757 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1758 else
1759 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001760 }
1761}
1762
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001763void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001764
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001765 // The CFG has one DeclStmt per Decl.
1766 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001767
1768 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001769 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001770
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001771 const VarDecl* VD = dyn_cast<VarDecl>(D);
1772
Ted Kremenekaf337412008-11-12 19:24:17 +00001773 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001774
1775 // FIXME: static variables may have an initializer, but the second
1776 // time a function is called those values may not be current.
1777 NodeSet Tmp;
1778
Ted Kremenekaf337412008-11-12 19:24:17 +00001779 if (InitEx)
1780 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001781
1782 if (Tmp.empty())
1783 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001784
1785 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001786 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001787 unsigned Count = Builder->getCurrentBlockCount();
1788
1789 if (InitEx) {
1790 SVal InitVal = GetSVal(St, InitEx);
1791 QualType T = VD->getType();
1792
1793 // Recover some path-sensitivity if a scalar value evaluated to
1794 // UnknownVal.
1795 if (InitVal.isUnknown()) {
1796 if (Loc::IsLocType(T)) {
1797 SymbolID Sym = SymMgr.getConjuredSymbol(InitEx, Count);
1798 InitVal = loc::SymbolVal(Sym);
1799 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001800 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenekaf337412008-11-12 19:24:17 +00001801 SymbolID Sym = SymMgr.getConjuredSymbol(InitEx, Count);
1802 InitVal = nonloc::SymbolVal(Sym);
1803 }
1804 }
1805
1806 St = StateMgr.BindDecl(St, VD, &InitVal, Count);
1807 }
1808 else
1809 St = StateMgr.BindDecl(St, VD, 0, Count);
1810
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001811 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001812 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001813}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001814
Ted Kremenekf75b1862008-10-30 17:47:32 +00001815namespace {
1816 // This class is used by VisitInitListExpr as an item in a worklist
1817 // for processing the values contained in an InitListExpr.
1818class VISIBILITY_HIDDEN InitListWLItem {
1819public:
1820 llvm::ImmutableList<SVal> Vals;
1821 GRExprEngine::NodeTy* N;
1822 InitListExpr::reverse_iterator Itr;
1823
1824 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1825 InitListExpr::reverse_iterator itr)
1826 : Vals(vals), N(n), Itr(itr) {}
1827};
1828}
1829
1830
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001831void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1832 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001833
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001834 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001835 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001836 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001837
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001838 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001839
Ted Kremeneka49e3672008-10-30 23:14:36 +00001840 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001841
Ted Kremeneka49e3672008-10-30 23:14:36 +00001842 // Handle base case where the initializer has no elements.
1843 // e.g: static int* myArray[] = {};
1844 if (NumInitElements == 0) {
1845 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1846 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1847 return;
1848 }
1849
1850 // Create a worklist to process the initializers.
1851 llvm::SmallVector<InitListWLItem, 10> WorkList;
1852 WorkList.reserve(NumInitElements);
1853 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001854 InitListExpr::reverse_iterator ItrEnd = E->rend();
1855
Ted Kremeneka49e3672008-10-30 23:14:36 +00001856 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001857 while (!WorkList.empty()) {
1858 InitListWLItem X = WorkList.back();
1859 WorkList.pop_back();
1860
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001861 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001862 Visit(*X.Itr, X.N, Tmp);
1863
1864 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001865
Ted Kremenekf75b1862008-10-30 17:47:32 +00001866 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1867 // Get the last initializer value.
1868 state = GetState(*NI);
1869 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1870
1871 // Construct the new list of values by prepending the new value to
1872 // the already constructed list.
1873 llvm::ImmutableList<SVal> NewVals =
1874 getBasicVals().consVals(InitV, X.Vals);
1875
1876 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001877 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001878 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001879
Ted Kremenekf75b1862008-10-30 17:47:32 +00001880 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001881 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001882 }
1883 else {
1884 // Still some initializer values to go. Push them onto the worklist.
1885 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1886 }
1887 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001888 }
Ted Kremenek87903072008-10-30 18:34:31 +00001889
1890 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001891 }
1892
Ted Kremenek062e2f92008-11-13 06:10:40 +00001893 if (T->isUnionType() || T->isVectorType()) {
1894 // FIXME: to be implemented.
1895 // Note: That vectors can return true for T->isIntegerType()
1896 MakeNode(Dst, E, Pred, state);
1897 return;
1898 }
1899
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001900 if (Loc::IsLocType(T) || T->isIntegerType()) {
1901 assert (E->getNumInits() == 1);
1902 NodeSet Tmp;
1903 Expr* Init = E->getInit(0);
1904 Visit(Init, Pred, Tmp);
1905 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1906 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001907 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001908 }
1909 return;
1910 }
1911
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001912
1913 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1914 assert(0 && "unprocessed InitListExpr type");
1915}
Ted Kremenekf233d482008-02-05 00:26:40 +00001916
Sebastian Redl05189992008-11-11 17:56:53 +00001917/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1918void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1919 NodeTy* Pred,
1920 NodeSet& Dst) {
1921 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001922 uint64_t amt;
1923
1924 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001925
Ted Kremenek87e80342008-03-15 03:13:20 +00001926 // FIXME: Add support for VLAs.
1927 if (!T.getTypePtr()->isConstantSizeType())
1928 return;
1929
Ted Kremenekf342d182008-04-30 21:31:12 +00001930 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1931 // the compiler has laid out its representation. Just report Unknown
1932 // for these.
1933 if (T->isObjCInterfaceType())
1934 return;
1935
Ted Kremenek87e80342008-03-15 03:13:20 +00001936 amt = 1; // Handle sizeof(void)
1937
1938 if (T != getContext().VoidTy)
1939 amt = getContext().getTypeSize(T) / 8;
1940
1941 }
1942 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001943 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001944
Ted Kremenek0e561a32008-03-21 21:30:14 +00001945 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001946 BindExpr(GetState(Pred), Ex,
1947 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001948}
1949
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001950
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001951void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001952 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001953
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001954 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001955
1956 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001957 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001958
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001959 case UnaryOperator::Deref: {
1960
1961 Expr* Ex = U->getSubExpr()->IgnoreParens();
1962 NodeSet Tmp;
1963 Visit(Ex, Pred, Tmp);
1964
1965 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001966
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001967 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001968 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001969
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001970 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001971 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001972 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001973 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001974 }
1975
1976 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001977 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001978
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001979 case UnaryOperator::Real: {
1980
1981 Expr* Ex = U->getSubExpr()->IgnoreParens();
1982 NodeSet Tmp;
1983 Visit(Ex, Pred, Tmp);
1984
1985 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1986
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001987 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001988 if (Ex->getType()->isAnyComplexType()) {
1989 // Just report "Unknown."
1990 Dst.Add(*I);
1991 continue;
1992 }
1993
1994 // For all other types, UnaryOperator::Real is an identity operation.
1995 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001996 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001997 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001998 }
1999
2000 return;
2001 }
2002
2003 case UnaryOperator::Imag: {
2004
2005 Expr* Ex = U->getSubExpr()->IgnoreParens();
2006 NodeSet Tmp;
2007 Visit(Ex, Pred, Tmp);
2008
2009 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002010 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002011 if (Ex->getType()->isAnyComplexType()) {
2012 // Just report "Unknown."
2013 Dst.Add(*I);
2014 continue;
2015 }
2016
2017 // For all other types, UnaryOperator::Float returns 0.
2018 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002019 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002020 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002021 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002022 }
2023
2024 return;
2025 }
2026
2027 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002028 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002029 Dst.Add(Pred);
2030 return;
2031
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002032 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002033 case UnaryOperator::Extension: {
2034
2035 // Unary "+" is a no-op, similar to a parentheses. We still have places
2036 // where it may be a block-level expression, so we need to
2037 // generate an extra node that just propagates the value of the
2038 // subexpression.
2039
2040 Expr* Ex = U->getSubExpr()->IgnoreParens();
2041 NodeSet Tmp;
2042 Visit(Ex, Pred, Tmp);
2043
2044 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002045 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002046 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002047 }
2048
2049 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002050 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002051
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002052 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002053
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002054 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002055 Expr* Ex = U->getSubExpr()->IgnoreParens();
2056 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002057 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002058
2059 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002060 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002061 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002062 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002063 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002064 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002065
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002066 return;
2067 }
2068
2069 case UnaryOperator::LNot:
2070 case UnaryOperator::Minus:
2071 case UnaryOperator::Not: {
2072
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002073 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002074 Expr* Ex = U->getSubExpr()->IgnoreParens();
2075 NodeSet Tmp;
2076 Visit(Ex, Pred, Tmp);
2077
2078 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002079 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002080
2081 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002082 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002083
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002084 if (V.isUnknownOrUndef()) {
2085 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2086 continue;
2087 }
2088
Ted Kremenek60595da2008-11-15 04:01:56 +00002089// QualType DstT = getContext().getCanonicalType(U->getType());
2090// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2091//
2092// if (DstT != SrcT) // Perform promotions.
2093// V = EvalCast(V, DstT);
2094//
2095// if (V.isUnknownOrUndef()) {
2096// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2097// continue;
2098// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002099
2100 switch (U->getOpcode()) {
2101 default:
2102 assert(false && "Invalid Opcode.");
2103 break;
2104
2105 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002106 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002107 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002108 break;
2109
2110 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002111 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002112 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002113 break;
2114
2115 case UnaryOperator::LNot:
2116
2117 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2118 //
2119 // Note: technically we do "E == 0", but this is the same in the
2120 // transfer functions as "0 == E".
2121
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002122 if (isa<Loc>(V)) {
2123 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2124 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002125 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002126 }
2127 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002128 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002129#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002130 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2131 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002132#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002133 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002134 continue;
2135#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002136 }
2137
2138 break;
2139 }
2140
2141 MakeNode(Dst, U, *I, St);
2142 }
2143
2144 return;
2145 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002146 }
2147
2148 // Handle ++ and -- (both pre- and post-increment).
2149
2150 assert (U->isIncrementDecrementOp());
2151 NodeSet Tmp;
2152 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002153 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002154
2155 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2156
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002157 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002158 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002159
2160 // Perform a load.
2161 NodeSet Tmp2;
2162 EvalLoad(Tmp2, Ex, *I, St, V1);
2163
2164 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2165
2166 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002167 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002168
2169 // Propagate unknown and undefined values.
2170 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002171 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002172 continue;
2173 }
2174
Ted Kremenek443003b2008-02-21 19:29:23 +00002175 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002176
2177 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2178 : BinaryOperator::Sub;
2179
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002180 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002181 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002182
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002183 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002184 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002185 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002186 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002187}
2188
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002189void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2190 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2191}
2192
2193void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2194 AsmStmt::outputs_iterator I,
2195 AsmStmt::outputs_iterator E,
2196 NodeTy* Pred, NodeSet& Dst) {
2197 if (I == E) {
2198 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2199 return;
2200 }
2201
2202 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002203 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002204
2205 ++I;
2206
2207 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2208 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2209}
2210
2211void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2212 AsmStmt::inputs_iterator I,
2213 AsmStmt::inputs_iterator E,
2214 NodeTy* Pred, NodeSet& Dst) {
2215 if (I == E) {
2216
2217 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002218 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002219
2220 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2221 // which interprets the inline asm and stores proper results in the
2222 // outputs.
2223
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002224 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002225
2226 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2227 OE = A->end_outputs(); OI != OE; ++OI) {
2228
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002229 SVal X = GetSVal(St, *OI);
2230 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002231
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002232 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002233 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002234 }
2235
Ted Kremenek0e561a32008-03-21 21:30:14 +00002236 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002237 return;
2238 }
2239
2240 NodeSet Tmp;
2241 Visit(*I, Pred, Tmp);
2242
2243 ++I;
2244
2245 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2246 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2247}
2248
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002249void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2250 assert (Builder && "GRStmtNodeBuilder must be defined.");
2251
2252 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002253
Ted Kremenek186350f2008-04-23 20:12:28 +00002254 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2255 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002256
Ted Kremenek729a9a22008-07-17 23:15:45 +00002257 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002258
Ted Kremenekb0533962008-04-18 20:35:30 +00002259 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002260
Ted Kremenekb0533962008-04-18 20:35:30 +00002261 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002262 MakeNode(Dst, S, Pred, GetState(Pred));
2263}
2264
Ted Kremenek02737ed2008-03-31 15:02:58 +00002265void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2266
2267 Expr* R = S->getRetValue();
2268
2269 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002270 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002271 return;
2272 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002273
Ted Kremenek5917d782008-11-21 00:27:44 +00002274 NodeSet Tmp;
2275 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002276
Ted Kremenek5917d782008-11-21 00:27:44 +00002277 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2278 SVal X = GetSVal((*I)->getState(), R);
2279
2280 // Check if we return the address of a stack variable.
2281 if (isa<loc::MemRegionVal>(X)) {
2282 // Determine if the value is on the stack.
2283 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002284
Ted Kremenek5917d782008-11-21 00:27:44 +00002285 if (R && getStateManager().hasStackStorage(R)) {
2286 // Create a special node representing the error.
2287 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2288 N->markAsSink();
2289 RetsStackAddr.insert(N);
2290 }
2291 continue;
2292 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002293 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002294 // Check if we return an undefined value.
2295 else if (X.isUndef()) {
2296 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2297 N->markAsSink();
2298 RetsUndef.insert(N);
2299 }
2300 continue;
2301 }
2302
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002303 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002304 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002305}
Ted Kremenek55deb972008-03-25 00:34:37 +00002306
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002307//===----------------------------------------------------------------------===//
2308// Transfer functions: Binary operators.
2309//===----------------------------------------------------------------------===//
2310
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002311const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2312 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002313
2314 // Divide by undefined? (potentially zero)
2315
2316 if (Denom.isUndef()) {
2317 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2318
2319 if (DivUndef) {
2320 DivUndef->markAsSink();
2321 ExplicitBadDivides.insert(DivUndef);
2322 }
2323
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002324 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002325 }
2326
2327 // Check for divide/remainder-by-zero.
2328 // First, "assume" that the denominator is 0 or undefined.
2329
2330 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002331 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002332
2333 // Second, "assume" that the denominator cannot be 0.
2334
2335 bool isFeasibleNotZero = false;
2336 St = Assume(St, Denom, true, isFeasibleNotZero);
2337
2338 // Create the node for the divide-by-zero (if it occurred).
2339
2340 if (isFeasibleZero)
2341 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2342 DivZeroNode->markAsSink();
2343
2344 if (isFeasibleNotZero)
2345 ImplicitBadDivides.insert(DivZeroNode);
2346 else
2347 ExplicitBadDivides.insert(DivZeroNode);
2348
2349 }
2350
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002351 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002352}
2353
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002354void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002355 GRExprEngine::NodeTy* Pred,
2356 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002357
2358 NodeSet Tmp1;
2359 Expr* LHS = B->getLHS()->IgnoreParens();
2360 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002361
2362 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002363 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002364 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002365 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002366
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002367 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002368
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002369 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002370
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002371 // Process the RHS.
2372
2373 NodeSet Tmp2;
2374 Visit(RHS, *I1, Tmp2);
2375
2376 // With both the LHS and RHS evaluated, process the operation itself.
2377
2378 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002379
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002380 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002381 const GRState* OldSt = St;
2382
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002383 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002384 BinaryOperator::Opcode Op = B->getOpcode();
2385
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002386 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002387
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002388 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002389
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002390 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002391 // FIXME: Handle structs.
2392 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002393
Ted Kremenek062e2f92008-11-13 06:10:40 +00002394 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2395 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002396 unsigned Count = Builder->getCurrentBlockCount();
2397 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2398
Ted Kremenek062e2f92008-11-13 06:10:40 +00002399 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002400 ? cast<SVal>(loc::SymbolVal(Sym))
2401 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002402 }
2403
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002404 // Simulate the effects of a "store": bind the value of the RHS
2405 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002406
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002407 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002408 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002409 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002410
2411 case BinaryOperator::Div:
2412 case BinaryOperator::Rem:
2413
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002414 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002415 if (RHS->getType()->isIntegerType() &&
2416 RHS->getType()->isScalarType()) {
2417
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002418 St = CheckDivideZero(B, St, *I2, RightV);
2419 if (!St) continue;
2420 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002421
2422 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002423
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002424 default: {
2425
2426 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002427 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002428
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002429 // Process non-assignements except commas or short-circuited
2430 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002431
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002432 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002433
2434 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002435 if (OldSt != St) {
2436 // Generate a new node if we have already created a new state.
2437 MakeNode(Dst, B, *I2, St);
2438 }
2439 else
2440 Dst.Add(*I2);
2441
Ted Kremenek89063af2008-02-21 19:15:37 +00002442 continue;
2443 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002444
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002445 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002446
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002447 // The operands were *not* undefined, but the result is undefined.
2448 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002449
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002450 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002451 UndefNode->markAsSink();
2452 UndefResults.insert(UndefNode);
2453 }
2454
2455 continue;
2456 }
2457
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002458 // Otherwise, create a new node.
2459
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002460 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002461 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002462 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002463 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002464
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002465 assert (B->isCompoundAssignmentOp());
2466
Ted Kremenek934e3e92008-10-27 23:02:39 +00002467 if (Op >= BinaryOperator::AndAssign) {
2468 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2469 BinaryOperator::And));
2470 }
2471 else {
2472 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2473 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002474
2475 // Perform a load (the LHS). This performs the checks for
2476 // null dereferences, and so on.
2477 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002478 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002479 EvalLoad(Tmp3, LHS, *I2, St, location);
2480
2481 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2482
2483 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002484 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002485
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002486 // Check for divide-by-zero.
2487 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002488 && RHS->getType()->isIntegerType()
2489 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002490
2491 // CheckDivideZero returns a new state where the denominator
2492 // is assumed to be non-zero.
2493 St = CheckDivideZero(B, St, *I3, RightV);
2494
2495 if (!St)
2496 continue;
2497 }
2498
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002499 // Propagate undefined values (left-side).
2500 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002501 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002502 continue;
2503 }
2504
2505 // Propagate unknown values (left and right-side).
2506 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002507 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002508 UnknownVal());
2509 continue;
2510 }
2511
2512 // At this point:
2513 //
2514 // The LHS is not Undef/Unknown.
2515 // The RHS is not Unknown.
2516
2517 // Get the computation type.
2518 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002519 CTy = getContext().getCanonicalType(CTy);
2520
2521 QualType LTy = getContext().getCanonicalType(LHS->getType());
2522 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002523
2524 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002525 if (LTy != CTy) V = EvalCast(V, CTy);
2526 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002527
2528 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002529 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002530 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002531 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002532 continue;
2533 }
2534
Ted Kremenek60595da2008-11-15 04:01:56 +00002535 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002536 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002537
2538 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002539 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002540 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2541 UndefNode->markAsSink();
2542 UndefResults.insert(UndefNode);
2543 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002544 continue;
2545 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002546
2547 // EXPERIMENTAL: "Conjured" symbols.
2548 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002549
2550 SVal LHSVal;
2551
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002552 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2553 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002554
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002555 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002556
Ted Kremenek60595da2008-11-15 04:01:56 +00002557 // The symbolic value is actually for the type of the left-hand side
2558 // expression, not the computation type, as this is the value the
2559 // LValue on the LHS will bind to.
2560 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
2561 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002562 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002563 : cast<SVal>(nonloc::SymbolVal(Sym));
2564
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002565 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002566 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002567 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002568 else {
2569 // The left-hand side may bind to a different value then the
2570 // computation type.
2571 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2572 }
2573
2574 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002575 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002576 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002577 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002578}
Ted Kremenekee985462008-01-16 18:18:48 +00002579
2580//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002581// Transfer-function Helpers.
2582//===----------------------------------------------------------------------===//
2583
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002584void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002585 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002586 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002587 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002588
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002589 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002590 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2591
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002592 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002593 MakeNode(Dst, Ex, Pred, *I);
2594}
2595
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002596void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002597 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002598 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002599
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002600 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002601 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002602}
2603
2604//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002605// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002606//===----------------------------------------------------------------------===//
2607
Ted Kremenekaa66a322008-01-16 21:46:15 +00002608#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002609static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002610static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002611
Ted Kremenekaa66a322008-01-16 21:46:15 +00002612namespace llvm {
2613template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002614struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002615 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002616
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002617 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2618
2619 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002620 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002621 GraphPrintCheckerState->isUndefDeref(N) ||
2622 GraphPrintCheckerState->isUndefStore(N) ||
2623 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002624 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2625 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002626 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002627 GraphPrintCheckerState->isBadCall(N) ||
2628 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002629 return "color=\"red\",style=\"filled\"";
2630
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002631 if (GraphPrintCheckerState->isNoReturnCall(N))
2632 return "color=\"blue\",style=\"filled\"";
2633
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002634 return "";
2635 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002636
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002637 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002638 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002639
2640 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002641 ProgramPoint Loc = N->getLocation();
2642
2643 switch (Loc.getKind()) {
2644 case ProgramPoint::BlockEntranceKind:
2645 Out << "Block Entrance: B"
2646 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2647 break;
2648
2649 case ProgramPoint::BlockExitKind:
2650 assert (false);
2651 break;
2652
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002653 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002654 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002655 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002656 const PostStmt& L = cast<PostStmt>(Loc);
2657 Stmt* S = L.getStmt();
2658 SourceLocation SLoc = S->getLocStart();
2659
2660 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002661 llvm::raw_os_ostream OutS(Out);
2662 S->printPretty(OutS);
2663 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002664
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002665 if (SLoc.isFileID()) {
2666 Out << "\\lline="
2667 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002668 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002669 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002670
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002671 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002672 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002673 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002674 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002675 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002676 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002677 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002678 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002679 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2680 Out << "\\|Explicit divide-by zero or undefined value.";
2681 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2682 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002683 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002684 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002685 else if (GraphPrintCheckerState->isNoReturnCall(N))
2686 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002687 else if (GraphPrintCheckerState->isBadCall(N))
2688 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002689 else if (GraphPrintCheckerState->isUndefArg(N))
2690 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002691
Ted Kremenekaa66a322008-01-16 21:46:15 +00002692 break;
2693 }
2694
2695 default: {
2696 const BlockEdge& E = cast<BlockEdge>(Loc);
2697 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2698 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002699
2700 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002701
2702 SourceLocation SLoc = T->getLocStart();
2703
Ted Kremenekb38911f2008-01-30 23:03:39 +00002704 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002705
Ted Kremeneka95d3752008-09-13 05:16:45 +00002706 llvm::raw_os_ostream OutS(Out);
2707 E.getSrc()->printTerminator(OutS);
2708 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002709
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002710 if (SLoc.isFileID()) {
2711 Out << "\\lline="
2712 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2713 << GraphPrintSourceManager->getColumnNumber(SLoc);
2714 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002715
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002716 if (isa<SwitchStmt>(T)) {
2717 Stmt* Label = E.getDst()->getLabel();
2718
2719 if (Label) {
2720 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2721 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002722 llvm::raw_os_ostream OutS(Out);
2723 C->getLHS()->printPretty(OutS);
2724 OutS.flush();
2725
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002726 if (Stmt* RHS = C->getRHS()) {
2727 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002728 RHS->printPretty(OutS);
2729 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002730 }
2731
2732 Out << ":";
2733 }
2734 else {
2735 assert (isa<DefaultStmt>(Label));
2736 Out << "\\ldefault:";
2737 }
2738 }
2739 else
2740 Out << "\\l(implicit) default:";
2741 }
2742 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002743 // FIXME
2744 }
2745 else {
2746 Out << "\\lCondition: ";
2747 if (*E.getSrc()->succ_begin() == E.getDst())
2748 Out << "true";
2749 else
2750 Out << "false";
2751 }
2752
2753 Out << "\\l";
2754 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002755
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002756 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2757 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002758 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002759 }
2760 }
2761
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002762 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002763
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002764 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2765 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002766
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002767 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002768 return Out.str();
2769 }
2770};
2771} // end llvm namespace
2772#endif
2773
Ted Kremenekffe0f432008-03-07 22:58:01 +00002774#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002775
2776template <typename ITERATOR>
2777GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2778
2779template <>
2780GRExprEngine::NodeTy*
2781GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2782 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2783 return I->first;
2784}
2785
Ted Kremenekffe0f432008-03-07 22:58:01 +00002786template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002787static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002788 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002789
Ted Kremenekd4527582008-09-16 18:44:52 +00002790 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002791
2792 for ( ; I != E; ++I ) {
2793 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002794 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002795
Ted Kremenekd4527582008-09-16 18:44:52 +00002796 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002797 continue;
2798
Ted Kremenekd4527582008-09-16 18:44:52 +00002799 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002800 Sources.push_back(N);
2801 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002802}
2803#endif
2804
2805void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002806#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002807 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002808 std::vector<NodeTy*> Src;
2809
2810 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002811 AddSources(Src, null_derefs_begin(), null_derefs_end());
2812 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2813 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2814 AddSources(Src, undef_results_begin(), undef_results_end());
2815 AddSources(Src, bad_calls_begin(), bad_calls_end());
2816 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002817 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002818
Ted Kremenekcb612922008-04-18 19:23:43 +00002819 // The new way.
2820 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2821 (*I)->GetErrorNodes(Src);
2822
2823
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002824 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002825 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002826 else {
2827 GraphPrintCheckerState = this;
2828 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002829
Ted Kremenekffe0f432008-03-07 22:58:01 +00002830 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002831
2832 GraphPrintCheckerState = NULL;
2833 GraphPrintSourceManager = NULL;
2834 }
2835#endif
2836}
2837
2838void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2839#ifndef NDEBUG
2840 GraphPrintCheckerState = this;
2841 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002842
Ted Kremenek493d7a22008-03-11 18:25:33 +00002843 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2844
2845 if (!TrimmedG)
2846 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2847 else {
2848 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2849 delete TrimmedG;
2850 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002851
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002852 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002853 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002854#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002855}