blob: 8dee018931eca6297bb6dbabc3b145f2ba807093 [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 Kremenekbdb435d2008-07-11 18:37:32 +000016#include "clang/Analysis/PathSensitive/BasicStore.h"
Ted Kremenek77349cb2008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000019#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000020#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000021#include "llvm/ADT/ImmutableList.h"
22#include "llvm/Support/Compiler.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
88 virtual bool Audit(NodeTy* N) {
89 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
99 isSink |= (*I)->Audit(N);
100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
118 LiveVariables& L)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 Builder(NULL),
Ted Kremenek4323a572008-07-10 22:03:41 +0000123 StateMgr(G.getContext(), CreateBasicStoreManager(G.getAllocator()),
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000124 G.getAllocator(), G.getCFG()),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000125 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000126 CurrentStmt(NULL),
127 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000128 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000129
Ted Kremenek1a654b62008-06-20 21:45:25 +0000130GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000131 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
132 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000133
Ted Kremeneke448ab42008-05-01 18:33:28 +0000134
135 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000136}
137
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000138//===----------------------------------------------------------------------===//
139// Utility methods.
140//===----------------------------------------------------------------------===//
141
142// SaveAndRestore - A utility class that uses RIIA to save and restore
143// the value of a variable.
144template<typename T>
145struct VISIBILITY_HIDDEN SaveAndRestore {
146 SaveAndRestore(T& x) : X(x), old_value(x) {}
147 ~SaveAndRestore() { X = old_value; }
148 T get() { return old_value; }
149
150 T& X;
151 T old_value;
152};
153
Ted Kremenek186350f2008-04-23 20:12:28 +0000154// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
155// value of a variable is saved, and during the dstor the old value is
156// or'ed with the new value.
157struct VISIBILITY_HIDDEN SaveOr {
158 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
159 ~SaveOr() { X |= old_value; }
160
161 bool& X;
162 bool old_value;
163};
164
165
Ted Kremenekc0959972008-07-02 21:24:01 +0000166void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000167 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000168 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000169 (*I)->EmitWarnings(BR);
170 }
171
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000172 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000173 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000174 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000175 }
176}
177
178void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000179 StateMgr.TF = tf;
180 getTF().RegisterChecks(*this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000181}
182
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000183void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
184 if (!BatchAuditor)
185 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
186
187 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000188}
189
Ted Kremenek4323a572008-07-10 22:03:41 +0000190const ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +0000191
192 // The LiveVariables information already has a compilation of all VarDecls
193 // used in the function. Iterate through this set, and "symbolicate"
194 // any VarDecl whose value originally comes from outside the function.
195
196 typedef LiveVariables::AnalysisDataTy LVDataTy;
197 LVDataTy& D = Liveness.getAnalysisData();
198
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000199 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +0000200
201 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
202
Chris Lattner41110242008-06-17 18:05:57 +0000203 ScopedDecl *SD = const_cast<ScopedDecl*>(I->first);
204 if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
205 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
206 RVal X = RVal::GetSymbolValue(SymMgr, VD);
Ted Kremenek4323a572008-07-10 22:03:41 +0000207 StateMgr.SetRVal(StateImpl, lval::DeclVal(VD), X);
Chris Lattner41110242008-06-17 18:05:57 +0000208 }
209 } else if (ImplicitParamDecl *IPD = dyn_cast<ImplicitParamDecl>(SD)) {
210 RVal X = RVal::GetSymbolValue(SymMgr, IPD);
Ted Kremenek4323a572008-07-10 22:03:41 +0000211 StateMgr.SetRVal(StateImpl, lval::DeclVal(IPD), X);
Ted Kremenek07932632008-02-27 06:47:26 +0000212 }
Chris Lattner41110242008-06-17 18:05:57 +0000213
214
Ted Kremenek07932632008-02-27 06:47:26 +0000215 }
216
217 return StateMgr.getPersistentState(StateImpl);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000218}
219
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000220//===----------------------------------------------------------------------===//
221// Top-level transfer function logic (Dispatcher).
222//===----------------------------------------------------------------------===//
223
224void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
225
226 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000227 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000228
229 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000230 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000231 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000232
233 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000234 if (BatchAuditor)
235 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000236
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000237 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000238 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
239 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000240
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000241 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000243
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000244 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000245 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000246 else {
247 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000248 SaveOr OldHasGen(Builder->HasGeneratedNode);
249
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000250 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
251 Builder->PurgingDeadSymbols = true;
252
Ted Kremenek729a9a22008-07-17 23:15:45 +0000253 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000254 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000255
256 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
257 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000258 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000259
260 bool HasAutoGenerated = false;
261
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000262 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000263
264 NodeSet Dst;
265
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000266 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000267 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
268
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000269 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000270 Visit(S, *I, Dst);
271
272 // Do we need to auto-generate a node? We only need to do this to generate
273 // a node with a "cleaned" state; GRCoreEngine will actually handle
274 // auto-transitions for other cases.
275 if (Dst.size() == 1 && *Dst.begin() == EntryNode
276 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
277 HasAutoGenerated = true;
278 builder.generateNode(S, GetState(EntryNode), *I);
279 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000280 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000281
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000282 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000283 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000284 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000285
286 // FIXME: Consolidate.
287 StateMgr.CurrentStmt = 0;
288 CurrentStmt = 0;
289
Ted Kremenek846d4e92008-04-24 23:35:58 +0000290 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000291}
292
293void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
294
295 // FIXME: add metadata to the CFG so that we can disable
296 // this check when we KNOW that there is no block-level subexpression.
297 // The motivation is that this check requires a hashtable lookup.
298
299 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
300 Dst.Add(Pred);
301 return;
302 }
303
304 switch (S->getStmtClass()) {
305
306 default:
307 // Cases we intentionally have "default" handle:
308 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
309
310 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
311 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000312
313 case Stmt::ArraySubscriptExprClass:
314 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
315 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000316
317 case Stmt::AsmStmtClass:
318 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
319 break;
320
321 case Stmt::BinaryOperatorClass: {
322 BinaryOperator* B = cast<BinaryOperator>(S);
323
324 if (B->isLogicalOp()) {
325 VisitLogicalExpr(B, Pred, Dst);
326 break;
327 }
328 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000329 const ValueState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000330 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
331 break;
332 }
333
334 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
335 break;
336 }
337
338 case Stmt::CallExprClass: {
339 CallExpr* C = cast<CallExpr>(S);
340 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
341 break;
342 }
343
344 case Stmt::CastExprClass: {
345 CastExpr* C = cast<CastExpr>(S);
346 VisitCast(C, C->getSubExpr(), Pred, Dst);
347 break;
348 }
349
350 // FIXME: ChooseExpr is really a constant. We need to fix
351 // the CFG do not model them as explicit control-flow.
352
353 case Stmt::ChooseExprClass: { // __builtin_choose_expr
354 ChooseExpr* C = cast<ChooseExpr>(S);
355 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
356 break;
357 }
358
359 case Stmt::CompoundAssignOperatorClass:
360 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
361 break;
362
363 case Stmt::ConditionalOperatorClass: { // '?' operator
364 ConditionalOperator* C = cast<ConditionalOperator>(S);
365 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
366 break;
367 }
368
369 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000370 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000371 break;
372
373 case Stmt::DeclStmtClass:
374 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
375 break;
376
377 case Stmt::ImplicitCastExprClass: {
378 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
379 VisitCast(C, C->getSubExpr(), Pred, Dst);
380 break;
381 }
382
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000383 case Stmt::MemberExprClass: {
384 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
385 break;
386 }
387
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000388 case Stmt::ObjCMessageExprClass: {
389 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
390 break;
391 }
392
393 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000394 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000395 break;
396
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000397 case Stmt::ReturnStmtClass:
398 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
399 break;
400
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000401 case Stmt::SizeOfAlignOfTypeExprClass:
402 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
403 break;
404
405 case Stmt::StmtExprClass: {
406 StmtExpr* SE = cast<StmtExpr>(S);
407
Ted Kremenek4323a572008-07-10 22:03:41 +0000408 const ValueState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000409
410 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
411 // probably just remove these from the CFG.
412 assert (!SE->getSubStmt()->body_empty());
413
414 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
415 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
416 else
417 Dst.Add(Pred);
418
419 break;
420 }
421
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000422 case Stmt::UnaryOperatorClass:
423 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000424 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000425 }
426}
427
428void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
429
430 Ex = Ex->IgnoreParens();
431
432 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
433 Dst.Add(Pred);
434 return;
435 }
436
437 switch (Ex->getStmtClass()) {
438 default:
439 Visit(Ex, Pred, Dst);
440 return;
441
442 case Stmt::ArraySubscriptExprClass:
443 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
444 return;
445
446 case Stmt::DeclRefExprClass:
447 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
448 return;
449
450 case Stmt::UnaryOperatorClass:
451 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
452 return;
453
454 case Stmt::MemberExprClass:
455 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
456 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000457 }
458}
459
460//===----------------------------------------------------------------------===//
461// Block entrance. (Update counters).
462//===----------------------------------------------------------------------===//
463
Ted Kremenek4323a572008-07-10 22:03:41 +0000464bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const ValueState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000465 GRBlockCounter BC) {
466
467 return BC.getNumVisited(B->getBlockID()) < 3;
468}
469
470//===----------------------------------------------------------------------===//
471// Branch processing.
472//===----------------------------------------------------------------------===//
473
Ted Kremenek4323a572008-07-10 22:03:41 +0000474const ValueState* GRExprEngine::MarkBranch(const ValueState* St,
475 Stmt* Terminator,
476 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000477
478 switch (Terminator->getStmtClass()) {
479 default:
480 return St;
481
482 case Stmt::BinaryOperatorClass: { // '&&' and '||'
483
484 BinaryOperator* B = cast<BinaryOperator>(Terminator);
485 BinaryOperator::Opcode Op = B->getOpcode();
486
487 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
488
489 // For &&, if we take the true branch, then the value of the whole
490 // expression is that of the RHS expression.
491 //
492 // For ||, if we take the false branch, then the value of the whole
493 // expression is that of the RHS expression.
494
495 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
496 (Op == BinaryOperator::LOr && !branchTaken)
497 ? B->getRHS() : B->getLHS();
498
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000499 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000500 }
501
502 case Stmt::ConditionalOperatorClass: { // ?:
503
504 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
505
506 // For ?, if branchTaken == true then the value is either the LHS or
507 // the condition itself. (GNU extension).
508
509 Expr* Ex;
510
511 if (branchTaken)
512 Ex = C->getLHS() ? C->getLHS() : C->getCond();
513 else
514 Ex = C->getRHS();
515
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000516 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000517 }
518
519 case Stmt::ChooseExprClass: { // ?:
520
521 ChooseExpr* C = cast<ChooseExpr>(Terminator);
522
523 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000524 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000525 }
526 }
527}
528
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000529void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000530 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000531
Ted Kremeneke7d22112008-02-11 19:21:59 +0000532 // Remove old bindings for subexpressions.
Ted Kremenek4323a572008-07-10 22:03:41 +0000533 const ValueState* PrevState =
534 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000535
Ted Kremenekb2331832008-02-15 22:29:00 +0000536 // Check for NULL conditions; e.g. "for(;;)"
537 if (!Condition) {
538 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000539 return;
540 }
541
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000542 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000543
544 switch (V.getBaseKind()) {
545 default:
546 break;
547
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000548 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000549 builder.generateNode(MarkBranch(PrevState, Term, true), true);
550 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000551 return;
552
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000553 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000554 NodeTy* N = builder.generateNode(PrevState, true);
555
556 if (N) {
557 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000558 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000559 }
560
561 builder.markInfeasible(false);
562 return;
563 }
564 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000565
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000566 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000567
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000568 bool isFeasible = false;
Ted Kremenek4323a572008-07-10 22:03:41 +0000569 const ValueState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000570
571 if (isFeasible)
572 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000573 else
574 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000575
576 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000577
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000578 isFeasible = false;
579 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000580
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000581 if (isFeasible)
582 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000583 else
584 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000585}
586
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000587/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000588/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000589void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000590
Ted Kremenek4323a572008-07-10 22:03:41 +0000591 const ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000592 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000593
594 // Three possibilities:
595 //
596 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000597 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000598 // (3) We have no clue about the label. Dispatch to all targets.
599 //
600
601 typedef IndirectGotoNodeBuilder::iterator iterator;
602
603 if (isa<lval::GotoLabel>(V)) {
604 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
605
606 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000607 if (I.getLabel() == L) {
608 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000609 return;
610 }
611 }
612
613 assert (false && "No block with label.");
614 return;
615 }
616
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000617 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000618 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000619 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000620 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000621 return;
622 }
623
624 // This is really a catch-all. We don't support symbolics yet.
625
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000626 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000627
628 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000629 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000630}
Ted Kremenekf233d482008-02-05 00:26:40 +0000631
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000632
633void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
634 NodeTy* Pred, NodeSet& Dst) {
635
636 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
637
Ted Kremenek4323a572008-07-10 22:03:41 +0000638 const ValueState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000639 RVal X = GetBlkExprRVal(St, Ex);
640
641 assert (X.isUndef());
642
643 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
644
645 assert (SE);
646
647 X = GetBlkExprRVal(St, SE);
648
649 // Make sure that we invalidate the previous binding.
650 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
651}
652
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000653/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
654/// nodes by processing the 'effects' of a switch statement.
655void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
656
657 typedef SwitchNodeBuilder::iterator iterator;
658
Ted Kremenek4323a572008-07-10 22:03:41 +0000659 const ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000660 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000661 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000662
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000663 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000664 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000665 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000666 return;
667 }
668
Ted Kremenek4323a572008-07-10 22:03:41 +0000669 const ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000670
671 // While most of this can be assumed (such as the signedness), having it
672 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000673
Chris Lattner98be4942008-03-05 18:54:05 +0000674 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000675
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000676 APSInt V1(bits, false);
677 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000678 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000680 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000681
682 CaseStmt* Case = cast<CaseStmt>(I.getCase());
683
684 // Evaluate the case.
685 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
686 assert (false && "Case condition must evaluate to an integer constant.");
687 return;
688 }
689
690 // Get the RHS of the case, if it exists.
691
692 if (Expr* E = Case->getRHS()) {
693 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
694 assert (false &&
695 "Case condition (RHS) must evaluate to an integer constant.");
696 return ;
697 }
698
699 assert (V1 <= V2);
700 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000701 else
702 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000703
704 // FIXME: Eventually we should replace the logic below with a range
705 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000706 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000707
Ted Kremenek14a11402008-03-17 22:17:56 +0000708 do {
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000709 nonlval::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000710
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000711 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000712
713 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000714
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000715 bool isFeasible = false;
Ted Kremenek4323a572008-07-10 22:03:41 +0000716 const ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000717
718 if (isFeasible) {
719 builder.generateCaseStmtNode(I, StNew);
720
721 // If CondV evaluates to a constant, then we know that this
722 // is the *only* case that we can take, so stop evaluating the
723 // others.
724 if (isa<nonlval::ConcreteInt>(CondV))
725 return;
726 }
727
728 // Now "assume" that the case doesn't match. Add this state
729 // to the default state (if it is feasible).
730
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000731 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000732 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000733
Ted Kremenek5014ab12008-04-23 05:03:18 +0000734 if (isFeasible) {
735 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000736 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000737 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738
Ted Kremenek14a11402008-03-17 22:17:56 +0000739 // Concretize the next value in the range.
740 if (V1 == V2)
741 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000742
Ted Kremenek14a11402008-03-17 22:17:56 +0000743 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000744 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000745
746 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000747 }
748
749 // If we reach here, than we know that the default branch is
750 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000751 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000752}
753
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000754//===----------------------------------------------------------------------===//
755// Transfer functions: logical operations ('&&', '||').
756//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000757
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000758void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000759 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000760
Ted Kremenek05a23782008-02-26 19:05:15 +0000761 assert (B->getOpcode() == BinaryOperator::LAnd ||
762 B->getOpcode() == BinaryOperator::LOr);
763
764 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
765
Ted Kremenek4323a572008-07-10 22:03:41 +0000766 const ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000767 RVal X = GetBlkExprRVal(St, B);
768
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000769 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000770
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000771 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000772
773 assert (Ex);
774
775 if (Ex == B->getRHS()) {
776
777 X = GetBlkExprRVal(St, Ex);
778
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000779 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000780
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000781 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000782 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000783 return;
784 }
785
Ted Kremenek05a23782008-02-26 19:05:15 +0000786 // We took the RHS. Because the value of the '&&' or '||' expression must
787 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
788 // or 1. Alternatively, we could take a lazy approach, and calculate this
789 // value later when necessary. We don't have the machinery in place for
790 // this right now, and since most logical expressions are used for branches,
791 // the payoff is not likely to be large. Instead, we do eager evaluation.
792
793 bool isFeasible = false;
Ted Kremenek4323a572008-07-10 22:03:41 +0000794 const ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000795
796 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000797 MakeNode(Dst, B, Pred,
798 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000799
800 isFeasible = false;
801 NewState = Assume(St, X, false, isFeasible);
802
803 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000804 MakeNode(Dst, B, Pred,
805 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000806 }
807 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000808 // We took the LHS expression. Depending on whether we are '&&' or
809 // '||' we know what the value of the expression is via properties of
810 // the short-circuiting.
811
812 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000813 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000814 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000815}
Ted Kremenek05a23782008-02-26 19:05:15 +0000816
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000817//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000818// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000819//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000820
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000821void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
822 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000823
Ted Kremenek4323a572008-07-10 22:03:41 +0000824 const ValueState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000825 RVal X = RVal::MakeVal(getBasicVals(), D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000826
827 if (asLVal)
828 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
829 else {
830 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
831 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
832 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000833}
834
Ted Kremenek540cbe22008-04-22 04:56:29 +0000835/// VisitArraySubscriptExpr - Transfer function for array accesses
836void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
837 NodeSet& Dst, bool asLVal) {
838
839 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000840 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000841
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000842 // Always visit the base as an LVal expression. This computes the
843 // abstract address of the base object.
844 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000845
Ted Kremenek0e470a52008-05-09 23:45:33 +0000846 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000847 Visit(Base, Pred, Tmp);
848 else
849 VisitLVal(Base, Pred, Tmp);
850
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000851 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000852
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000853 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000854
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000855 NodeSet Tmp2;
856 Visit(Idx, *I1, Tmp2);
857
858 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
859
Ted Kremenek4323a572008-07-10 22:03:41 +0000860 const ValueState* St = GetState(*I2);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000861 RVal BaseV = GetRVal(St, Base);
862 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000863
864 // If IdxV is 0, return just BaseV.
865
866 bool useBase = false;
867
868 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
869 useBase = IdxInt->getValue() == 0;
870
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000871 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(getBasicVals(), BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000872
873 if (asLVal)
874 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
875 else
876 EvalLoad(Dst, A, *I2, St, V);
877 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000878 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000879}
880
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000881/// VisitMemberExpr - Transfer function for member expressions.
882void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
883 NodeSet& Dst, bool asLVal) {
884
885 Expr* Base = M->getBase()->IgnoreParens();
886
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000887 // Always visit the base as an LVal expression. This computes the
888 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000889 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000890
Ted Kremenekee90dba2008-04-30 22:17:15 +0000891 if (asLVal) {
892
Ted Kremenek0e470a52008-05-09 23:45:33 +0000893 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000894 Visit(Base, Pred, Tmp);
895 else
896 VisitLVal(Base, Pred, Tmp);
897
898 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000899 const ValueState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000900 RVal BaseV = GetRVal(St, Base);
901
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000902 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000903 M->getMemberDecl());
904
905 MakeNode(Dst, M, *I, SetRVal(St, M, V));
906 }
907
908 return;
909 }
910
911 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
912 // or not isArrow() is true).
913 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000914
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000915 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000916
Ted Kremenek4323a572008-07-10 22:03:41 +0000917 const ValueState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000918 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000919
Ted Kremenek0e470a52008-05-09 23:45:33 +0000920 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000921
Ted Kremenekee90dba2008-04-30 22:17:15 +0000922 assert (M->isArrow());
923
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000924 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000925 M->getMemberDecl());
926
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000927 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000928 }
929 else {
930
931 assert (!M->isArrow());
932
933 if (BaseV.isUnknownOrUndef()) {
934 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
935 continue;
936 }
937
938 // FIXME: Implement nonlval objects representing struct temporaries.
939 assert (isa<NonLVal>(BaseV));
940 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
941 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000942 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000943}
944
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000945void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4323a572008-07-10 22:03:41 +0000946 const ValueState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000947
948 assert (Builder && "GRStmtNodeBuilder must be defined.");
949
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000950 // Evaluate the location (checks for bad dereferences).
951 St = EvalLocation(Ex, Pred, St, location);
952
953 if (!St)
954 return;
955
956 // Proceed with the store.
957
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000958 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000959
Ted Kremenek186350f2008-04-23 20:12:28 +0000960 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
961 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000962
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000963 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000964
Ted Kremenek729a9a22008-07-17 23:15:45 +0000965 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000966
967 // Handle the case where no nodes where generated. Auto-generate that
968 // contains the updated state if we aren't generating sinks.
969
Ted Kremenekb0533962008-04-18 20:35:30 +0000970 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000971 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000972 location, Val);
973}
974
975void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4323a572008-07-10 22:03:41 +0000976 const ValueState* St, RVal location,
977 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978
979 // Evaluate the location (checks for bad dereferences).
980
981 St = EvalLocation(Ex, Pred, St, location, true);
982
983 if (!St)
984 return;
985
986 // Proceed with the load.
987
988 // FIXME: Currently symbolic analysis "generates" new symbols
989 // for the contents of values. We need a better approach.
990
991 // FIXME: The "CheckOnly" option exists only because Array and Field
992 // loads aren't fully implemented. Eventually this option will go away.
993
Ted Kremenek436f2b92008-04-30 04:23:07 +0000994 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000995 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000996 else if (location.isUnknown()) {
997 // This is important. We must nuke the old binding.
998 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
999 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001000 else
1001 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
1002 Ex->getType())));
1003}
1004
Ted Kremenek4323a572008-07-10 22:03:41 +00001005const ValueState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
1006 const ValueState* St,
1007 RVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001008
1009 // Check for loads/stores from/to undefined values.
1010 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001011 ProgramPoint::Kind K =
1012 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1013
1014 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001015 Succ->markAsSink();
1016 UndefDeref.insert(Succ);
1017 }
1018
1019 return NULL;
1020 }
1021
1022 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1023 if (location.isUnknown())
1024 return St;
1025
1026 // During a load, one of two possible situations arise:
1027 // (1) A crash, because the location (pointer) was NULL.
1028 // (2) The location (pointer) is not NULL, and the dereference works.
1029 //
1030 // We add these assumptions.
1031
1032 LVal LV = cast<LVal>(location);
1033
1034 // "Assume" that the pointer is not NULL.
1035
1036 bool isFeasibleNotNull = false;
Ted Kremenek4323a572008-07-10 22:03:41 +00001037 const ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001038
1039 // "Assume" that the pointer is NULL.
1040
1041 bool isFeasibleNull = false;
Ted Kremenek4323a572008-07-10 22:03:41 +00001042 const ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001043
1044 if (isFeasibleNull) {
1045
1046 // We don't use "MakeNode" here because the node will be a sink
1047 // and we have no intention of processing it later.
1048
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001049 ProgramPoint::Kind K =
1050 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1051
1052 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053
1054 if (NullNode) {
1055
1056 NullNode->markAsSink();
1057
1058 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1059 else ExplicitNullDeref.insert(NullNode);
1060 }
1061 }
1062
1063 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001064}
1065
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001066//===----------------------------------------------------------------------===//
1067// Transfer function: Function calls.
1068//===----------------------------------------------------------------------===//
1069
Ted Kremenekde434242008-02-19 01:44:53 +00001070void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001071 CallExpr::arg_iterator AI,
1072 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001073 NodeSet& Dst) {
1074
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001075 // Process the arguments.
1076
1077 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001078
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001079 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001080 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001081 ++AI;
1082
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001083 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001084 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001085
1086 return;
1087 }
1088
1089 // If we reach here we have processed all of the arguments. Evaluate
1090 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001091
Ted Kremenek994a09b2008-02-25 21:16:03 +00001092 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001093 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001094
Ted Kremenek994a09b2008-02-25 21:16:03 +00001095 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001096
Ted Kremenekde434242008-02-19 01:44:53 +00001097 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001098 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1099
Ted Kremenek4323a572008-07-10 22:03:41 +00001100 const ValueState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001101 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001102
Ted Kremeneka1354a52008-03-03 16:47:31 +00001103 // FIXME: Add support for symbolic function calls (calls involving
1104 // function pointer values that are symbolic).
1105
1106 // Check for undefined control-flow or calls to NULL.
1107
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001108 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001109 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001110
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001111 if (N) {
1112 N->markAsSink();
1113 BadCalls.insert(N);
1114 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001115
Ted Kremenekde434242008-02-19 01:44:53 +00001116 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001117 }
1118
1119 // Check for the "noreturn" attribute.
1120
1121 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1122
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001123 if (isa<lval::FuncVal>(L)) {
1124
1125 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1126
1127 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001128 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001129 else {
1130 // HACK: Some functions are not marked noreturn, and don't return.
1131 // Here are a few hardwired ones. If this takes too long, we can
1132 // potentially cache these results.
1133 const char* s = FD->getIdentifier()->getName();
1134 unsigned n = strlen(s);
1135
1136 switch (n) {
1137 default:
1138 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001139
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001140 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001141 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1142 break;
1143
1144 case 5:
1145 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1146 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001147
1148 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001149 if (!memcmp(s, "Assert", 6)) {
1150 Builder->BuildSinks = true;
1151 break;
1152 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001153
1154 // FIXME: This is just a wrapper around throwing an exception.
1155 // Eventually inter-procedural analysis should handle this easily.
1156 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1157
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001158 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001159
1160 case 7:
1161 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1162 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001163
Ted Kremenekf47bb782008-04-30 17:54:04 +00001164 case 8:
1165 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1166 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001167
1168 case 12:
1169 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1170 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001171
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001172 case 14:
1173 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1174 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001175
1176 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001177 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1178 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001179 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001180
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001181 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001182 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001183
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001184 }
1185 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001186
1187 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001188
1189 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001190
1191 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1192
Ted Kremenek186350f2008-04-23 20:12:28 +00001193 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001194 switch (id) {
1195 case Builtin::BI__builtin_expect: {
1196 // For __builtin_expect, just return the value of the subexpression.
1197 assert (CE->arg_begin() != CE->arg_end());
1198 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001199 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001200 continue;
1201 }
1202
1203 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001204 break;
1205 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001206 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001207
Ted Kremenek186350f2008-04-23 20:12:28 +00001208 // Check any arguments passed-by-value against being undefined.
1209
1210 bool badArg = false;
1211
1212 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1213 I != E; ++I) {
1214
1215 if (GetRVal(GetState(*DI), *I).isUndef()) {
1216 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001217
Ted Kremenek186350f2008-04-23 20:12:28 +00001218 if (N) {
1219 N->markAsSink();
1220 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001221 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001222
Ted Kremenek186350f2008-04-23 20:12:28 +00001223 badArg = true;
1224 break;
1225 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001226 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001227
1228 if (badArg)
1229 continue;
1230
1231 // Dispatch to the plug-in transfer function.
1232
1233 unsigned size = Dst.size();
1234 SaveOr OldHasGen(Builder->HasGeneratedNode);
1235 EvalCall(Dst, CE, L, *DI);
1236
1237 // Handle the case where no nodes where generated. Auto-generate that
1238 // contains the updated state if we aren't generating sinks.
1239
1240 if (!Builder->BuildSinks && Dst.size() == size &&
1241 !Builder->HasGeneratedNode)
1242 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001243 }
1244}
1245
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001246//===----------------------------------------------------------------------===//
1247// Transfer function: Objective-C message expressions.
1248//===----------------------------------------------------------------------===//
1249
1250void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1251 NodeSet& Dst){
1252
1253 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1254 Pred, Dst);
1255}
1256
1257void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1258 ObjCMessageExpr::arg_iterator AI,
1259 ObjCMessageExpr::arg_iterator AE,
1260 NodeTy* Pred, NodeSet& Dst) {
1261 if (AI == AE) {
1262
1263 // Process the receiver.
1264
1265 if (Expr* Receiver = ME->getReceiver()) {
1266 NodeSet Tmp;
1267 Visit(Receiver, Pred, Tmp);
1268
1269 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1270 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1271
1272 return;
1273 }
1274
1275 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1276 return;
1277 }
1278
1279 NodeSet Tmp;
1280 Visit(*AI, Pred, Tmp);
1281
1282 ++AI;
1283
1284 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1285 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1286}
1287
1288void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1289 NodeTy* Pred,
1290 NodeSet& Dst) {
1291
1292 // FIXME: More logic for the processing the method call.
1293
Ted Kremenek4323a572008-07-10 22:03:41 +00001294 const ValueState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001295 bool RaisesException = false;
1296
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001297
1298 if (Expr* Receiver = ME->getReceiver()) {
1299
1300 RVal L = GetRVal(St, Receiver);
1301
1302 // Check for undefined control-flow or calls to NULL.
1303
1304 if (L.isUndef()) {
1305 NodeTy* N = Builder->generateNode(ME, St, Pred);
1306
1307 if (N) {
1308 N->markAsSink();
1309 UndefReceivers.insert(N);
1310 }
1311
1312 return;
1313 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001314
1315 // Check if the "raise" message was sent.
1316 if (ME->getSelector() == RaiseSel)
1317 RaisesException = true;
1318 }
1319 else {
1320
1321 IdentifierInfo* ClsName = ME->getClassName();
1322 Selector S = ME->getSelector();
1323
1324 // Check for special instance methods.
1325
1326 if (!NSExceptionII) {
1327 ASTContext& Ctx = getContext();
1328
1329 NSExceptionII = &Ctx.Idents.get("NSException");
1330 }
1331
1332 if (ClsName == NSExceptionII) {
1333
1334 enum { NUM_RAISE_SELECTORS = 2 };
1335
1336 // Lazily create a cache of the selectors.
1337
1338 if (!NSExceptionInstanceRaiseSelectors) {
1339
1340 ASTContext& Ctx = getContext();
1341
1342 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1343
1344 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1345 unsigned idx = 0;
1346
1347 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001348 II.push_back(&Ctx.Idents.get("raise"));
1349 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001350 NSExceptionInstanceRaiseSelectors[idx++] =
1351 Ctx.Selectors.getSelector(II.size(), &II[0]);
1352
1353 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001354 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001355 NSExceptionInstanceRaiseSelectors[idx++] =
1356 Ctx.Selectors.getSelector(II.size(), &II[0]);
1357 }
1358
1359 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1360 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1361 RaisesException = true; break;
1362 }
1363 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001364 }
1365
1366 // Check for any arguments that are uninitialized/undefined.
1367
1368 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1369 I != E; ++I) {
1370
1371 if (GetRVal(St, *I).isUndef()) {
1372
1373 // Generate an error node for passing an uninitialized/undefined value
1374 // as an argument to a message expression. This node is a sink.
1375 NodeTy* N = Builder->generateNode(ME, St, Pred);
1376
1377 if (N) {
1378 N->markAsSink();
1379 MsgExprUndefArgs[N] = *I;
1380 }
1381
1382 return;
1383 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001384 }
1385
1386 // Check if we raise an exception. For now treat these as sinks. Eventually
1387 // we will want to handle exceptions properly.
1388
1389 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1390
1391 if (RaisesException)
1392 Builder->BuildSinks = true;
1393
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001394 // Dispatch to plug-in transfer function.
1395
1396 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001397 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001398
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001399 EvalObjCMessageExpr(Dst, ME, Pred);
1400
1401 // Handle the case where no nodes where generated. Auto-generate that
1402 // contains the updated state if we aren't generating sinks.
1403
Ted Kremenekb0533962008-04-18 20:35:30 +00001404 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001405 MakeNode(Dst, ME, Pred, St);
1406}
1407
1408//===----------------------------------------------------------------------===//
1409// Transfer functions: Miscellaneous statements.
1410//===----------------------------------------------------------------------===//
1411
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001412void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001413
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001414 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001415 QualType T = CastE->getType();
1416
Ted Kremenek65cfb732008-03-04 22:16:08 +00001417 if (T->isReferenceType())
1418 VisitLVal(Ex, Pred, S1);
1419 else
1420 Visit(Ex, Pred, S1);
1421
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001422 // Check for casting to "void".
1423 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001424
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001425 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001426 Dst.Add(*I1);
1427
Ted Kremenek874d63f2008-01-24 02:02:54 +00001428 return;
1429 }
1430
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001431 // FIXME: The rest of this should probably just go into EvalCall, and
1432 // let the transfer function object be responsible for constructing
1433 // nodes.
1434
1435 QualType ExTy = Ex->getType();
1436
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001437 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001438 NodeTy* N = *I1;
Ted Kremenek4323a572008-07-10 22:03:41 +00001439 const ValueState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001440 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001441
1442 // Unknown?
1443
1444 if (V.isUnknown()) {
1445 Dst.Add(N);
1446 continue;
1447 }
1448
1449 // Undefined?
1450
1451 if (V.isUndef()) {
1452 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1453 continue;
1454 }
1455
1456 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001457 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001458 unsigned bits = getContext().getTypeSize(ExTy);
1459
1460 // FIXME: Determine if the number of bits of the target type is
1461 // equal or exceeds the number of bits to store the pointer value.
1462 // If not, flag an error.
1463
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001464 V = nonlval::LValAsInteger::Make(getBasicVals(), cast<LVal>(V), bits);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001465 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1466 continue;
1467 }
1468
1469 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001470 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001471 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1472 // Just unpackage the lval and return it.
1473 V = LV->getLVal();
1474 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1475 continue;
1476 }
1477
1478 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001479
Ted Kremenek0e561a32008-03-21 21:30:14 +00001480 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001481 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001482}
1483
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001484void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1485 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1486}
1487
1488void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1489 NodeTy* Pred, NodeSet& Dst) {
1490
1491 if (!D)
1492 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001493
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001494 if (!isa<VarDecl>(D)) {
1495 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1496 return;
1497 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001498
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001499 const VarDecl* VD = dyn_cast<VarDecl>(D);
1500
1501 // FIXME: Add support for local arrays.
1502 if (VD->getType()->isArrayType()) {
1503 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1504 return;
1505 }
1506
1507 Expr* Ex = const_cast<Expr*>(VD->getInit());
1508
1509 // FIXME: static variables may have an initializer, but the second
1510 // time a function is called those values may not be current.
1511 NodeSet Tmp;
1512
1513 if (Ex) Visit(Ex, Pred, Tmp);
1514 if (Tmp.empty()) Tmp.Add(Pred);
1515
1516 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001517
Ted Kremenek4323a572008-07-10 22:03:41 +00001518 const ValueState* St = GetState(*I);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001519
1520 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001521
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001522 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001523
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001524 // FIXME: static variables may have an initializer, but the second
1525 // time a function is called those values may not be current.
1526
1527
1528 // In this context, Static => Local variable.
1529
1530 assert (!VD->getStorageClass() == VarDecl::Static ||
1531 !VD->isFileVarDecl());
1532
1533 // If there is no initializer, set the value of the
1534 // variable to "Undefined".
1535
1536 if (VD->getStorageClass() == VarDecl::Static) {
1537
1538 // C99: 6.7.8 Initialization
1539 // If an object that has static storage duration is not initialized
1540 // explicitly, then:
1541 // —if it has pointer type, it is initialized to a null pointer;
1542 // —if it has arithmetic type, it is initialized to (positive or
1543 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001544
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001545 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001546
1547 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001548
Ted Kremenek0e470a52008-05-09 23:45:33 +00001549 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001550 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001551 lval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001552 else if (T->isIntegerType())
1553 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001554 nonlval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001555
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001556 // FIXME: Handle structs. Now we treat them as unknown. What
1557 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001558 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001559 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001560 else {
1561
1562 // FIXME: Handle structs. Now we treat them as unknown. What
1563 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001564
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001565 QualType T = VD->getType();
1566
Ted Kremenek0e470a52008-05-09 23:45:33 +00001567 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001568
1569 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1570
1571 if (Ex && V.isUnknown()) {
1572
1573 // EXPERIMENTAL: "Conjured" symbols.
1574
1575 unsigned Count = Builder->getCurrentBlockCount();
1576 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1577
Ted Kremenek0e470a52008-05-09 23:45:33 +00001578 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001579 ? cast<RVal>(lval::SymbolVal(Sym))
1580 : cast<RVal>(nonlval::SymbolVal(Sym));
1581 }
1582
1583 St = SetRVal(St, lval::DeclVal(VD), V);
1584 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001585 }
1586
1587 // Create a new node. We don't really need to create a new NodeSet
1588 // here, but it simplifies things and doesn't cost much.
1589 NodeSet Tmp2;
1590 MakeNode(Tmp2, DS, *I, St);
1591 if (Tmp2.empty()) Tmp2.Add(*I);
1592
1593 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1594 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1595 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001596}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001597
Ted Kremenekf233d482008-02-05 00:26:40 +00001598
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001599/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001600void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1601 NodeTy* Pred,
1602 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001603 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001604 uint64_t amt;
1605
1606 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001607
Ted Kremenek87e80342008-03-15 03:13:20 +00001608 // FIXME: Add support for VLAs.
1609 if (!T.getTypePtr()->isConstantSizeType())
1610 return;
1611
Ted Kremenekf342d182008-04-30 21:31:12 +00001612 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1613 // the compiler has laid out its representation. Just report Unknown
1614 // for these.
1615 if (T->isObjCInterfaceType())
1616 return;
1617
Ted Kremenek87e80342008-03-15 03:13:20 +00001618 amt = 1; // Handle sizeof(void)
1619
1620 if (T != getContext().VoidTy)
1621 amt = getContext().getTypeSize(T) / 8;
1622
1623 }
1624 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001625 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001626
Ted Kremenek0e561a32008-03-21 21:30:14 +00001627 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001628 SetRVal(GetState(Pred), Ex,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001629 NonLVal::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001630}
1631
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001632
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001633void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001634 NodeSet& Dst, bool asLVal) {
1635
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001636 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001637
1638 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001639 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001640
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001641 case UnaryOperator::Deref: {
1642
1643 Expr* Ex = U->getSubExpr()->IgnoreParens();
1644 NodeSet Tmp;
1645 Visit(Ex, Pred, Tmp);
1646
1647 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001648
Ted Kremenek4323a572008-07-10 22:03:41 +00001649 const ValueState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001650 RVal location = GetRVal(St, Ex);
1651
1652 if (asLVal)
1653 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1654 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001655 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001656 }
1657
1658 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001659 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001660
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001661 case UnaryOperator::Real: {
1662
1663 Expr* Ex = U->getSubExpr()->IgnoreParens();
1664 NodeSet Tmp;
1665 Visit(Ex, Pred, Tmp);
1666
1667 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1668
1669 // FIXME: We don't have complex RValues yet.
1670 if (Ex->getType()->isAnyComplexType()) {
1671 // Just report "Unknown."
1672 Dst.Add(*I);
1673 continue;
1674 }
1675
1676 // For all other types, UnaryOperator::Real is an identity operation.
1677 assert (U->getType() == Ex->getType());
Ted Kremenek4323a572008-07-10 22:03:41 +00001678 const ValueState* St = GetState(*I);
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001679 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1680 }
1681
1682 return;
1683 }
1684
1685 case UnaryOperator::Imag: {
1686
1687 Expr* Ex = U->getSubExpr()->IgnoreParens();
1688 NodeSet Tmp;
1689 Visit(Ex, Pred, Tmp);
1690
1691 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1692 // FIXME: We don't have complex RValues yet.
1693 if (Ex->getType()->isAnyComplexType()) {
1694 // Just report "Unknown."
1695 Dst.Add(*I);
1696 continue;
1697 }
1698
1699 // For all other types, UnaryOperator::Float returns 0.
1700 assert (Ex->getType()->isIntegerType());
Ted Kremenek4323a572008-07-10 22:03:41 +00001701 const ValueState* St = GetState(*I);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001702 RVal X = NonLVal::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001703 MakeNode(Dst, U, *I, SetRVal(St, U, X));
1704 }
1705
1706 return;
1707 }
1708
1709 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001710 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001711 Dst.Add(Pred);
1712 return;
1713
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001714 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1715 case UnaryOperator::Extension: {
1716
1717 // Unary "+" is a no-op, similar to a parentheses. We still have places
1718 // where it may be a block-level expression, so we need to
1719 // generate an extra node that just propagates the value of the
1720 // subexpression.
1721
1722 Expr* Ex = U->getSubExpr()->IgnoreParens();
1723 NodeSet Tmp;
1724 Visit(Ex, Pred, Tmp);
1725
1726 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4323a572008-07-10 22:03:41 +00001727 const ValueState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001728 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1729 }
1730
1731 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001732 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001733
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001734 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001735
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001736 assert (!asLVal);
1737 Expr* Ex = U->getSubExpr()->IgnoreParens();
1738 NodeSet Tmp;
1739 VisitLVal(Ex, Pred, Tmp);
1740
1741 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4323a572008-07-10 22:03:41 +00001742 const ValueState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001743 RVal V = GetRVal(St, Ex);
1744 St = SetRVal(St, U, V);
1745 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001746 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001747
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001748 return;
1749 }
1750
1751 case UnaryOperator::LNot:
1752 case UnaryOperator::Minus:
1753 case UnaryOperator::Not: {
1754
1755 assert (!asLVal);
1756 Expr* Ex = U->getSubExpr()->IgnoreParens();
1757 NodeSet Tmp;
1758 Visit(Ex, Pred, Tmp);
1759
1760 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4323a572008-07-10 22:03:41 +00001761 const ValueState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001762 RVal V = GetRVal(St, Ex);
1763
1764 if (V.isUnknownOrUndef()) {
1765 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1766 continue;
1767 }
1768
1769 switch (U->getOpcode()) {
1770 default:
1771 assert(false && "Invalid Opcode.");
1772 break;
1773
1774 case UnaryOperator::Not:
1775 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1776 break;
1777
1778 case UnaryOperator::Minus:
1779 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1780 break;
1781
1782 case UnaryOperator::LNot:
1783
1784 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1785 //
1786 // Note: technically we do "E == 0", but this is the same in the
1787 // transfer functions as "0 == E".
1788
1789 if (isa<LVal>(V)) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001790 lval::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001791 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1792 St = SetRVal(St, U, Result);
1793 }
1794 else {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001795 nonlval::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001796#if 0
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001797 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1798 St = SetRVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001799#else
1800 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLVal>(V), X, *I);
1801 continue;
1802#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001803 }
1804
1805 break;
1806 }
1807
1808 MakeNode(Dst, U, *I, St);
1809 }
1810
1811 return;
1812 }
1813
1814 case UnaryOperator::SizeOf: {
1815
1816 QualType T = U->getSubExpr()->getType();
1817
1818 // FIXME: Add support for VLAs.
1819
1820 if (!T.getTypePtr()->isConstantSizeType())
1821 return;
1822
1823 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4323a572008-07-10 22:03:41 +00001824 const ValueState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001825 St = SetRVal(St, U, NonLVal::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001826
1827 MakeNode(Dst, U, Pred, St);
1828 return;
1829 }
1830 }
1831
1832 // Handle ++ and -- (both pre- and post-increment).
1833
1834 assert (U->isIncrementDecrementOp());
1835 NodeSet Tmp;
1836 Expr* Ex = U->getSubExpr()->IgnoreParens();
1837 VisitLVal(Ex, Pred, Tmp);
1838
1839 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1840
Ted Kremenek4323a572008-07-10 22:03:41 +00001841 const ValueState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001842 RVal V1 = GetRVal(St, Ex);
1843
1844 // Perform a load.
1845 NodeSet Tmp2;
1846 EvalLoad(Tmp2, Ex, *I, St, V1);
1847
1848 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1849
1850 St = GetState(*I2);
1851 RVal V2 = GetRVal(St, Ex);
1852
1853 // Propagate unknown and undefined values.
1854 if (V2.isUnknownOrUndef()) {
1855 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001856 continue;
1857 }
1858
Ted Kremenek443003b2008-02-21 19:29:23 +00001859 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001860
1861 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1862 : BinaryOperator::Sub;
1863
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001864 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1865 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001866
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001867 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001868 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001869 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001870 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001871}
1872
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001873void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1874 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1875}
1876
1877void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1878 AsmStmt::outputs_iterator I,
1879 AsmStmt::outputs_iterator E,
1880 NodeTy* Pred, NodeSet& Dst) {
1881 if (I == E) {
1882 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1883 return;
1884 }
1885
1886 NodeSet Tmp;
1887 VisitLVal(*I, Pred, Tmp);
1888
1889 ++I;
1890
1891 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1892 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1893}
1894
1895void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1896 AsmStmt::inputs_iterator I,
1897 AsmStmt::inputs_iterator E,
1898 NodeTy* Pred, NodeSet& Dst) {
1899 if (I == E) {
1900
1901 // We have processed both the inputs and the outputs. All of the outputs
1902 // should evaluate to LVals. Nuke all of their values.
1903
1904 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1905 // which interprets the inline asm and stores proper results in the
1906 // outputs.
1907
Ted Kremenek4323a572008-07-10 22:03:41 +00001908 const ValueState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001909
1910 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1911 OE = A->end_outputs(); OI != OE; ++OI) {
1912
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001913 RVal X = GetRVal(St, *OI);
1914 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001915
1916 if (isa<LVal>(X))
1917 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1918 }
1919
Ted Kremenek0e561a32008-03-21 21:30:14 +00001920 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001921 return;
1922 }
1923
1924 NodeSet Tmp;
1925 Visit(*I, Pred, Tmp);
1926
1927 ++I;
1928
1929 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1930 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1931}
1932
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001933void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1934 assert (Builder && "GRStmtNodeBuilder must be defined.");
1935
1936 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001937
Ted Kremenek186350f2008-04-23 20:12:28 +00001938 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1939 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001940
Ted Kremenek729a9a22008-07-17 23:15:45 +00001941 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001942
Ted Kremenekb0533962008-04-18 20:35:30 +00001943 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001944
Ted Kremenekb0533962008-04-18 20:35:30 +00001945 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001946 MakeNode(Dst, S, Pred, GetState(Pred));
1947}
1948
Ted Kremenek02737ed2008-03-31 15:02:58 +00001949void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1950
1951 Expr* R = S->getRetValue();
1952
1953 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001954 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001955 return;
1956 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001957
1958 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001959 QualType T = R->getType();
1960
Chris Lattner423a3c92008-04-02 17:45:06 +00001961 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001962
1963 // Check if any of the return values return the address of a stack variable.
1964
1965 NodeSet Tmp;
1966 Visit(R, Pred, Tmp);
1967
1968 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1969 RVal X = GetRVal((*I)->getState(), R);
1970
1971 if (isa<lval::DeclVal>(X)) {
1972
1973 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1974
1975 // Create a special node representing the v
1976
1977 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1978
1979 if (RetStackNode) {
1980 RetStackNode->markAsSink();
1981 RetsStackAddr.insert(RetStackNode);
1982 }
1983
1984 continue;
1985 }
1986 }
1987
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001988 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001989 }
1990 }
1991 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001992 Visit(R, Pred, DstRet);
1993
1994 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1995 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001996}
Ted Kremenek55deb972008-03-25 00:34:37 +00001997
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001998//===----------------------------------------------------------------------===//
1999// Transfer functions: Binary operators.
2000//===----------------------------------------------------------------------===//
2001
Ted Kremenek4323a572008-07-10 22:03:41 +00002002bool GRExprEngine::CheckDivideZero(Expr* Ex, const ValueState* St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002003 NodeTy* Pred, RVal Denom) {
2004
2005 // Divide by undefined? (potentially zero)
2006
2007 if (Denom.isUndef()) {
2008 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2009
2010 if (DivUndef) {
2011 DivUndef->markAsSink();
2012 ExplicitBadDivides.insert(DivUndef);
2013 }
2014
2015 return true;
2016 }
2017
2018 // Check for divide/remainder-by-zero.
2019 // First, "assume" that the denominator is 0 or undefined.
2020
2021 bool isFeasibleZero = false;
Ted Kremenek4323a572008-07-10 22:03:41 +00002022 const ValueState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002023
2024 // Second, "assume" that the denominator cannot be 0.
2025
2026 bool isFeasibleNotZero = false;
2027 St = Assume(St, Denom, true, isFeasibleNotZero);
2028
2029 // Create the node for the divide-by-zero (if it occurred).
2030
2031 if (isFeasibleZero)
2032 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2033 DivZeroNode->markAsSink();
2034
2035 if (isFeasibleNotZero)
2036 ImplicitBadDivides.insert(DivZeroNode);
2037 else
2038 ExplicitBadDivides.insert(DivZeroNode);
2039
2040 }
2041
2042 return !isFeasibleNotZero;
2043}
2044
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002045void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002046 GRExprEngine::NodeTy* Pred,
2047 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002048
2049 NodeSet Tmp1;
2050 Expr* LHS = B->getLHS()->IgnoreParens();
2051 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002052
2053 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002054 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002055 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002056 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002057
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002058 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002059
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002060 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002061
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002062 // Process the RHS.
2063
2064 NodeSet Tmp2;
2065 Visit(RHS, *I1, Tmp2);
2066
2067 // With both the LHS and RHS evaluated, process the operation itself.
2068
2069 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002070
Ted Kremenek4323a572008-07-10 22:03:41 +00002071 const ValueState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002072 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002073 BinaryOperator::Opcode Op = B->getOpcode();
2074
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002075 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002076
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002077 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002078
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002079 // EXPERIMENTAL: "Conjured" symbols.
2080
2081 if (RightV.isUnknown()) {
2082 unsigned Count = Builder->getCurrentBlockCount();
2083 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2084
Ted Kremenek0e470a52008-05-09 23:45:33 +00002085 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002086 ? cast<RVal>(lval::SymbolVal(Sym))
2087 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002088 }
2089
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002090 // Simulate the effects of a "store": bind the value of the RHS
2091 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002092
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002093 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002094 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002095 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002096
2097 case BinaryOperator::Div:
2098 case BinaryOperator::Rem:
2099
2100 // Special checking for integer denominators.
2101
2102 if (RHS->getType()->isIntegerType()
2103 && CheckDivideZero(B, St, *I2, RightV))
2104 continue;
2105
2106 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002107
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002108 default: {
2109
2110 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002111 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002112
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002113 // Process non-assignements except commas or short-circuited
2114 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002115
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002116 RVal Result = EvalBinOp(Op, LeftV, RightV);
2117
2118 if (Result.isUnknown()) {
2119 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002120 continue;
2121 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002122
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002123 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002124
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002125 // The operands were *not* undefined, but the result is undefined.
2126 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002127
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002128 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002129 UndefNode->markAsSink();
2130 UndefResults.insert(UndefNode);
2131 }
2132
2133 continue;
2134 }
2135
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002136 // Otherwise, create a new node.
2137
2138 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002139 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002140 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002141 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002142
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002143 assert (B->isCompoundAssignmentOp());
2144
2145 if (Op >= BinaryOperator::AndAssign)
2146 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2147 else
2148 ((int&) Op) -= BinaryOperator::MulAssign;
2149
2150 // Perform a load (the LHS). This performs the checks for
2151 // null dereferences, and so on.
2152 NodeSet Tmp3;
2153 RVal location = GetRVal(St, LHS);
2154 EvalLoad(Tmp3, LHS, *I2, St, location);
2155
2156 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2157
2158 St = GetState(*I3);
2159 RVal V = GetRVal(St, LHS);
2160
2161 // Propagate undefined values (left-side).
2162 if (V.isUndef()) {
2163 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2164 continue;
2165 }
2166
2167 // Propagate unknown values (left and right-side).
2168 if (RightV.isUnknown() || V.isUnknown()) {
2169 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2170 UnknownVal());
2171 continue;
2172 }
2173
2174 // At this point:
2175 //
2176 // The LHS is not Undef/Unknown.
2177 // The RHS is not Unknown.
2178
2179 // Get the computation type.
2180 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2181
2182 // Perform promotions.
2183 V = EvalCast(V, CTy);
2184 RightV = EvalCast(RightV, CTy);
2185
2186 // Evaluate operands and promote to result type.
2187
2188 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2189 && RHS->getType()->isIntegerType()) {
2190
2191 if (CheckDivideZero(B, St, *I3, RightV))
2192 continue;
2193 }
2194 else if (RightV.isUndef()) {
2195
2196 // Propagate undefined values (right-side).
2197
2198 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2199 continue;
2200 }
2201
2202 // Compute the result of the operation.
2203
2204 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2205
2206 if (Result.isUndef()) {
2207
2208 // The operands were not undefined, but the result is undefined.
2209
2210 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2211 UndefNode->markAsSink();
2212 UndefResults.insert(UndefNode);
2213 }
2214
2215 continue;
2216 }
2217
2218 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2219 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002220 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002221 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002222}
Ted Kremenekee985462008-01-16 18:18:48 +00002223
2224//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002225// Transfer-function Helpers.
2226//===----------------------------------------------------------------------===//
2227
2228void GRExprEngine::EvalBinOp(ExplodedNodeSet<ValueState>& Dst, Expr* Ex,
2229 BinaryOperator::Opcode Op,
2230 NonLVal L, NonLVal R,
2231 ExplodedNode<ValueState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002232
2233 ValueStateSet OStates;
2234 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2235
2236 for (ValueStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
2237 MakeNode(Dst, Ex, Pred, *I);
2238}
2239
2240void GRExprEngine::EvalBinOp(ValueStateSet& OStates, const ValueState* St,
2241 Expr* Ex, BinaryOperator::Opcode Op,
2242 NonLVal L, NonLVal R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002243
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002244 ValueStateSet::AutoPopulate AP(OStates, St);
2245 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002246}
2247
2248//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002249// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002250//===----------------------------------------------------------------------===//
2251
Ted Kremenekaa66a322008-01-16 21:46:15 +00002252#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002253static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002254static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002255static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002256
Ted Kremenekaa66a322008-01-16 21:46:15 +00002257namespace llvm {
2258template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002259struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002260 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002261
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002262 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002263
2264 Out << "Variables:\\l";
2265
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002266 bool isFirst = true;
2267
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002268 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002269
2270 if (isFirst)
2271 isFirst = false;
2272 else
2273 Out << "\\l";
2274
2275 Out << ' ' << I.getKey()->getName() << " : ";
2276 I.getData().print(Out);
2277 }
2278
2279 }
2280
Ted Kremeneke7d22112008-02-11 19:21:59 +00002281
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002282 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002283
2284 bool isFirst = true;
2285
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002286 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002287
2288 if (isFirst) {
2289 Out << "\\l\\lSub-Expressions:\\l";
2290 isFirst = false;
2291 }
2292 else
2293 Out << "\\l";
2294
2295 Out << " (" << (void*) I.getKey() << ") ";
2296 I.getKey()->printPretty(Out);
2297 Out << " : ";
2298 I.getData().print(Out);
2299 }
2300 }
2301
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002302 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002303
Ted Kremenek016f52f2008-02-08 21:10:02 +00002304 bool isFirst = true;
2305
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002306 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002307 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002308 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002309 isFirst = false;
2310 }
2311 else
2312 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002313
Ted Kremeneke7d22112008-02-11 19:21:59 +00002314 Out << " (" << (void*) I.getKey() << ") ";
2315 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002316 Out << " : ";
2317 I.getData().print(Out);
2318 }
2319 }
2320
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002321 static void PrintEQ(std::ostream& Out, ValueState* St) {
2322 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002323
2324 if (CE.isEmpty())
2325 return;
2326
2327 Out << "\\l\\|'==' constraints:";
2328
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002329 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002330 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2331 }
2332
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002333 static void PrintNE(std::ostream& Out, ValueState* St) {
2334 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002335
2336 if (NE.isEmpty())
2337 return;
2338
2339 Out << "\\l\\|'!=' constraints:";
2340
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002341 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002342 I != EI; ++I){
2343
2344 Out << "\\l $" << I.getKey() << " : ";
2345 bool isFirst = true;
2346
2347 ValueState::IntSetTy::iterator J=I.getData().begin(),
2348 EJ=I.getData().end();
2349 for ( ; J != EJ; ++J) {
2350 if (isFirst) isFirst = false;
2351 else Out << ", ";
2352
2353 Out << (*J)->toString();
2354 }
2355 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002356 }
2357
2358 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2359
2360 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002361 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002362 GraphPrintCheckerState->isUndefDeref(N) ||
2363 GraphPrintCheckerState->isUndefStore(N) ||
2364 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002365 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2366 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002367 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002368 GraphPrintCheckerState->isBadCall(N) ||
2369 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002370 return "color=\"red\",style=\"filled\"";
2371
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002372 if (GraphPrintCheckerState->isNoReturnCall(N))
2373 return "color=\"blue\",style=\"filled\"";
2374
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002375 return "";
2376 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002377
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002378 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002379 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002380
2381 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002382 ProgramPoint Loc = N->getLocation();
2383
2384 switch (Loc.getKind()) {
2385 case ProgramPoint::BlockEntranceKind:
2386 Out << "Block Entrance: B"
2387 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2388 break;
2389
2390 case ProgramPoint::BlockExitKind:
2391 assert (false);
2392 break;
2393
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002394 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002395 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002396 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002397 const PostStmt& L = cast<PostStmt>(Loc);
2398 Stmt* S = L.getStmt();
2399 SourceLocation SLoc = S->getLocStart();
2400
2401 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2402 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002403
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002404 if (SLoc.isFileID()) {
2405 Out << "\\lline="
2406 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2407 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2408 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002409
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002410 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002411 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002412 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002413 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002414 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002415 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002416 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002417 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002418 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2419 Out << "\\|Explicit divide-by zero or undefined value.";
2420 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2421 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002422 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002423 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002424 else if (GraphPrintCheckerState->isNoReturnCall(N))
2425 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002426 else if (GraphPrintCheckerState->isBadCall(N))
2427 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002428 else if (GraphPrintCheckerState->isUndefArg(N))
2429 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002430
Ted Kremenekaa66a322008-01-16 21:46:15 +00002431 break;
2432 }
2433
2434 default: {
2435 const BlockEdge& E = cast<BlockEdge>(Loc);
2436 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2437 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002438
2439 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002440
2441 SourceLocation SLoc = T->getLocStart();
2442
Ted Kremenekb38911f2008-01-30 23:03:39 +00002443 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002444
Ted Kremenekb38911f2008-01-30 23:03:39 +00002445 E.getSrc()->printTerminator(Out);
2446
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002447 if (SLoc.isFileID()) {
2448 Out << "\\lline="
2449 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2450 << GraphPrintSourceManager->getColumnNumber(SLoc);
2451 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002452
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002453 if (isa<SwitchStmt>(T)) {
2454 Stmt* Label = E.getDst()->getLabel();
2455
2456 if (Label) {
2457 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2458 Out << "\\lcase ";
2459 C->getLHS()->printPretty(Out);
2460
2461 if (Stmt* RHS = C->getRHS()) {
2462 Out << " .. ";
2463 RHS->printPretty(Out);
2464 }
2465
2466 Out << ":";
2467 }
2468 else {
2469 assert (isa<DefaultStmt>(Label));
2470 Out << "\\ldefault:";
2471 }
2472 }
2473 else
2474 Out << "\\l(implicit) default:";
2475 }
2476 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002477 // FIXME
2478 }
2479 else {
2480 Out << "\\lCondition: ";
2481 if (*E.getSrc()->succ_begin() == E.getDst())
2482 Out << "true";
2483 else
2484 Out << "false";
2485 }
2486
2487 Out << "\\l";
2488 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002489
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002490 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2491 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002492 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002493 }
2494 }
2495
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002496 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002497
Ted Kremenek75da3e82008-03-11 19:02:40 +00002498 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002499
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002500 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002501 return Out.str();
2502 }
2503};
2504} // end llvm namespace
2505#endif
2506
Ted Kremenekffe0f432008-03-07 22:58:01 +00002507#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002508
2509template <typename ITERATOR>
2510GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2511
2512template <>
2513GRExprEngine::NodeTy*
2514GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2515 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2516 return I->first;
2517}
2518
Ted Kremenekffe0f432008-03-07 22:58:01 +00002519template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002520static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002521 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002522
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002523 llvm::SmallPtrSet<void*,10> CachedSources;
2524
2525 for ( ; I != E; ++I ) {
2526 GRExprEngine::NodeTy* N = GetGraphNode(I);
2527 void* p = N->getLocation().getRawData();
2528
2529 if (CachedSources.count(p))
2530 continue;
2531
2532 CachedSources.insert(p);
2533
2534 Sources.push_back(N);
2535 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002536}
2537#endif
2538
2539void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002540#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002541 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002542 std::vector<NodeTy*> Src;
2543
2544 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002545 AddSources(Src, null_derefs_begin(), null_derefs_end());
2546 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2547 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2548 AddSources(Src, undef_results_begin(), undef_results_end());
2549 AddSources(Src, bad_calls_begin(), bad_calls_end());
2550 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002551 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002552
Ted Kremenekcb612922008-04-18 19:23:43 +00002553 // The new way.
2554 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2555 (*I)->GetErrorNodes(Src);
2556
2557
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002558 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002559 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002560 else {
2561 GraphPrintCheckerState = this;
2562 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek729a9a22008-07-17 23:15:45 +00002563 GraphCheckerStatePrinter = getTF().getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002564
Ted Kremenekffe0f432008-03-07 22:58:01 +00002565 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002566
2567 GraphPrintCheckerState = NULL;
2568 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002569 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002570 }
2571#endif
2572}
2573
2574void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2575#ifndef NDEBUG
2576 GraphPrintCheckerState = this;
2577 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek729a9a22008-07-17 23:15:45 +00002578 GraphCheckerStatePrinter = getTF().getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002579
Ted Kremenek493d7a22008-03-11 18:25:33 +00002580 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2581
2582 if (!TrimmedG)
2583 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2584 else {
2585 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2586 delete TrimmedG;
2587 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002588
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002589 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002590 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002591 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002592#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002593}