blob: 71d7ed34035564d076b950a465a9bf4cac19b622 [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 Kremenekb387a3f2008-02-14 22:16:04 +000020
Ted Kremenek0f5f0592008-02-27 06:07:00 +000021#ifndef NDEBUG
22#include "llvm/Support/GraphWriter.h"
23#include <sstream>
24#endif
25
Ted Kremenekb387a3f2008-02-14 22:16:04 +000026using namespace clang;
27using llvm::dyn_cast;
28using llvm::cast;
29using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000030
Ted Kremeneke695e1c2008-04-15 23:06:53 +000031//===----------------------------------------------------------------------===//
32// Engine construction and deletion.
33//===----------------------------------------------------------------------===//
34
Ted Kremeneke448ab42008-05-01 18:33:28 +000035static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
36 IdentifierInfo* II = &Ctx.Idents.get(name);
37 return Ctx.Selectors.getSelector(0, &II);
38}
39
Ted Kremenekdaa497e2008-03-09 18:05:48 +000040
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000041GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx)
42 : CoreEngine(cfg, CD, Ctx, *this),
43 G(CoreEngine.getGraph()),
44 Liveness(G.getCFG()),
45 Builder(NULL),
46 StateMgr(G.getContext(), G.getAllocator()),
47 BasicVals(StateMgr.getBasicValueFactory()),
48 TF(NULL), // FIXME
49 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +000050 CurrentStmt(NULL),
51 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
52 RaiseSel(GetNullarySelector("raise", G.getContext())) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000053
54 // Compute liveness information.
55 Liveness.runOnCFG(G.getCFG());
56 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
57}
58
59GRExprEngine::~GRExprEngine() {
60 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
61 delete *I;
62
63 for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end();
64 I != E; ++I)
65 delete *I;
66
67 for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end();
68 I != E; ++I)
69 delete *I;
Ted Kremeneke448ab42008-05-01 18:33:28 +000070
71 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000072}
73
Ted Kremeneke695e1c2008-04-15 23:06:53 +000074//===----------------------------------------------------------------------===//
75// Utility methods.
76//===----------------------------------------------------------------------===//
77
78// SaveAndRestore - A utility class that uses RIIA to save and restore
79// the value of a variable.
80template<typename T>
81struct VISIBILITY_HIDDEN SaveAndRestore {
82 SaveAndRestore(T& x) : X(x), old_value(x) {}
83 ~SaveAndRestore() { X = old_value; }
84 T get() { return old_value; }
85
86 T& X;
87 T old_value;
88};
89
Ted Kremenek186350f2008-04-23 20:12:28 +000090// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
91// value of a variable is saved, and during the dstor the old value is
92// or'ed with the new value.
93struct VISIBILITY_HIDDEN SaveOr {
94 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
95 ~SaveOr() { X |= old_value; }
96
97 bool& X;
98 bool old_value;
99};
100
101
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000102void GRExprEngine::EmitWarnings(Diagnostic& Diag, PathDiagnosticClient* PD) {
103 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
104 BugReporter BR(Diag, PD, getContext(), *this);
105 (*I)->EmitWarnings(BR);
106 }
107
108 for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end();
109 I != E; ++I) {
110 BugReporter BR(Diag, PD, getContext(), *this);
111 (*I)->EmitWarnings(BR);
112 }
113
114 for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end();
115 I != E; ++I) {
116 BugReporter BR(Diag, PD, getContext(), *this);
117 (*I)->EmitWarnings(BR);
118 }
119}
120
121void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
122 TF = tf;
123 TF->RegisterChecks(*this);
124}
125
126void GRExprEngine::AddCallCheck(GRSimpleAPICheck* A) {
127 CallChecks.push_back(A);
128}
129
130void GRExprEngine::AddObjCMessageExprCheck(GRSimpleAPICheck* A) {
131 MsgExprChecks.push_back(A);
132}
133
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000134ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +0000135
136 // The LiveVariables information already has a compilation of all VarDecls
137 // used in the function. Iterate through this set, and "symbolicate"
138 // any VarDecl whose value originally comes from outside the function.
139
140 typedef LiveVariables::AnalysisDataTy LVDataTy;
141 LVDataTy& D = Liveness.getAnalysisData();
142
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000143 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +0000144
145 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
146
147 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
148
149 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
150 RVal X = RVal::GetSymbolValue(SymMgr, VD);
151 StateMgr.BindVar(StateImpl, VD, X);
152 }
153 }
154
155 return StateMgr.getPersistentState(StateImpl);
156}
157
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000158ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000159
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000160 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000161
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000162 if (Ex == CurrentStmt) {
163 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000164
165 if (!isBlkExpr)
166 return St;
167 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000168
Ted Kremenek436f2b92008-04-30 04:23:07 +0000169 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, true);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000170}
171
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000172//===----------------------------------------------------------------------===//
173// Top-level transfer function logic (Dispatcher).
174//===----------------------------------------------------------------------===//
175
176void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
177
178 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000179 EntryNode = builder.getLastNode();
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000180 CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000181
182 // Set up our simple checks.
183
184 // FIXME: This can probably be installed directly in GRCoreEngine, obviating
185 // the need to do a copy every time we hit a block-level statement.
186
187 if (!MsgExprChecks.empty())
188 Builder->setObjCMsgExprAuditors((GRAuditor<ValueState>**) &MsgExprChecks[0],
189 (GRAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size()));
190
191
192 if (!CallChecks.empty())
193 Builder->setCallExprAuditors((GRAuditor<ValueState>**) &CallChecks[0],
194 (GRAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size()));
195
196 // Create the cleaned state.
197
Ted Kremenek846d4e92008-04-24 23:35:58 +0000198 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
199 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000200
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000201 // Process any special transfer function for dead symbols.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000202
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000203 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000204
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000205 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000206 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000207 else {
208 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000209 SaveOr OldHasGen(Builder->HasGeneratedNode);
210
Ted Kremenek910e9992008-04-25 01:25:15 +0000211 TF->EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
212 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000213
214 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
215 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000216 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000217
218 bool HasAutoGenerated = false;
219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221
222 NodeSet Dst;
223
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000224 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000225 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
226
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000227 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000228 Visit(S, *I, Dst);
229
230 // Do we need to auto-generate a node? We only need to do this to generate
231 // a node with a "cleaned" state; GRCoreEngine will actually handle
232 // auto-transitions for other cases.
233 if (Dst.size() == 1 && *Dst.begin() == EntryNode
234 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
235 HasAutoGenerated = true;
236 builder.generateNode(S, GetState(EntryNode), *I);
237 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000239
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000240 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000241 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242 EntryNode = NULL;
243 CurrentStmt = NULL;
244 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000245}
246
247void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
248
249 // FIXME: add metadata to the CFG so that we can disable
250 // this check when we KNOW that there is no block-level subexpression.
251 // The motivation is that this check requires a hashtable lookup.
252
253 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
254 Dst.Add(Pred);
255 return;
256 }
257
258 switch (S->getStmtClass()) {
259
260 default:
261 // Cases we intentionally have "default" handle:
262 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
263
264 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
265 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000266
267 case Stmt::ArraySubscriptExprClass:
268 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
269 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000270
271 case Stmt::AsmStmtClass:
272 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
273 break;
274
275 case Stmt::BinaryOperatorClass: {
276 BinaryOperator* B = cast<BinaryOperator>(S);
277
278 if (B->isLogicalOp()) {
279 VisitLogicalExpr(B, Pred, Dst);
280 break;
281 }
282 else if (B->getOpcode() == BinaryOperator::Comma) {
283 ValueState* St = GetState(Pred);
284 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
285 break;
286 }
287
288 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
289 break;
290 }
291
292 case Stmt::CallExprClass: {
293 CallExpr* C = cast<CallExpr>(S);
294 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
295 break;
296 }
297
298 case Stmt::CastExprClass: {
299 CastExpr* C = cast<CastExpr>(S);
300 VisitCast(C, C->getSubExpr(), Pred, Dst);
301 break;
302 }
303
304 // FIXME: ChooseExpr is really a constant. We need to fix
305 // the CFG do not model them as explicit control-flow.
306
307 case Stmt::ChooseExprClass: { // __builtin_choose_expr
308 ChooseExpr* C = cast<ChooseExpr>(S);
309 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
310 break;
311 }
312
313 case Stmt::CompoundAssignOperatorClass:
314 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
315 break;
316
317 case Stmt::ConditionalOperatorClass: { // '?' operator
318 ConditionalOperator* C = cast<ConditionalOperator>(S);
319 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
320 break;
321 }
322
323 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000324 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000325 break;
326
327 case Stmt::DeclStmtClass:
328 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
329 break;
330
331 case Stmt::ImplicitCastExprClass: {
332 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
333 VisitCast(C, C->getSubExpr(), Pred, Dst);
334 break;
335 }
336
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000337 case Stmt::MemberExprClass: {
338 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
339 break;
340 }
341
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000342 case Stmt::ObjCMessageExprClass: {
343 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
344 break;
345 }
346
347 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000348 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000349 break;
350
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000351 case Stmt::ReturnStmtClass:
352 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
353 break;
354
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000355 case Stmt::SizeOfAlignOfTypeExprClass:
356 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
357 break;
358
359 case Stmt::StmtExprClass: {
360 StmtExpr* SE = cast<StmtExpr>(S);
361
362 ValueState* St = GetState(Pred);
363
364 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
365 // probably just remove these from the CFG.
366 assert (!SE->getSubStmt()->body_empty());
367
368 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
369 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
370 else
371 Dst.Add(Pred);
372
373 break;
374 }
375
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000376 case Stmt::UnaryOperatorClass:
377 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000378 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000379 }
380}
381
382void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
383
384 Ex = Ex->IgnoreParens();
385
386 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
387 Dst.Add(Pred);
388 return;
389 }
390
391 switch (Ex->getStmtClass()) {
392 default:
393 Visit(Ex, Pred, Dst);
394 return;
395
396 case Stmt::ArraySubscriptExprClass:
397 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
398 return;
399
400 case Stmt::DeclRefExprClass:
401 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
402 return;
403
404 case Stmt::UnaryOperatorClass:
405 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
406 return;
407
408 case Stmt::MemberExprClass:
409 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
410 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 }
412}
413
414//===----------------------------------------------------------------------===//
415// Block entrance. (Update counters).
416//===----------------------------------------------------------------------===//
417
418bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
419 GRBlockCounter BC) {
420
421 return BC.getNumVisited(B->getBlockID()) < 3;
422}
423
424//===----------------------------------------------------------------------===//
425// Branch processing.
426//===----------------------------------------------------------------------===//
427
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000428ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
429 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000430
431 switch (Terminator->getStmtClass()) {
432 default:
433 return St;
434
435 case Stmt::BinaryOperatorClass: { // '&&' and '||'
436
437 BinaryOperator* B = cast<BinaryOperator>(Terminator);
438 BinaryOperator::Opcode Op = B->getOpcode();
439
440 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
441
442 // For &&, if we take the true branch, then the value of the whole
443 // expression is that of the RHS expression.
444 //
445 // For ||, if we take the false branch, then the value of the whole
446 // expression is that of the RHS expression.
447
448 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
449 (Op == BinaryOperator::LOr && !branchTaken)
450 ? B->getRHS() : B->getLHS();
451
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000452 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000453 }
454
455 case Stmt::ConditionalOperatorClass: { // ?:
456
457 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
458
459 // For ?, if branchTaken == true then the value is either the LHS or
460 // the condition itself. (GNU extension).
461
462 Expr* Ex;
463
464 if (branchTaken)
465 Ex = C->getLHS() ? C->getLHS() : C->getCond();
466 else
467 Ex = C->getRHS();
468
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000469 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000470 }
471
472 case Stmt::ChooseExprClass: { // ?:
473
474 ChooseExpr* C = cast<ChooseExpr>(Terminator);
475
476 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000477 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000478 }
479 }
480}
481
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000482void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000483 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000484
Ted Kremeneke7d22112008-02-11 19:21:59 +0000485 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000486 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000487
Ted Kremenekb2331832008-02-15 22:29:00 +0000488 // Check for NULL conditions; e.g. "for(;;)"
489 if (!Condition) {
490 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000491 return;
492 }
493
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000494 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000495
496 switch (V.getBaseKind()) {
497 default:
498 break;
499
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000500 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000501 builder.generateNode(MarkBranch(PrevState, Term, true), true);
502 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000503 return;
504
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000505 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000506 NodeTy* N = builder.generateNode(PrevState, true);
507
508 if (N) {
509 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000510 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000511 }
512
513 builder.markInfeasible(false);
514 return;
515 }
516 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000517
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000518 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000519
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000520 bool isFeasible = false;
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000521 ValueState* St = Assume(PrevState, V, true, isFeasible);
522
523 if (isFeasible)
524 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000525 else
526 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000527
528 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000529
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000530 isFeasible = false;
531 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000532
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000533 if (isFeasible)
534 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000535 else
536 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000537}
538
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000539/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000540/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000541void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000542
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000543 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000544 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000545
546 // Three possibilities:
547 //
548 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000549 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000550 // (3) We have no clue about the label. Dispatch to all targets.
551 //
552
553 typedef IndirectGotoNodeBuilder::iterator iterator;
554
555 if (isa<lval::GotoLabel>(V)) {
556 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
557
558 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000559 if (I.getLabel() == L) {
560 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000561 return;
562 }
563 }
564
565 assert (false && "No block with label.");
566 return;
567 }
568
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000569 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000570 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000571 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000572 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000573 return;
574 }
575
576 // This is really a catch-all. We don't support symbolics yet.
577
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000578 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000579
580 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000581 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000582}
Ted Kremenekf233d482008-02-05 00:26:40 +0000583
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000584
585void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
586 NodeTy* Pred, NodeSet& Dst) {
587
588 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
589
590 ValueState* St = GetState(Pred);
591 RVal X = GetBlkExprRVal(St, Ex);
592
593 assert (X.isUndef());
594
595 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
596
597 assert (SE);
598
599 X = GetBlkExprRVal(St, SE);
600
601 // Make sure that we invalidate the previous binding.
602 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
603}
604
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000605/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
606/// nodes by processing the 'effects' of a switch statement.
607void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
608
609 typedef SwitchNodeBuilder::iterator iterator;
610
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000611 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000612 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000613 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000614
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000615 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000616 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000617 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000618 return;
619 }
620
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000621 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000622
623 // While most of this can be assumed (such as the signedness), having it
624 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000625
Chris Lattner98be4942008-03-05 18:54:05 +0000626 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000627
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000628 APSInt V1(bits, false);
629 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000630 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000631
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000632 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000633
634 CaseStmt* Case = cast<CaseStmt>(I.getCase());
635
636 // Evaluate the case.
637 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
638 assert (false && "Case condition must evaluate to an integer constant.");
639 return;
640 }
641
642 // Get the RHS of the case, if it exists.
643
644 if (Expr* E = Case->getRHS()) {
645 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
646 assert (false &&
647 "Case condition (RHS) must evaluate to an integer constant.");
648 return ;
649 }
650
651 assert (V1 <= V2);
652 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000653 else
654 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000655
656 // FIXME: Eventually we should replace the logic below with a range
657 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000658 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000659
Ted Kremenek14a11402008-03-17 22:17:56 +0000660 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000661 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000662
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000663 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000664
665 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000666
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000667 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000668 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000669
670 if (isFeasible) {
671 builder.generateCaseStmtNode(I, StNew);
672
673 // If CondV evaluates to a constant, then we know that this
674 // is the *only* case that we can take, so stop evaluating the
675 // others.
676 if (isa<nonlval::ConcreteInt>(CondV))
677 return;
678 }
679
680 // Now "assume" that the case doesn't match. Add this state
681 // to the default state (if it is feasible).
682
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000683 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000684 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000685
Ted Kremenek5014ab12008-04-23 05:03:18 +0000686 if (isFeasible) {
687 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000688 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000689 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000690
Ted Kremenek14a11402008-03-17 22:17:56 +0000691 // Concretize the next value in the range.
692 if (V1 == V2)
693 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000694
Ted Kremenek14a11402008-03-17 22:17:56 +0000695 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000696 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000697
698 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000699 }
700
701 // If we reach here, than we know that the default branch is
702 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000703 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000704}
705
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000706//===----------------------------------------------------------------------===//
707// Transfer functions: logical operations ('&&', '||').
708//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000709
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000710void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000711 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000712
Ted Kremenek05a23782008-02-26 19:05:15 +0000713 assert (B->getOpcode() == BinaryOperator::LAnd ||
714 B->getOpcode() == BinaryOperator::LOr);
715
716 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
717
Ted Kremenek0d093d32008-03-10 04:11:42 +0000718 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000719 RVal X = GetBlkExprRVal(St, B);
720
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000721 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000722
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000723 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000724
725 assert (Ex);
726
727 if (Ex == B->getRHS()) {
728
729 X = GetBlkExprRVal(St, Ex);
730
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000731 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000732
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000733 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000734 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000735 return;
736 }
737
Ted Kremenek05a23782008-02-26 19:05:15 +0000738 // We took the RHS. Because the value of the '&&' or '||' expression must
739 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
740 // or 1. Alternatively, we could take a lazy approach, and calculate this
741 // value later when necessary. We don't have the machinery in place for
742 // this right now, and since most logical expressions are used for branches,
743 // the payoff is not likely to be large. Instead, we do eager evaluation.
744
745 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000746 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000747
748 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000749 MakeNode(Dst, B, Pred,
750 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000751
752 isFeasible = false;
753 NewState = Assume(St, X, false, isFeasible);
754
755 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000756 MakeNode(Dst, B, Pred,
757 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000758 }
759 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000760 // We took the LHS expression. Depending on whether we are '&&' or
761 // '||' we know what the value of the expression is via properties of
762 // the short-circuiting.
763
764 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000765 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000766 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000767}
Ted Kremenek05a23782008-02-26 19:05:15 +0000768
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000769//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000770// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000771//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000772
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000773void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
774 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000775
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000776 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000777 RVal X = RVal::MakeVal(BasicVals, D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000778
779 if (asLVal)
780 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
781 else {
782 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
783 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
784 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000785}
786
Ted Kremenek540cbe22008-04-22 04:56:29 +0000787/// VisitArraySubscriptExpr - Transfer function for array accesses
788void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
789 NodeSet& Dst, bool asLVal) {
790
791 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000792 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000793
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000794 // Always visit the base as an LVal expression. This computes the
795 // abstract address of the base object.
796 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000797
Ted Kremenek0e470a52008-05-09 23:45:33 +0000798 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000799 Visit(Base, Pred, Tmp);
800 else
801 VisitLVal(Base, Pred, Tmp);
802
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000803 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000804
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000805 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000806
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000807 NodeSet Tmp2;
808 Visit(Idx, *I1, Tmp2);
809
810 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
811
812 ValueState* St = GetState(*I2);
813 RVal BaseV = GetRVal(St, Base);
814 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000815
816 // If IdxV is 0, return just BaseV.
817
818 bool useBase = false;
819
820 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
821 useBase = IdxInt->getValue() == 0;
822
823 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(BasicVals, BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000824
825 if (asLVal)
826 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
827 else
828 EvalLoad(Dst, A, *I2, St, V);
829 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000830 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000831}
832
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000833/// VisitMemberExpr - Transfer function for member expressions.
834void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
835 NodeSet& Dst, bool asLVal) {
836
837 Expr* Base = M->getBase()->IgnoreParens();
838
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000839 // Always visit the base as an LVal expression. This computes the
840 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000841 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000842
Ted Kremenekee90dba2008-04-30 22:17:15 +0000843 if (asLVal) {
844
Ted Kremenek0e470a52008-05-09 23:45:33 +0000845 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000846 Visit(Base, Pred, Tmp);
847 else
848 VisitLVal(Base, Pred, Tmp);
849
850 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
851 ValueState* St = GetState(*I);
852 RVal BaseV = GetRVal(St, Base);
853
854 RVal V = lval::FieldOffset::Make(BasicVals, GetRVal(St, Base),
855 M->getMemberDecl());
856
857 MakeNode(Dst, M, *I, SetRVal(St, M, V));
858 }
859
860 return;
861 }
862
863 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
864 // or not isArrow() is true).
865 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000866
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000867 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000868
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000869 ValueState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000870 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000871
Ted Kremenek0e470a52008-05-09 23:45:33 +0000872 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000873
Ted Kremenekee90dba2008-04-30 22:17:15 +0000874 assert (M->isArrow());
875
876 RVal V = lval::FieldOffset::Make(BasicVals, GetRVal(St, Base),
877 M->getMemberDecl());
878
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000879 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000880 }
881 else {
882
883 assert (!M->isArrow());
884
885 if (BaseV.isUnknownOrUndef()) {
886 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
887 continue;
888 }
889
890 // FIXME: Implement nonlval objects representing struct temporaries.
891 assert (isa<NonLVal>(BaseV));
892 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
893 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000894 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000895}
896
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000897void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
898 ValueState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000899
900 assert (Builder && "GRStmtNodeBuilder must be defined.");
901
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000902 // Evaluate the location (checks for bad dereferences).
903 St = EvalLocation(Ex, Pred, St, location);
904
905 if (!St)
906 return;
907
908 // Proceed with the store.
909
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000910 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000911
Ted Kremenek186350f2008-04-23 20:12:28 +0000912 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
913 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000914
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000915 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000916
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000917 TF->EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000918
919 // Handle the case where no nodes where generated. Auto-generate that
920 // contains the updated state if we aren't generating sinks.
921
Ted Kremenekb0533962008-04-18 20:35:30 +0000922 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000923 TF->GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
924 location, Val);
925}
926
927void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
928 ValueState* St, RVal location, bool CheckOnly) {
929
930 // Evaluate the location (checks for bad dereferences).
931
932 St = EvalLocation(Ex, Pred, St, location, true);
933
934 if (!St)
935 return;
936
937 // Proceed with the load.
938
939 // FIXME: Currently symbolic analysis "generates" new symbols
940 // for the contents of values. We need a better approach.
941
942 // FIXME: The "CheckOnly" option exists only because Array and Field
943 // loads aren't fully implemented. Eventually this option will go away.
944
Ted Kremenek436f2b92008-04-30 04:23:07 +0000945 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000946 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000947 else if (location.isUnknown()) {
948 // This is important. We must nuke the old binding.
949 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
950 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000951 else
952 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
953 Ex->getType())));
954}
955
956ValueState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
957 ValueState* St, RVal location,
958 bool isLoad) {
959
960 // Check for loads/stores from/to undefined values.
961 if (location.isUndef()) {
962 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, isLoad)) {
963 Succ->markAsSink();
964 UndefDeref.insert(Succ);
965 }
966
967 return NULL;
968 }
969
970 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
971 if (location.isUnknown())
972 return St;
973
974 // During a load, one of two possible situations arise:
975 // (1) A crash, because the location (pointer) was NULL.
976 // (2) The location (pointer) is not NULL, and the dereference works.
977 //
978 // We add these assumptions.
979
980 LVal LV = cast<LVal>(location);
981
982 // "Assume" that the pointer is not NULL.
983
984 bool isFeasibleNotNull = false;
985 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
986
987 // "Assume" that the pointer is NULL.
988
989 bool isFeasibleNull = false;
990 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
991
992 if (isFeasibleNull) {
993
994 // We don't use "MakeNode" here because the node will be a sink
995 // and we have no intention of processing it later.
996
997 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, isLoad);
998
999 if (NullNode) {
1000
1001 NullNode->markAsSink();
1002
1003 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1004 else ExplicitNullDeref.insert(NullNode);
1005 }
1006 }
1007
1008 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001009}
1010
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001011//===----------------------------------------------------------------------===//
1012// Transfer function: Function calls.
1013//===----------------------------------------------------------------------===//
1014
Ted Kremenekde434242008-02-19 01:44:53 +00001015void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001016 CallExpr::arg_iterator AI,
1017 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001018 NodeSet& Dst) {
1019
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001020 // Process the arguments.
1021
1022 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001023
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001024 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001025 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001026 ++AI;
1027
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001028 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001029 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001030
1031 return;
1032 }
1033
1034 // If we reach here we have processed all of the arguments. Evaluate
1035 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001036
Ted Kremenek994a09b2008-02-25 21:16:03 +00001037 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001038 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001039
Ted Kremenek994a09b2008-02-25 21:16:03 +00001040 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001041
Ted Kremenekde434242008-02-19 01:44:53 +00001042 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001043 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1044
Ted Kremenek0d093d32008-03-10 04:11:42 +00001045 ValueState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001047
Ted Kremeneka1354a52008-03-03 16:47:31 +00001048 // FIXME: Add support for symbolic function calls (calls involving
1049 // function pointer values that are symbolic).
1050
1051 // Check for undefined control-flow or calls to NULL.
1052
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001053 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001054 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001055
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001056 if (N) {
1057 N->markAsSink();
1058 BadCalls.insert(N);
1059 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001060
Ted Kremenekde434242008-02-19 01:44:53 +00001061 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001062 }
1063
1064 // Check for the "noreturn" attribute.
1065
1066 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1067
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001068 if (isa<lval::FuncVal>(L)) {
1069
1070 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1071
1072 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001073 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001074 else {
1075 // HACK: Some functions are not marked noreturn, and don't return.
1076 // Here are a few hardwired ones. If this takes too long, we can
1077 // potentially cache these results.
1078 const char* s = FD->getIdentifier()->getName();
1079 unsigned n = strlen(s);
1080
1081 switch (n) {
1082 default:
1083 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001084
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001085 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001086 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1087 break;
1088
1089 case 5:
1090 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1091 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001092
1093 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001094 if (!memcmp(s, "Assert", 6)) {
1095 Builder->BuildSinks = true;
1096 break;
1097 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001098
1099 // FIXME: This is just a wrapper around throwing an exception.
1100 // Eventually inter-procedural analysis should handle this easily.
1101 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1102
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001103 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001104
1105 case 7:
1106 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1107 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001108
Ted Kremenekf47bb782008-04-30 17:54:04 +00001109 case 8:
1110 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1111 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001112
1113 case 12:
1114 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1115 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001116
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001117 case 14:
1118 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1119 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001120
1121 case 26:
Ted Kremenek05a91122008-05-17 00:40:45 +00001122 if (!memcmp(s, "_XCAssertionFailureHandler", 26))
1123 Builder->BuildSinks = true;
1124
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001125 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001126 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001127
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001128 }
1129 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001130
1131 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001132
1133 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001134
1135 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1136
Ted Kremenek186350f2008-04-23 20:12:28 +00001137 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001138 switch (id) {
1139 case Builtin::BI__builtin_expect: {
1140 // For __builtin_expect, just return the value of the subexpression.
1141 assert (CE->arg_begin() != CE->arg_end());
1142 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001143 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001144 continue;
1145 }
1146
1147 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001148 break;
1149 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001150 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001151
Ted Kremenek186350f2008-04-23 20:12:28 +00001152 // Check any arguments passed-by-value against being undefined.
1153
1154 bool badArg = false;
1155
1156 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1157 I != E; ++I) {
1158
1159 if (GetRVal(GetState(*DI), *I).isUndef()) {
1160 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001161
Ted Kremenek186350f2008-04-23 20:12:28 +00001162 if (N) {
1163 N->markAsSink();
1164 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001165 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001166
Ted Kremenek186350f2008-04-23 20:12:28 +00001167 badArg = true;
1168 break;
1169 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001170 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001171
1172 if (badArg)
1173 continue;
1174
1175 // Dispatch to the plug-in transfer function.
1176
1177 unsigned size = Dst.size();
1178 SaveOr OldHasGen(Builder->HasGeneratedNode);
1179 EvalCall(Dst, CE, L, *DI);
1180
1181 // Handle the case where no nodes where generated. Auto-generate that
1182 // contains the updated state if we aren't generating sinks.
1183
1184 if (!Builder->BuildSinks && Dst.size() == size &&
1185 !Builder->HasGeneratedNode)
1186 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001187 }
1188}
1189
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001190//===----------------------------------------------------------------------===//
1191// Transfer function: Objective-C message expressions.
1192//===----------------------------------------------------------------------===//
1193
1194void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1195 NodeSet& Dst){
1196
1197 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1198 Pred, Dst);
1199}
1200
1201void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1202 ObjCMessageExpr::arg_iterator AI,
1203 ObjCMessageExpr::arg_iterator AE,
1204 NodeTy* Pred, NodeSet& Dst) {
1205 if (AI == AE) {
1206
1207 // Process the receiver.
1208
1209 if (Expr* Receiver = ME->getReceiver()) {
1210 NodeSet Tmp;
1211 Visit(Receiver, Pred, Tmp);
1212
1213 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1214 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1215
1216 return;
1217 }
1218
1219 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1220 return;
1221 }
1222
1223 NodeSet Tmp;
1224 Visit(*AI, Pred, Tmp);
1225
1226 ++AI;
1227
1228 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1229 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1230}
1231
1232void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1233 NodeTy* Pred,
1234 NodeSet& Dst) {
1235
1236 // FIXME: More logic for the processing the method call.
1237
1238 ValueState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001239 bool RaisesException = false;
1240
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001241
1242 if (Expr* Receiver = ME->getReceiver()) {
1243
1244 RVal L = GetRVal(St, Receiver);
1245
1246 // Check for undefined control-flow or calls to NULL.
1247
1248 if (L.isUndef()) {
1249 NodeTy* N = Builder->generateNode(ME, St, Pred);
1250
1251 if (N) {
1252 N->markAsSink();
1253 UndefReceivers.insert(N);
1254 }
1255
1256 return;
1257 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001258
1259 // Check if the "raise" message was sent.
1260 if (ME->getSelector() == RaiseSel)
1261 RaisesException = true;
1262 }
1263 else {
1264
1265 IdentifierInfo* ClsName = ME->getClassName();
1266 Selector S = ME->getSelector();
1267
1268 // Check for special instance methods.
1269
1270 if (!NSExceptionII) {
1271 ASTContext& Ctx = getContext();
1272
1273 NSExceptionII = &Ctx.Idents.get("NSException");
1274 }
1275
1276 if (ClsName == NSExceptionII) {
1277
1278 enum { NUM_RAISE_SELECTORS = 2 };
1279
1280 // Lazily create a cache of the selectors.
1281
1282 if (!NSExceptionInstanceRaiseSelectors) {
1283
1284 ASTContext& Ctx = getContext();
1285
1286 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1287
1288 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1289 unsigned idx = 0;
1290
1291 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001292 II.push_back(&Ctx.Idents.get("raise"));
1293 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001294 NSExceptionInstanceRaiseSelectors[idx++] =
1295 Ctx.Selectors.getSelector(II.size(), &II[0]);
1296
1297 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001298 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001299 NSExceptionInstanceRaiseSelectors[idx++] =
1300 Ctx.Selectors.getSelector(II.size(), &II[0]);
1301 }
1302
1303 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1304 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1305 RaisesException = true; break;
1306 }
1307 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001308 }
1309
1310 // Check for any arguments that are uninitialized/undefined.
1311
1312 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1313 I != E; ++I) {
1314
1315 if (GetRVal(St, *I).isUndef()) {
1316
1317 // Generate an error node for passing an uninitialized/undefined value
1318 // as an argument to a message expression. This node is a sink.
1319 NodeTy* N = Builder->generateNode(ME, St, Pred);
1320
1321 if (N) {
1322 N->markAsSink();
1323 MsgExprUndefArgs[N] = *I;
1324 }
1325
1326 return;
1327 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001328 }
1329
1330 // Check if we raise an exception. For now treat these as sinks. Eventually
1331 // we will want to handle exceptions properly.
1332
1333 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1334
1335 if (RaisesException)
1336 Builder->BuildSinks = true;
1337
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001338 // Dispatch to plug-in transfer function.
1339
1340 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001341 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001342
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001343 EvalObjCMessageExpr(Dst, ME, Pred);
1344
1345 // Handle the case where no nodes where generated. Auto-generate that
1346 // contains the updated state if we aren't generating sinks.
1347
Ted Kremenekb0533962008-04-18 20:35:30 +00001348 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001349 MakeNode(Dst, ME, Pred, St);
1350}
1351
1352//===----------------------------------------------------------------------===//
1353// Transfer functions: Miscellaneous statements.
1354//===----------------------------------------------------------------------===//
1355
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001356void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001357
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001358 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001359 QualType T = CastE->getType();
1360
Ted Kremenek65cfb732008-03-04 22:16:08 +00001361 if (T->isReferenceType())
1362 VisitLVal(Ex, Pred, S1);
1363 else
1364 Visit(Ex, Pred, S1);
1365
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001366 // Check for casting to "void".
1367 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001368
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001369 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001370 Dst.Add(*I1);
1371
Ted Kremenek874d63f2008-01-24 02:02:54 +00001372 return;
1373 }
1374
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001375 // FIXME: The rest of this should probably just go into EvalCall, and
1376 // let the transfer function object be responsible for constructing
1377 // nodes.
1378
1379 QualType ExTy = Ex->getType();
1380
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001381 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001382 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001383 ValueState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001384 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001385
1386 // Unknown?
1387
1388 if (V.isUnknown()) {
1389 Dst.Add(N);
1390 continue;
1391 }
1392
1393 // Undefined?
1394
1395 if (V.isUndef()) {
1396 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1397 continue;
1398 }
1399
1400 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001401 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001402 unsigned bits = getContext().getTypeSize(ExTy);
1403
1404 // FIXME: Determine if the number of bits of the target type is
1405 // equal or exceeds the number of bits to store the pointer value.
1406 // If not, flag an error.
1407
1408 V = nonlval::LValAsInteger::Make(BasicVals, cast<LVal>(V), bits);
1409 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1410 continue;
1411 }
1412
1413 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001414 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001415 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1416 // Just unpackage the lval and return it.
1417 V = LV->getLVal();
1418 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1419 continue;
1420 }
1421
1422 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001423
Ted Kremenek0e561a32008-03-21 21:30:14 +00001424 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001425 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001426}
1427
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001428void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1429 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1430}
1431
1432void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1433 NodeTy* Pred, NodeSet& Dst) {
1434
1435 if (!D)
1436 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001437
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001438 if (!isa<VarDecl>(D)) {
1439 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1440 return;
1441 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001442
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001443 const VarDecl* VD = dyn_cast<VarDecl>(D);
1444
1445 // FIXME: Add support for local arrays.
1446 if (VD->getType()->isArrayType()) {
1447 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1448 return;
1449 }
1450
1451 Expr* Ex = const_cast<Expr*>(VD->getInit());
1452
1453 // FIXME: static variables may have an initializer, but the second
1454 // time a function is called those values may not be current.
1455 NodeSet Tmp;
1456
1457 if (Ex) Visit(Ex, Pred, Tmp);
1458 if (Tmp.empty()) Tmp.Add(Pred);
1459
1460 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001461
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001462 ValueState* St = GetState(*I);
1463
1464 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001465
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001466 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001467
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001468 // FIXME: static variables may have an initializer, but the second
1469 // time a function is called those values may not be current.
1470
1471
1472 // In this context, Static => Local variable.
1473
1474 assert (!VD->getStorageClass() == VarDecl::Static ||
1475 !VD->isFileVarDecl());
1476
1477 // If there is no initializer, set the value of the
1478 // variable to "Undefined".
1479
1480 if (VD->getStorageClass() == VarDecl::Static) {
1481
1482 // C99: 6.7.8 Initialization
1483 // If an object that has static storage duration is not initialized
1484 // explicitly, then:
1485 // —if it has pointer type, it is initialized to a null pointer;
1486 // —if it has arithmetic type, it is initialized to (positive or
1487 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001488
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001489 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001490
1491 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001492
Ted Kremenek0e470a52008-05-09 23:45:33 +00001493 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001494 St = SetRVal(St, lval::DeclVal(VD),
1495 lval::ConcreteInt(BasicVals.getValue(0, T)));
1496 else if (T->isIntegerType())
1497 St = SetRVal(St, lval::DeclVal(VD),
1498 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001499
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001500 // FIXME: Handle structs. Now we treat them as unknown. What
1501 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001502 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001503 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001504 else {
1505
1506 // FIXME: Handle structs. Now we treat them as unknown. What
1507 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001508
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001509 QualType T = VD->getType();
1510
Ted Kremenek0e470a52008-05-09 23:45:33 +00001511 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001512
1513 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1514
1515 if (Ex && V.isUnknown()) {
1516
1517 // EXPERIMENTAL: "Conjured" symbols.
1518
1519 unsigned Count = Builder->getCurrentBlockCount();
1520 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1521
Ted Kremenek0e470a52008-05-09 23:45:33 +00001522 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001523 ? cast<RVal>(lval::SymbolVal(Sym))
1524 : cast<RVal>(nonlval::SymbolVal(Sym));
1525 }
1526
1527 St = SetRVal(St, lval::DeclVal(VD), V);
1528 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001529 }
1530
1531 // Create a new node. We don't really need to create a new NodeSet
1532 // here, but it simplifies things and doesn't cost much.
1533 NodeSet Tmp2;
1534 MakeNode(Tmp2, DS, *I, St);
1535 if (Tmp2.empty()) Tmp2.Add(*I);
1536
1537 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1538 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1539 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001540}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001541
Ted Kremenekf233d482008-02-05 00:26:40 +00001542
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001543/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001544void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1545 NodeTy* Pred,
1546 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001547 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001548 uint64_t amt;
1549
1550 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001551
Ted Kremenek87e80342008-03-15 03:13:20 +00001552 // FIXME: Add support for VLAs.
1553 if (!T.getTypePtr()->isConstantSizeType())
1554 return;
1555
Ted Kremenekf342d182008-04-30 21:31:12 +00001556 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1557 // the compiler has laid out its representation. Just report Unknown
1558 // for these.
1559 if (T->isObjCInterfaceType())
1560 return;
1561
Ted Kremenek87e80342008-03-15 03:13:20 +00001562 amt = 1; // Handle sizeof(void)
1563
1564 if (T != getContext().VoidTy)
1565 amt = getContext().getTypeSize(T) / 8;
1566
1567 }
1568 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001569 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001570
Ted Kremenek0e561a32008-03-21 21:30:14 +00001571 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001572 SetRVal(GetState(Pred), Ex,
1573 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001574}
1575
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001576
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001577void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001578 NodeSet& Dst, bool asLVal) {
1579
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001580 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001581
1582 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001583 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001584
1585 case UnaryOperator::Deref: {
1586
1587 Expr* Ex = U->getSubExpr()->IgnoreParens();
1588 NodeSet Tmp;
1589 Visit(Ex, Pred, Tmp);
1590
1591 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001592
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001593 ValueState* St = GetState(*I);
1594 RVal location = GetRVal(St, Ex);
1595
1596 if (asLVal)
1597 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1598 else
1599 EvalLoad(Dst, Ex, *I, St, location);
1600 }
1601
1602 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001603 }
1604
Ted Kremeneka084bb62008-04-30 21:45:55 +00001605
1606 case UnaryOperator::OffsetOf:
1607 // FIXME: Just report "Unknown" known for OffsetOf.
1608 Dst.Add(Pred);
1609 return;
1610
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001611 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1612 case UnaryOperator::Extension: {
1613
1614 // Unary "+" is a no-op, similar to a parentheses. We still have places
1615 // where it may be a block-level expression, so we need to
1616 // generate an extra node that just propagates the value of the
1617 // subexpression.
1618
1619 Expr* Ex = U->getSubExpr()->IgnoreParens();
1620 NodeSet Tmp;
1621 Visit(Ex, Pred, Tmp);
1622
1623 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1624 ValueState* St = GetState(*I);
1625 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1626 }
1627
1628 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001629 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001630
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001631 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001632
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001633 assert (!asLVal);
1634 Expr* Ex = U->getSubExpr()->IgnoreParens();
1635 NodeSet Tmp;
1636 VisitLVal(Ex, Pred, Tmp);
1637
1638 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1639 ValueState* St = GetState(*I);
1640 RVal V = GetRVal(St, Ex);
1641 St = SetRVal(St, U, V);
1642 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001643 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001644
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001645 return;
1646 }
1647
1648 case UnaryOperator::LNot:
1649 case UnaryOperator::Minus:
1650 case UnaryOperator::Not: {
1651
1652 assert (!asLVal);
1653 Expr* Ex = U->getSubExpr()->IgnoreParens();
1654 NodeSet Tmp;
1655 Visit(Ex, Pred, Tmp);
1656
1657 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1658 ValueState* St = GetState(*I);
1659 RVal V = GetRVal(St, Ex);
1660
1661 if (V.isUnknownOrUndef()) {
1662 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1663 continue;
1664 }
1665
1666 switch (U->getOpcode()) {
1667 default:
1668 assert(false && "Invalid Opcode.");
1669 break;
1670
1671 case UnaryOperator::Not:
1672 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1673 break;
1674
1675 case UnaryOperator::Minus:
1676 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1677 break;
1678
1679 case UnaryOperator::LNot:
1680
1681 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1682 //
1683 // Note: technically we do "E == 0", but this is the same in the
1684 // transfer functions as "0 == E".
1685
1686 if (isa<LVal>(V)) {
1687 lval::ConcreteInt X(BasicVals.getZeroWithPtrWidth());
1688 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1689 St = SetRVal(St, U, Result);
1690 }
1691 else {
1692 nonlval::ConcreteInt X(BasicVals.getValue(0, Ex->getType()));
1693 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1694 St = SetRVal(St, U, Result);
1695 }
1696
1697 break;
1698 }
1699
1700 MakeNode(Dst, U, *I, St);
1701 }
1702
1703 return;
1704 }
1705
1706 case UnaryOperator::SizeOf: {
1707
1708 QualType T = U->getSubExpr()->getType();
1709
1710 // FIXME: Add support for VLAs.
1711
1712 if (!T.getTypePtr()->isConstantSizeType())
1713 return;
1714
1715 uint64_t size = getContext().getTypeSize(T) / 8;
1716 ValueState* St = GetState(Pred);
1717 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
1718
1719 MakeNode(Dst, U, Pred, St);
1720 return;
1721 }
1722 }
1723
1724 // Handle ++ and -- (both pre- and post-increment).
1725
1726 assert (U->isIncrementDecrementOp());
1727 NodeSet Tmp;
1728 Expr* Ex = U->getSubExpr()->IgnoreParens();
1729 VisitLVal(Ex, Pred, Tmp);
1730
1731 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1732
1733 ValueState* St = GetState(*I);
1734 RVal V1 = GetRVal(St, Ex);
1735
1736 // Perform a load.
1737 NodeSet Tmp2;
1738 EvalLoad(Tmp2, Ex, *I, St, V1);
1739
1740 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1741
1742 St = GetState(*I2);
1743 RVal V2 = GetRVal(St, Ex);
1744
1745 // Propagate unknown and undefined values.
1746 if (V2.isUnknownOrUndef()) {
1747 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001748 continue;
1749 }
1750
Ted Kremenek443003b2008-02-21 19:29:23 +00001751 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001752
1753 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1754 : BinaryOperator::Sub;
1755
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001756 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1757 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001758
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001759 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001760 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001761 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001762 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001763}
1764
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001765void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1766 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1767}
1768
1769void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1770 AsmStmt::outputs_iterator I,
1771 AsmStmt::outputs_iterator E,
1772 NodeTy* Pred, NodeSet& Dst) {
1773 if (I == E) {
1774 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1775 return;
1776 }
1777
1778 NodeSet Tmp;
1779 VisitLVal(*I, Pred, Tmp);
1780
1781 ++I;
1782
1783 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1784 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1785}
1786
1787void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1788 AsmStmt::inputs_iterator I,
1789 AsmStmt::inputs_iterator E,
1790 NodeTy* Pred, NodeSet& Dst) {
1791 if (I == E) {
1792
1793 // We have processed both the inputs and the outputs. All of the outputs
1794 // should evaluate to LVals. Nuke all of their values.
1795
1796 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1797 // which interprets the inline asm and stores proper results in the
1798 // outputs.
1799
1800 ValueState* St = GetState(Pred);
1801
1802 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1803 OE = A->end_outputs(); OI != OE; ++OI) {
1804
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001805 RVal X = GetRVal(St, *OI);
1806 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001807
1808 if (isa<LVal>(X))
1809 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1810 }
1811
Ted Kremenek0e561a32008-03-21 21:30:14 +00001812 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001813 return;
1814 }
1815
1816 NodeSet Tmp;
1817 Visit(*I, Pred, Tmp);
1818
1819 ++I;
1820
1821 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1822 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1823}
1824
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001825void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1826 assert (Builder && "GRStmtNodeBuilder must be defined.");
1827
1828 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001829
Ted Kremenek186350f2008-04-23 20:12:28 +00001830 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1831 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001832
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001833 TF->EvalReturn(Dst, *this, *Builder, S, Pred);
1834
Ted Kremenekb0533962008-04-18 20:35:30 +00001835 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001836
Ted Kremenekb0533962008-04-18 20:35:30 +00001837 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001838 MakeNode(Dst, S, Pred, GetState(Pred));
1839}
1840
Ted Kremenek02737ed2008-03-31 15:02:58 +00001841void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1842
1843 Expr* R = S->getRetValue();
1844
1845 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001846 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001847 return;
1848 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001849
1850 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001851 QualType T = R->getType();
1852
Chris Lattner423a3c92008-04-02 17:45:06 +00001853 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001854
1855 // Check if any of the return values return the address of a stack variable.
1856
1857 NodeSet Tmp;
1858 Visit(R, Pred, Tmp);
1859
1860 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1861 RVal X = GetRVal((*I)->getState(), R);
1862
1863 if (isa<lval::DeclVal>(X)) {
1864
1865 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1866
1867 // Create a special node representing the v
1868
1869 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1870
1871 if (RetStackNode) {
1872 RetStackNode->markAsSink();
1873 RetsStackAddr.insert(RetStackNode);
1874 }
1875
1876 continue;
1877 }
1878 }
1879
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001880 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001881 }
1882 }
1883 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001884 Visit(R, Pred, DstRet);
1885
1886 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1887 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001888}
Ted Kremenek55deb972008-03-25 00:34:37 +00001889
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001890//===----------------------------------------------------------------------===//
1891// Transfer functions: Binary operators.
1892//===----------------------------------------------------------------------===//
1893
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001894bool GRExprEngine::CheckDivideZero(Expr* Ex, ValueState* St,
1895 NodeTy* Pred, RVal Denom) {
1896
1897 // Divide by undefined? (potentially zero)
1898
1899 if (Denom.isUndef()) {
1900 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
1901
1902 if (DivUndef) {
1903 DivUndef->markAsSink();
1904 ExplicitBadDivides.insert(DivUndef);
1905 }
1906
1907 return true;
1908 }
1909
1910 // Check for divide/remainder-by-zero.
1911 // First, "assume" that the denominator is 0 or undefined.
1912
1913 bool isFeasibleZero = false;
1914 ValueState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
1915
1916 // Second, "assume" that the denominator cannot be 0.
1917
1918 bool isFeasibleNotZero = false;
1919 St = Assume(St, Denom, true, isFeasibleNotZero);
1920
1921 // Create the node for the divide-by-zero (if it occurred).
1922
1923 if (isFeasibleZero)
1924 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
1925 DivZeroNode->markAsSink();
1926
1927 if (isFeasibleNotZero)
1928 ImplicitBadDivides.insert(DivZeroNode);
1929 else
1930 ExplicitBadDivides.insert(DivZeroNode);
1931
1932 }
1933
1934 return !isFeasibleNotZero;
1935}
1936
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001937void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001938 GRExprEngine::NodeTy* Pred,
1939 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001940
1941 NodeSet Tmp1;
1942 Expr* LHS = B->getLHS()->IgnoreParens();
1943 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001944
1945 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001946 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001947 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001948 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001949
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001950 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001951
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001952 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001953
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001954 // Process the RHS.
1955
1956 NodeSet Tmp2;
1957 Visit(RHS, *I1, Tmp2);
1958
1959 // With both the LHS and RHS evaluated, process the operation itself.
1960
1961 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001962
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001963 ValueState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001964 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001965 BinaryOperator::Opcode Op = B->getOpcode();
1966
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001967 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001968
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001969 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001970
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001971 // EXPERIMENTAL: "Conjured" symbols.
1972
1973 if (RightV.isUnknown()) {
1974 unsigned Count = Builder->getCurrentBlockCount();
1975 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1976
Ted Kremenek0e470a52008-05-09 23:45:33 +00001977 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001978 ? cast<RVal>(lval::SymbolVal(Sym))
1979 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001980 }
1981
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001982 // Simulate the effects of a "store": bind the value of the RHS
1983 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00001984
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001985 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00001986 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001987 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001988
1989 case BinaryOperator::Div:
1990 case BinaryOperator::Rem:
1991
1992 // Special checking for integer denominators.
1993
1994 if (RHS->getType()->isIntegerType()
1995 && CheckDivideZero(B, St, *I2, RightV))
1996 continue;
1997
1998 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001999
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002000 default: {
2001
2002 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002003 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002004
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002005 // Process non-assignements except commas or short-circuited
2006 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002007
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002008 RVal Result = EvalBinOp(Op, LeftV, RightV);
2009
2010 if (Result.isUnknown()) {
2011 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002012 continue;
2013 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002014
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002015 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002016
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002017 // The operands were *not* undefined, but the result is undefined.
2018 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002019
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002020 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002021 UndefNode->markAsSink();
2022 UndefResults.insert(UndefNode);
2023 }
2024
2025 continue;
2026 }
2027
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002028 // Otherwise, create a new node.
2029
2030 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002031 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002032 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002033 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002034
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002035 assert (B->isCompoundAssignmentOp());
2036
2037 if (Op >= BinaryOperator::AndAssign)
2038 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2039 else
2040 ((int&) Op) -= BinaryOperator::MulAssign;
2041
2042 // Perform a load (the LHS). This performs the checks for
2043 // null dereferences, and so on.
2044 NodeSet Tmp3;
2045 RVal location = GetRVal(St, LHS);
2046 EvalLoad(Tmp3, LHS, *I2, St, location);
2047
2048 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2049
2050 St = GetState(*I3);
2051 RVal V = GetRVal(St, LHS);
2052
2053 // Propagate undefined values (left-side).
2054 if (V.isUndef()) {
2055 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2056 continue;
2057 }
2058
2059 // Propagate unknown values (left and right-side).
2060 if (RightV.isUnknown() || V.isUnknown()) {
2061 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2062 UnknownVal());
2063 continue;
2064 }
2065
2066 // At this point:
2067 //
2068 // The LHS is not Undef/Unknown.
2069 // The RHS is not Unknown.
2070
2071 // Get the computation type.
2072 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2073
2074 // Perform promotions.
2075 V = EvalCast(V, CTy);
2076 RightV = EvalCast(RightV, CTy);
2077
2078 // Evaluate operands and promote to result type.
2079
2080 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2081 && RHS->getType()->isIntegerType()) {
2082
2083 if (CheckDivideZero(B, St, *I3, RightV))
2084 continue;
2085 }
2086 else if (RightV.isUndef()) {
2087
2088 // Propagate undefined values (right-side).
2089
2090 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2091 continue;
2092 }
2093
2094 // Compute the result of the operation.
2095
2096 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2097
2098 if (Result.isUndef()) {
2099
2100 // The operands were not undefined, but the result is undefined.
2101
2102 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2103 UndefNode->markAsSink();
2104 UndefResults.insert(UndefNode);
2105 }
2106
2107 continue;
2108 }
2109
2110 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2111 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002112 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002113 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002114}
Ted Kremenekee985462008-01-16 18:18:48 +00002115
2116//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00002117// "Assume" logic.
2118//===----------------------------------------------------------------------===//
2119
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002120ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002121 bool Assumption, bool& isFeasible) {
2122
2123 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002124
2125 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2126 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002127}
2128
2129ValueState* GRExprEngine::AssumeAux(ValueState* St, LVal Cond,
2130 bool Assumption, bool& isFeasible) {
2131
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002132 switch (Cond.getSubKind()) {
2133 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002134 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002135 return St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002136
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002137 case lval::SymbolValKind:
2138 if (Assumption)
2139 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002140 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002141 else
2142 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002143 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek550a0f92008-04-18 17:20:23 +00002144
2145
Ted Kremenek329f8542008-02-05 21:52:21 +00002146 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00002147 case lval::FuncValKind:
2148 case lval::GotoLabelKind:
Ted Kremeneka5488462008-04-22 21:39:21 +00002149 case lval::StringLiteralValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002150 isFeasible = Assumption;
2151 return St;
Ted Kremenek718c4f72008-04-29 22:17:41 +00002152
2153 case lval::FieldOffsetKind:
2154 return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(),
2155 Assumption, isFeasible);
2156
Ted Kremenek4d0348b2008-04-29 23:24:44 +00002157 case lval::ArrayOffsetKind:
2158 return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(),
2159 Assumption, isFeasible);
2160
Ted Kremenek329f8542008-02-05 21:52:21 +00002161 case lval::ConcreteIntKind: {
2162 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002163 isFeasible = b ? Assumption : !Assumption;
2164 return St;
2165 }
2166 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002167}
2168
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002169ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002170 bool Assumption, bool& isFeasible) {
2171
2172 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002173
2174 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2175 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002176}
2177
2178ValueState* GRExprEngine::AssumeAux(ValueState* St, NonLVal Cond,
2179 bool Assumption, bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002180 switch (Cond.getSubKind()) {
2181 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002182 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00002183 return St;
2184
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002185
2186 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00002187 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002188 SymbolID sym = SV.getSymbol();
2189
2190 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00002191 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002192 isFeasible);
2193 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00002194 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002195 isFeasible);
2196 }
2197
Ted Kremenek08b66252008-02-06 04:31:33 +00002198 case nonlval::SymIntConstraintValKind:
2199 return
2200 AssumeSymInt(St, Assumption,
2201 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
2202 isFeasible);
2203
Ted Kremenek329f8542008-02-05 21:52:21 +00002204 case nonlval::ConcreteIntKind: {
2205 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00002206 isFeasible = b ? Assumption : !Assumption;
2207 return St;
2208 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002209
2210 case nonlval::LValAsIntegerKind: {
2211 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
2212 Assumption, isFeasible);
2213 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002214 }
2215}
2216
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002217ValueState*
2218GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002219 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00002220
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002221 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002222 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002223 isFeasible = *X != V;
2224 return St;
2225 }
2226
2227 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002228 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002229 isFeasible = true;
2230 return St;
2231 }
2232
2233 // If we reach here, sym is not a constant and we don't know if it is != V.
2234 // Make that assumption.
2235
2236 isFeasible = true;
2237 return StateMgr.AddNE(St, sym, V);
2238}
2239
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002240ValueState*
2241GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002242 const llvm::APSInt& V, bool& isFeasible) {
2243
2244 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002245 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002246 isFeasible = *X == V;
2247 return St;
2248 }
2249
2250 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002251 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002252 isFeasible = false;
2253 return St;
2254 }
2255
2256 // If we reach here, sym is not a constant and we don't know if it is == V.
2257 // Make that assumption.
2258
2259 isFeasible = true;
2260 return StateMgr.AddEQ(St, sym, V);
2261}
Ted Kremenekb38911f2008-01-30 23:03:39 +00002262
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002263ValueState*
2264GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00002265 const SymIntConstraint& C, bool& isFeasible) {
2266
2267 switch (C.getOpcode()) {
2268 default:
2269 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002270 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00002271 return St;
2272
2273 case BinaryOperator::EQ:
2274 if (Assumption)
2275 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2276 else
2277 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2278
2279 case BinaryOperator::NE:
2280 if (Assumption)
2281 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2282 else
2283 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2284 }
2285}
2286
Ted Kremenekb38911f2008-01-30 23:03:39 +00002287//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002288// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002289//===----------------------------------------------------------------------===//
2290
Ted Kremenekaa66a322008-01-16 21:46:15 +00002291#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002292static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002293static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002294static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002295
Ted Kremenekaa66a322008-01-16 21:46:15 +00002296namespace llvm {
2297template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002298struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002299 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002300
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002301 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002302
2303 Out << "Variables:\\l";
2304
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002305 bool isFirst = true;
2306
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002307 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002308
2309 if (isFirst)
2310 isFirst = false;
2311 else
2312 Out << "\\l";
2313
2314 Out << ' ' << I.getKey()->getName() << " : ";
2315 I.getData().print(Out);
2316 }
2317
2318 }
2319
Ted Kremeneke7d22112008-02-11 19:21:59 +00002320
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002321 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002322
2323 bool isFirst = true;
2324
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002325 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002326
2327 if (isFirst) {
2328 Out << "\\l\\lSub-Expressions:\\l";
2329 isFirst = false;
2330 }
2331 else
2332 Out << "\\l";
2333
2334 Out << " (" << (void*) I.getKey() << ") ";
2335 I.getKey()->printPretty(Out);
2336 Out << " : ";
2337 I.getData().print(Out);
2338 }
2339 }
2340
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002341 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002342
Ted Kremenek016f52f2008-02-08 21:10:02 +00002343 bool isFirst = true;
2344
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002345 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002346 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002347 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002348 isFirst = false;
2349 }
2350 else
2351 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002352
Ted Kremeneke7d22112008-02-11 19:21:59 +00002353 Out << " (" << (void*) I.getKey() << ") ";
2354 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002355 Out << " : ";
2356 I.getData().print(Out);
2357 }
2358 }
2359
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002360 static void PrintEQ(std::ostream& Out, ValueState* St) {
2361 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002362
2363 if (CE.isEmpty())
2364 return;
2365
2366 Out << "\\l\\|'==' constraints:";
2367
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002368 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002369 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2370 }
2371
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002372 static void PrintNE(std::ostream& Out, ValueState* St) {
2373 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002374
2375 if (NE.isEmpty())
2376 return;
2377
2378 Out << "\\l\\|'!=' constraints:";
2379
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002380 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002381 I != EI; ++I){
2382
2383 Out << "\\l $" << I.getKey() << " : ";
2384 bool isFirst = true;
2385
2386 ValueState::IntSetTy::iterator J=I.getData().begin(),
2387 EJ=I.getData().end();
2388 for ( ; J != EJ; ++J) {
2389 if (isFirst) isFirst = false;
2390 else Out << ", ";
2391
2392 Out << (*J)->toString();
2393 }
2394 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002395 }
2396
2397 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2398
2399 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002400 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002401 GraphPrintCheckerState->isUndefDeref(N) ||
2402 GraphPrintCheckerState->isUndefStore(N) ||
2403 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002404 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2405 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002406 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002407 GraphPrintCheckerState->isBadCall(N) ||
2408 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002409 return "color=\"red\",style=\"filled\"";
2410
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002411 if (GraphPrintCheckerState->isNoReturnCall(N))
2412 return "color=\"blue\",style=\"filled\"";
2413
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002414 return "";
2415 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002416
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002417 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002418 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002419
2420 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002421 ProgramPoint Loc = N->getLocation();
2422
2423 switch (Loc.getKind()) {
2424 case ProgramPoint::BlockEntranceKind:
2425 Out << "Block Entrance: B"
2426 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2427 break;
2428
2429 case ProgramPoint::BlockExitKind:
2430 assert (false);
2431 break;
2432
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002433 case ProgramPoint::PostLoadKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002434 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002435 const PostStmt& L = cast<PostStmt>(Loc);
2436 Stmt* S = L.getStmt();
2437 SourceLocation SLoc = S->getLocStart();
2438
2439 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2440 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002441
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002442 if (SLoc.isFileID()) {
2443 Out << "\\lline="
2444 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2445 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2446 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002447
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002448 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002449 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002450 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002451 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002452 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002453 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002454 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002455 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002456 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2457 Out << "\\|Explicit divide-by zero or undefined value.";
2458 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2459 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002460 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002461 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002462 else if (GraphPrintCheckerState->isNoReturnCall(N))
2463 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002464 else if (GraphPrintCheckerState->isBadCall(N))
2465 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002466 else if (GraphPrintCheckerState->isUndefArg(N))
2467 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002468
Ted Kremenekaa66a322008-01-16 21:46:15 +00002469 break;
2470 }
2471
2472 default: {
2473 const BlockEdge& E = cast<BlockEdge>(Loc);
2474 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2475 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002476
2477 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002478
2479 SourceLocation SLoc = T->getLocStart();
2480
Ted Kremenekb38911f2008-01-30 23:03:39 +00002481 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002482
Ted Kremenekb38911f2008-01-30 23:03:39 +00002483 E.getSrc()->printTerminator(Out);
2484
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002485 if (SLoc.isFileID()) {
2486 Out << "\\lline="
2487 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2488 << GraphPrintSourceManager->getColumnNumber(SLoc);
2489 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002490
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002491 if (isa<SwitchStmt>(T)) {
2492 Stmt* Label = E.getDst()->getLabel();
2493
2494 if (Label) {
2495 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2496 Out << "\\lcase ";
2497 C->getLHS()->printPretty(Out);
2498
2499 if (Stmt* RHS = C->getRHS()) {
2500 Out << " .. ";
2501 RHS->printPretty(Out);
2502 }
2503
2504 Out << ":";
2505 }
2506 else {
2507 assert (isa<DefaultStmt>(Label));
2508 Out << "\\ldefault:";
2509 }
2510 }
2511 else
2512 Out << "\\l(implicit) default:";
2513 }
2514 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002515 // FIXME
2516 }
2517 else {
2518 Out << "\\lCondition: ";
2519 if (*E.getSrc()->succ_begin() == E.getDst())
2520 Out << "true";
2521 else
2522 Out << "false";
2523 }
2524
2525 Out << "\\l";
2526 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002527
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002528 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2529 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002530 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002531 }
2532 }
2533
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002534 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002535
Ted Kremenek75da3e82008-03-11 19:02:40 +00002536 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002537
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002538 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002539 return Out.str();
2540 }
2541};
2542} // end llvm namespace
2543#endif
2544
Ted Kremenekffe0f432008-03-07 22:58:01 +00002545#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002546
2547template <typename ITERATOR>
2548GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2549
2550template <>
2551GRExprEngine::NodeTy*
2552GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2553 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2554 return I->first;
2555}
2556
Ted Kremenekffe0f432008-03-07 22:58:01 +00002557template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002558static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002559 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002560
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002561 llvm::SmallPtrSet<void*,10> CachedSources;
2562
2563 for ( ; I != E; ++I ) {
2564 GRExprEngine::NodeTy* N = GetGraphNode(I);
2565 void* p = N->getLocation().getRawData();
2566
2567 if (CachedSources.count(p))
2568 continue;
2569
2570 CachedSources.insert(p);
2571
2572 Sources.push_back(N);
2573 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002574}
2575#endif
2576
2577void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002578#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002579 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002580 std::vector<NodeTy*> Src;
2581
2582 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002583 AddSources(Src, null_derefs_begin(), null_derefs_end());
2584 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2585 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2586 AddSources(Src, undef_results_begin(), undef_results_end());
2587 AddSources(Src, bad_calls_begin(), bad_calls_end());
2588 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002589 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002590
Ted Kremenekcb612922008-04-18 19:23:43 +00002591 // The new way.
2592 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2593 (*I)->GetErrorNodes(Src);
2594
2595
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002596 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002597 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002598 else {
2599 GraphPrintCheckerState = this;
2600 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002601 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002602
Ted Kremenekffe0f432008-03-07 22:58:01 +00002603 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002604
2605 GraphPrintCheckerState = NULL;
2606 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002607 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002608 }
2609#endif
2610}
2611
2612void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2613#ifndef NDEBUG
2614 GraphPrintCheckerState = this;
2615 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002616 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002617
Ted Kremenek493d7a22008-03-11 18:25:33 +00002618 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2619
2620 if (!TrimmedG)
2621 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2622 else {
2623 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2624 delete TrimmedG;
2625 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002626
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002627 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002628 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002629 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002630#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002631}