blob: 7433509c85e80f067c814cd3c60a7c28e56b7f6f [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
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
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 Kremenekcaa37242008-08-19 16:51:45 +0000124 G.getAllocator(), G.getCFG(), L),
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;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000180 tf->RegisterChecks(*this);
181 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000182}
183
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000184void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
185 if (!BatchAuditor)
186 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
187
188 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000189}
190
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000191const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000192 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000193}
194
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000195//===----------------------------------------------------------------------===//
196// Top-level transfer function logic (Dispatcher).
197//===----------------------------------------------------------------------===//
198
199void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
200
201 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000202 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000203
204 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000205 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000206 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207
208 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000209 if (BatchAuditor)
210 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000211
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000212 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000213 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
214 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000215
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000216 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000218
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000219 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000220 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 else {
222 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000223 SaveOr OldHasGen(Builder->HasGeneratedNode);
224
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000225 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
226 Builder->PurgingDeadSymbols = true;
227
Ted Kremenek729a9a22008-07-17 23:15:45 +0000228 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000229 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000230
231 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
232 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000233 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000234
235 bool HasAutoGenerated = false;
236
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000237 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000238
239 NodeSet Dst;
240
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000241 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
243
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000244 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000245 Visit(S, *I, Dst);
246
247 // Do we need to auto-generate a node? We only need to do this to generate
248 // a node with a "cleaned" state; GRCoreEngine will actually handle
249 // auto-transitions for other cases.
250 if (Dst.size() == 1 && *Dst.begin() == EntryNode
251 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
252 HasAutoGenerated = true;
253 builder.generateNode(S, GetState(EntryNode), *I);
254 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000255 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000256
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000259 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000260
261 // FIXME: Consolidate.
262 StateMgr.CurrentStmt = 0;
263 CurrentStmt = 0;
264
Ted Kremenek846d4e92008-04-24 23:35:58 +0000265 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000266}
267
268void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
269
270 // FIXME: add metadata to the CFG so that we can disable
271 // this check when we KNOW that there is no block-level subexpression.
272 // The motivation is that this check requires a hashtable lookup.
273
274 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
275 Dst.Add(Pred);
276 return;
277 }
278
279 switch (S->getStmtClass()) {
280
281 default:
282 // Cases we intentionally have "default" handle:
283 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
284
285 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
286 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000287
288 case Stmt::ArraySubscriptExprClass:
289 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
290 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000291
292 case Stmt::AsmStmtClass:
293 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
294 break;
295
296 case Stmt::BinaryOperatorClass: {
297 BinaryOperator* B = cast<BinaryOperator>(S);
298
299 if (B->isLogicalOp()) {
300 VisitLogicalExpr(B, Pred, Dst);
301 break;
302 }
303 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000304 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000305 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
306 break;
307 }
308
309 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
310 break;
311 }
312
313 case Stmt::CallExprClass: {
314 CallExpr* C = cast<CallExpr>(S);
315 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
316 break;
317 }
318
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000319 // FIXME: ChooseExpr is really a constant. We need to fix
320 // the CFG do not model them as explicit control-flow.
321
322 case Stmt::ChooseExprClass: { // __builtin_choose_expr
323 ChooseExpr* C = cast<ChooseExpr>(S);
324 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
325 break;
326 }
327
328 case Stmt::CompoundAssignOperatorClass:
329 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
330 break;
331
332 case Stmt::ConditionalOperatorClass: { // '?' operator
333 ConditionalOperator* C = cast<ConditionalOperator>(S);
334 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
335 break;
336 }
337
338 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000339 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000340 break;
341
342 case Stmt::DeclStmtClass:
343 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
344 break;
345
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000346 case Stmt::ImplicitCastExprClass:
347 case Stmt::ExplicitCastExprClass: {
348 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000349 VisitCast(C, C->getSubExpr(), Pred, Dst);
350 break;
351 }
352
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000353 case Stmt::MemberExprClass: {
354 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
355 break;
356 }
357
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000358 case Stmt::ObjCMessageExprClass: {
359 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
360 break;
361 }
362
363 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000364 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000365 break;
366
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000367 case Stmt::ReturnStmtClass:
368 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
369 break;
370
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000371 case Stmt::SizeOfAlignOfTypeExprClass:
372 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
373 break;
374
375 case Stmt::StmtExprClass: {
376 StmtExpr* SE = cast<StmtExpr>(S);
377
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000378 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000379
380 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
381 // probably just remove these from the CFG.
382 assert (!SE->getSubStmt()->body_empty());
383
384 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
385 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
386 else
387 Dst.Add(Pred);
388
389 break;
390 }
391
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000392 case Stmt::UnaryOperatorClass:
393 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000394 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000395 }
396}
397
398void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
399
400 Ex = Ex->IgnoreParens();
401
402 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
403 Dst.Add(Pred);
404 return;
405 }
406
407 switch (Ex->getStmtClass()) {
408 default:
409 Visit(Ex, Pred, Dst);
410 return;
411
412 case Stmt::ArraySubscriptExprClass:
413 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
414 return;
415
416 case Stmt::DeclRefExprClass:
417 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
418 return;
419
420 case Stmt::UnaryOperatorClass:
421 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
422 return;
423
424 case Stmt::MemberExprClass:
425 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
426 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000427 }
428}
429
430//===----------------------------------------------------------------------===//
431// Block entrance. (Update counters).
432//===----------------------------------------------------------------------===//
433
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000434bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000435 GRBlockCounter BC) {
436
437 return BC.getNumVisited(B->getBlockID()) < 3;
438}
439
440//===----------------------------------------------------------------------===//
441// Branch processing.
442//===----------------------------------------------------------------------===//
443
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000444const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000445 Stmt* Terminator,
446 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000447
448 switch (Terminator->getStmtClass()) {
449 default:
450 return St;
451
452 case Stmt::BinaryOperatorClass: { // '&&' and '||'
453
454 BinaryOperator* B = cast<BinaryOperator>(Terminator);
455 BinaryOperator::Opcode Op = B->getOpcode();
456
457 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
458
459 // For &&, if we take the true branch, then the value of the whole
460 // expression is that of the RHS expression.
461 //
462 // For ||, if we take the false branch, then the value of the whole
463 // expression is that of the RHS expression.
464
465 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
466 (Op == BinaryOperator::LOr && !branchTaken)
467 ? B->getRHS() : B->getLHS();
468
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000469 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000470 }
471
472 case Stmt::ConditionalOperatorClass: { // ?:
473
474 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
475
476 // For ?, if branchTaken == true then the value is either the LHS or
477 // the condition itself. (GNU extension).
478
479 Expr* Ex;
480
481 if (branchTaken)
482 Ex = C->getLHS() ? C->getLHS() : C->getCond();
483 else
484 Ex = C->getRHS();
485
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000486 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000487 }
488
489 case Stmt::ChooseExprClass: { // ?:
490
491 ChooseExpr* C = cast<ChooseExpr>(Terminator);
492
493 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000494 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000495 }
496 }
497}
498
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000499void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000500 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000501
Ted Kremeneke7d22112008-02-11 19:21:59 +0000502 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000503 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000504 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000505
Ted Kremenekb2331832008-02-15 22:29:00 +0000506 // Check for NULL conditions; e.g. "for(;;)"
507 if (!Condition) {
508 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000509 return;
510 }
511
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000512 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000513
514 switch (V.getBaseKind()) {
515 default:
516 break;
517
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000518 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000519 builder.generateNode(MarkBranch(PrevState, Term, true), true);
520 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000521 return;
522
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000523 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000524 NodeTy* N = builder.generateNode(PrevState, true);
525
526 if (N) {
527 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000528 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000529 }
530
531 builder.markInfeasible(false);
532 return;
533 }
534 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000535
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000536 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000537
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000538 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000539 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000540
541 if (isFeasible)
542 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000543 else
544 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000545
546 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000547
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000548 isFeasible = false;
549 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000550
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000551 if (isFeasible)
552 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000553 else
554 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000555}
556
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000557/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000558/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000559void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000560
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000561 const GRState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000562 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000563
564 // Three possibilities:
565 //
566 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000567 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000568 // (3) We have no clue about the label. Dispatch to all targets.
569 //
570
571 typedef IndirectGotoNodeBuilder::iterator iterator;
572
573 if (isa<lval::GotoLabel>(V)) {
574 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
575
576 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000577 if (I.getLabel() == L) {
578 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000579 return;
580 }
581 }
582
583 assert (false && "No block with label.");
584 return;
585 }
586
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000587 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000588 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000589 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000590 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000591 return;
592 }
593
594 // This is really a catch-all. We don't support symbolics yet.
595
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000596 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000597
598 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000599 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000600}
Ted Kremenekf233d482008-02-05 00:26:40 +0000601
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000602
603void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
604 NodeTy* Pred, NodeSet& Dst) {
605
606 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
607
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000608 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000609 RVal X = GetBlkExprRVal(St, Ex);
610
611 assert (X.isUndef());
612
613 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
614
615 assert (SE);
616
617 X = GetBlkExprRVal(St, SE);
618
619 // Make sure that we invalidate the previous binding.
620 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
621}
622
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000623/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
624/// nodes by processing the 'effects' of a switch statement.
625void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
626
627 typedef SwitchNodeBuilder::iterator iterator;
628
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000629 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000630 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000631 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000632
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000633 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000634 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000635 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000636 return;
637 }
638
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000639 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000640
641 // While most of this can be assumed (such as the signedness), having it
642 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000643
Chris Lattner98be4942008-03-05 18:54:05 +0000644 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000645
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000646 APSInt V1(bits, false);
647 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000648 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000649
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000650 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000651
652 CaseStmt* Case = cast<CaseStmt>(I.getCase());
653
654 // Evaluate the case.
655 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
656 assert (false && "Case condition must evaluate to an integer constant.");
657 return;
658 }
659
660 // Get the RHS of the case, if it exists.
661
662 if (Expr* E = Case->getRHS()) {
663 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
664 assert (false &&
665 "Case condition (RHS) must evaluate to an integer constant.");
666 return ;
667 }
668
669 assert (V1 <= V2);
670 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000671 else
672 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000673
674 // FIXME: Eventually we should replace the logic below with a range
675 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000676 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000677
Ted Kremenek14a11402008-03-17 22:17:56 +0000678 do {
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000679 nonlval::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000680
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000681 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000682
683 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000684
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000685 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000686 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000687
688 if (isFeasible) {
689 builder.generateCaseStmtNode(I, StNew);
690
691 // If CondV evaluates to a constant, then we know that this
692 // is the *only* case that we can take, so stop evaluating the
693 // others.
694 if (isa<nonlval::ConcreteInt>(CondV))
695 return;
696 }
697
698 // Now "assume" that the case doesn't match. Add this state
699 // to the default state (if it is feasible).
700
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000701 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000702 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000703
Ted Kremenek5014ab12008-04-23 05:03:18 +0000704 if (isFeasible) {
705 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000707 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000708
Ted Kremenek14a11402008-03-17 22:17:56 +0000709 // Concretize the next value in the range.
710 if (V1 == V2)
711 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000712
Ted Kremenek14a11402008-03-17 22:17:56 +0000713 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000714 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000715
716 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000717 }
718
719 // If we reach here, than we know that the default branch is
720 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000721 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722}
723
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000724//===----------------------------------------------------------------------===//
725// Transfer functions: logical operations ('&&', '||').
726//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000728void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000729 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000730
Ted Kremenek05a23782008-02-26 19:05:15 +0000731 assert (B->getOpcode() == BinaryOperator::LAnd ||
732 B->getOpcode() == BinaryOperator::LOr);
733
734 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
735
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000736 const GRState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000737 RVal X = GetBlkExprRVal(St, B);
738
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000739 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000740
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000741 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000742
743 assert (Ex);
744
745 if (Ex == B->getRHS()) {
746
747 X = GetBlkExprRVal(St, Ex);
748
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000749 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000750
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000751 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000752 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000753 return;
754 }
755
Ted Kremenek05a23782008-02-26 19:05:15 +0000756 // We took the RHS. Because the value of the '&&' or '||' expression must
757 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
758 // or 1. Alternatively, we could take a lazy approach, and calculate this
759 // value later when necessary. We don't have the machinery in place for
760 // this right now, and since most logical expressions are used for branches,
761 // the payoff is not likely to be large. Instead, we do eager evaluation.
762
763 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000764 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000765
766 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000767 MakeNode(Dst, B, Pred,
768 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000769
770 isFeasible = false;
771 NewState = Assume(St, X, false, isFeasible);
772
773 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000774 MakeNode(Dst, B, Pred,
775 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000776 }
777 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000778 // We took the LHS expression. Depending on whether we are '&&' or
779 // '||' we know what the value of the expression is via properties of
780 // the short-circuiting.
781
782 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000783 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000784 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000785}
Ted Kremenek05a23782008-02-26 19:05:15 +0000786
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000787//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000788// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000789//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000790
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000791void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
792 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000793
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000794 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000795 RVal X = RVal::MakeVal(getBasicVals(), D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000796
797 if (asLVal)
798 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
799 else {
800 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
801 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
802 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000803}
804
Ted Kremenek540cbe22008-04-22 04:56:29 +0000805/// VisitArraySubscriptExpr - Transfer function for array accesses
806void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
807 NodeSet& Dst, bool asLVal) {
808
809 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000810 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000811
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000812 // Always visit the base as an LVal expression. This computes the
813 // abstract address of the base object.
814 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000815
Ted Kremenek0e470a52008-05-09 23:45:33 +0000816 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000817 Visit(Base, Pred, Tmp);
818 else
819 VisitLVal(Base, Pred, Tmp);
820
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000821 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000822
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000823 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000824
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000825 NodeSet Tmp2;
826 Visit(Idx, *I1, Tmp2);
827
828 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
829
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000830 const GRState* St = GetState(*I2);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000831 RVal BaseV = GetRVal(St, Base);
832 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000833
834 // If IdxV is 0, return just BaseV.
835
836 bool useBase = false;
837
838 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
839 useBase = IdxInt->getValue() == 0;
840
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000841 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(getBasicVals(), BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000842
843 if (asLVal)
844 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
845 else
846 EvalLoad(Dst, A, *I2, St, V);
847 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000848 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000849}
850
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000851/// VisitMemberExpr - Transfer function for member expressions.
852void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
853 NodeSet& Dst, bool asLVal) {
854
855 Expr* Base = M->getBase()->IgnoreParens();
856
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000857 // Always visit the base as an LVal expression. This computes the
858 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000859 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000860
Ted Kremenekee90dba2008-04-30 22:17:15 +0000861 if (asLVal) {
862
Ted Kremenek0e470a52008-05-09 23:45:33 +0000863 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000864 Visit(Base, Pred, Tmp);
865 else
866 VisitLVal(Base, Pred, Tmp);
867
868 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000869 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000870 RVal BaseV = GetRVal(St, Base);
871
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000872 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000873 M->getMemberDecl());
874
875 MakeNode(Dst, M, *I, SetRVal(St, M, V));
876 }
877
878 return;
879 }
880
881 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
882 // or not isArrow() is true).
883 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000884
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000885 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000886
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000887 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000888 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000889
Ted Kremenek0e470a52008-05-09 23:45:33 +0000890 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000891
Ted Kremenekee90dba2008-04-30 22:17:15 +0000892 assert (M->isArrow());
893
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000894 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000895 M->getMemberDecl());
896
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000897 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000898 }
899 else {
900
901 assert (!M->isArrow());
902
903 if (BaseV.isUnknownOrUndef()) {
904 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
905 continue;
906 }
907
908 // FIXME: Implement nonlval objects representing struct temporaries.
909 assert (isa<NonLVal>(BaseV));
910 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
911 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000912 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000913}
914
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000915void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000916 const GRState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000917
918 assert (Builder && "GRStmtNodeBuilder must be defined.");
919
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000920 // Evaluate the location (checks for bad dereferences).
921 St = EvalLocation(Ex, Pred, St, location);
922
923 if (!St)
924 return;
925
926 // Proceed with the store.
927
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000928 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000929
Ted Kremenek186350f2008-04-23 20:12:28 +0000930 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
931 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000932
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000933 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000934
Ted Kremenek729a9a22008-07-17 23:15:45 +0000935 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000936
937 // Handle the case where no nodes where generated. Auto-generate that
938 // contains the updated state if we aren't generating sinks.
939
Ted Kremenekb0533962008-04-18 20:35:30 +0000940 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000941 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000942 location, Val);
943}
944
945void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000946 const GRState* St, RVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000947 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000948
949 // Evaluate the location (checks for bad dereferences).
950
951 St = EvalLocation(Ex, Pred, St, location, true);
952
953 if (!St)
954 return;
955
956 // Proceed with the load.
957
958 // FIXME: Currently symbolic analysis "generates" new symbols
959 // for the contents of values. We need a better approach.
960
961 // FIXME: The "CheckOnly" option exists only because Array and Field
962 // loads aren't fully implemented. Eventually this option will go away.
963
Ted Kremenek436f2b92008-04-30 04:23:07 +0000964 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000965 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000966 else if (location.isUnknown()) {
967 // This is important. We must nuke the old binding.
968 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
969 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000970 else
971 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
972 Ex->getType())));
973}
974
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000975const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
976 const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000977 RVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978
979 // Check for loads/stores from/to undefined values.
980 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000981 ProgramPoint::Kind K =
982 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
983
984 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000985 Succ->markAsSink();
986 UndefDeref.insert(Succ);
987 }
988
989 return NULL;
990 }
991
992 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
993 if (location.isUnknown())
994 return St;
995
996 // During a load, one of two possible situations arise:
997 // (1) A crash, because the location (pointer) was NULL.
998 // (2) The location (pointer) is not NULL, and the dereference works.
999 //
1000 // We add these assumptions.
1001
1002 LVal LV = cast<LVal>(location);
1003
1004 // "Assume" that the pointer is not NULL.
1005
1006 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001007 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001008
1009 // "Assume" that the pointer is NULL.
1010
1011 bool isFeasibleNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001012 const GRState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001013
1014 if (isFeasibleNull) {
1015
1016 // We don't use "MakeNode" here because the node will be a sink
1017 // and we have no intention of processing it later.
1018
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001019 ProgramPoint::Kind K =
1020 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1021
1022 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023
1024 if (NullNode) {
1025
1026 NullNode->markAsSink();
1027
1028 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1029 else ExplicitNullDeref.insert(NullNode);
1030 }
1031 }
1032
1033 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001034}
1035
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001036//===----------------------------------------------------------------------===//
1037// Transfer function: Function calls.
1038//===----------------------------------------------------------------------===//
1039
Ted Kremenekde434242008-02-19 01:44:53 +00001040void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001041 CallExpr::arg_iterator AI,
1042 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001043 NodeSet& Dst) {
1044
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001045 // Process the arguments.
1046
1047 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001048
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001049 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001050 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001051 ++AI;
1052
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001053 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001054 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001055
1056 return;
1057 }
1058
1059 // If we reach here we have processed all of the arguments. Evaluate
1060 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001061
Ted Kremenek994a09b2008-02-25 21:16:03 +00001062 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001063 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001064
Ted Kremenek994a09b2008-02-25 21:16:03 +00001065 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001066
Ted Kremenekde434242008-02-19 01:44:53 +00001067 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001068 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1069
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001070 const GRState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001071 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001072
Ted Kremeneka1354a52008-03-03 16:47:31 +00001073 // FIXME: Add support for symbolic function calls (calls involving
1074 // function pointer values that are symbolic).
1075
1076 // Check for undefined control-flow or calls to NULL.
1077
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001078 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001079 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001080
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001081 if (N) {
1082 N->markAsSink();
1083 BadCalls.insert(N);
1084 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001085
Ted Kremenekde434242008-02-19 01:44:53 +00001086 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001087 }
1088
1089 // Check for the "noreturn" attribute.
1090
1091 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1092
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001093 if (isa<lval::FuncVal>(L)) {
1094
1095 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1096
1097 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001098 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001099 else {
1100 // HACK: Some functions are not marked noreturn, and don't return.
1101 // Here are a few hardwired ones. If this takes too long, we can
1102 // potentially cache these results.
1103 const char* s = FD->getIdentifier()->getName();
1104 unsigned n = strlen(s);
1105
1106 switch (n) {
1107 default:
1108 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001109
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001110 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001111 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1112 break;
1113
1114 case 5:
1115 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1116 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001117
1118 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001119 if (!memcmp(s, "Assert", 6)) {
1120 Builder->BuildSinks = true;
1121 break;
1122 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001123
1124 // FIXME: This is just a wrapper around throwing an exception.
1125 // Eventually inter-procedural analysis should handle this easily.
1126 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1127
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001128 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001129
1130 case 7:
1131 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1132 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001133
Ted Kremenekf47bb782008-04-30 17:54:04 +00001134 case 8:
1135 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1136 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001137
1138 case 12:
1139 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1140 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001141
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001142 case 14:
1143 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1144 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001145
1146 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001147 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1148 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001149 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001150
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001151 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001152 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001153
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001154 }
1155 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001156
1157 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001158
1159 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001160
1161 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1162
Ted Kremenek186350f2008-04-23 20:12:28 +00001163 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001164 switch (id) {
1165 case Builtin::BI__builtin_expect: {
1166 // For __builtin_expect, just return the value of the subexpression.
1167 assert (CE->arg_begin() != CE->arg_end());
1168 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001169 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001170 continue;
1171 }
1172
1173 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001174 break;
1175 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001176 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001177
Ted Kremenek186350f2008-04-23 20:12:28 +00001178 // Check any arguments passed-by-value against being undefined.
1179
1180 bool badArg = false;
1181
1182 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1183 I != E; ++I) {
1184
1185 if (GetRVal(GetState(*DI), *I).isUndef()) {
1186 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001187
Ted Kremenek186350f2008-04-23 20:12:28 +00001188 if (N) {
1189 N->markAsSink();
1190 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001191 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001192
Ted Kremenek186350f2008-04-23 20:12:28 +00001193 badArg = true;
1194 break;
1195 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001196 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001197
1198 if (badArg)
1199 continue;
1200
1201 // Dispatch to the plug-in transfer function.
1202
1203 unsigned size = Dst.size();
1204 SaveOr OldHasGen(Builder->HasGeneratedNode);
1205 EvalCall(Dst, CE, L, *DI);
1206
1207 // Handle the case where no nodes where generated. Auto-generate that
1208 // contains the updated state if we aren't generating sinks.
1209
1210 if (!Builder->BuildSinks && Dst.size() == size &&
1211 !Builder->HasGeneratedNode)
1212 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001213 }
1214}
1215
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001216//===----------------------------------------------------------------------===//
1217// Transfer function: Objective-C message expressions.
1218//===----------------------------------------------------------------------===//
1219
1220void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1221 NodeSet& Dst){
1222
1223 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1224 Pred, Dst);
1225}
1226
1227void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1228 ObjCMessageExpr::arg_iterator AI,
1229 ObjCMessageExpr::arg_iterator AE,
1230 NodeTy* Pred, NodeSet& Dst) {
1231 if (AI == AE) {
1232
1233 // Process the receiver.
1234
1235 if (Expr* Receiver = ME->getReceiver()) {
1236 NodeSet Tmp;
1237 Visit(Receiver, Pred, Tmp);
1238
1239 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1240 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1241
1242 return;
1243 }
1244
1245 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1246 return;
1247 }
1248
1249 NodeSet Tmp;
1250 Visit(*AI, Pred, Tmp);
1251
1252 ++AI;
1253
1254 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1255 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1256}
1257
1258void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1259 NodeTy* Pred,
1260 NodeSet& Dst) {
1261
1262 // FIXME: More logic for the processing the method call.
1263
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001264 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001265 bool RaisesException = false;
1266
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001267
1268 if (Expr* Receiver = ME->getReceiver()) {
1269
1270 RVal L = GetRVal(St, Receiver);
1271
1272 // Check for undefined control-flow or calls to NULL.
1273
1274 if (L.isUndef()) {
1275 NodeTy* N = Builder->generateNode(ME, St, Pred);
1276
1277 if (N) {
1278 N->markAsSink();
1279 UndefReceivers.insert(N);
1280 }
1281
1282 return;
1283 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001284
1285 // Check if the "raise" message was sent.
1286 if (ME->getSelector() == RaiseSel)
1287 RaisesException = true;
1288 }
1289 else {
1290
1291 IdentifierInfo* ClsName = ME->getClassName();
1292 Selector S = ME->getSelector();
1293
1294 // Check for special instance methods.
1295
1296 if (!NSExceptionII) {
1297 ASTContext& Ctx = getContext();
1298
1299 NSExceptionII = &Ctx.Idents.get("NSException");
1300 }
1301
1302 if (ClsName == NSExceptionII) {
1303
1304 enum { NUM_RAISE_SELECTORS = 2 };
1305
1306 // Lazily create a cache of the selectors.
1307
1308 if (!NSExceptionInstanceRaiseSelectors) {
1309
1310 ASTContext& Ctx = getContext();
1311
1312 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1313
1314 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1315 unsigned idx = 0;
1316
1317 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001318 II.push_back(&Ctx.Idents.get("raise"));
1319 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001320 NSExceptionInstanceRaiseSelectors[idx++] =
1321 Ctx.Selectors.getSelector(II.size(), &II[0]);
1322
1323 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001324 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001325 NSExceptionInstanceRaiseSelectors[idx++] =
1326 Ctx.Selectors.getSelector(II.size(), &II[0]);
1327 }
1328
1329 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1330 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1331 RaisesException = true; break;
1332 }
1333 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001334 }
1335
1336 // Check for any arguments that are uninitialized/undefined.
1337
1338 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1339 I != E; ++I) {
1340
1341 if (GetRVal(St, *I).isUndef()) {
1342
1343 // Generate an error node for passing an uninitialized/undefined value
1344 // as an argument to a message expression. This node is a sink.
1345 NodeTy* N = Builder->generateNode(ME, St, Pred);
1346
1347 if (N) {
1348 N->markAsSink();
1349 MsgExprUndefArgs[N] = *I;
1350 }
1351
1352 return;
1353 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001354 }
1355
1356 // Check if we raise an exception. For now treat these as sinks. Eventually
1357 // we will want to handle exceptions properly.
1358
1359 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1360
1361 if (RaisesException)
1362 Builder->BuildSinks = true;
1363
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001364 // Dispatch to plug-in transfer function.
1365
1366 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001367 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001368
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001369 EvalObjCMessageExpr(Dst, ME, Pred);
1370
1371 // Handle the case where no nodes where generated. Auto-generate that
1372 // contains the updated state if we aren't generating sinks.
1373
Ted Kremenekb0533962008-04-18 20:35:30 +00001374 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001375 MakeNode(Dst, ME, Pred, St);
1376}
1377
1378//===----------------------------------------------------------------------===//
1379// Transfer functions: Miscellaneous statements.
1380//===----------------------------------------------------------------------===//
1381
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001382void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001383
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001384 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001385 QualType T = CastE->getType();
1386
Ted Kremenek65cfb732008-03-04 22:16:08 +00001387 if (T->isReferenceType())
1388 VisitLVal(Ex, Pred, S1);
1389 else
1390 Visit(Ex, Pred, S1);
1391
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001392 // Check for casting to "void".
1393 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001394
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001395 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001396 Dst.Add(*I1);
1397
Ted Kremenek874d63f2008-01-24 02:02:54 +00001398 return;
1399 }
1400
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001401 // FIXME: The rest of this should probably just go into EvalCall, and
1402 // let the transfer function object be responsible for constructing
1403 // nodes.
1404
1405 QualType ExTy = Ex->getType();
1406
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001407 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001408 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001409 const GRState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001410 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001411
1412 // Unknown?
1413
1414 if (V.isUnknown()) {
1415 Dst.Add(N);
1416 continue;
1417 }
1418
1419 // Undefined?
1420
1421 if (V.isUndef()) {
1422 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1423 continue;
1424 }
1425
1426 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001427 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001428 unsigned bits = getContext().getTypeSize(ExTy);
1429
1430 // FIXME: Determine if the number of bits of the target type is
1431 // equal or exceeds the number of bits to store the pointer value.
1432 // If not, flag an error.
1433
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001434 V = nonlval::LValAsInteger::Make(getBasicVals(), cast<LVal>(V), bits);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001435 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1436 continue;
1437 }
1438
1439 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001440 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001441 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1442 // Just unpackage the lval and return it.
1443 V = LV->getLVal();
1444 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1445 continue;
1446 }
1447
1448 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001449
Ted Kremenek0e561a32008-03-21 21:30:14 +00001450 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001451 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001452}
1453
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001454void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1455 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1456}
1457
1458void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1459 NodeTy* Pred, NodeSet& Dst) {
1460
1461 if (!D)
1462 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001463
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001464 if (!isa<VarDecl>(D)) {
1465 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1466 return;
1467 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001468
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001469 const VarDecl* VD = dyn_cast<VarDecl>(D);
1470
1471 // FIXME: Add support for local arrays.
1472 if (VD->getType()->isArrayType()) {
1473 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1474 return;
1475 }
1476
1477 Expr* Ex = const_cast<Expr*>(VD->getInit());
1478
1479 // FIXME: static variables may have an initializer, but the second
1480 // time a function is called those values may not be current.
1481 NodeSet Tmp;
1482
1483 if (Ex) Visit(Ex, Pred, Tmp);
1484 if (Tmp.empty()) Tmp.Add(Pred);
1485
1486 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001487
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001488 const GRState* St = GetState(*I);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001489
1490 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001491
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001492 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001493
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001494 // FIXME: static variables may have an initializer, but the second
1495 // time a function is called those values may not be current.
1496
1497
1498 // In this context, Static => Local variable.
1499
1500 assert (!VD->getStorageClass() == VarDecl::Static ||
1501 !VD->isFileVarDecl());
1502
1503 // If there is no initializer, set the value of the
1504 // variable to "Undefined".
1505
1506 if (VD->getStorageClass() == VarDecl::Static) {
1507
1508 // C99: 6.7.8 Initialization
1509 // If an object that has static storage duration is not initialized
1510 // explicitly, then:
1511 // —if it has pointer type, it is initialized to a null pointer;
1512 // —if it has arithmetic type, it is initialized to (positive or
1513 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001514
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001515 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001516
1517 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001518
Ted Kremenek0e470a52008-05-09 23:45:33 +00001519 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001520 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001521 lval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001522 else if (T->isIntegerType())
1523 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001524 nonlval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001525
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001526 // FIXME: Handle structs. Now we treat them as unknown. What
1527 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001528 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001529 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001530 else {
1531
1532 // FIXME: Handle structs. Now we treat them as unknown. What
1533 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001534
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001535 QualType T = VD->getType();
1536
Ted Kremenek0e470a52008-05-09 23:45:33 +00001537 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001538
1539 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1540
1541 if (Ex && V.isUnknown()) {
1542
1543 // EXPERIMENTAL: "Conjured" symbols.
1544
1545 unsigned Count = Builder->getCurrentBlockCount();
1546 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1547
Ted Kremenek0e470a52008-05-09 23:45:33 +00001548 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001549 ? cast<RVal>(lval::SymbolVal(Sym))
1550 : cast<RVal>(nonlval::SymbolVal(Sym));
1551 }
1552
1553 St = SetRVal(St, lval::DeclVal(VD), V);
1554 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001555 }
1556
1557 // Create a new node. We don't really need to create a new NodeSet
1558 // here, but it simplifies things and doesn't cost much.
1559 NodeSet Tmp2;
1560 MakeNode(Tmp2, DS, *I, St);
1561 if (Tmp2.empty()) Tmp2.Add(*I);
1562
1563 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1564 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1565 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001566}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001567
Ted Kremenekf233d482008-02-05 00:26:40 +00001568
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001569/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001570void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1571 NodeTy* Pred,
1572 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001573 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001574 uint64_t amt;
1575
1576 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001577
Ted Kremenek87e80342008-03-15 03:13:20 +00001578 // FIXME: Add support for VLAs.
1579 if (!T.getTypePtr()->isConstantSizeType())
1580 return;
1581
Ted Kremenekf342d182008-04-30 21:31:12 +00001582 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1583 // the compiler has laid out its representation. Just report Unknown
1584 // for these.
1585 if (T->isObjCInterfaceType())
1586 return;
1587
Ted Kremenek87e80342008-03-15 03:13:20 +00001588 amt = 1; // Handle sizeof(void)
1589
1590 if (T != getContext().VoidTy)
1591 amt = getContext().getTypeSize(T) / 8;
1592
1593 }
1594 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001595 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001596
Ted Kremenek0e561a32008-03-21 21:30:14 +00001597 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001598 SetRVal(GetState(Pred), Ex,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001599 NonLVal::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001600}
1601
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001602
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001603void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001604 NodeSet& Dst, bool asLVal) {
1605
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001606 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001607
1608 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001609 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001610
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001611 case UnaryOperator::Deref: {
1612
1613 Expr* Ex = U->getSubExpr()->IgnoreParens();
1614 NodeSet Tmp;
1615 Visit(Ex, Pred, Tmp);
1616
1617 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001618
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001619 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001620 RVal location = GetRVal(St, Ex);
1621
1622 if (asLVal)
1623 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1624 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001625 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001626 }
1627
1628 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001629 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001630
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001631 case UnaryOperator::Real: {
1632
1633 Expr* Ex = U->getSubExpr()->IgnoreParens();
1634 NodeSet Tmp;
1635 Visit(Ex, Pred, Tmp);
1636
1637 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1638
1639 // FIXME: We don't have complex RValues yet.
1640 if (Ex->getType()->isAnyComplexType()) {
1641 // Just report "Unknown."
1642 Dst.Add(*I);
1643 continue;
1644 }
1645
1646 // For all other types, UnaryOperator::Real is an identity operation.
1647 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001648 const GRState* St = GetState(*I);
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001649 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1650 }
1651
1652 return;
1653 }
1654
1655 case UnaryOperator::Imag: {
1656
1657 Expr* Ex = U->getSubExpr()->IgnoreParens();
1658 NodeSet Tmp;
1659 Visit(Ex, Pred, Tmp);
1660
1661 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1662 // FIXME: We don't have complex RValues yet.
1663 if (Ex->getType()->isAnyComplexType()) {
1664 // Just report "Unknown."
1665 Dst.Add(*I);
1666 continue;
1667 }
1668
1669 // For all other types, UnaryOperator::Float returns 0.
1670 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001671 const GRState* St = GetState(*I);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001672 RVal X = NonLVal::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001673 MakeNode(Dst, U, *I, SetRVal(St, U, X));
1674 }
1675
1676 return;
1677 }
1678
1679 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001680 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001681 Dst.Add(Pred);
1682 return;
1683
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001684 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1685 case UnaryOperator::Extension: {
1686
1687 // Unary "+" is a no-op, similar to a parentheses. We still have places
1688 // where it may be a block-level expression, so we need to
1689 // generate an extra node that just propagates the value of the
1690 // subexpression.
1691
1692 Expr* Ex = U->getSubExpr()->IgnoreParens();
1693 NodeSet Tmp;
1694 Visit(Ex, Pred, Tmp);
1695
1696 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001697 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001698 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1699 }
1700
1701 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001702 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001703
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001704 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001705
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001706 assert (!asLVal);
1707 Expr* Ex = U->getSubExpr()->IgnoreParens();
1708 NodeSet Tmp;
1709 VisitLVal(Ex, Pred, Tmp);
1710
1711 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001712 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001713 RVal V = GetRVal(St, Ex);
1714 St = SetRVal(St, U, V);
1715 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001716 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001717
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001718 return;
1719 }
1720
1721 case UnaryOperator::LNot:
1722 case UnaryOperator::Minus:
1723 case UnaryOperator::Not: {
1724
1725 assert (!asLVal);
1726 Expr* Ex = U->getSubExpr()->IgnoreParens();
1727 NodeSet Tmp;
1728 Visit(Ex, Pred, Tmp);
1729
1730 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001731 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001732 RVal V = GetRVal(St, Ex);
1733
1734 if (V.isUnknownOrUndef()) {
1735 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1736 continue;
1737 }
1738
1739 switch (U->getOpcode()) {
1740 default:
1741 assert(false && "Invalid Opcode.");
1742 break;
1743
1744 case UnaryOperator::Not:
1745 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1746 break;
1747
1748 case UnaryOperator::Minus:
1749 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1750 break;
1751
1752 case UnaryOperator::LNot:
1753
1754 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1755 //
1756 // Note: technically we do "E == 0", but this is the same in the
1757 // transfer functions as "0 == E".
1758
1759 if (isa<LVal>(V)) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001760 lval::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001761 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1762 St = SetRVal(St, U, Result);
1763 }
1764 else {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001765 nonlval::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001766#if 0
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001767 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1768 St = SetRVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001769#else
1770 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLVal>(V), X, *I);
1771 continue;
1772#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001773 }
1774
1775 break;
1776 }
1777
1778 MakeNode(Dst, U, *I, St);
1779 }
1780
1781 return;
1782 }
1783
1784 case UnaryOperator::SizeOf: {
1785
1786 QualType T = U->getSubExpr()->getType();
1787
1788 // FIXME: Add support for VLAs.
1789
1790 if (!T.getTypePtr()->isConstantSizeType())
1791 return;
1792
1793 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001794 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001795 St = SetRVal(St, U, NonLVal::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001796
1797 MakeNode(Dst, U, Pred, St);
1798 return;
1799 }
1800 }
1801
1802 // Handle ++ and -- (both pre- and post-increment).
1803
1804 assert (U->isIncrementDecrementOp());
1805 NodeSet Tmp;
1806 Expr* Ex = U->getSubExpr()->IgnoreParens();
1807 VisitLVal(Ex, Pred, Tmp);
1808
1809 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1810
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001811 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001812 RVal V1 = GetRVal(St, Ex);
1813
1814 // Perform a load.
1815 NodeSet Tmp2;
1816 EvalLoad(Tmp2, Ex, *I, St, V1);
1817
1818 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1819
1820 St = GetState(*I2);
1821 RVal V2 = GetRVal(St, Ex);
1822
1823 // Propagate unknown and undefined values.
1824 if (V2.isUnknownOrUndef()) {
1825 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001826 continue;
1827 }
1828
Ted Kremenek443003b2008-02-21 19:29:23 +00001829 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001830
1831 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1832 : BinaryOperator::Sub;
1833
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001834 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1835 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001836
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001837 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001838 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001839 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001840 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001841}
1842
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001843void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1844 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1845}
1846
1847void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1848 AsmStmt::outputs_iterator I,
1849 AsmStmt::outputs_iterator E,
1850 NodeTy* Pred, NodeSet& Dst) {
1851 if (I == E) {
1852 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1853 return;
1854 }
1855
1856 NodeSet Tmp;
1857 VisitLVal(*I, Pred, Tmp);
1858
1859 ++I;
1860
1861 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1862 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1863}
1864
1865void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1866 AsmStmt::inputs_iterator I,
1867 AsmStmt::inputs_iterator E,
1868 NodeTy* Pred, NodeSet& Dst) {
1869 if (I == E) {
1870
1871 // We have processed both the inputs and the outputs. All of the outputs
1872 // should evaluate to LVals. Nuke all of their values.
1873
1874 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1875 // which interprets the inline asm and stores proper results in the
1876 // outputs.
1877
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001878 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001879
1880 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1881 OE = A->end_outputs(); OI != OE; ++OI) {
1882
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001883 RVal X = GetRVal(St, *OI);
1884 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001885
1886 if (isa<LVal>(X))
1887 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1888 }
1889
Ted Kremenek0e561a32008-03-21 21:30:14 +00001890 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001891 return;
1892 }
1893
1894 NodeSet Tmp;
1895 Visit(*I, Pred, Tmp);
1896
1897 ++I;
1898
1899 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1900 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1901}
1902
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001903void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1904 assert (Builder && "GRStmtNodeBuilder must be defined.");
1905
1906 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001907
Ted Kremenek186350f2008-04-23 20:12:28 +00001908 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1909 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001910
Ted Kremenek729a9a22008-07-17 23:15:45 +00001911 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001912
Ted Kremenekb0533962008-04-18 20:35:30 +00001913 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001914
Ted Kremenekb0533962008-04-18 20:35:30 +00001915 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001916 MakeNode(Dst, S, Pred, GetState(Pred));
1917}
1918
Ted Kremenek02737ed2008-03-31 15:02:58 +00001919void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1920
1921 Expr* R = S->getRetValue();
1922
1923 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001924 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001925 return;
1926 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001927
1928 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001929 QualType T = R->getType();
1930
Chris Lattner423a3c92008-04-02 17:45:06 +00001931 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001932
1933 // Check if any of the return values return the address of a stack variable.
1934
1935 NodeSet Tmp;
1936 Visit(R, Pred, Tmp);
1937
1938 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1939 RVal X = GetRVal((*I)->getState(), R);
1940
1941 if (isa<lval::DeclVal>(X)) {
1942
1943 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1944
1945 // Create a special node representing the v
1946
1947 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1948
1949 if (RetStackNode) {
1950 RetStackNode->markAsSink();
1951 RetsStackAddr.insert(RetStackNode);
1952 }
1953
1954 continue;
1955 }
1956 }
1957
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001958 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001959 }
1960 }
1961 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001962 Visit(R, Pred, DstRet);
1963
1964 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1965 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001966}
Ted Kremenek55deb972008-03-25 00:34:37 +00001967
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001968//===----------------------------------------------------------------------===//
1969// Transfer functions: Binary operators.
1970//===----------------------------------------------------------------------===//
1971
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001972bool GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001973 NodeTy* Pred, RVal Denom) {
1974
1975 // Divide by undefined? (potentially zero)
1976
1977 if (Denom.isUndef()) {
1978 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
1979
1980 if (DivUndef) {
1981 DivUndef->markAsSink();
1982 ExplicitBadDivides.insert(DivUndef);
1983 }
1984
1985 return true;
1986 }
1987
1988 // Check for divide/remainder-by-zero.
1989 // First, "assume" that the denominator is 0 or undefined.
1990
1991 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001992 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001993
1994 // Second, "assume" that the denominator cannot be 0.
1995
1996 bool isFeasibleNotZero = false;
1997 St = Assume(St, Denom, true, isFeasibleNotZero);
1998
1999 // Create the node for the divide-by-zero (if it occurred).
2000
2001 if (isFeasibleZero)
2002 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2003 DivZeroNode->markAsSink();
2004
2005 if (isFeasibleNotZero)
2006 ImplicitBadDivides.insert(DivZeroNode);
2007 else
2008 ExplicitBadDivides.insert(DivZeroNode);
2009
2010 }
2011
2012 return !isFeasibleNotZero;
2013}
2014
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002015void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002016 GRExprEngine::NodeTy* Pred,
2017 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002018
2019 NodeSet Tmp1;
2020 Expr* LHS = B->getLHS()->IgnoreParens();
2021 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002022
2023 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002024 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002025 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002026 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002027
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002028 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002029
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002030 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002031
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002032 // Process the RHS.
2033
2034 NodeSet Tmp2;
2035 Visit(RHS, *I1, Tmp2);
2036
2037 // With both the LHS and RHS evaluated, process the operation itself.
2038
2039 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002040
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002041 const GRState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002042 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002043 BinaryOperator::Opcode Op = B->getOpcode();
2044
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002045 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002046
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002047 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002048
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002049 // EXPERIMENTAL: "Conjured" symbols.
2050
2051 if (RightV.isUnknown()) {
2052 unsigned Count = Builder->getCurrentBlockCount();
2053 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2054
Ted Kremenek0e470a52008-05-09 23:45:33 +00002055 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002056 ? cast<RVal>(lval::SymbolVal(Sym))
2057 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002058 }
2059
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002060 // Simulate the effects of a "store": bind the value of the RHS
2061 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002062
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002063 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002064 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002065 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002066
2067 case BinaryOperator::Div:
2068 case BinaryOperator::Rem:
2069
2070 // Special checking for integer denominators.
2071
2072 if (RHS->getType()->isIntegerType()
2073 && CheckDivideZero(B, St, *I2, RightV))
2074 continue;
2075
2076 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002077
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002078 default: {
2079
2080 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002081 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002082
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002083 // Process non-assignements except commas or short-circuited
2084 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002085
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002086 RVal Result = EvalBinOp(Op, LeftV, RightV);
2087
2088 if (Result.isUnknown()) {
2089 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002090 continue;
2091 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002092
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002093 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002094
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002095 // The operands were *not* undefined, but the result is undefined.
2096 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002097
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002098 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002099 UndefNode->markAsSink();
2100 UndefResults.insert(UndefNode);
2101 }
2102
2103 continue;
2104 }
2105
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002106 // Otherwise, create a new node.
2107
2108 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002109 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002110 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002111 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002112
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002113 assert (B->isCompoundAssignmentOp());
2114
2115 if (Op >= BinaryOperator::AndAssign)
2116 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2117 else
2118 ((int&) Op) -= BinaryOperator::MulAssign;
2119
2120 // Perform a load (the LHS). This performs the checks for
2121 // null dereferences, and so on.
2122 NodeSet Tmp3;
2123 RVal location = GetRVal(St, LHS);
2124 EvalLoad(Tmp3, LHS, *I2, St, location);
2125
2126 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2127
2128 St = GetState(*I3);
2129 RVal V = GetRVal(St, LHS);
2130
2131 // Propagate undefined values (left-side).
2132 if (V.isUndef()) {
2133 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2134 continue;
2135 }
2136
2137 // Propagate unknown values (left and right-side).
2138 if (RightV.isUnknown() || V.isUnknown()) {
2139 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2140 UnknownVal());
2141 continue;
2142 }
2143
2144 // At this point:
2145 //
2146 // The LHS is not Undef/Unknown.
2147 // The RHS is not Unknown.
2148
2149 // Get the computation type.
2150 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2151
2152 // Perform promotions.
2153 V = EvalCast(V, CTy);
2154 RightV = EvalCast(RightV, CTy);
2155
2156 // Evaluate operands and promote to result type.
2157
2158 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2159 && RHS->getType()->isIntegerType()) {
2160
2161 if (CheckDivideZero(B, St, *I3, RightV))
2162 continue;
2163 }
2164 else if (RightV.isUndef()) {
2165
2166 // Propagate undefined values (right-side).
2167
2168 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2169 continue;
2170 }
2171
2172 // Compute the result of the operation.
2173
2174 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2175
2176 if (Result.isUndef()) {
2177
2178 // The operands were not undefined, but the result is undefined.
2179
2180 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2181 UndefNode->markAsSink();
2182 UndefResults.insert(UndefNode);
2183 }
2184
2185 continue;
2186 }
2187
2188 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2189 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002190 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002191 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002192}
Ted Kremenekee985462008-01-16 18:18:48 +00002193
2194//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002195// Transfer-function Helpers.
2196//===----------------------------------------------------------------------===//
2197
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002198void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002199 BinaryOperator::Opcode Op,
2200 NonLVal L, NonLVal R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002201 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002202
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002203 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002204 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2205
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002206 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002207 MakeNode(Dst, Ex, Pred, *I);
2208}
2209
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002210void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002211 Expr* Ex, BinaryOperator::Opcode Op,
2212 NonLVal L, NonLVal R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002213
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002214 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002215 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002216}
2217
2218//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002219// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002220//===----------------------------------------------------------------------===//
2221
Ted Kremenekaa66a322008-01-16 21:46:15 +00002222#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002223static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002224static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002225
Ted Kremenekaa66a322008-01-16 21:46:15 +00002226namespace llvm {
2227template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002228struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002229 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002230
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002231 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2232
2233 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002234 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002235 GraphPrintCheckerState->isUndefDeref(N) ||
2236 GraphPrintCheckerState->isUndefStore(N) ||
2237 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002238 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2239 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002240 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002241 GraphPrintCheckerState->isBadCall(N) ||
2242 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002243 return "color=\"red\",style=\"filled\"";
2244
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002245 if (GraphPrintCheckerState->isNoReturnCall(N))
2246 return "color=\"blue\",style=\"filled\"";
2247
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002248 return "";
2249 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002250
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002251 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002252 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002253
2254 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002255 ProgramPoint Loc = N->getLocation();
2256
2257 switch (Loc.getKind()) {
2258 case ProgramPoint::BlockEntranceKind:
2259 Out << "Block Entrance: B"
2260 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2261 break;
2262
2263 case ProgramPoint::BlockExitKind:
2264 assert (false);
2265 break;
2266
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002267 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002268 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002269 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002270 const PostStmt& L = cast<PostStmt>(Loc);
2271 Stmt* S = L.getStmt();
2272 SourceLocation SLoc = S->getLocStart();
2273
2274 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2275 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002276
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002277 if (SLoc.isFileID()) {
2278 Out << "\\lline="
2279 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2280 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2281 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002282
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002283 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002284 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002285 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002286 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002287 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002288 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002289 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002290 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002291 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2292 Out << "\\|Explicit divide-by zero or undefined value.";
2293 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2294 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002295 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002296 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002297 else if (GraphPrintCheckerState->isNoReturnCall(N))
2298 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002299 else if (GraphPrintCheckerState->isBadCall(N))
2300 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002301 else if (GraphPrintCheckerState->isUndefArg(N))
2302 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002303
Ted Kremenekaa66a322008-01-16 21:46:15 +00002304 break;
2305 }
2306
2307 default: {
2308 const BlockEdge& E = cast<BlockEdge>(Loc);
2309 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2310 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002311
2312 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002313
2314 SourceLocation SLoc = T->getLocStart();
2315
Ted Kremenekb38911f2008-01-30 23:03:39 +00002316 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002317
Ted Kremenekb38911f2008-01-30 23:03:39 +00002318 E.getSrc()->printTerminator(Out);
2319
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002320 if (SLoc.isFileID()) {
2321 Out << "\\lline="
2322 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2323 << GraphPrintSourceManager->getColumnNumber(SLoc);
2324 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002325
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002326 if (isa<SwitchStmt>(T)) {
2327 Stmt* Label = E.getDst()->getLabel();
2328
2329 if (Label) {
2330 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2331 Out << "\\lcase ";
2332 C->getLHS()->printPretty(Out);
2333
2334 if (Stmt* RHS = C->getRHS()) {
2335 Out << " .. ";
2336 RHS->printPretty(Out);
2337 }
2338
2339 Out << ":";
2340 }
2341 else {
2342 assert (isa<DefaultStmt>(Label));
2343 Out << "\\ldefault:";
2344 }
2345 }
2346 else
2347 Out << "\\l(implicit) default:";
2348 }
2349 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002350 // FIXME
2351 }
2352 else {
2353 Out << "\\lCondition: ";
2354 if (*E.getSrc()->succ_begin() == E.getDst())
2355 Out << "true";
2356 else
2357 Out << "false";
2358 }
2359
2360 Out << "\\l";
2361 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002362
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002363 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2364 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002365 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002366 }
2367 }
2368
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002369 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002370
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002371 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2372 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002373
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002374 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002375 return Out.str();
2376 }
2377};
2378} // end llvm namespace
2379#endif
2380
Ted Kremenekffe0f432008-03-07 22:58:01 +00002381#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002382
2383template <typename ITERATOR>
2384GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2385
2386template <>
2387GRExprEngine::NodeTy*
2388GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2389 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2390 return I->first;
2391}
2392
Ted Kremenekffe0f432008-03-07 22:58:01 +00002393template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002394static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002395 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002396
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002397 llvm::SmallPtrSet<void*,10> CachedSources;
2398
2399 for ( ; I != E; ++I ) {
2400 GRExprEngine::NodeTy* N = GetGraphNode(I);
2401 void* p = N->getLocation().getRawData();
2402
2403 if (CachedSources.count(p))
2404 continue;
2405
2406 CachedSources.insert(p);
2407
2408 Sources.push_back(N);
2409 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002410}
2411#endif
2412
2413void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002414#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002415 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002416 std::vector<NodeTy*> Src;
2417
2418 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002419 AddSources(Src, null_derefs_begin(), null_derefs_end());
2420 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2421 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2422 AddSources(Src, undef_results_begin(), undef_results_end());
2423 AddSources(Src, bad_calls_begin(), bad_calls_end());
2424 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002425 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002426
Ted Kremenekcb612922008-04-18 19:23:43 +00002427 // The new way.
2428 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2429 (*I)->GetErrorNodes(Src);
2430
2431
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002432 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002433 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002434 else {
2435 GraphPrintCheckerState = this;
2436 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002437
Ted Kremenekffe0f432008-03-07 22:58:01 +00002438 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002439
2440 GraphPrintCheckerState = NULL;
2441 GraphPrintSourceManager = NULL;
2442 }
2443#endif
2444}
2445
2446void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2447#ifndef NDEBUG
2448 GraphPrintCheckerState = this;
2449 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002450
Ted Kremenek493d7a22008-03-11 18:25:33 +00002451 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2452
2453 if (!TrimmedG)
2454 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2455 else {
2456 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2457 delete TrimmedG;
2458 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002459
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002460 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002461 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002462#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002463}