blob: a9303330feeeef0ee9e90460a90d53b4f77387cb [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000020#include "llvm/ADT/ImmutableList.h"
21#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000118 LiveVariables& L,
119 GRStateManager::StoreManagerCreator SMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000120 : CoreEngine(cfg, CD, Ctx, *this),
121 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000122 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000123 Builder(NULL),
Ted Kremenek95c7b002008-10-24 01:04:59 +0000124 StateMgr(G.getContext(), SMC,
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000125 CreateBasicConstraintManager, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000127 CurrentStmt(NULL),
128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000129 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000130
Ted Kremenek1a654b62008-06-20 21:45:25 +0000131GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
133 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000134
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135
136 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000137}
138
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000139//===----------------------------------------------------------------------===//
140// Utility methods.
141//===----------------------------------------------------------------------===//
142
143// SaveAndRestore - A utility class that uses RIIA to save and restore
144// the value of a variable.
145template<typename T>
146struct VISIBILITY_HIDDEN SaveAndRestore {
147 SaveAndRestore(T& x) : X(x), old_value(x) {}
148 ~SaveAndRestore() { X = old_value; }
149 T get() { return old_value; }
150
151 T& X;
152 T old_value;
153};
154
Ted Kremenek186350f2008-04-23 20:12:28 +0000155// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
156// value of a variable is saved, and during the dstor the old value is
157// or'ed with the new value.
158struct VISIBILITY_HIDDEN SaveOr {
159 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
160 ~SaveOr() { X |= old_value; }
161
162 bool& X;
163 bool old_value;
164};
165
166
Ted Kremenekc0959972008-07-02 21:24:01 +0000167void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000168 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000169 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000170 (*I)->EmitWarnings(BR);
171 }
172
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000173 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000174 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000175 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000176 }
177}
178
179void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000180 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181 tf->RegisterChecks(*this);
182 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000183}
184
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000185void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
186 if (!BatchAuditor)
187 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
188
189 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000190}
191
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000193 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194}
195
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196//===----------------------------------------------------------------------===//
197// Top-level transfer function logic (Dispatcher).
198//===----------------------------------------------------------------------===//
199
200void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
201
202 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000203 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000204
205 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000206 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000207 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000208
209 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000210 if (BatchAuditor)
211 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000212
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000213 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000214 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
215 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000216
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 else {
223 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 SaveOr OldHasGen(Builder->HasGeneratedNode);
225
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000226 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
227 Builder->PurgingDeadSymbols = true;
228
Ted Kremenek729a9a22008-07-17 23:15:45 +0000229 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000230 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000231
232 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
233 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000234 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 bool HasAutoGenerated = false;
237
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 NodeSet Dst;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Visit(S, *I, Dst);
247
248 // Do we need to auto-generate a node? We only need to do this to generate
249 // a node with a "cleaned" state; GRCoreEngine will actually handle
250 // auto-transitions for other cases.
251 if (Dst.size() == 1 && *Dst.begin() == EntryNode
252 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
253 HasAutoGenerated = true;
254 builder.generateNode(S, GetState(EntryNode), *I);
255 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000261
262 // FIXME: Consolidate.
263 StateMgr.CurrentStmt = 0;
264 CurrentStmt = 0;
265
Ted Kremenek846d4e92008-04-24 23:35:58 +0000266 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000267}
268
269void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
270
271 // FIXME: add metadata to the CFG so that we can disable
272 // this check when we KNOW that there is no block-level subexpression.
273 // The motivation is that this check requires a hashtable lookup.
274
275 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
276 Dst.Add(Pred);
277 return;
278 }
279
280 switch (S->getStmtClass()) {
281
282 default:
283 // Cases we intentionally have "default" handle:
284 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
285
286 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
287 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000288
289 case Stmt::ArraySubscriptExprClass:
290 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
291 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000292
293 case Stmt::AsmStmtClass:
294 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
295 break;
296
297 case Stmt::BinaryOperatorClass: {
298 BinaryOperator* B = cast<BinaryOperator>(S);
299
300 if (B->isLogicalOp()) {
301 VisitLogicalExpr(B, Pred, Dst);
302 break;
303 }
304 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000305 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000306 MakeNode(Dst, B, Pred, SetSVal(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000307 break;
308 }
309
310 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
311 break;
312 }
313
314 case Stmt::CallExprClass: {
315 CallExpr* C = cast<CallExpr>(S);
316 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
317 break;
318 }
319
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000320 // FIXME: ChooseExpr is really a constant. We need to fix
321 // the CFG do not model them as explicit control-flow.
322
323 case Stmt::ChooseExprClass: { // __builtin_choose_expr
324 ChooseExpr* C = cast<ChooseExpr>(S);
325 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
326 break;
327 }
328
329 case Stmt::CompoundAssignOperatorClass:
330 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
331 break;
332
333 case Stmt::ConditionalOperatorClass: { // '?' operator
334 ConditionalOperator* C = cast<ConditionalOperator>(S);
335 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
336 break;
337 }
338
339 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000340 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000341 break;
342
343 case Stmt::DeclStmtClass:
344 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
345 break;
346
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000347 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000348 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000349 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000350 VisitCast(C, C->getSubExpr(), Pred, Dst);
351 break;
352 }
353
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000354 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000355 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
356 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000357
358 case Stmt::ObjCIvarRefExprClass:
359 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
360 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000361
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000362 case Stmt::ObjCMessageExprClass: {
363 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
364 break;
365 }
366
367 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000368 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000369 break;
370
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000371 case Stmt::ReturnStmtClass:
372 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
373 break;
374
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000375 case Stmt::SizeOfAlignOfTypeExprClass:
376 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
377 break;
378
379 case Stmt::StmtExprClass: {
380 StmtExpr* SE = cast<StmtExpr>(S);
381
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000382 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000383
384 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
385 // probably just remove these from the CFG.
386 assert (!SE->getSubStmt()->body_empty());
387
388 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000389 MakeNode(Dst, SE, Pred, SetSVal(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000390 else
391 Dst.Add(Pred);
392
393 break;
394 }
395
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000396 case Stmt::UnaryOperatorClass:
397 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000398 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000399 }
400}
401
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000402void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000403
404 Ex = Ex->IgnoreParens();
405
406 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
407 Dst.Add(Pred);
408 return;
409 }
410
411 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000412
413 case Stmt::ArraySubscriptExprClass:
414 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
415 return;
416
417 case Stmt::DeclRefExprClass:
418 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
419 return;
420
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000421 case Stmt::ObjCIvarRefExprClass:
422 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
423 return;
424
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000425 case Stmt::UnaryOperatorClass:
426 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
427 return;
428
429 case Stmt::MemberExprClass:
430 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
431 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000432
Ted Kremenek4f090272008-10-27 21:54:31 +0000433 case Stmt::CompoundLiteralExprClass:
434 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst);
435 return;
436
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000437 case Stmt::ObjCPropertyRefExprClass:
438 // FIXME: Property assignments are lvalues, but not really "locations".
439 // e.g.: self.x = something;
440 // Here the "self.x" really can translate to a method call (setter) when
441 // the assignment is made. Moreover, the entire assignment expression
442 // evaluate to whatever "something" is, not calling the "getter" for
443 // the property (which would make sense since it can have side effects).
444 // We'll probably treat this as a location, but not one that we can
445 // take the address of. Perhaps we need a new SVal class for cases
446 // like thsis?
447 // Note that we have a similar problem for bitfields, since they don't
448 // have "locations" in the sense that we can take their address.
449 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000450 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000451
452 case Stmt::StringLiteralClass: {
453 const GRState* St = GetState(Pred);
454 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
455 MakeNode(Dst, Ex, Pred, SetSVal(St, Ex, V));
456 return;
457 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000458
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000459 default:
460 // Arbitrary subexpressions can return aggregate temporaries that
461 // can be used in a lvalue context. We need to enhance our support
462 // of such temporaries in both the environment and the store, so right
463 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000464 assert ((Ex->getType()->isAggregateType() ||
465 Ex->getType()->isUnionType()) &&
466 "Other kinds of expressions with non-aggregate/union types do"
467 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000468
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000469 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000470 }
471}
472
473//===----------------------------------------------------------------------===//
474// Block entrance. (Update counters).
475//===----------------------------------------------------------------------===//
476
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000477bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000478 GRBlockCounter BC) {
479
480 return BC.getNumVisited(B->getBlockID()) < 3;
481}
482
483//===----------------------------------------------------------------------===//
484// Branch processing.
485//===----------------------------------------------------------------------===//
486
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000487const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000488 Stmt* Terminator,
489 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000490
491 switch (Terminator->getStmtClass()) {
492 default:
493 return St;
494
495 case Stmt::BinaryOperatorClass: { // '&&' and '||'
496
497 BinaryOperator* B = cast<BinaryOperator>(Terminator);
498 BinaryOperator::Opcode Op = B->getOpcode();
499
500 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
501
502 // For &&, if we take the true branch, then the value of the whole
503 // expression is that of the RHS expression.
504 //
505 // For ||, if we take the false branch, then the value of the whole
506 // expression is that of the RHS expression.
507
508 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
509 (Op == BinaryOperator::LOr && !branchTaken)
510 ? B->getRHS() : B->getLHS();
511
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000512 return SetBlkExprSVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000513 }
514
515 case Stmt::ConditionalOperatorClass: { // ?:
516
517 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
518
519 // For ?, if branchTaken == true then the value is either the LHS or
520 // the condition itself. (GNU extension).
521
522 Expr* Ex;
523
524 if (branchTaken)
525 Ex = C->getLHS() ? C->getLHS() : C->getCond();
526 else
527 Ex = C->getRHS();
528
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000529 return SetBlkExprSVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000530 }
531
532 case Stmt::ChooseExprClass: { // ?:
533
534 ChooseExpr* C = cast<ChooseExpr>(Terminator);
535
536 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000537 return SetBlkExprSVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000538 }
539 }
540}
541
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000542void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000543 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000544
Ted Kremeneke7d22112008-02-11 19:21:59 +0000545 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000546 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000547 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000548
Ted Kremenekb2331832008-02-15 22:29:00 +0000549 // Check for NULL conditions; e.g. "for(;;)"
550 if (!Condition) {
551 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000552 return;
553 }
554
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000555 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000556
557 switch (V.getBaseKind()) {
558 default:
559 break;
560
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000561 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000562 builder.generateNode(MarkBranch(PrevState, Term, true), true);
563 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000564 return;
565
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000566 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000567 NodeTy* N = builder.generateNode(PrevState, true);
568
569 if (N) {
570 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000571 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000572 }
573
574 builder.markInfeasible(false);
575 return;
576 }
577 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000578
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000579 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000580
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000581 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000582 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000583
584 if (isFeasible)
585 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000586 else
587 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000588
589 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000590
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000591 isFeasible = false;
592 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000593
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000594 if (isFeasible)
595 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000596 else
597 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000598}
599
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000600/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000601/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000602void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000603
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000604 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000605 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000606
607 // Three possibilities:
608 //
609 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000610 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000611 // (3) We have no clue about the label. Dispatch to all targets.
612 //
613
614 typedef IndirectGotoNodeBuilder::iterator iterator;
615
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000616 if (isa<loc::GotoLabel>(V)) {
617 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000618
619 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000620 if (I.getLabel() == L) {
621 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000622 return;
623 }
624 }
625
626 assert (false && "No block with label.");
627 return;
628 }
629
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000630 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000631 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000632 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000633 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000634 return;
635 }
636
637 // This is really a catch-all. We don't support symbolics yet.
638
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000639 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000640
641 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000642 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000643}
Ted Kremenekf233d482008-02-05 00:26:40 +0000644
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000645
646void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
647 NodeTy* Pred, NodeSet& Dst) {
648
649 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
650
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000651 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000652 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000653
654 assert (X.isUndef());
655
656 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
657
658 assert (SE);
659
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000660 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000661
662 // Make sure that we invalidate the previous binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000663 MakeNode(Dst, Ex, Pred, StateMgr.SetSVal(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000664}
665
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000666/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
667/// nodes by processing the 'effects' of a switch statement.
668void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
669
670 typedef SwitchNodeBuilder::iterator iterator;
671
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000672 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000673 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000674 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000675
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000676 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000677 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000678 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679 return;
680 }
681
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000682 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000683
684 // While most of this can be assumed (such as the signedness), having it
685 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000686
Chris Lattner98be4942008-03-05 18:54:05 +0000687 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000688
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000689 APSInt V1(bits, false);
690 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000691 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000692
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000693 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000694
695 CaseStmt* Case = cast<CaseStmt>(I.getCase());
696
697 // Evaluate the case.
698 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
699 assert (false && "Case condition must evaluate to an integer constant.");
700 return;
701 }
702
703 // Get the RHS of the case, if it exists.
704
705 if (Expr* E = Case->getRHS()) {
706 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
707 assert (false &&
708 "Case condition (RHS) must evaluate to an integer constant.");
709 return ;
710 }
711
712 assert (V1 <= V2);
713 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000714 else
715 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000716
717 // FIXME: Eventually we should replace the logic below with a range
718 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000719 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000720
Ted Kremenek14a11402008-03-17 22:17:56 +0000721 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000722 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000723
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000724 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000725
726 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000728 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000729 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000730
731 if (isFeasible) {
732 builder.generateCaseStmtNode(I, StNew);
733
734 // If CondV evaluates to a constant, then we know that this
735 // is the *only* case that we can take, so stop evaluating the
736 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000737 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738 return;
739 }
740
741 // Now "assume" that the case doesn't match. Add this state
742 // to the default state (if it is feasible).
743
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000744 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000745 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000746
Ted Kremenek5014ab12008-04-23 05:03:18 +0000747 if (isFeasible) {
748 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000749 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000750 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751
Ted Kremenek14a11402008-03-17 22:17:56 +0000752 // Concretize the next value in the range.
753 if (V1 == V2)
754 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000755
Ted Kremenek14a11402008-03-17 22:17:56 +0000756 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000757 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000758
759 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000760 }
761
762 // If we reach here, than we know that the default branch is
763 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000764 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000765}
766
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000767//===----------------------------------------------------------------------===//
768// Transfer functions: logical operations ('&&', '||').
769//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000770
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000771void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000772 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000773
Ted Kremenek05a23782008-02-26 19:05:15 +0000774 assert (B->getOpcode() == BinaryOperator::LAnd ||
775 B->getOpcode() == BinaryOperator::LOr);
776
777 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
778
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000779 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000780 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000781
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000782 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000783
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000784 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000785
786 assert (Ex);
787
788 if (Ex == B->getRHS()) {
789
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000790 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000791
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000792 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000793
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000794 if (X.isUndef()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000795 MakeNode(Dst, B, Pred, SetBlkExprSVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000796 return;
797 }
798
Ted Kremenek05a23782008-02-26 19:05:15 +0000799 // We took the RHS. Because the value of the '&&' or '||' expression must
800 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
801 // or 1. Alternatively, we could take a lazy approach, and calculate this
802 // value later when necessary. We don't have the machinery in place for
803 // this right now, and since most logical expressions are used for branches,
804 // the payoff is not likely to be large. Instead, we do eager evaluation.
805
806 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000807 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000808
809 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000810 MakeNode(Dst, B, Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000811 SetBlkExprSVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000812
813 isFeasible = false;
814 NewState = Assume(St, X, false, isFeasible);
815
816 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000817 MakeNode(Dst, B, Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000818 SetBlkExprSVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000819 }
820 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000821 // We took the LHS expression. Depending on whether we are '&&' or
822 // '||' we know what the value of the expression is via properties of
823 // the short-circuiting.
824
825 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000826 MakeNode(Dst, B, Pred, SetBlkExprSVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000827 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000828}
Ted Kremenek05a23782008-02-26 19:05:15 +0000829
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000830//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000831// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000832//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000833
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000834void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
835 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000836
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000837 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000838
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000839 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000840
841 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
842
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000843 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000844
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000845 if (asLValue)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000846 MakeNode(Dst, Ex, Pred, SetSVal(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000847 else
848 EvalLoad(Dst, Ex, Pred, St, V);
849 return;
850
851 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
852 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
853
854 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000855 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
856 MakeNode(Dst, Ex, Pred, SetSVal(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000857 return;
858
859 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000860 // We return the loc::FuncVal for an FunctionDecl in both rvalue
Ted Kremenekcd162cc2008-10-17 00:55:33 +0000861 // and lvalue contexts.
862 // FIXME: Does this need to be revised? We were getting cases in
863 // real code that did this.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000864 SVal V = loc::FuncVal(FD);
865 MakeNode(Dst, Ex, Pred, SetSVal(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000866 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000867 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000868
869 assert (false &&
870 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000871}
872
Ted Kremenek540cbe22008-04-22 04:56:29 +0000873/// VisitArraySubscriptExpr - Transfer function for array accesses
874void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000875 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000876
877 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000878 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000879 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000880 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000881
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000882 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000883 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000884 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000885
886 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000887 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000888 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000889
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000890 if (asLValue)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000891 MakeNode(Dst, A, *I2, SetSVal(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000892 else
893 EvalLoad(Dst, A, *I2, St, V);
894 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000895 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000896}
897
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000898/// VisitMemberExpr - Transfer function for member expressions.
899void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000900 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000901
902 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000903 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000904
905 if (M->isArrow())
906 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
907 else
908 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
909
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000910 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000911 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000912 // FIXME: Should we insert some assumption logic in here to determine
913 // if "Base" is a valid piece of memory? Before we put this assumption
914 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000915 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000916
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000917 if (asLValue)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000918 MakeNode(Dst, M, *I, SetSVal(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000919 else
920 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000921 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000922}
923
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000924void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000925 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000926
927 assert (Builder && "GRStmtNodeBuilder must be defined.");
928
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000929 // Evaluate the location (checks for bad dereferences).
930 St = EvalLocation(Ex, Pred, St, location);
931
932 if (!St)
933 return;
934
935 // Proceed with the store.
936
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000937 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000938
Ted Kremenek186350f2008-04-23 20:12:28 +0000939 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000940 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000941 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000942
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000943 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000944 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000945
Ted Kremenek729a9a22008-07-17 23:15:45 +0000946 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000947
948 // Handle the case where no nodes where generated. Auto-generate that
949 // contains the updated state if we aren't generating sinks.
950
Ted Kremenekb0533962008-04-18 20:35:30 +0000951 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000952 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953 location, Val);
954}
955
956void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000957 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000958 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000959
960 // Evaluate the location (checks for bad dereferences).
961
962 St = EvalLocation(Ex, Pred, St, location, true);
963
964 if (!St)
965 return;
966
967 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000968 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969
970 // FIXME: Currently symbolic analysis "generates" new symbols
971 // for the contents of values. We need a better approach.
972
973 // FIXME: The "CheckOnly" option exists only because Array and Field
974 // loads aren't fully implemented. Eventually this option will go away.
Ted Kremenek982e6742008-08-28 18:43:46 +0000975
Ted Kremenek436f2b92008-04-30 04:23:07 +0000976 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000977 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000978 else if (location.isUnknown()) {
979 // This is important. We must nuke the old binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000980 MakeNode(Dst, Ex, Pred, SetSVal(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000981 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000982 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000983 MakeNode(Dst, Ex, Pred, SetSVal(St, Ex, GetSVal(St, cast<Loc>(location),
Ted Kremenek982e6742008-08-28 18:43:46 +0000984 Ex->getType())), K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000985}
986
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000987void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000988 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000989
990 NodeSet TmpDst;
991 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
992
993 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
994 MakeNode(Dst, Ex, *I, (*I)->getState());
995}
996
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000997const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
998 const GRState* St,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000999 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001000
1001 // Check for loads/stores from/to undefined values.
1002 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001003 ProgramPoint::Kind K =
1004 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1005
1006 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001007 Succ->markAsSink();
1008 UndefDeref.insert(Succ);
1009 }
1010
1011 return NULL;
1012 }
1013
1014 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1015 if (location.isUnknown())
1016 return St;
1017
1018 // During a load, one of two possible situations arise:
1019 // (1) A crash, because the location (pointer) was NULL.
1020 // (2) The location (pointer) is not NULL, and the dereference works.
1021 //
1022 // We add these assumptions.
1023
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001024 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001025
1026 // "Assume" that the pointer is not NULL.
1027
1028 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001029 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001030
1031 // "Assume" that the pointer is NULL.
1032
1033 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001034 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1035 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001036
1037 if (isFeasibleNull) {
1038
Ted Kremenek7360fda2008-09-18 23:09:54 +00001039 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001040 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001041 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1042
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001043 // We don't use "MakeNode" here because the node will be a sink
1044 // and we have no intention of processing it later.
1045
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001046 ProgramPoint::Kind K =
1047 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1048
1049 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001050
1051 if (NullNode) {
1052
1053 NullNode->markAsSink();
1054
1055 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1056 else ExplicitNullDeref.insert(NullNode);
1057 }
1058 }
1059
1060 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001061}
1062
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001063//===----------------------------------------------------------------------===//
1064// Transfer function: Function calls.
1065//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001066void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001067 CallExpr::arg_iterator AI,
1068 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001069 NodeSet& Dst)
1070{
1071 // Determine the type of function we're calling (if available).
1072 const FunctionTypeProto *Proto = NULL;
1073 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1074 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1075 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1076
1077 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1078}
1079
1080void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1081 CallExpr::arg_iterator AI,
1082 CallExpr::arg_iterator AE,
1083 NodeSet& Dst, const FunctionTypeProto *Proto,
1084 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001085
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001086 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001087 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001088 // If the call argument is being bound to a reference parameter,
1089 // visit it as an lvalue, not an rvalue.
1090 bool VisitAsLvalue = false;
1091 if (Proto && ParamIdx < Proto->getNumArgs())
1092 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1093
1094 NodeSet DstTmp;
1095 if (VisitAsLvalue)
1096 VisitLValue(*AI, Pred, DstTmp);
1097 else
1098 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001099 ++AI;
1100
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001101 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001102 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001103
1104 return;
1105 }
1106
1107 // If we reach here we have processed all of the arguments. Evaluate
1108 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001109
Ted Kremenek994a09b2008-02-25 21:16:03 +00001110 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001111 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001112
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001113 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001114
Ted Kremenekde434242008-02-19 01:44:53 +00001115 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001116 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1117
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001118 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001119 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001120
Ted Kremeneka1354a52008-03-03 16:47:31 +00001121 // FIXME: Add support for symbolic function calls (calls involving
1122 // function pointer values that are symbolic).
1123
1124 // Check for undefined control-flow or calls to NULL.
1125
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001126 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001127 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001128
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001129 if (N) {
1130 N->markAsSink();
1131 BadCalls.insert(N);
1132 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001133
Ted Kremenekde434242008-02-19 01:44:53 +00001134 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001135 }
1136
1137 // Check for the "noreturn" attribute.
1138
1139 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1140
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001141 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001142
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001143 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001144
1145 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001146 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001147 else {
1148 // HACK: Some functions are not marked noreturn, and don't return.
1149 // Here are a few hardwired ones. If this takes too long, we can
1150 // potentially cache these results.
1151 const char* s = FD->getIdentifier()->getName();
1152 unsigned n = strlen(s);
1153
1154 switch (n) {
1155 default:
1156 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001157
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001158 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001159 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1160 break;
1161
1162 case 5:
1163 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001164 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001165 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001166 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001167 // FIXME: use Assume to inspect the possible symbolic value of
1168 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001169 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001170 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001171 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001172 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001173 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001174 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001175
1176 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001177 if (!memcmp(s, "Assert", 6)) {
1178 Builder->BuildSinks = true;
1179 break;
1180 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001181
1182 // FIXME: This is just a wrapper around throwing an exception.
1183 // Eventually inter-procedural analysis should handle this easily.
1184 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1185
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001186 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001187
1188 case 7:
1189 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1190 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001191
Ted Kremenekf47bb782008-04-30 17:54:04 +00001192 case 8:
1193 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1194 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001195
1196 case 12:
1197 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1198 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001199
Ted Kremenekf9683082008-09-19 02:30:47 +00001200 case 13:
1201 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1202 break;
1203
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001204 case 14:
1205 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1206 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001207
1208 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001209 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1210 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001211 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001212
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001213 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001214 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001215
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001216 }
1217 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001218
1219 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001220
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001221 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001222
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001223 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001224
Ted Kremenek186350f2008-04-23 20:12:28 +00001225 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001226 switch (id) {
1227 case Builtin::BI__builtin_expect: {
1228 // For __builtin_expect, just return the value of the subexpression.
1229 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001230 SVal X = GetSVal(St, *(CE->arg_begin()));
1231 MakeNode(Dst, CE, *DI, SetSVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001232 continue;
1233 }
1234
1235 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001236 break;
1237 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001238 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001239
Ted Kremenek186350f2008-04-23 20:12:28 +00001240 // Check any arguments passed-by-value against being undefined.
1241
1242 bool badArg = false;
1243
1244 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1245 I != E; ++I) {
1246
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001247 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001248 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001249
Ted Kremenek186350f2008-04-23 20:12:28 +00001250 if (N) {
1251 N->markAsSink();
1252 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001253 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001254
Ted Kremenek186350f2008-04-23 20:12:28 +00001255 badArg = true;
1256 break;
1257 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001258 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001259
1260 if (badArg)
1261 continue;
1262
1263 // Dispatch to the plug-in transfer function.
1264
1265 unsigned size = Dst.size();
1266 SaveOr OldHasGen(Builder->HasGeneratedNode);
1267 EvalCall(Dst, CE, L, *DI);
1268
1269 // Handle the case where no nodes where generated. Auto-generate that
1270 // contains the updated state if we aren't generating sinks.
1271
1272 if (!Builder->BuildSinks && Dst.size() == size &&
1273 !Builder->HasGeneratedNode)
1274 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001275 }
1276}
1277
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001278//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001279// Transfer function: Objective-C ivar references.
1280//===----------------------------------------------------------------------===//
1281
1282void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1283 NodeTy* Pred, NodeSet& Dst,
1284 bool asLValue) {
1285
1286 Expr* Base = cast<Expr>(Ex->getBase());
1287 NodeSet Tmp;
1288 Visit(Base, Pred, Tmp);
1289
1290 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1291 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001292 SVal BaseVal = GetSVal(St, Base);
1293 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001294
1295 if (asLValue)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001296 MakeNode(Dst, Ex, *I, SetSVal(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001297 else
1298 EvalLoad(Dst, Ex, *I, St, location);
1299 }
1300}
1301
1302//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001303// Transfer function: Objective-C message expressions.
1304//===----------------------------------------------------------------------===//
1305
1306void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1307 NodeSet& Dst){
1308
1309 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1310 Pred, Dst);
1311}
1312
1313void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1314 ObjCMessageExpr::arg_iterator AI,
1315 ObjCMessageExpr::arg_iterator AE,
1316 NodeTy* Pred, NodeSet& Dst) {
1317 if (AI == AE) {
1318
1319 // Process the receiver.
1320
1321 if (Expr* Receiver = ME->getReceiver()) {
1322 NodeSet Tmp;
1323 Visit(Receiver, Pred, Tmp);
1324
1325 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1326 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1327
1328 return;
1329 }
1330
1331 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1332 return;
1333 }
1334
1335 NodeSet Tmp;
1336 Visit(*AI, Pred, Tmp);
1337
1338 ++AI;
1339
1340 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1341 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1342}
1343
1344void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1345 NodeTy* Pred,
1346 NodeSet& Dst) {
1347
1348 // FIXME: More logic for the processing the method call.
1349
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001350 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001351 bool RaisesException = false;
1352
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001353
1354 if (Expr* Receiver = ME->getReceiver()) {
1355
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001356 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001357
1358 // Check for undefined control-flow or calls to NULL.
1359
1360 if (L.isUndef()) {
1361 NodeTy* N = Builder->generateNode(ME, St, Pred);
1362
1363 if (N) {
1364 N->markAsSink();
1365 UndefReceivers.insert(N);
1366 }
1367
1368 return;
1369 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001370
1371 // Check if the "raise" message was sent.
1372 if (ME->getSelector() == RaiseSel)
1373 RaisesException = true;
1374 }
1375 else {
1376
1377 IdentifierInfo* ClsName = ME->getClassName();
1378 Selector S = ME->getSelector();
1379
1380 // Check for special instance methods.
1381
1382 if (!NSExceptionII) {
1383 ASTContext& Ctx = getContext();
1384
1385 NSExceptionII = &Ctx.Idents.get("NSException");
1386 }
1387
1388 if (ClsName == NSExceptionII) {
1389
1390 enum { NUM_RAISE_SELECTORS = 2 };
1391
1392 // Lazily create a cache of the selectors.
1393
1394 if (!NSExceptionInstanceRaiseSelectors) {
1395
1396 ASTContext& Ctx = getContext();
1397
1398 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1399
1400 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1401 unsigned idx = 0;
1402
1403 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001404 II.push_back(&Ctx.Idents.get("raise"));
1405 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001406 NSExceptionInstanceRaiseSelectors[idx++] =
1407 Ctx.Selectors.getSelector(II.size(), &II[0]);
1408
1409 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001410 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001411 NSExceptionInstanceRaiseSelectors[idx++] =
1412 Ctx.Selectors.getSelector(II.size(), &II[0]);
1413 }
1414
1415 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1416 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1417 RaisesException = true; break;
1418 }
1419 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001420 }
1421
1422 // Check for any arguments that are uninitialized/undefined.
1423
1424 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1425 I != E; ++I) {
1426
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001427 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001428
1429 // Generate an error node for passing an uninitialized/undefined value
1430 // as an argument to a message expression. This node is a sink.
1431 NodeTy* N = Builder->generateNode(ME, St, Pred);
1432
1433 if (N) {
1434 N->markAsSink();
1435 MsgExprUndefArgs[N] = *I;
1436 }
1437
1438 return;
1439 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001440 }
1441
1442 // Check if we raise an exception. For now treat these as sinks. Eventually
1443 // we will want to handle exceptions properly.
1444
1445 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1446
1447 if (RaisesException)
1448 Builder->BuildSinks = true;
1449
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001450 // Dispatch to plug-in transfer function.
1451
1452 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001453 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001454
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001455 EvalObjCMessageExpr(Dst, ME, Pred);
1456
1457 // Handle the case where no nodes where generated. Auto-generate that
1458 // contains the updated state if we aren't generating sinks.
1459
Ted Kremenekb0533962008-04-18 20:35:30 +00001460 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001461 MakeNode(Dst, ME, Pred, St);
1462}
1463
1464//===----------------------------------------------------------------------===//
1465// Transfer functions: Miscellaneous statements.
1466//===----------------------------------------------------------------------===//
1467
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001468void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001469 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001470 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001471 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001472
Douglas Gregor49badde2008-10-27 19:41:14 +00001473 if (const ExplicitCastExpr *ExCast = dyn_cast_or_null<ExplicitCastExpr>(CastE))
1474 T = ExCast->getTypeAsWritten();
1475
Zhongxing Xued340f72008-10-22 08:02:16 +00001476 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001477 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001478 else
1479 Visit(Ex, Pred, S1);
1480
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001481 // Check for casting to "void".
1482 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001483
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001484 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001485 Dst.Add(*I1);
1486
Ted Kremenek874d63f2008-01-24 02:02:54 +00001487 return;
1488 }
1489
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001490 // FIXME: The rest of this should probably just go into EvalCall, and
1491 // let the transfer function object be responsible for constructing
1492 // nodes.
1493
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001494 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001495 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001496 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001497 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001498
1499 // Unknown?
1500
1501 if (V.isUnknown()) {
1502 Dst.Add(N);
1503 continue;
1504 }
1505
1506 // Undefined?
1507
1508 if (V.isUndef()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001509 MakeNode(Dst, CastE, N, SetSVal(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001510 continue;
1511 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001512
1513 // For const casts, just propagate the value.
1514 ASTContext& C = getContext();
1515
1516 if (C.getCanonicalType(T).getUnqualifiedType() ==
1517 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001518 MakeNode(Dst, CastE, N, SetSVal(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001519 continue;
1520 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001521
1522 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001523 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001524 unsigned bits = getContext().getTypeSize(ExTy);
1525
1526 // FIXME: Determine if the number of bits of the target type is
1527 // equal or exceeds the number of bits to store the pointer value.
1528 // If not, flag an error.
1529
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001530 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1531 MakeNode(Dst, CastE, N, SetSVal(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001532 continue;
1533 }
1534
1535 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001536 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1537 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001538 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001539 V = LV->getLoc();
1540 MakeNode(Dst, CastE, N, SetSVal(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001541 continue;
1542 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001543
1544 // StoreManager casts array to different values.
1545 if (ExTy->isArrayType()) {
Ted Kremenek0b50f5b2008-10-24 21:10:49 +00001546 assert(T->isPointerType() || T->isReferenceType());
Zhongxing Xue564b522008-10-23 04:19:25 +00001547
Zhongxing Xue1911af2008-10-23 03:10:39 +00001548 V = StateMgr.ArrayToPointer(V);
1549 MakeNode(Dst, CastE, N, SetSVal(St, CastE, V));
1550 continue;
1551 }
1552
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001553 // All other cases.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001554 MakeNode(Dst, CastE, N, SetSVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001555 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001556}
1557
Ted Kremenek4f090272008-10-27 21:54:31 +00001558void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
1559 NodeTy* Pred, NodeSet& Dst) {
1560
1561 // FIXME: Can getInitializer() be NULL?
1562 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1563 NodeSet Tmp;
1564 Visit(ILE, Pred, Tmp);
1565
1566 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
1567 // Retrieve the initializer values from the environment and store them
1568 // into a vector that will then be handed off to the Store.
1569 const GRState* St = GetState(*I);
1570 llvm::SmallVector<SVal, 10> IVals;
1571 IVals.reserve(ILE->getNumInits());
1572
1573 for (Stmt::child_iterator J=ILE->child_begin(), EJ=ILE->child_end();
1574 J!=EJ; ++J)
1575 IVals.push_back(GetSVal(St, cast<Expr>(*J)));
1576
1577 const CompoundLiteralRegion* R =
1578 StateMgr.getRegionManager().getCompoundLiteralRegion(CL);
1579
1580 assert (!IVals.empty() && "Initializer cannot be empty.");
1581
1582 St = StateMgr.BindCompoundLiteral(St, R, &IVals[0], &IVals[0]+IVals.size());
1583 MakeNode(Dst, CL, *I, SetSVal(St, CL, loc::MemRegionVal(R)));
1584 }
1585}
1586
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001587void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001588
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001589 // The CFG has one DeclStmt per Decl.
1590 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001591
1592 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001593 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001594
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001595 const VarDecl* VD = dyn_cast<VarDecl>(D);
1596
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001597 Expr* Ex = const_cast<Expr*>(VD->getInit());
1598
1599 // FIXME: static variables may have an initializer, but the second
1600 // time a function is called those values may not be current.
1601 NodeSet Tmp;
1602
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001603 if (Ex)
1604 Visit(Ex, Pred, Tmp);
1605
1606 if (Tmp.empty())
1607 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001608
1609 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001610 const GRState* St = GetState(*I);
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +00001611 St = StateMgr.BindDecl(St, VD, Ex, Builder->getCurrentBlockCount());
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001612 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001613 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001614}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001615
Ted Kremenekf233d482008-02-05 00:26:40 +00001616
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001617/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001618void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1619 NodeTy* Pred,
1620 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001621 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001622 uint64_t amt;
1623
1624 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001625
Ted Kremenek87e80342008-03-15 03:13:20 +00001626 // FIXME: Add support for VLAs.
1627 if (!T.getTypePtr()->isConstantSizeType())
1628 return;
1629
Ted Kremenekf342d182008-04-30 21:31:12 +00001630 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1631 // the compiler has laid out its representation. Just report Unknown
1632 // for these.
1633 if (T->isObjCInterfaceType())
1634 return;
1635
Ted Kremenek87e80342008-03-15 03:13:20 +00001636 amt = 1; // Handle sizeof(void)
1637
1638 if (T != getContext().VoidTy)
1639 amt = getContext().getTypeSize(T) / 8;
1640
1641 }
1642 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001643 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001644
Ted Kremenek0e561a32008-03-21 21:30:14 +00001645 MakeNode(Dst, Ex, Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001646 SetSVal(GetState(Pred), Ex,
1647 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001648}
1649
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001650
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001651void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001652 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001653
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001654 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001655
1656 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001657 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001658
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001659 case UnaryOperator::Deref: {
1660
1661 Expr* Ex = U->getSubExpr()->IgnoreParens();
1662 NodeSet Tmp;
1663 Visit(Ex, Pred, Tmp);
1664
1665 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001666
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001667 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001668 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001669
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001670 if (asLValue)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001671 MakeNode(Dst, U, *I, SetSVal(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001672 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001673 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001674 }
1675
1676 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001677 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001678
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001679 case UnaryOperator::Real: {
1680
1681 Expr* Ex = U->getSubExpr()->IgnoreParens();
1682 NodeSet Tmp;
1683 Visit(Ex, Pred, Tmp);
1684
1685 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1686
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001687 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001688 if (Ex->getType()->isAnyComplexType()) {
1689 // Just report "Unknown."
1690 Dst.Add(*I);
1691 continue;
1692 }
1693
1694 // For all other types, UnaryOperator::Real is an identity operation.
1695 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001696 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001697 MakeNode(Dst, U, *I, SetSVal(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001698 }
1699
1700 return;
1701 }
1702
1703 case UnaryOperator::Imag: {
1704
1705 Expr* Ex = U->getSubExpr()->IgnoreParens();
1706 NodeSet Tmp;
1707 Visit(Ex, Pred, Tmp);
1708
1709 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001710 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001711 if (Ex->getType()->isAnyComplexType()) {
1712 // Just report "Unknown."
1713 Dst.Add(*I);
1714 continue;
1715 }
1716
1717 // For all other types, UnaryOperator::Float returns 0.
1718 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001719 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001720 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
1721 MakeNode(Dst, U, *I, SetSVal(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001722 }
1723
1724 return;
1725 }
1726
1727 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001728 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001729 Dst.Add(Pred);
1730 return;
1731
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001732 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001733 case UnaryOperator::Extension: {
1734
1735 // Unary "+" is a no-op, similar to a parentheses. We still have places
1736 // where it may be a block-level expression, so we need to
1737 // generate an extra node that just propagates the value of the
1738 // subexpression.
1739
1740 Expr* Ex = U->getSubExpr()->IgnoreParens();
1741 NodeSet Tmp;
1742 Visit(Ex, Pred, Tmp);
1743
1744 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001745 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001746 MakeNode(Dst, U, *I, SetSVal(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001747 }
1748
1749 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001750 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001751
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001752 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001753
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001754 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001755 Expr* Ex = U->getSubExpr()->IgnoreParens();
1756 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001757 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001758
1759 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001760 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001761 SVal V = GetSVal(St, Ex);
1762 St = SetSVal(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001763 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001764 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001765
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001766 return;
1767 }
1768
1769 case UnaryOperator::LNot:
1770 case UnaryOperator::Minus:
1771 case UnaryOperator::Not: {
1772
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001773 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001774 Expr* Ex = U->getSubExpr()->IgnoreParens();
1775 NodeSet Tmp;
1776 Visit(Ex, Pred, Tmp);
1777
1778 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001779 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00001780
1781 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001782 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00001783
1784 // Perform promotions.
Ted Kremenek5a236cb2008-09-30 05:35:42 +00001785 // FIXME: This is the right thing to do, but it currently breaks
1786 // a bunch of tests.
1787 // V = EvalCast(V, U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001788
1789 if (V.isUnknownOrUndef()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001790 MakeNode(Dst, U, *I, SetSVal(St, U, V));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001791 continue;
1792 }
1793
1794 switch (U->getOpcode()) {
1795 default:
1796 assert(false && "Invalid Opcode.");
1797 break;
1798
1799 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00001800 // FIXME: Do we need to handle promotions?
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001801 St = SetSVal(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001802 break;
1803
1804 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00001805 // FIXME: Do we need to handle promotions?
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001806 St = SetSVal(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001807 break;
1808
1809 case UnaryOperator::LNot:
1810
1811 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1812 //
1813 // Note: technically we do "E == 0", but this is the same in the
1814 // transfer functions as "0 == E".
1815
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001816 if (isa<Loc>(V)) {
1817 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
1818 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
1819 St = SetSVal(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001820 }
1821 else {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001822 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001823#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001824 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
1825 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001826#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001827 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001828 continue;
1829#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001830 }
1831
1832 break;
1833 }
1834
1835 MakeNode(Dst, U, *I, St);
1836 }
1837
1838 return;
1839 }
Ted Kremenek6dfe2f52008-10-18 22:20:20 +00001840
1841 case UnaryOperator::AlignOf: {
1842
1843 QualType T = U->getSubExpr()->getType();
1844
1845 // FIXME: Add support for VLAs.
1846
1847 if (!T.getTypePtr()->isConstantSizeType())
1848 return;
1849
1850 uint64_t size = getContext().getTypeAlign(T) / 8;
1851 const GRState* St = GetState(Pred);
1852 St = SetSVal(St, U, NonLoc::MakeVal(getBasicVals(), size, U->getType()));
1853
1854 MakeNode(Dst, U, Pred, St);
1855 return;
1856 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001857
1858 case UnaryOperator::SizeOf: {
1859
1860 QualType T = U->getSubExpr()->getType();
1861
1862 // FIXME: Add support for VLAs.
1863
1864 if (!T.getTypePtr()->isConstantSizeType())
1865 return;
1866
1867 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001868 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001869 St = SetSVal(St, U, NonLoc::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001870
1871 MakeNode(Dst, U, Pred, St);
1872 return;
1873 }
1874 }
1875
1876 // Handle ++ and -- (both pre- and post-increment).
1877
1878 assert (U->isIncrementDecrementOp());
1879 NodeSet Tmp;
1880 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001881 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001882
1883 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1884
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001885 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001886 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001887
1888 // Perform a load.
1889 NodeSet Tmp2;
1890 EvalLoad(Tmp2, Ex, *I, St, V1);
1891
1892 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1893
1894 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001895 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001896
1897 // Propagate unknown and undefined values.
1898 if (V2.isUnknownOrUndef()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001899 MakeNode(Dst, U, *I2, SetSVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001900 continue;
1901 }
1902
Ted Kremenek443003b2008-02-21 19:29:23 +00001903 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001904
1905 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1906 : BinaryOperator::Sub;
1907
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001908 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1909 St = SetSVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001910
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001911 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001912 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001913 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001914 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001915}
1916
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001917void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1918 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1919}
1920
1921void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1922 AsmStmt::outputs_iterator I,
1923 AsmStmt::outputs_iterator E,
1924 NodeTy* Pred, NodeSet& Dst) {
1925 if (I == E) {
1926 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1927 return;
1928 }
1929
1930 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001931 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001932
1933 ++I;
1934
1935 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1936 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1937}
1938
1939void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1940 AsmStmt::inputs_iterator I,
1941 AsmStmt::inputs_iterator E,
1942 NodeTy* Pred, NodeSet& Dst) {
1943 if (I == E) {
1944
1945 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001946 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001947
1948 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1949 // which interprets the inline asm and stores proper results in the
1950 // outputs.
1951
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001952 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001953
1954 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1955 OE = A->end_outputs(); OI != OE; ++OI) {
1956
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001957 SVal X = GetSVal(St, *OI);
1958 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001959
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001960 if (isa<Loc>(X))
1961 St = SetSVal(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001962 }
1963
Ted Kremenek0e561a32008-03-21 21:30:14 +00001964 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001965 return;
1966 }
1967
1968 NodeSet Tmp;
1969 Visit(*I, Pred, Tmp);
1970
1971 ++I;
1972
1973 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1974 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1975}
1976
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001977void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1978 assert (Builder && "GRStmtNodeBuilder must be defined.");
1979
1980 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001981
Ted Kremenek186350f2008-04-23 20:12:28 +00001982 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1983 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001984
Ted Kremenek729a9a22008-07-17 23:15:45 +00001985 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001986
Ted Kremenekb0533962008-04-18 20:35:30 +00001987 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001988
Ted Kremenekb0533962008-04-18 20:35:30 +00001989 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001990 MakeNode(Dst, S, Pred, GetState(Pred));
1991}
1992
Ted Kremenek02737ed2008-03-31 15:02:58 +00001993void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1994
1995 Expr* R = S->getRetValue();
1996
1997 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001998 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001999 return;
2000 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002001
2002 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00002003 QualType T = R->getType();
2004
Chris Lattner423a3c92008-04-02 17:45:06 +00002005 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002006
2007 // Check if any of the return values return the address of a stack variable.
2008
2009 NodeSet Tmp;
2010 Visit(R, Pred, Tmp);
2011
2012 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002013 SVal X = GetSVal((*I)->getState(), R);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002014
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002015 if (isa<loc::MemRegionVal>(X)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002016
Ted Kremenek9e240492008-10-04 05:50:14 +00002017 // Determine if the value is on the stack.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002018 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00002019
2020 if (R && getStateManager().hasStackStorage(R)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002021
2022 // Create a special node representing the v
2023
2024 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
2025
2026 if (RetStackNode) {
2027 RetStackNode->markAsSink();
2028 RetsStackAddr.insert(RetStackNode);
2029 }
2030
2031 continue;
2032 }
2033 }
2034
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002035 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002036 }
2037 }
2038 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002039 Visit(R, Pred, DstRet);
2040
2041 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2042 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002043}
Ted Kremenek55deb972008-03-25 00:34:37 +00002044
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002045//===----------------------------------------------------------------------===//
2046// Transfer functions: Binary operators.
2047//===----------------------------------------------------------------------===//
2048
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002049const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2050 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002051
2052 // Divide by undefined? (potentially zero)
2053
2054 if (Denom.isUndef()) {
2055 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2056
2057 if (DivUndef) {
2058 DivUndef->markAsSink();
2059 ExplicitBadDivides.insert(DivUndef);
2060 }
2061
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002062 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002063 }
2064
2065 // Check for divide/remainder-by-zero.
2066 // First, "assume" that the denominator is 0 or undefined.
2067
2068 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002069 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002070
2071 // Second, "assume" that the denominator cannot be 0.
2072
2073 bool isFeasibleNotZero = false;
2074 St = Assume(St, Denom, true, isFeasibleNotZero);
2075
2076 // Create the node for the divide-by-zero (if it occurred).
2077
2078 if (isFeasibleZero)
2079 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2080 DivZeroNode->markAsSink();
2081
2082 if (isFeasibleNotZero)
2083 ImplicitBadDivides.insert(DivZeroNode);
2084 else
2085 ExplicitBadDivides.insert(DivZeroNode);
2086
2087 }
2088
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002089 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002090}
2091
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002092void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002093 GRExprEngine::NodeTy* Pred,
2094 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002095
2096 NodeSet Tmp1;
2097 Expr* LHS = B->getLHS()->IgnoreParens();
2098 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002099
2100 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002101 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002102 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002103 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002104
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002105 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002106
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002107 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002108
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002109 // Process the RHS.
2110
2111 NodeSet Tmp2;
2112 Visit(RHS, *I1, Tmp2);
2113
2114 // With both the LHS and RHS evaluated, process the operation itself.
2115
2116 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002117
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002118 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002119 const GRState* OldSt = St;
2120
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002121 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002122 BinaryOperator::Opcode Op = B->getOpcode();
2123
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002124 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002125
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002126 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002127
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002128 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002129 // FIXME: Handle structs.
2130 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002131
Ted Kremenekfd301942008-10-17 22:23:12 +00002132 if (RightV.isUnknown() && (T->isIntegerType() || Loc::IsLocType(T))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002133 unsigned Count = Builder->getCurrentBlockCount();
2134 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2135
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002136 RightV = Loc::IsLocType(B->getRHS()->getType())
2137 ? cast<SVal>(loc::SymbolVal(Sym))
2138 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002139 }
2140
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002141 // Simulate the effects of a "store": bind the value of the RHS
2142 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002143
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002144 EvalStore(Dst, B, LHS, *I2, SetSVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002145 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002146 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002147
2148 case BinaryOperator::Div:
2149 case BinaryOperator::Rem:
2150
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002151 // Special checking for integer denominators.
2152 if (RHS->getType()->isIntegerType()) {
2153 St = CheckDivideZero(B, St, *I2, RightV);
2154 if (!St) continue;
2155 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002156
2157 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002158
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002159 default: {
2160
2161 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002162 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002163
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002164 // Process non-assignements except commas or short-circuited
2165 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002166
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002167 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002168
2169 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002170 if (OldSt != St) {
2171 // Generate a new node if we have already created a new state.
2172 MakeNode(Dst, B, *I2, St);
2173 }
2174 else
2175 Dst.Add(*I2);
2176
Ted Kremenek89063af2008-02-21 19:15:37 +00002177 continue;
2178 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002179
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002180 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002181
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002182 // The operands were *not* undefined, but the result is undefined.
2183 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002184
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002185 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002186 UndefNode->markAsSink();
2187 UndefResults.insert(UndefNode);
2188 }
2189
2190 continue;
2191 }
2192
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002193 // Otherwise, create a new node.
2194
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002195 MakeNode(Dst, B, *I2, SetSVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002196 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002197 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002198 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002199
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002200 assert (B->isCompoundAssignmentOp());
2201
Ted Kremenek934e3e92008-10-27 23:02:39 +00002202 if (Op >= BinaryOperator::AndAssign) {
2203 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2204 BinaryOperator::And));
2205 }
2206 else {
2207 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2208 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002209
2210 // Perform a load (the LHS). This performs the checks for
2211 // null dereferences, and so on.
2212 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002213 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002214 EvalLoad(Tmp3, LHS, *I2, St, location);
2215
2216 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2217
2218 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002219 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002220
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002221 // Check for divide-by-zero.
2222 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2223 && RHS->getType()->isIntegerType()) {
2224
2225 // CheckDivideZero returns a new state where the denominator
2226 // is assumed to be non-zero.
2227 St = CheckDivideZero(B, St, *I3, RightV);
2228
2229 if (!St)
2230 continue;
2231 }
2232
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002233 // Propagate undefined values (left-side).
2234 if (V.isUndef()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002235 EvalStore(Dst, B, LHS, *I3, SetSVal(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002236 continue;
2237 }
2238
2239 // Propagate unknown values (left and right-side).
2240 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002241 EvalStore(Dst, B, LHS, *I3, SetSVal(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002242 UnknownVal());
2243 continue;
2244 }
2245
2246 // At this point:
2247 //
2248 // The LHS is not Undef/Unknown.
2249 // The RHS is not Unknown.
2250
2251 // Get the computation type.
2252 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2253
2254 // Perform promotions.
2255 V = EvalCast(V, CTy);
2256 RightV = EvalCast(RightV, CTy);
2257
2258 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002259 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002260 // Propagate undefined values (right-side).
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002261 EvalStore(Dst, B, LHS, *I3, SetSVal(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002262 continue;
2263 }
2264
2265 // Compute the result of the operation.
2266
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002267 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002268
2269 if (Result.isUndef()) {
2270
2271 // The operands were not undefined, but the result is undefined.
2272
2273 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2274 UndefNode->markAsSink();
2275 UndefResults.insert(UndefNode);
2276 }
2277
2278 continue;
2279 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002280
2281 // EXPERIMENTAL: "Conjured" symbols.
2282 // FIXME: Handle structs.
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002283 if (Result.isUnknown() &&
2284 (CTy->isIntegerType() || Loc::IsLocType(CTy))) {
2285
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002286 unsigned Count = Builder->getCurrentBlockCount();
2287 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2288
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002289 Result = Loc::IsLocType(CTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002290 ? cast<SVal>(loc::SymbolVal(Sym))
2291 : cast<SVal>(nonloc::SymbolVal(Sym));
2292 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002293
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002294 EvalStore(Dst, B, LHS, *I3, SetSVal(St, B, Result), location, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002295 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002296 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002297 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002298}
Ted Kremenekee985462008-01-16 18:18:48 +00002299
2300//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002301// Transfer-function Helpers.
2302//===----------------------------------------------------------------------===//
2303
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002304void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002305 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002306 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002307 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002308
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002309 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002310 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2311
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002312 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002313 MakeNode(Dst, Ex, Pred, *I);
2314}
2315
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002316void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002317 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002318 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002319
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002320 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002321 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002322}
2323
2324//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002325// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002326//===----------------------------------------------------------------------===//
2327
Ted Kremenekaa66a322008-01-16 21:46:15 +00002328#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002329static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002330static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002331
Ted Kremenekaa66a322008-01-16 21:46:15 +00002332namespace llvm {
2333template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002334struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002335 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002336
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002337 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2338
2339 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002340 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002341 GraphPrintCheckerState->isUndefDeref(N) ||
2342 GraphPrintCheckerState->isUndefStore(N) ||
2343 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002344 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2345 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002346 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002347 GraphPrintCheckerState->isBadCall(N) ||
2348 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002349 return "color=\"red\",style=\"filled\"";
2350
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002351 if (GraphPrintCheckerState->isNoReturnCall(N))
2352 return "color=\"blue\",style=\"filled\"";
2353
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002354 return "";
2355 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002356
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002357 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002358 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002359
2360 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002361 ProgramPoint Loc = N->getLocation();
2362
2363 switch (Loc.getKind()) {
2364 case ProgramPoint::BlockEntranceKind:
2365 Out << "Block Entrance: B"
2366 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2367 break;
2368
2369 case ProgramPoint::BlockExitKind:
2370 assert (false);
2371 break;
2372
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002373 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002374 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002375 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002376 const PostStmt& L = cast<PostStmt>(Loc);
2377 Stmt* S = L.getStmt();
2378 SourceLocation SLoc = S->getLocStart();
2379
2380 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002381 llvm::raw_os_ostream OutS(Out);
2382 S->printPretty(OutS);
2383 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002384
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002385 if (SLoc.isFileID()) {
2386 Out << "\\lline="
2387 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002388 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002389 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002390
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002391 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002392 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002393 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002394 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002395 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002396 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002397 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002398 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002399 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2400 Out << "\\|Explicit divide-by zero or undefined value.";
2401 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2402 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002403 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002404 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002405 else if (GraphPrintCheckerState->isNoReturnCall(N))
2406 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002407 else if (GraphPrintCheckerState->isBadCall(N))
2408 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002409 else if (GraphPrintCheckerState->isUndefArg(N))
2410 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002411
Ted Kremenekaa66a322008-01-16 21:46:15 +00002412 break;
2413 }
2414
2415 default: {
2416 const BlockEdge& E = cast<BlockEdge>(Loc);
2417 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2418 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002419
2420 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002421
2422 SourceLocation SLoc = T->getLocStart();
2423
Ted Kremenekb38911f2008-01-30 23:03:39 +00002424 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002425
Ted Kremeneka95d3752008-09-13 05:16:45 +00002426 llvm::raw_os_ostream OutS(Out);
2427 E.getSrc()->printTerminator(OutS);
2428 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002429
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002430 if (SLoc.isFileID()) {
2431 Out << "\\lline="
2432 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2433 << GraphPrintSourceManager->getColumnNumber(SLoc);
2434 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002435
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002436 if (isa<SwitchStmt>(T)) {
2437 Stmt* Label = E.getDst()->getLabel();
2438
2439 if (Label) {
2440 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2441 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002442 llvm::raw_os_ostream OutS(Out);
2443 C->getLHS()->printPretty(OutS);
2444 OutS.flush();
2445
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002446 if (Stmt* RHS = C->getRHS()) {
2447 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002448 RHS->printPretty(OutS);
2449 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002450 }
2451
2452 Out << ":";
2453 }
2454 else {
2455 assert (isa<DefaultStmt>(Label));
2456 Out << "\\ldefault:";
2457 }
2458 }
2459 else
2460 Out << "\\l(implicit) default:";
2461 }
2462 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002463 // FIXME
2464 }
2465 else {
2466 Out << "\\lCondition: ";
2467 if (*E.getSrc()->succ_begin() == E.getDst())
2468 Out << "true";
2469 else
2470 Out << "false";
2471 }
2472
2473 Out << "\\l";
2474 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002475
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002476 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2477 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002478 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002479 }
2480 }
2481
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002482 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002483
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002484 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2485 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002486
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002487 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002488 return Out.str();
2489 }
2490};
2491} // end llvm namespace
2492#endif
2493
Ted Kremenekffe0f432008-03-07 22:58:01 +00002494#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002495
2496template <typename ITERATOR>
2497GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2498
2499template <>
2500GRExprEngine::NodeTy*
2501GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2502 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2503 return I->first;
2504}
2505
Ted Kremenekffe0f432008-03-07 22:58:01 +00002506template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002507static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002508 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002509
Ted Kremenekd4527582008-09-16 18:44:52 +00002510 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002511
2512 for ( ; I != E; ++I ) {
2513 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002514 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002515
Ted Kremenekd4527582008-09-16 18:44:52 +00002516 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002517 continue;
2518
Ted Kremenekd4527582008-09-16 18:44:52 +00002519 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002520 Sources.push_back(N);
2521 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002522}
2523#endif
2524
2525void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002526#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002527 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002528 std::vector<NodeTy*> Src;
2529
2530 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002531 AddSources(Src, null_derefs_begin(), null_derefs_end());
2532 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2533 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2534 AddSources(Src, undef_results_begin(), undef_results_end());
2535 AddSources(Src, bad_calls_begin(), bad_calls_end());
2536 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002537 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002538
Ted Kremenekcb612922008-04-18 19:23:43 +00002539 // The new way.
2540 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2541 (*I)->GetErrorNodes(Src);
2542
2543
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002544 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002545 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002546 else {
2547 GraphPrintCheckerState = this;
2548 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002549
Ted Kremenekffe0f432008-03-07 22:58:01 +00002550 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002551
2552 GraphPrintCheckerState = NULL;
2553 GraphPrintSourceManager = NULL;
2554 }
2555#endif
2556}
2557
2558void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2559#ifndef NDEBUG
2560 GraphPrintCheckerState = this;
2561 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002562
Ted Kremenek493d7a22008-03-11 18:25:33 +00002563 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2564
2565 if (!TrimmedG)
2566 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2567 else {
2568 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2569 delete TrimmedG;
2570 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002571
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002572 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002573 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002574#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002575}