blob: fb520675734d5d2495905e64658d79be40f3a658 [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,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000119 StoreManagerCreator SMC,
120 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000121 : CoreEngine(cfg, CD, Ctx, *this),
122 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000123 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000125 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000127 CurrentStmt(NULL),
128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000129 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000130
Ted Kremenek1a654b62008-06-20 21:45:25 +0000131GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
133 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000134
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135
136 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000137}
138
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000139//===----------------------------------------------------------------------===//
140// Utility methods.
141//===----------------------------------------------------------------------===//
142
143// SaveAndRestore - A utility class that uses RIIA to save and restore
144// the value of a variable.
145template<typename T>
146struct VISIBILITY_HIDDEN SaveAndRestore {
147 SaveAndRestore(T& x) : X(x), old_value(x) {}
148 ~SaveAndRestore() { X = old_value; }
149 T get() { return old_value; }
150
151 T& X;
152 T old_value;
153};
154
Ted Kremenek186350f2008-04-23 20:12:28 +0000155// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
156// value of a variable is saved, and during the dstor the old value is
157// or'ed with the new value.
158struct VISIBILITY_HIDDEN SaveOr {
159 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
160 ~SaveOr() { X |= old_value; }
161
162 bool& X;
163 bool old_value;
164};
165
166
Ted Kremenekc0959972008-07-02 21:24:01 +0000167void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000168 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000169 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000170 (*I)->EmitWarnings(BR);
171 }
172
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000173 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000174 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000175 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000176 }
177}
178
179void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000180 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181 tf->RegisterChecks(*this);
182 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000183}
184
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000185void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
186 if (!BatchAuditor)
187 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
188
189 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000190}
191
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000193 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194}
195
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196//===----------------------------------------------------------------------===//
197// Top-level transfer function logic (Dispatcher).
198//===----------------------------------------------------------------------===//
199
200void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
201
202 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000203 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000204
205 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000206 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000207 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000208
209 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000210 if (BatchAuditor)
211 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000212
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000213 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000214 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
215 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000216
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 else {
223 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 SaveOr OldHasGen(Builder->HasGeneratedNode);
225
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000226 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
227 Builder->PurgingDeadSymbols = true;
228
Ted Kremenek729a9a22008-07-17 23:15:45 +0000229 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000230 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000231
232 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
233 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000234 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 bool HasAutoGenerated = false;
237
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 NodeSet Dst;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Visit(S, *I, Dst);
247
248 // Do we need to auto-generate a node? We only need to do this to generate
249 // a node with a "cleaned" state; GRCoreEngine will actually handle
250 // auto-transitions for other cases.
251 if (Dst.size() == 1 && *Dst.begin() == EntryNode
252 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
253 HasAutoGenerated = true;
254 builder.generateNode(S, GetState(EntryNode), *I);
255 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000261
262 // FIXME: Consolidate.
263 StateMgr.CurrentStmt = 0;
264 CurrentStmt = 0;
265
Ted Kremenek846d4e92008-04-24 23:35:58 +0000266 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000267}
268
269void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
270
271 // FIXME: add metadata to the CFG so that we can disable
272 // this check when we KNOW that there is no block-level subexpression.
273 // The motivation is that this check requires a hashtable lookup.
274
275 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
276 Dst.Add(Pred);
277 return;
278 }
279
280 switch (S->getStmtClass()) {
281
282 default:
283 // Cases we intentionally have "default" handle:
284 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
285
286 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
287 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000288
289 case Stmt::ArraySubscriptExprClass:
290 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
291 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000292
293 case Stmt::AsmStmtClass:
294 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
295 break;
296
297 case Stmt::BinaryOperatorClass: {
298 BinaryOperator* B = cast<BinaryOperator>(S);
299
300 if (B->isLogicalOp()) {
301 VisitLogicalExpr(B, Pred, Dst);
302 break;
303 }
304 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000305 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000306 MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000307 break;
308 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000309
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000310 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
311 break;
312 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000313
Douglas Gregorb4609802008-11-14 16:09:21 +0000314 case Stmt::CallExprClass:
315 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000316 CallExpr* C = cast<CallExpr>(S);
317 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000318 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000319 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000320
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000321 // FIXME: ChooseExpr is really a constant. We need to fix
322 // the CFG do not model them as explicit control-flow.
323
324 case Stmt::ChooseExprClass: { // __builtin_choose_expr
325 ChooseExpr* C = cast<ChooseExpr>(S);
326 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
327 break;
328 }
329
330 case Stmt::CompoundAssignOperatorClass:
331 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
332 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000333
334 case Stmt::CompoundLiteralExprClass:
335 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
336 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000337
338 case Stmt::ConditionalOperatorClass: { // '?' operator
339 ConditionalOperator* C = cast<ConditionalOperator>(S);
340 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
341 break;
342 }
343
344 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000345 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000346 break;
347
348 case Stmt::DeclStmtClass:
349 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
350 break;
351
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000352 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000353 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000354 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000355 VisitCast(C, C->getSubExpr(), Pred, Dst);
356 break;
357 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000358
359 case Stmt::InitListExprClass:
360 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
361 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000362
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000363 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000364 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
365 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000366
367 case Stmt::ObjCIvarRefExprClass:
368 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
369 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000370
371 case Stmt::ObjCForCollectionStmtClass:
372 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
373 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000374
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000375 case Stmt::ObjCMessageExprClass: {
376 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
377 break;
378 }
379
380 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000381 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000382 break;
383
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000384 case Stmt::ReturnStmtClass:
385 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
386 break;
387
Sebastian Redl05189992008-11-11 17:56:53 +0000388 case Stmt::SizeOfAlignOfExprClass:
389 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000390 break;
391
392 case Stmt::StmtExprClass: {
393 StmtExpr* SE = cast<StmtExpr>(S);
394
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000395 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396
397 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
398 // probably just remove these from the CFG.
399 assert (!SE->getSubStmt()->body_empty());
400
401 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000402 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 else
404 Dst.Add(Pred);
405
406 break;
407 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000408
409 case Stmt::StringLiteralClass:
410 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
411 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000412
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000413 case Stmt::UnaryOperatorClass:
414 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000415 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000416 }
417}
418
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000419void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000420
421 Ex = Ex->IgnoreParens();
422
423 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
424 Dst.Add(Pred);
425 return;
426 }
427
428 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000429
430 case Stmt::ArraySubscriptExprClass:
431 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
432 return;
433
434 case Stmt::DeclRefExprClass:
435 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
436 return;
437
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000438 case Stmt::ObjCIvarRefExprClass:
439 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
440 return;
441
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000442 case Stmt::UnaryOperatorClass:
443 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
444 return;
445
446 case Stmt::MemberExprClass:
447 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
448 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000449
Ted Kremenek4f090272008-10-27 21:54:31 +0000450 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000451 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000452 return;
453
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000454 case Stmt::ObjCPropertyRefExprClass:
455 // FIXME: Property assignments are lvalues, but not really "locations".
456 // e.g.: self.x = something;
457 // Here the "self.x" really can translate to a method call (setter) when
458 // the assignment is made. Moreover, the entire assignment expression
459 // evaluate to whatever "something" is, not calling the "getter" for
460 // the property (which would make sense since it can have side effects).
461 // We'll probably treat this as a location, but not one that we can
462 // take the address of. Perhaps we need a new SVal class for cases
463 // like thsis?
464 // Note that we have a similar problem for bitfields, since they don't
465 // have "locations" in the sense that we can take their address.
466 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000467 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000468
469 case Stmt::StringLiteralClass: {
470 const GRState* St = GetState(Pred);
471 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000472 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000473 return;
474 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000475
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000476 default:
477 // Arbitrary subexpressions can return aggregate temporaries that
478 // can be used in a lvalue context. We need to enhance our support
479 // of such temporaries in both the environment and the store, so right
480 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000481 assert ((Ex->getType()->isAggregateType() ||
482 Ex->getType()->isUnionType()) &&
483 "Other kinds of expressions with non-aggregate/union types do"
484 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000485
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000486 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000487 }
488}
489
490//===----------------------------------------------------------------------===//
491// Block entrance. (Update counters).
492//===----------------------------------------------------------------------===//
493
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000494bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000495 GRBlockCounter BC) {
496
497 return BC.getNumVisited(B->getBlockID()) < 3;
498}
499
500//===----------------------------------------------------------------------===//
501// Branch processing.
502//===----------------------------------------------------------------------===//
503
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000504const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000505 Stmt* Terminator,
506 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000507
508 switch (Terminator->getStmtClass()) {
509 default:
510 return St;
511
512 case Stmt::BinaryOperatorClass: { // '&&' and '||'
513
514 BinaryOperator* B = cast<BinaryOperator>(Terminator);
515 BinaryOperator::Opcode Op = B->getOpcode();
516
517 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
518
519 // For &&, if we take the true branch, then the value of the whole
520 // expression is that of the RHS expression.
521 //
522 // For ||, if we take the false branch, then the value of the whole
523 // expression is that of the RHS expression.
524
525 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
526 (Op == BinaryOperator::LOr && !branchTaken)
527 ? B->getRHS() : B->getLHS();
528
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000529 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000530 }
531
532 case Stmt::ConditionalOperatorClass: { // ?:
533
534 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
535
536 // For ?, if branchTaken == true then the value is either the LHS or
537 // the condition itself. (GNU extension).
538
539 Expr* Ex;
540
541 if (branchTaken)
542 Ex = C->getLHS() ? C->getLHS() : C->getCond();
543 else
544 Ex = C->getRHS();
545
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000546 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000547 }
548
549 case Stmt::ChooseExprClass: { // ?:
550
551 ChooseExpr* C = cast<ChooseExpr>(Terminator);
552
553 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000554 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000555 }
556 }
557}
558
Ted Kremenekaf337412008-11-12 19:24:17 +0000559void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000560 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000561
Ted Kremeneke7d22112008-02-11 19:21:59 +0000562 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000563 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000564 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000565
Ted Kremenekb2331832008-02-15 22:29:00 +0000566 // Check for NULL conditions; e.g. "for(;;)"
567 if (!Condition) {
568 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000569 return;
570 }
571
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000572 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000573
574 switch (V.getBaseKind()) {
575 default:
576 break;
577
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000578 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000579 builder.generateNode(MarkBranch(PrevState, Term, true), true);
580 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000581 return;
582
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000583 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000584 NodeTy* N = builder.generateNode(PrevState, true);
585
586 if (N) {
587 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000588 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000589 }
590
591 builder.markInfeasible(false);
592 return;
593 }
594 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000595
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000596 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000597
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000598 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000599 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000600
601 if (isFeasible)
602 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000603 else
604 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000605
606 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000607
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000608 isFeasible = false;
609 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000610
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000611 if (isFeasible)
612 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000613 else
614 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000615}
616
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000617/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000618/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000619void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000620
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000621 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000622 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000623
624 // Three possibilities:
625 //
626 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000627 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000628 // (3) We have no clue about the label. Dispatch to all targets.
629 //
630
631 typedef IndirectGotoNodeBuilder::iterator iterator;
632
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000633 if (isa<loc::GotoLabel>(V)) {
634 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000635
636 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000637 if (I.getLabel() == L) {
638 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000639 return;
640 }
641 }
642
643 assert (false && "No block with label.");
644 return;
645 }
646
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000647 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000648 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000649 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000650 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000651 return;
652 }
653
654 // This is really a catch-all. We don't support symbolics yet.
655
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000656 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000657
658 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000659 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000660}
Ted Kremenekf233d482008-02-05 00:26:40 +0000661
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000662
663void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
664 NodeTy* Pred, NodeSet& Dst) {
665
666 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
667
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000668 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000669 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000670
671 assert (X.isUndef());
672
673 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
674
675 assert (SE);
676
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000677 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000678
679 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000680 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000681}
682
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000683/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
684/// nodes by processing the 'effects' of a switch statement.
685void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
686
687 typedef SwitchNodeBuilder::iterator iterator;
688
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000689 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000690 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000691 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000692
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000693 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000694 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000695 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000696 return;
697 }
698
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000699 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000700
701 // While most of this can be assumed (such as the signedness), having it
702 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000703
Chris Lattner98be4942008-03-05 18:54:05 +0000704 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000705
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706 APSInt V1(bits, false);
707 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000708 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000709
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000710 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000711
712 CaseStmt* Case = cast<CaseStmt>(I.getCase());
713
714 // Evaluate the case.
715 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
716 assert (false && "Case condition must evaluate to an integer constant.");
717 return;
718 }
719
720 // Get the RHS of the case, if it exists.
721
722 if (Expr* E = Case->getRHS()) {
723 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
724 assert (false &&
725 "Case condition (RHS) must evaluate to an integer constant.");
726 return ;
727 }
728
729 assert (V1 <= V2);
730 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000731 else
732 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000733
734 // FIXME: Eventually we should replace the logic below with a range
735 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000736 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000737
Ted Kremenek14a11402008-03-17 22:17:56 +0000738 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000739 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000740
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000741 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000742
743 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000744
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000745 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000746 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000747
748 if (isFeasible) {
749 builder.generateCaseStmtNode(I, StNew);
750
751 // If CondV evaluates to a constant, then we know that this
752 // is the *only* case that we can take, so stop evaluating the
753 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000754 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000755 return;
756 }
757
758 // Now "assume" that the case doesn't match. Add this state
759 // to the default state (if it is feasible).
760
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000761 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000762 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000763
Ted Kremenek5014ab12008-04-23 05:03:18 +0000764 if (isFeasible) {
765 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000766 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000767 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000768
Ted Kremenek14a11402008-03-17 22:17:56 +0000769 // Concretize the next value in the range.
770 if (V1 == V2)
771 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000772
Ted Kremenek14a11402008-03-17 22:17:56 +0000773 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000774 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000775
776 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777 }
778
779 // If we reach here, than we know that the default branch is
780 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000781 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000782}
783
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000784//===----------------------------------------------------------------------===//
785// Transfer functions: logical operations ('&&', '||').
786//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000787
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000788void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000789 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000790
Ted Kremenek05a23782008-02-26 19:05:15 +0000791 assert (B->getOpcode() == BinaryOperator::LAnd ||
792 B->getOpcode() == BinaryOperator::LOr);
793
794 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
795
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000796 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000797 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000798
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000799 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000800
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000801 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000802
803 assert (Ex);
804
805 if (Ex == B->getRHS()) {
806
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000807 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000808
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000809 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000810
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000811 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000812 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000813 return;
814 }
815
Ted Kremenek05a23782008-02-26 19:05:15 +0000816 // We took the RHS. Because the value of the '&&' or '||' expression must
817 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
818 // or 1. Alternatively, we could take a lazy approach, and calculate this
819 // value later when necessary. We don't have the machinery in place for
820 // this right now, and since most logical expressions are used for branches,
821 // the payoff is not likely to be large. Instead, we do eager evaluation.
822
823 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000824 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000825
826 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000827 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000828 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000829
830 isFeasible = false;
831 NewState = Assume(St, X, false, isFeasible);
832
833 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000834 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000835 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000836 }
837 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000838 // We took the LHS expression. Depending on whether we are '&&' or
839 // '||' we know what the value of the expression is via properties of
840 // the short-circuiting.
841
842 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000843 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000844 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000845}
Ted Kremenek05a23782008-02-26 19:05:15 +0000846
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000847//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000848// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000849//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000850
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000851void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
852 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000853
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000854 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000855
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000856 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000857
858 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
859
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000860 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000861
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000862 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000863 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000864 else
865 EvalLoad(Dst, Ex, Pred, St, V);
866 return;
867
868 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
869 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
870
871 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000872 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000873 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000874 return;
875
876 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000877 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000878 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000879 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000880 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000881 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000882
883 assert (false &&
884 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000885}
886
Ted Kremenek540cbe22008-04-22 04:56:29 +0000887/// VisitArraySubscriptExpr - Transfer function for array accesses
888void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000889 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000890
891 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000892 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000893 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000894 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000895
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000896 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000897 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000898 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000899
900 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000901 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000902 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000903
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000904 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000905 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000906 else
907 EvalLoad(Dst, A, *I2, St, V);
908 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000909 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000910}
911
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000912/// VisitMemberExpr - Transfer function for member expressions.
913void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000914 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000915
916 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000917 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000918
919 if (M->isArrow())
920 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
921 else
922 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
923
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000924 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000925 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000926 // FIXME: Should we insert some assumption logic in here to determine
927 // if "Base" is a valid piece of memory? Before we put this assumption
928 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000929 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000930
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000931 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000932 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000933 else
934 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000935 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000936}
937
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000938void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000939 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000940
941 assert (Builder && "GRStmtNodeBuilder must be defined.");
942
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000943 // Evaluate the location (checks for bad dereferences).
944 St = EvalLocation(Ex, Pred, St, location);
945
946 if (!St)
947 return;
948
949 // Proceed with the store.
950
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000951 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000952
Ted Kremenek186350f2008-04-23 20:12:28 +0000953 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000954 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000955 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000956
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000957 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000958 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000959
Ted Kremenek729a9a22008-07-17 23:15:45 +0000960 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000961
962 // Handle the case where no nodes where generated. Auto-generate that
963 // contains the updated state if we aren't generating sinks.
964
Ted Kremenekb0533962008-04-18 20:35:30 +0000965 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000966 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000967 location, Val);
968}
969
970void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000971 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000972 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000973
974 // Evaluate the location (checks for bad dereferences).
975
976 St = EvalLocation(Ex, Pred, St, location, true);
977
978 if (!St)
979 return;
980
981 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000982 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000983
984 // FIXME: Currently symbolic analysis "generates" new symbols
985 // for the contents of values. We need a better approach.
986
987 // FIXME: The "CheckOnly" option exists only because Array and Field
988 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000989 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +0000990
Ted Kremenek436f2b92008-04-30 04:23:07 +0000991 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000992 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000993 else if (location.isUnknown()) {
994 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000995 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000996 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000997 else {
998 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
999 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1000 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001001}
1002
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001003void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001004 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001005
1006 NodeSet TmpDst;
1007 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1008
1009 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1010 MakeNode(Dst, Ex, *I, (*I)->getState());
1011}
1012
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001013const GRState* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001014 const GRState* St,
1015 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001016
1017 // Check for loads/stores from/to undefined values.
1018 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001019 ProgramPoint::Kind K =
1020 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1021
1022 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023 Succ->markAsSink();
1024 UndefDeref.insert(Succ);
1025 }
1026
1027 return NULL;
1028 }
1029
1030 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1031 if (location.isUnknown())
1032 return St;
1033
1034 // During a load, one of two possible situations arise:
1035 // (1) A crash, because the location (pointer) was NULL.
1036 // (2) The location (pointer) is not NULL, and the dereference works.
1037 //
1038 // We add these assumptions.
1039
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001040 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001041
1042 // "Assume" that the pointer is not NULL.
1043
1044 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001045 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046
1047 // "Assume" that the pointer is NULL.
1048
1049 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001050 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1051 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052
1053 if (isFeasibleNull) {
1054
Ted Kremenek7360fda2008-09-18 23:09:54 +00001055 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001056 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001057 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1058
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001059 // We don't use "MakeNode" here because the node will be a sink
1060 // and we have no intention of processing it later.
1061
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001062 ProgramPoint::Kind K =
1063 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1064
1065 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001066
1067 if (NullNode) {
1068
1069 NullNode->markAsSink();
1070
1071 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1072 else ExplicitNullDeref.insert(NullNode);
1073 }
1074 }
Zhongxing Xu60156f02008-11-08 03:45:42 +00001075
1076 // Check for out-of-bound array access.
1077 if (isFeasibleNotNull && isa<loc::MemRegionVal>(LV)) {
1078 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1079 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1080 // Get the index of the accessed element.
1081 SVal Idx = ER->getIndex();
1082 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001083 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1084 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001085
1086 bool isFeasibleInBound = false;
1087 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1088 true, isFeasibleInBound);
1089
1090 bool isFeasibleOutBound = false;
1091 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1092 false, isFeasibleOutBound);
1093
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001094 if (isFeasibleOutBound) {
1095 // Report warning.
1096
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001097 // Make sink node manually.
1098 ProgramPoint::Kind K = isLoad ? ProgramPoint::PostLoadKind
1099 : ProgramPoint::PostStoreKind;
1100
1101 NodeTy* OOBNode = Builder->generateNode(Ex, StOutBound, Pred, K);
1102
1103 if (OOBNode) {
1104 OOBNode->markAsSink();
1105
1106 if (isFeasibleInBound)
1107 ImplicitOOBMemAccesses.insert(OOBNode);
1108 else
1109 ExplicitOOBMemAccesses.insert(OOBNode);
1110 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001111 }
1112
1113 return isFeasibleInBound ? StInBound : NULL;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001114 }
1115 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116
1117 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001118}
1119
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001120//===----------------------------------------------------------------------===//
1121// Transfer function: Function calls.
1122//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001123void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001124 CallExpr::arg_iterator AI,
1125 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001126 NodeSet& Dst)
1127{
1128 // Determine the type of function we're calling (if available).
1129 const FunctionTypeProto *Proto = NULL;
1130 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1131 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1132 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1133
1134 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1135}
1136
1137void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1138 CallExpr::arg_iterator AI,
1139 CallExpr::arg_iterator AE,
1140 NodeSet& Dst, const FunctionTypeProto *Proto,
1141 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001142
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001143 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001144 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001145 // If the call argument is being bound to a reference parameter,
1146 // visit it as an lvalue, not an rvalue.
1147 bool VisitAsLvalue = false;
1148 if (Proto && ParamIdx < Proto->getNumArgs())
1149 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1150
1151 NodeSet DstTmp;
1152 if (VisitAsLvalue)
1153 VisitLValue(*AI, Pred, DstTmp);
1154 else
1155 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001156 ++AI;
1157
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001158 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001159 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001160
1161 return;
1162 }
1163
1164 // If we reach here we have processed all of the arguments. Evaluate
1165 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001166
Ted Kremenek994a09b2008-02-25 21:16:03 +00001167 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001168 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001169
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001170 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001171
Ted Kremenekde434242008-02-19 01:44:53 +00001172 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001173 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1174
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001175 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001176 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001177
Ted Kremeneka1354a52008-03-03 16:47:31 +00001178 // FIXME: Add support for symbolic function calls (calls involving
1179 // function pointer values that are symbolic).
1180
1181 // Check for undefined control-flow or calls to NULL.
1182
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001183 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001184 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001185
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001186 if (N) {
1187 N->markAsSink();
1188 BadCalls.insert(N);
1189 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001190
Ted Kremenekde434242008-02-19 01:44:53 +00001191 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001192 }
1193
1194 // Check for the "noreturn" attribute.
1195
1196 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1197
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001198 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001199
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001200 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001201
1202 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001203 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001204 else {
1205 // HACK: Some functions are not marked noreturn, and don't return.
1206 // Here are a few hardwired ones. If this takes too long, we can
1207 // potentially cache these results.
1208 const char* s = FD->getIdentifier()->getName();
1209 unsigned n = strlen(s);
1210
1211 switch (n) {
1212 default:
1213 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001214
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001215 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001216 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1217 break;
1218
1219 case 5:
1220 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001221 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001222 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001223 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001224 // FIXME: use Assume to inspect the possible symbolic value of
1225 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001226 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001227 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001228 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001229 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001230 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001231 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001232
1233 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001234 if (!memcmp(s, "Assert", 6)) {
1235 Builder->BuildSinks = true;
1236 break;
1237 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001238
1239 // FIXME: This is just a wrapper around throwing an exception.
1240 // Eventually inter-procedural analysis should handle this easily.
1241 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1242
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001243 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001244
1245 case 7:
1246 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1247 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001248
Ted Kremenekf47bb782008-04-30 17:54:04 +00001249 case 8:
1250 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1251 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001252
1253 case 12:
1254 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1255 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001256
Ted Kremenekf9683082008-09-19 02:30:47 +00001257 case 13:
1258 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1259 break;
1260
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001261 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001262 if (!memcmp(s, "dtrace_assfail", 14) ||
1263 !memcmp(s, "yy_fatal_error", 14))
1264 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001265 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001266
1267 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001268 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1269 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001270 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001271
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001272 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001273 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001274
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001275 }
1276 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001277
1278 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001279
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001280 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001281
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001282 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001283
Ted Kremenek186350f2008-04-23 20:12:28 +00001284 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001285 switch (id) {
1286 case Builtin::BI__builtin_expect: {
1287 // For __builtin_expect, just return the value of the subexpression.
1288 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001289 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001290 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001291 continue;
1292 }
1293
Ted Kremenekb3021332008-11-02 00:35:01 +00001294 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001295 // FIXME: Refactor into StoreManager itself?
1296 MemRegionManager& RM = getStateManager().getRegionManager();
1297 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001298 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001299
1300 // Set the extent of the region in bytes. This enables us to use the
1301 // SVal of the argument directly. If we save the extent in bits, we
1302 // cannot represent values like symbol*8.
1303 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1304 St = getStoreManager().setExtent(St, R, Extent);
1305
Ted Kremenekb3021332008-11-02 00:35:01 +00001306 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1307 continue;
1308 }
1309
Ted Kremenek55aea312008-03-05 22:59:42 +00001310 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001311 break;
1312 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001313 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001314
Ted Kremenek186350f2008-04-23 20:12:28 +00001315 // Check any arguments passed-by-value against being undefined.
1316
1317 bool badArg = false;
1318
1319 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1320 I != E; ++I) {
1321
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001322 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001323 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001324
Ted Kremenek186350f2008-04-23 20:12:28 +00001325 if (N) {
1326 N->markAsSink();
1327 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001328 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001329
Ted Kremenek186350f2008-04-23 20:12:28 +00001330 badArg = true;
1331 break;
1332 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001333 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001334
1335 if (badArg)
1336 continue;
1337
1338 // Dispatch to the plug-in transfer function.
1339
1340 unsigned size = Dst.size();
1341 SaveOr OldHasGen(Builder->HasGeneratedNode);
1342 EvalCall(Dst, CE, L, *DI);
1343
1344 // Handle the case where no nodes where generated. Auto-generate that
1345 // contains the updated state if we aren't generating sinks.
1346
1347 if (!Builder->BuildSinks && Dst.size() == size &&
1348 !Builder->HasGeneratedNode)
1349 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001350 }
1351}
1352
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001353//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001354// Transfer function: Objective-C ivar references.
1355//===----------------------------------------------------------------------===//
1356
1357void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1358 NodeTy* Pred, NodeSet& Dst,
1359 bool asLValue) {
1360
1361 Expr* Base = cast<Expr>(Ex->getBase());
1362 NodeSet Tmp;
1363 Visit(Base, Pred, Tmp);
1364
1365 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1366 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001367 SVal BaseVal = GetSVal(St, Base);
1368 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001369
1370 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001371 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001372 else
1373 EvalLoad(Dst, Ex, *I, St, location);
1374 }
1375}
1376
1377//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001378// Transfer function: Objective-C fast enumeration 'for' statements.
1379//===----------------------------------------------------------------------===//
1380
1381void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1382 NodeTy* Pred, NodeSet& Dst) {
1383
1384 // ObjCForCollectionStmts are processed in two places. This method
1385 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1386 // statements within a basic block. This transfer function does two things:
1387 //
1388 // (1) binds the next container value to 'element'. This creates a new
1389 // node in the ExplodedGraph.
1390 //
1391 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1392 // whether or not the container has any more elements. This value
1393 // will be tested in ProcessBranch. We need to explicitly bind
1394 // this value because a container can contain nil elements.
1395 //
1396 // FIXME: Eventually this logic should actually do dispatches to
1397 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1398 // This will require simulating a temporary NSFastEnumerationState, either
1399 // through an SVal or through the use of MemRegions. This value can
1400 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1401 // terminates we reclaim the temporary (it goes out of scope) and we
1402 // we can test if the SVal is 0 or if the MemRegion is null (depending
1403 // on what approach we take).
1404 //
1405 // For now: simulate (1) by assigning either a symbol or nil if the
1406 // container is empty. Thus this transfer function will by default
1407 // result in state splitting.
1408
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001409 Stmt* elem = S->getElement();
1410 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001411
1412 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001413 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001414 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001415 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1416 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1417 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001418 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001419
1420 NodeSet Tmp;
1421 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001422
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001423 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1424 const GRState* state = GetState(*I);
1425 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1426 }
1427}
1428
1429void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1430 NodeTy* Pred, NodeSet& Dst,
1431 SVal ElementV) {
1432
1433
Ted Kremenekaf337412008-11-12 19:24:17 +00001434
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001435 // Get the current state. Use 'EvalLocation' to determine if it is a null
1436 // pointer, etc.
1437 Stmt* elem = S->getElement();
1438 GRStateRef state = GRStateRef(EvalLocation(elem, Pred, GetState(Pred),
1439 ElementV, false),
1440 getStateManager());
Ted Kremenekaf337412008-11-12 19:24:17 +00001441
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001442 if (!state)
1443 return;
1444
Ted Kremenekaf337412008-11-12 19:24:17 +00001445 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001446 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001447 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1448 GRStateRef hasElems = state.BindExpr(S, TrueV);
1449
Ted Kremenekaf337412008-11-12 19:24:17 +00001450 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001451 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1452 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001453
1454 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1455 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1456 // FIXME: The proper thing to do is to really iterate over the
1457 // container. We will do this with dispatch logic to the store.
1458 // For now, just 'conjure' up a symbolic value.
1459 QualType T = R->getType(getContext());
1460 assert (Loc::IsLocType(T));
1461 unsigned Count = Builder->getCurrentBlockCount();
1462 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1463 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001464
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001465 // Bind the location to 'nil' on the false branch.
1466 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1467 noElems = noElems.BindLoc(ElementV, nilV);
1468 }
1469
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001470 // Create the new nodes.
1471 MakeNode(Dst, S, Pred, hasElems);
1472 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001473}
1474
1475//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001476// Transfer function: Objective-C message expressions.
1477//===----------------------------------------------------------------------===//
1478
1479void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1480 NodeSet& Dst){
1481
1482 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1483 Pred, Dst);
1484}
1485
1486void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001487 ObjCMessageExpr::arg_iterator AI,
1488 ObjCMessageExpr::arg_iterator AE,
1489 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001490 if (AI == AE) {
1491
1492 // Process the receiver.
1493
1494 if (Expr* Receiver = ME->getReceiver()) {
1495 NodeSet Tmp;
1496 Visit(Receiver, Pred, Tmp);
1497
1498 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1499 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1500
1501 return;
1502 }
1503
1504 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1505 return;
1506 }
1507
1508 NodeSet Tmp;
1509 Visit(*AI, Pred, Tmp);
1510
1511 ++AI;
1512
1513 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1514 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1515}
1516
1517void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1518 NodeTy* Pred,
1519 NodeSet& Dst) {
1520
1521 // FIXME: More logic for the processing the method call.
1522
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001523 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001524 bool RaisesException = false;
1525
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001526
1527 if (Expr* Receiver = ME->getReceiver()) {
1528
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001529 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001530
1531 // Check for undefined control-flow or calls to NULL.
1532
1533 if (L.isUndef()) {
1534 NodeTy* N = Builder->generateNode(ME, St, Pred);
1535
1536 if (N) {
1537 N->markAsSink();
1538 UndefReceivers.insert(N);
1539 }
1540
1541 return;
1542 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001543
1544 // Check if the "raise" message was sent.
1545 if (ME->getSelector() == RaiseSel)
1546 RaisesException = true;
1547 }
1548 else {
1549
1550 IdentifierInfo* ClsName = ME->getClassName();
1551 Selector S = ME->getSelector();
1552
1553 // Check for special instance methods.
1554
1555 if (!NSExceptionII) {
1556 ASTContext& Ctx = getContext();
1557
1558 NSExceptionII = &Ctx.Idents.get("NSException");
1559 }
1560
1561 if (ClsName == NSExceptionII) {
1562
1563 enum { NUM_RAISE_SELECTORS = 2 };
1564
1565 // Lazily create a cache of the selectors.
1566
1567 if (!NSExceptionInstanceRaiseSelectors) {
1568
1569 ASTContext& Ctx = getContext();
1570
1571 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1572
1573 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1574 unsigned idx = 0;
1575
1576 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001577 II.push_back(&Ctx.Idents.get("raise"));
1578 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001579 NSExceptionInstanceRaiseSelectors[idx++] =
1580 Ctx.Selectors.getSelector(II.size(), &II[0]);
1581
1582 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001583 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001584 NSExceptionInstanceRaiseSelectors[idx++] =
1585 Ctx.Selectors.getSelector(II.size(), &II[0]);
1586 }
1587
1588 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1589 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1590 RaisesException = true; break;
1591 }
1592 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001593 }
1594
1595 // Check for any arguments that are uninitialized/undefined.
1596
1597 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1598 I != E; ++I) {
1599
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001600 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001601
1602 // Generate an error node for passing an uninitialized/undefined value
1603 // as an argument to a message expression. This node is a sink.
1604 NodeTy* N = Builder->generateNode(ME, St, Pred);
1605
1606 if (N) {
1607 N->markAsSink();
1608 MsgExprUndefArgs[N] = *I;
1609 }
1610
1611 return;
1612 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001613 }
1614
1615 // Check if we raise an exception. For now treat these as sinks. Eventually
1616 // we will want to handle exceptions properly.
1617
1618 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1619
1620 if (RaisesException)
1621 Builder->BuildSinks = true;
1622
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001623 // Dispatch to plug-in transfer function.
1624
1625 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001626 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001627
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001628 EvalObjCMessageExpr(Dst, ME, Pred);
1629
1630 // Handle the case where no nodes where generated. Auto-generate that
1631 // contains the updated state if we aren't generating sinks.
1632
Ted Kremenekb0533962008-04-18 20:35:30 +00001633 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001634 MakeNode(Dst, ME, Pred, St);
1635}
1636
1637//===----------------------------------------------------------------------===//
1638// Transfer functions: Miscellaneous statements.
1639//===----------------------------------------------------------------------===//
1640
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001641void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001642 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001643 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001644 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001645
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001646 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001647 T = ExCast->getTypeAsWritten();
1648
Zhongxing Xued340f72008-10-22 08:02:16 +00001649 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001650 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001651 else
1652 Visit(Ex, Pred, S1);
1653
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001654 // Check for casting to "void".
1655 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001656
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001657 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001658 Dst.Add(*I1);
1659
Ted Kremenek874d63f2008-01-24 02:02:54 +00001660 return;
1661 }
1662
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001663 // FIXME: The rest of this should probably just go into EvalCall, and
1664 // let the transfer function object be responsible for constructing
1665 // nodes.
1666
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001667 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001668 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001669 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001670 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001671
1672 // Unknown?
1673
1674 if (V.isUnknown()) {
1675 Dst.Add(N);
1676 continue;
1677 }
1678
1679 // Undefined?
1680
1681 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001682 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001683 continue;
1684 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001685
1686 // For const casts, just propagate the value.
1687 ASTContext& C = getContext();
1688
1689 if (C.getCanonicalType(T).getUnqualifiedType() ==
1690 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001691 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001692 continue;
1693 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001694
1695 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001696 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001697 unsigned bits = getContext().getTypeSize(ExTy);
1698
1699 // FIXME: Determine if the number of bits of the target type is
1700 // equal or exceeds the number of bits to store the pointer value.
1701 // If not, flag an error.
1702
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001703 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001704 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001705 continue;
1706 }
1707
1708 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001709 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1710 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001711 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001712 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001713 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001714 continue;
1715 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001716
Zhongxing Xu37d682a2008-11-14 09:23:38 +00001717 // Check for casts from array type to pointer type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001718 if (ExTy->isArrayType()) {
Ted Kremenek0fb7c612008-11-15 05:00:27 +00001719 assert(T->isPointerType());
Zhongxing Xue1911af2008-10-23 03:10:39 +00001720 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001721 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001722 continue;
1723 }
1724
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001725 // Check for casts from AllocaRegion pointer to typed pointer.
1726 if (isa<loc::MemRegionVal>(V)) {
1727 assert(Loc::IsLocType(T));
1728 assert(Loc::IsLocType(ExTy));
1729
1730 // Delegate to store manager.
Zhongxing Xucb529b52008-11-16 07:06:26 +00001731 std::pair<const GRState*, SVal> Res =
1732 getStoreManager().CastRegion(St, V, T, CastE);
1733
1734 const GRState* NewSt = Res.first;
1735 SVal NewPtr = Res.second;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001736
1737 // If no new region is created, fall through to the default case.
1738 if (NewSt != St) {
Zhongxing Xucb529b52008-11-16 07:06:26 +00001739 MakeNode(Dst, CastE, N, BindExpr(NewSt, CastE, NewPtr));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001740 continue;
1741 }
1742 }
1743
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001744 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001745 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001746 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001747}
1748
Ted Kremenek4f090272008-10-27 21:54:31 +00001749void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001750 NodeTy* Pred, NodeSet& Dst,
1751 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001752 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1753 NodeSet Tmp;
1754 Visit(ILE, Pred, Tmp);
1755
1756 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001757 const GRState* St = GetState(*I);
1758 SVal ILV = GetSVal(St, ILE);
1759 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001760
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001761 if (asLValue)
1762 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1763 else
1764 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001765 }
1766}
1767
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001768void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001769
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001770 // The CFG has one DeclStmt per Decl.
1771 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001772
1773 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001774 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001775
Ted Kremenekefd59942008-12-08 22:47:34 +00001776 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001777 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001778
1779 // FIXME: static variables may have an initializer, but the second
1780 // time a function is called those values may not be current.
1781 NodeSet Tmp;
1782
Ted Kremenekaf337412008-11-12 19:24:17 +00001783 if (InitEx)
1784 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001785
1786 if (Tmp.empty())
1787 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001788
1789 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001790 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001791 unsigned Count = Builder->getCurrentBlockCount();
1792
1793 if (InitEx) {
1794 SVal InitVal = GetSVal(St, InitEx);
1795 QualType T = VD->getType();
1796
1797 // Recover some path-sensitivity if a scalar value evaluated to
1798 // UnknownVal.
1799 if (InitVal.isUnknown()) {
1800 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001801 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001802 InitVal = loc::SymbolVal(Sym);
1803 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001804 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001805 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001806 InitVal = nonloc::SymbolVal(Sym);
1807 }
1808 }
1809
1810 St = StateMgr.BindDecl(St, VD, &InitVal, Count);
1811 }
1812 else
1813 St = StateMgr.BindDecl(St, VD, 0, Count);
Ted Kremenekefd59942008-12-08 22:47:34 +00001814
1815
1816 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1817 QualType T = getContext().getCanonicalType(VD->getType());
1818 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1819 // FIXME: Handle multi-dimensional VLAs.
1820
1821 Expr* SE = VLA->getSizeExpr();
1822 SVal Size = GetSVal(St, SE);
1823
1824 bool isFeasibleZero = false;
1825 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1826
1827 bool isFeasibleNotZero = false;
1828 St = Assume(St, Size, true, isFeasibleNotZero);
1829
1830 if (isFeasibleZero) {
1831 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1832 N->markAsSink();
1833 if (isFeasibleNotZero) ImplicitZeroSizedVLA.insert(N);
1834 else ExplicitZeroSizedVLA.insert(N);
1835 }
1836 }
1837
1838 if (!isFeasibleNotZero)
1839 continue;
1840 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001841
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001842 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001843 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001844}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001845
Ted Kremenekf75b1862008-10-30 17:47:32 +00001846namespace {
1847 // This class is used by VisitInitListExpr as an item in a worklist
1848 // for processing the values contained in an InitListExpr.
1849class VISIBILITY_HIDDEN InitListWLItem {
1850public:
1851 llvm::ImmutableList<SVal> Vals;
1852 GRExprEngine::NodeTy* N;
1853 InitListExpr::reverse_iterator Itr;
1854
1855 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1856 InitListExpr::reverse_iterator itr)
1857 : Vals(vals), N(n), Itr(itr) {}
1858};
1859}
1860
1861
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001862void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1863 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001864
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001865 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001866 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001867 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001868
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001869 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001870
Ted Kremeneka49e3672008-10-30 23:14:36 +00001871 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001872
Ted Kremeneka49e3672008-10-30 23:14:36 +00001873 // Handle base case where the initializer has no elements.
1874 // e.g: static int* myArray[] = {};
1875 if (NumInitElements == 0) {
1876 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1877 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1878 return;
1879 }
1880
1881 // Create a worklist to process the initializers.
1882 llvm::SmallVector<InitListWLItem, 10> WorkList;
1883 WorkList.reserve(NumInitElements);
1884 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001885 InitListExpr::reverse_iterator ItrEnd = E->rend();
1886
Ted Kremeneka49e3672008-10-30 23:14:36 +00001887 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001888 while (!WorkList.empty()) {
1889 InitListWLItem X = WorkList.back();
1890 WorkList.pop_back();
1891
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001892 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001893 Visit(*X.Itr, X.N, Tmp);
1894
1895 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001896
Ted Kremenekf75b1862008-10-30 17:47:32 +00001897 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1898 // Get the last initializer value.
1899 state = GetState(*NI);
1900 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1901
1902 // Construct the new list of values by prepending the new value to
1903 // the already constructed list.
1904 llvm::ImmutableList<SVal> NewVals =
1905 getBasicVals().consVals(InitV, X.Vals);
1906
1907 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001908 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001909 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001910
Ted Kremenekf75b1862008-10-30 17:47:32 +00001911 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001912 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001913 }
1914 else {
1915 // Still some initializer values to go. Push them onto the worklist.
1916 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1917 }
1918 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001919 }
Ted Kremenek87903072008-10-30 18:34:31 +00001920
1921 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001922 }
1923
Ted Kremenek062e2f92008-11-13 06:10:40 +00001924 if (T->isUnionType() || T->isVectorType()) {
1925 // FIXME: to be implemented.
1926 // Note: That vectors can return true for T->isIntegerType()
1927 MakeNode(Dst, E, Pred, state);
1928 return;
1929 }
1930
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001931 if (Loc::IsLocType(T) || T->isIntegerType()) {
1932 assert (E->getNumInits() == 1);
1933 NodeSet Tmp;
1934 Expr* Init = E->getInit(0);
1935 Visit(Init, Pred, Tmp);
1936 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1937 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001938 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001939 }
1940 return;
1941 }
1942
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001943
1944 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1945 assert(0 && "unprocessed InitListExpr type");
1946}
Ted Kremenekf233d482008-02-05 00:26:40 +00001947
Sebastian Redl05189992008-11-11 17:56:53 +00001948/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1949void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1950 NodeTy* Pred,
1951 NodeSet& Dst) {
1952 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001953 uint64_t amt;
1954
1955 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001956
Ted Kremenek87e80342008-03-15 03:13:20 +00001957 // FIXME: Add support for VLAs.
1958 if (!T.getTypePtr()->isConstantSizeType())
1959 return;
1960
Ted Kremenekf342d182008-04-30 21:31:12 +00001961 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1962 // the compiler has laid out its representation. Just report Unknown
1963 // for these.
1964 if (T->isObjCInterfaceType())
1965 return;
1966
Ted Kremenek87e80342008-03-15 03:13:20 +00001967 amt = 1; // Handle sizeof(void)
1968
1969 if (T != getContext().VoidTy)
1970 amt = getContext().getTypeSize(T) / 8;
1971
1972 }
1973 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001974 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001975
Ted Kremenek0e561a32008-03-21 21:30:14 +00001976 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001977 BindExpr(GetState(Pred), Ex,
1978 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001979}
1980
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001981
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001982void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001983 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001984
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001985 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001986
1987 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001988 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001989
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001990 case UnaryOperator::Deref: {
1991
1992 Expr* Ex = U->getSubExpr()->IgnoreParens();
1993 NodeSet Tmp;
1994 Visit(Ex, Pred, Tmp);
1995
1996 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001997
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001998 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001999 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002000
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002001 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002002 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002003 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002004 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002005 }
2006
2007 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002008 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002009
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002010 case UnaryOperator::Real: {
2011
2012 Expr* Ex = U->getSubExpr()->IgnoreParens();
2013 NodeSet Tmp;
2014 Visit(Ex, Pred, Tmp);
2015
2016 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2017
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002018 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002019 if (Ex->getType()->isAnyComplexType()) {
2020 // Just report "Unknown."
2021 Dst.Add(*I);
2022 continue;
2023 }
2024
2025 // For all other types, UnaryOperator::Real is an identity operation.
2026 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002027 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002028 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002029 }
2030
2031 return;
2032 }
2033
2034 case UnaryOperator::Imag: {
2035
2036 Expr* Ex = U->getSubExpr()->IgnoreParens();
2037 NodeSet Tmp;
2038 Visit(Ex, Pred, Tmp);
2039
2040 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002041 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002042 if (Ex->getType()->isAnyComplexType()) {
2043 // Just report "Unknown."
2044 Dst.Add(*I);
2045 continue;
2046 }
2047
2048 // For all other types, UnaryOperator::Float returns 0.
2049 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002050 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002051 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002052 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002053 }
2054
2055 return;
2056 }
2057
2058 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002059 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002060 Dst.Add(Pred);
2061 return;
2062
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002063 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002064 case UnaryOperator::Extension: {
2065
2066 // Unary "+" is a no-op, similar to a parentheses. We still have places
2067 // where it may be a block-level expression, so we need to
2068 // generate an extra node that just propagates the value of the
2069 // subexpression.
2070
2071 Expr* Ex = U->getSubExpr()->IgnoreParens();
2072 NodeSet Tmp;
2073 Visit(Ex, Pred, Tmp);
2074
2075 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002076 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002077 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002078 }
2079
2080 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002081 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002082
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002083 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002084
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002085 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002086 Expr* Ex = U->getSubExpr()->IgnoreParens();
2087 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002088 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002089
2090 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002091 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002092 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002093 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002094 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002095 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002096
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002097 return;
2098 }
2099
2100 case UnaryOperator::LNot:
2101 case UnaryOperator::Minus:
2102 case UnaryOperator::Not: {
2103
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002104 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002105 Expr* Ex = U->getSubExpr()->IgnoreParens();
2106 NodeSet Tmp;
2107 Visit(Ex, Pred, Tmp);
2108
2109 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002110 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002111
2112 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002113 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002114
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002115 if (V.isUnknownOrUndef()) {
2116 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2117 continue;
2118 }
2119
Ted Kremenek60595da2008-11-15 04:01:56 +00002120// QualType DstT = getContext().getCanonicalType(U->getType());
2121// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2122//
2123// if (DstT != SrcT) // Perform promotions.
2124// V = EvalCast(V, DstT);
2125//
2126// if (V.isUnknownOrUndef()) {
2127// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2128// continue;
2129// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002130
2131 switch (U->getOpcode()) {
2132 default:
2133 assert(false && "Invalid Opcode.");
2134 break;
2135
2136 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002137 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002138 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002139 break;
2140
2141 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002142 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002143 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002144 break;
2145
2146 case UnaryOperator::LNot:
2147
2148 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2149 //
2150 // Note: technically we do "E == 0", but this is the same in the
2151 // transfer functions as "0 == E".
2152
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002153 if (isa<Loc>(V)) {
2154 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2155 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002156 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002157 }
2158 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002159 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002160#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002161 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2162 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002163#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002164 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002165 continue;
2166#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002167 }
2168
2169 break;
2170 }
2171
2172 MakeNode(Dst, U, *I, St);
2173 }
2174
2175 return;
2176 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002177 }
2178
2179 // Handle ++ and -- (both pre- and post-increment).
2180
2181 assert (U->isIncrementDecrementOp());
2182 NodeSet Tmp;
2183 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002184 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002185
2186 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2187
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002188 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002189 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002190
2191 // Perform a load.
2192 NodeSet Tmp2;
2193 EvalLoad(Tmp2, Ex, *I, St, V1);
2194
2195 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2196
2197 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002198 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002199
2200 // Propagate unknown and undefined values.
2201 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002202 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002203 continue;
2204 }
2205
Ted Kremenek443003b2008-02-21 19:29:23 +00002206 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002207
2208 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2209 : BinaryOperator::Sub;
2210
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002211 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002212 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002213
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002214 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002215 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002216 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002217 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002218}
2219
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002220void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2221 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2222}
2223
2224void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2225 AsmStmt::outputs_iterator I,
2226 AsmStmt::outputs_iterator E,
2227 NodeTy* Pred, NodeSet& Dst) {
2228 if (I == E) {
2229 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2230 return;
2231 }
2232
2233 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002234 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002235
2236 ++I;
2237
2238 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2239 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2240}
2241
2242void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2243 AsmStmt::inputs_iterator I,
2244 AsmStmt::inputs_iterator E,
2245 NodeTy* Pred, NodeSet& Dst) {
2246 if (I == E) {
2247
2248 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002249 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002250
2251 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2252 // which interprets the inline asm and stores proper results in the
2253 // outputs.
2254
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002255 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002256
2257 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2258 OE = A->end_outputs(); OI != OE; ++OI) {
2259
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002260 SVal X = GetSVal(St, *OI);
2261 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002262
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002263 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002264 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002265 }
2266
Ted Kremenek0e561a32008-03-21 21:30:14 +00002267 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002268 return;
2269 }
2270
2271 NodeSet Tmp;
2272 Visit(*I, Pred, Tmp);
2273
2274 ++I;
2275
2276 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2277 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2278}
2279
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002280void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2281 assert (Builder && "GRStmtNodeBuilder must be defined.");
2282
2283 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002284
Ted Kremenek186350f2008-04-23 20:12:28 +00002285 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2286 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002287
Ted Kremenek729a9a22008-07-17 23:15:45 +00002288 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002289
Ted Kremenekb0533962008-04-18 20:35:30 +00002290 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002291
Ted Kremenekb0533962008-04-18 20:35:30 +00002292 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002293 MakeNode(Dst, S, Pred, GetState(Pred));
2294}
2295
Ted Kremenek02737ed2008-03-31 15:02:58 +00002296void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2297
2298 Expr* R = S->getRetValue();
2299
2300 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002301 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002302 return;
2303 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002304
Ted Kremenek5917d782008-11-21 00:27:44 +00002305 NodeSet Tmp;
2306 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002307
Ted Kremenek5917d782008-11-21 00:27:44 +00002308 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2309 SVal X = GetSVal((*I)->getState(), R);
2310
2311 // Check if we return the address of a stack variable.
2312 if (isa<loc::MemRegionVal>(X)) {
2313 // Determine if the value is on the stack.
2314 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002315
Ted Kremenek5917d782008-11-21 00:27:44 +00002316 if (R && getStateManager().hasStackStorage(R)) {
2317 // Create a special node representing the error.
2318 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2319 N->markAsSink();
2320 RetsStackAddr.insert(N);
2321 }
2322 continue;
2323 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002324 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002325 // Check if we return an undefined value.
2326 else if (X.isUndef()) {
2327 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2328 N->markAsSink();
2329 RetsUndef.insert(N);
2330 }
2331 continue;
2332 }
2333
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002334 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002335 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002336}
Ted Kremenek55deb972008-03-25 00:34:37 +00002337
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002338//===----------------------------------------------------------------------===//
2339// Transfer functions: Binary operators.
2340//===----------------------------------------------------------------------===//
2341
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002342const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2343 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002344
2345 // Divide by undefined? (potentially zero)
2346
2347 if (Denom.isUndef()) {
2348 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2349
2350 if (DivUndef) {
2351 DivUndef->markAsSink();
2352 ExplicitBadDivides.insert(DivUndef);
2353 }
2354
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002355 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002356 }
2357
2358 // Check for divide/remainder-by-zero.
2359 // First, "assume" that the denominator is 0 or undefined.
2360
2361 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002362 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002363
2364 // Second, "assume" that the denominator cannot be 0.
2365
2366 bool isFeasibleNotZero = false;
2367 St = Assume(St, Denom, true, isFeasibleNotZero);
2368
2369 // Create the node for the divide-by-zero (if it occurred).
2370
2371 if (isFeasibleZero)
2372 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2373 DivZeroNode->markAsSink();
2374
2375 if (isFeasibleNotZero)
2376 ImplicitBadDivides.insert(DivZeroNode);
2377 else
2378 ExplicitBadDivides.insert(DivZeroNode);
2379
2380 }
2381
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002382 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002383}
2384
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002385void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002386 GRExprEngine::NodeTy* Pred,
2387 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002388
2389 NodeSet Tmp1;
2390 Expr* LHS = B->getLHS()->IgnoreParens();
2391 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002392
Ted Kremenek759623e2008-12-06 02:39:30 +00002393 // FIXME: Add proper support for ObjCKVCRefExpr.
2394 if (isa<ObjCKVCRefExpr>(LHS)) {
2395 Visit(RHS, Pred, Dst);
2396 return;
2397 }
2398
2399
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002400 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002401 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002402 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002403 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002404
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002405 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002406
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002407 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002408
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002409 // Process the RHS.
2410
2411 NodeSet Tmp2;
2412 Visit(RHS, *I1, Tmp2);
2413
2414 // With both the LHS and RHS evaluated, process the operation itself.
2415
2416 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002417
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002418 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002419 const GRState* OldSt = St;
2420
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002421 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002422 BinaryOperator::Opcode Op = B->getOpcode();
2423
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002424 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002425
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002426 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002427
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002428 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002429 // FIXME: Handle structs.
2430 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002431
Ted Kremenek062e2f92008-11-13 06:10:40 +00002432 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2433 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002434 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002435 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002436
Ted Kremenek062e2f92008-11-13 06:10:40 +00002437 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002438 ? cast<SVal>(loc::SymbolVal(Sym))
2439 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002440 }
2441
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002442 // Simulate the effects of a "store": bind the value of the RHS
2443 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002444
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002445 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002446 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002447 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002448
2449 case BinaryOperator::Div:
2450 case BinaryOperator::Rem:
2451
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002452 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002453 if (RHS->getType()->isIntegerType() &&
2454 RHS->getType()->isScalarType()) {
2455
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002456 St = CheckDivideZero(B, St, *I2, RightV);
2457 if (!St) continue;
2458 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002459
2460 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002461
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002462 default: {
2463
2464 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002465 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002466
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002467 // Process non-assignements except commas or short-circuited
2468 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002469
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002470 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002471
2472 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002473 if (OldSt != St) {
2474 // Generate a new node if we have already created a new state.
2475 MakeNode(Dst, B, *I2, St);
2476 }
2477 else
2478 Dst.Add(*I2);
2479
Ted Kremenek89063af2008-02-21 19:15:37 +00002480 continue;
2481 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002482
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002483 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002484
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002485 // The operands were *not* undefined, but the result is undefined.
2486 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002487
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002488 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002489 UndefNode->markAsSink();
2490 UndefResults.insert(UndefNode);
2491 }
2492
2493 continue;
2494 }
2495
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002496 // Otherwise, create a new node.
2497
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002498 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002499 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002500 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002501 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002502
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002503 assert (B->isCompoundAssignmentOp());
2504
Ted Kremenek934e3e92008-10-27 23:02:39 +00002505 if (Op >= BinaryOperator::AndAssign) {
2506 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2507 BinaryOperator::And));
2508 }
2509 else {
2510 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2511 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002512
2513 // Perform a load (the LHS). This performs the checks for
2514 // null dereferences, and so on.
2515 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002516 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002517 EvalLoad(Tmp3, LHS, *I2, St, location);
2518
2519 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2520
2521 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002522 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002523
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002524 // Check for divide-by-zero.
2525 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002526 && RHS->getType()->isIntegerType()
2527 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002528
2529 // CheckDivideZero returns a new state where the denominator
2530 // is assumed to be non-zero.
2531 St = CheckDivideZero(B, St, *I3, RightV);
2532
2533 if (!St)
2534 continue;
2535 }
2536
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002537 // Propagate undefined values (left-side).
2538 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002539 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002540 continue;
2541 }
2542
2543 // Propagate unknown values (left and right-side).
2544 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002545 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002546 UnknownVal());
2547 continue;
2548 }
2549
2550 // At this point:
2551 //
2552 // The LHS is not Undef/Unknown.
2553 // The RHS is not Unknown.
2554
2555 // Get the computation type.
2556 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002557 CTy = getContext().getCanonicalType(CTy);
2558
2559 QualType LTy = getContext().getCanonicalType(LHS->getType());
2560 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002561
2562 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002563 if (LTy != CTy) V = EvalCast(V, CTy);
2564 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002565
2566 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002567 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002568 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002569 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002570 continue;
2571 }
2572
Ted Kremenek60595da2008-11-15 04:01:56 +00002573 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002574 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002575
2576 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002577 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002578 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2579 UndefNode->markAsSink();
2580 UndefResults.insert(UndefNode);
2581 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002582 continue;
2583 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002584
2585 // EXPERIMENTAL: "Conjured" symbols.
2586 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002587
2588 SVal LHSVal;
2589
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002590 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2591 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002592
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002593 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002594
Ted Kremenek60595da2008-11-15 04:01:56 +00002595 // The symbolic value is actually for the type of the left-hand side
2596 // expression, not the computation type, as this is the value the
2597 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002598 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002599 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002600 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002601 : cast<SVal>(nonloc::SymbolVal(Sym));
2602
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002603 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002604 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002605 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002606 else {
2607 // The left-hand side may bind to a different value then the
2608 // computation type.
2609 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2610 }
2611
2612 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002613 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002614 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002615 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002616}
Ted Kremenekee985462008-01-16 18:18:48 +00002617
2618//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002619// Transfer-function Helpers.
2620//===----------------------------------------------------------------------===//
2621
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002622void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002623 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002624 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002625 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002626
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002627 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002628 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2629
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002630 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002631 MakeNode(Dst, Ex, Pred, *I);
2632}
2633
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002634void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002635 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002636 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002637
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002638 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002639 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002640}
2641
2642//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002643// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002644//===----------------------------------------------------------------------===//
2645
Ted Kremenekaa66a322008-01-16 21:46:15 +00002646#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002647static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002648static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002649
Ted Kremenekaa66a322008-01-16 21:46:15 +00002650namespace llvm {
2651template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002652struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002653 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002654
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002655 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2656
2657 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002658 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002659 GraphPrintCheckerState->isUndefDeref(N) ||
2660 GraphPrintCheckerState->isUndefStore(N) ||
2661 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002662 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2663 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002664 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002665 GraphPrintCheckerState->isBadCall(N) ||
2666 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002667 return "color=\"red\",style=\"filled\"";
2668
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002669 if (GraphPrintCheckerState->isNoReturnCall(N))
2670 return "color=\"blue\",style=\"filled\"";
2671
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002672 return "";
2673 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002674
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002675 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002676 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002677
2678 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002679 ProgramPoint Loc = N->getLocation();
2680
2681 switch (Loc.getKind()) {
2682 case ProgramPoint::BlockEntranceKind:
2683 Out << "Block Entrance: B"
2684 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2685 break;
2686
2687 case ProgramPoint::BlockExitKind:
2688 assert (false);
2689 break;
2690
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002691 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002692 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002693 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002694 const PostStmt& L = cast<PostStmt>(Loc);
2695 Stmt* S = L.getStmt();
2696 SourceLocation SLoc = S->getLocStart();
2697
2698 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002699 llvm::raw_os_ostream OutS(Out);
2700 S->printPretty(OutS);
2701 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002702
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002703 if (SLoc.isFileID()) {
2704 Out << "\\lline="
2705 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002706 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002707 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002708
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002709 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002710 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002711 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002712 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002713 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002714 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002715 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002716 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002717 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2718 Out << "\\|Explicit divide-by zero or undefined value.";
2719 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2720 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002721 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002722 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002723 else if (GraphPrintCheckerState->isNoReturnCall(N))
2724 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002725 else if (GraphPrintCheckerState->isBadCall(N))
2726 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002727 else if (GraphPrintCheckerState->isUndefArg(N))
2728 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002729
Ted Kremenekaa66a322008-01-16 21:46:15 +00002730 break;
2731 }
2732
2733 default: {
2734 const BlockEdge& E = cast<BlockEdge>(Loc);
2735 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2736 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002737
2738 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002739
2740 SourceLocation SLoc = T->getLocStart();
2741
Ted Kremenekb38911f2008-01-30 23:03:39 +00002742 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002743
Ted Kremeneka95d3752008-09-13 05:16:45 +00002744 llvm::raw_os_ostream OutS(Out);
2745 E.getSrc()->printTerminator(OutS);
2746 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002747
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002748 if (SLoc.isFileID()) {
2749 Out << "\\lline="
2750 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2751 << GraphPrintSourceManager->getColumnNumber(SLoc);
2752 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002753
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002754 if (isa<SwitchStmt>(T)) {
2755 Stmt* Label = E.getDst()->getLabel();
2756
2757 if (Label) {
2758 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2759 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002760 llvm::raw_os_ostream OutS(Out);
2761 C->getLHS()->printPretty(OutS);
2762 OutS.flush();
2763
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002764 if (Stmt* RHS = C->getRHS()) {
2765 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002766 RHS->printPretty(OutS);
2767 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002768 }
2769
2770 Out << ":";
2771 }
2772 else {
2773 assert (isa<DefaultStmt>(Label));
2774 Out << "\\ldefault:";
2775 }
2776 }
2777 else
2778 Out << "\\l(implicit) default:";
2779 }
2780 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002781 // FIXME
2782 }
2783 else {
2784 Out << "\\lCondition: ";
2785 if (*E.getSrc()->succ_begin() == E.getDst())
2786 Out << "true";
2787 else
2788 Out << "false";
2789 }
2790
2791 Out << "\\l";
2792 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002793
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002794 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2795 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002796 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002797 }
2798 }
2799
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002800 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002801
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002802 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2803 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002804
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002805 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002806 return Out.str();
2807 }
2808};
2809} // end llvm namespace
2810#endif
2811
Ted Kremenekffe0f432008-03-07 22:58:01 +00002812#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002813
2814template <typename ITERATOR>
2815GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2816
2817template <>
2818GRExprEngine::NodeTy*
2819GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2820 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2821 return I->first;
2822}
2823
Ted Kremenekffe0f432008-03-07 22:58:01 +00002824template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002825static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002826 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002827
Ted Kremenekd4527582008-09-16 18:44:52 +00002828 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002829
2830 for ( ; I != E; ++I ) {
2831 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002832 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002833
Ted Kremenekd4527582008-09-16 18:44:52 +00002834 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002835 continue;
2836
Ted Kremenekd4527582008-09-16 18:44:52 +00002837 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002838 Sources.push_back(N);
2839 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002840}
2841#endif
2842
2843void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002844#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002845 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002846 std::vector<NodeTy*> Src;
2847
2848 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002849 AddSources(Src, null_derefs_begin(), null_derefs_end());
2850 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2851 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2852 AddSources(Src, undef_results_begin(), undef_results_end());
2853 AddSources(Src, bad_calls_begin(), bad_calls_end());
2854 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002855 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002856
Ted Kremenekcb612922008-04-18 19:23:43 +00002857 // The new way.
2858 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2859 (*I)->GetErrorNodes(Src);
2860
2861
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002862 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002863 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002864 else {
2865 GraphPrintCheckerState = this;
2866 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002867
Ted Kremenekffe0f432008-03-07 22:58:01 +00002868 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002869
2870 GraphPrintCheckerState = NULL;
2871 GraphPrintSourceManager = NULL;
2872 }
2873#endif
2874}
2875
2876void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2877#ifndef NDEBUG
2878 GraphPrintCheckerState = this;
2879 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002880
Ted Kremenek493d7a22008-03-11 18:25:33 +00002881 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2882
2883 if (!TrimmedG)
2884 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2885 else {
2886 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2887 delete TrimmedG;
2888 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002889
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002890 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002891 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002892#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002893}