blob: 8e6d5ab5cc35057202b38b3308d1e766830444b8 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000020#include "llvm/ADT/ImmutableList.h"
21#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000118 LiveVariables& L,
119 GRStateManager::StoreManagerCreator SMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000120 : CoreEngine(cfg, CD, Ctx, *this),
121 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000122 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000123 Builder(NULL),
Ted Kremenek95c7b002008-10-24 01:04:59 +0000124 StateMgr(G.getContext(), SMC,
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000125 CreateBasicConstraintManager, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000127 CurrentStmt(NULL),
128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000129 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000130
Ted Kremenek1a654b62008-06-20 21:45:25 +0000131GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
133 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000134
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135
136 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000137}
138
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000139//===----------------------------------------------------------------------===//
140// Utility methods.
141//===----------------------------------------------------------------------===//
142
143// SaveAndRestore - A utility class that uses RIIA to save and restore
144// the value of a variable.
145template<typename T>
146struct VISIBILITY_HIDDEN SaveAndRestore {
147 SaveAndRestore(T& x) : X(x), old_value(x) {}
148 ~SaveAndRestore() { X = old_value; }
149 T get() { return old_value; }
150
151 T& X;
152 T old_value;
153};
154
Ted Kremenek186350f2008-04-23 20:12:28 +0000155// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
156// value of a variable is saved, and during the dstor the old value is
157// or'ed with the new value.
158struct VISIBILITY_HIDDEN SaveOr {
159 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
160 ~SaveOr() { X |= old_value; }
161
162 bool& X;
163 bool old_value;
164};
165
166
Ted Kremenekc0959972008-07-02 21:24:01 +0000167void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000168 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000169 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000170 (*I)->EmitWarnings(BR);
171 }
172
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000173 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000174 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000175 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000176 }
177}
178
179void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000180 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181 tf->RegisterChecks(*this);
182 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000183}
184
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000185void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
186 if (!BatchAuditor)
187 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
188
189 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000190}
191
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000193 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194}
195
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196//===----------------------------------------------------------------------===//
197// Top-level transfer function logic (Dispatcher).
198//===----------------------------------------------------------------------===//
199
200void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
201
202 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000203 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000204
205 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000206 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000207 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000208
209 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000210 if (BatchAuditor)
211 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000212
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000213 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000214 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
215 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000216
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 else {
223 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 SaveOr OldHasGen(Builder->HasGeneratedNode);
225
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000226 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
227 Builder->PurgingDeadSymbols = true;
228
Ted Kremenek729a9a22008-07-17 23:15:45 +0000229 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000230 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000231
232 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
233 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000234 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 bool HasAutoGenerated = false;
237
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 NodeSet Dst;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Visit(S, *I, Dst);
247
248 // Do we need to auto-generate a node? We only need to do this to generate
249 // a node with a "cleaned" state; GRCoreEngine will actually handle
250 // auto-transitions for other cases.
251 if (Dst.size() == 1 && *Dst.begin() == EntryNode
252 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
253 HasAutoGenerated = true;
254 builder.generateNode(S, GetState(EntryNode), *I);
255 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000261
262 // FIXME: Consolidate.
263 StateMgr.CurrentStmt = 0;
264 CurrentStmt = 0;
265
Ted Kremenek846d4e92008-04-24 23:35:58 +0000266 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000267}
268
269void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
270
271 // FIXME: add metadata to the CFG so that we can disable
272 // this check when we KNOW that there is no block-level subexpression.
273 // The motivation is that this check requires a hashtable lookup.
274
275 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
276 Dst.Add(Pred);
277 return;
278 }
279
280 switch (S->getStmtClass()) {
281
282 default:
283 // Cases we intentionally have "default" handle:
284 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
285
286 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
287 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000288
289 case Stmt::ArraySubscriptExprClass:
290 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
291 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000292
293 case Stmt::AsmStmtClass:
294 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
295 break;
296
297 case Stmt::BinaryOperatorClass: {
298 BinaryOperator* B = cast<BinaryOperator>(S);
299
300 if (B->isLogicalOp()) {
301 VisitLogicalExpr(B, Pred, Dst);
302 break;
303 }
304 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000305 const GRState* St = GetState(Pred);
Zhongxing 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 }
309
310 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
311 break;
312 }
313
314 case Stmt::CallExprClass: {
315 CallExpr* C = cast<CallExpr>(S);
316 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
317 break;
318 }
319
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000320 // FIXME: ChooseExpr is really a constant. We need to fix
321 // the CFG do not model them as explicit control-flow.
322
323 case Stmt::ChooseExprClass: { // __builtin_choose_expr
324 ChooseExpr* C = cast<ChooseExpr>(S);
325 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
326 break;
327 }
328
329 case Stmt::CompoundAssignOperatorClass:
330 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
331 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000332
333 case Stmt::CompoundLiteralExprClass:
334 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
335 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000336
337 case Stmt::ConditionalOperatorClass: { // '?' operator
338 ConditionalOperator* C = cast<ConditionalOperator>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000344 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000345 break;
346
347 case Stmt::DeclStmtClass:
348 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
349 break;
350
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000351 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000352 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000353 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000354 VisitCast(C, C->getSubExpr(), Pred, Dst);
355 break;
356 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000357
358 case Stmt::InitListExprClass:
359 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
360 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000361
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000362 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000363 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
364 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000365
366 case Stmt::ObjCIvarRefExprClass:
367 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
368 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000369
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000370 case Stmt::ObjCMessageExprClass: {
371 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
372 break;
373 }
374
375 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000376 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000377 break;
378
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000379 case Stmt::ReturnStmtClass:
380 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
381 break;
382
Sebastian Redl05189992008-11-11 17:56:53 +0000383 case Stmt::SizeOfAlignOfExprClass:
384 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000385 break;
386
387 case Stmt::StmtExprClass: {
388 StmtExpr* SE = cast<StmtExpr>(S);
389
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000390 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000391
392 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
393 // probably just remove these from the CFG.
394 assert (!SE->getSubStmt()->body_empty());
395
396 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000397 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000398 else
399 Dst.Add(Pred);
400
401 break;
402 }
403
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000404 case Stmt::UnaryOperatorClass:
405 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000406 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000407 }
408}
409
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000410void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000411
412 Ex = Ex->IgnoreParens();
413
414 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
415 Dst.Add(Pred);
416 return;
417 }
418
419 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000420
421 case Stmt::ArraySubscriptExprClass:
422 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
423 return;
424
425 case Stmt::DeclRefExprClass:
426 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
427 return;
428
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000429 case Stmt::ObjCIvarRefExprClass:
430 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
431 return;
432
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000433 case Stmt::UnaryOperatorClass:
434 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
435 return;
436
437 case Stmt::MemberExprClass:
438 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
439 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000440
Ted Kremenek4f090272008-10-27 21:54:31 +0000441 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000442 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000443 return;
444
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000445 case Stmt::ObjCPropertyRefExprClass:
446 // FIXME: Property assignments are lvalues, but not really "locations".
447 // e.g.: self.x = something;
448 // Here the "self.x" really can translate to a method call (setter) when
449 // the assignment is made. Moreover, the entire assignment expression
450 // evaluate to whatever "something" is, not calling the "getter" for
451 // the property (which would make sense since it can have side effects).
452 // We'll probably treat this as a location, but not one that we can
453 // take the address of. Perhaps we need a new SVal class for cases
454 // like thsis?
455 // Note that we have a similar problem for bitfields, since they don't
456 // have "locations" in the sense that we can take their address.
457 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000458 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000459
460 case Stmt::StringLiteralClass: {
461 const GRState* St = GetState(Pred);
462 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000463 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000464 return;
465 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000466
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000467 default:
468 // Arbitrary subexpressions can return aggregate temporaries that
469 // can be used in a lvalue context. We need to enhance our support
470 // of such temporaries in both the environment and the store, so right
471 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000472 assert ((Ex->getType()->isAggregateType() ||
473 Ex->getType()->isUnionType()) &&
474 "Other kinds of expressions with non-aggregate/union types do"
475 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000476
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000477 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000478 }
479}
480
481//===----------------------------------------------------------------------===//
482// Block entrance. (Update counters).
483//===----------------------------------------------------------------------===//
484
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000485bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000486 GRBlockCounter BC) {
487
488 return BC.getNumVisited(B->getBlockID()) < 3;
489}
490
491//===----------------------------------------------------------------------===//
492// Branch processing.
493//===----------------------------------------------------------------------===//
494
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000495const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000496 Stmt* Terminator,
497 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000498
499 switch (Terminator->getStmtClass()) {
500 default:
501 return St;
502
503 case Stmt::BinaryOperatorClass: { // '&&' and '||'
504
505 BinaryOperator* B = cast<BinaryOperator>(Terminator);
506 BinaryOperator::Opcode Op = B->getOpcode();
507
508 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
509
510 // For &&, if we take the true branch, then the value of the whole
511 // expression is that of the RHS expression.
512 //
513 // For ||, if we take the false branch, then the value of the whole
514 // expression is that of the RHS expression.
515
516 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
517 (Op == BinaryOperator::LOr && !branchTaken)
518 ? B->getRHS() : B->getLHS();
519
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000520 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000521 }
522
523 case Stmt::ConditionalOperatorClass: { // ?:
524
525 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
526
527 // For ?, if branchTaken == true then the value is either the LHS or
528 // the condition itself. (GNU extension).
529
530 Expr* Ex;
531
532 if (branchTaken)
533 Ex = C->getLHS() ? C->getLHS() : C->getCond();
534 else
535 Ex = C->getRHS();
536
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000537 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000538 }
539
540 case Stmt::ChooseExprClass: { // ?:
541
542 ChooseExpr* C = cast<ChooseExpr>(Terminator);
543
544 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000545 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000546 }
547 }
548}
549
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000550void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000551 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000552
Ted Kremeneke7d22112008-02-11 19:21:59 +0000553 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000554 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000555 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000556
Ted Kremenekb2331832008-02-15 22:29:00 +0000557 // Check for NULL conditions; e.g. "for(;;)"
558 if (!Condition) {
559 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000560 return;
561 }
562
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000563 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000564
565 switch (V.getBaseKind()) {
566 default:
567 break;
568
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000569 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000570 builder.generateNode(MarkBranch(PrevState, Term, true), true);
571 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000572 return;
573
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000574 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000575 NodeTy* N = builder.generateNode(PrevState, true);
576
577 if (N) {
578 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000579 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000580 }
581
582 builder.markInfeasible(false);
583 return;
584 }
585 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000586
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000587 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000588
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000589 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000590 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000591
592 if (isFeasible)
593 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000594 else
595 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000596
597 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000598
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000599 isFeasible = false;
600 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000601
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000602 if (isFeasible)
603 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000604 else
605 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000606}
607
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000608/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000609/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000610void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000611
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000612 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000613 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000614
615 // Three possibilities:
616 //
617 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000618 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000619 // (3) We have no clue about the label. Dispatch to all targets.
620 //
621
622 typedef IndirectGotoNodeBuilder::iterator iterator;
623
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000624 if (isa<loc::GotoLabel>(V)) {
625 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000626
627 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000628 if (I.getLabel() == L) {
629 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000630 return;
631 }
632 }
633
634 assert (false && "No block with label.");
635 return;
636 }
637
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000638 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000639 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000640 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000641 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000642 return;
643 }
644
645 // This is really a catch-all. We don't support symbolics yet.
646
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000647 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000648
649 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000650 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000651}
Ted Kremenekf233d482008-02-05 00:26:40 +0000652
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000653
654void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
655 NodeTy* Pred, NodeSet& Dst) {
656
657 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
658
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000659 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000660 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000661
662 assert (X.isUndef());
663
664 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
665
666 assert (SE);
667
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000668 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000669
670 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000671 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000672}
673
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000674/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
675/// nodes by processing the 'effects' of a switch statement.
676void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
677
678 typedef SwitchNodeBuilder::iterator iterator;
679
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000680 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000681 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000682 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000683
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000684 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000685 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000686 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000687 return;
688 }
689
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000690 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000691
692 // While most of this can be assumed (such as the signedness), having it
693 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000694
Chris Lattner98be4942008-03-05 18:54:05 +0000695 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000696
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000697 APSInt V1(bits, false);
698 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000699 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000700
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000701 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000702
703 CaseStmt* Case = cast<CaseStmt>(I.getCase());
704
705 // Evaluate the case.
706 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
707 assert (false && "Case condition must evaluate to an integer constant.");
708 return;
709 }
710
711 // Get the RHS of the case, if it exists.
712
713 if (Expr* E = Case->getRHS()) {
714 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
715 assert (false &&
716 "Case condition (RHS) must evaluate to an integer constant.");
717 return ;
718 }
719
720 assert (V1 <= V2);
721 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000722 else
723 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000724
725 // FIXME: Eventually we should replace the logic below with a range
726 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000727 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000728
Ted Kremenek14a11402008-03-17 22:17:56 +0000729 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000730 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000731
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000732 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000733
734 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000735
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000736 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000737 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738
739 if (isFeasible) {
740 builder.generateCaseStmtNode(I, StNew);
741
742 // If CondV evaluates to a constant, then we know that this
743 // is the *only* case that we can take, so stop evaluating the
744 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000745 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000746 return;
747 }
748
749 // Now "assume" that the case doesn't match. Add this state
750 // to the default state (if it is feasible).
751
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000752 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000753 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000754
Ted Kremenek5014ab12008-04-23 05:03:18 +0000755 if (isFeasible) {
756 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000757 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000758 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759
Ted Kremenek14a11402008-03-17 22:17:56 +0000760 // Concretize the next value in the range.
761 if (V1 == V2)
762 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000763
Ted Kremenek14a11402008-03-17 22:17:56 +0000764 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000765 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000766
767 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000768 }
769
770 // If we reach here, than we know that the default branch is
771 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000772 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000773}
774
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000775//===----------------------------------------------------------------------===//
776// Transfer functions: logical operations ('&&', '||').
777//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000778
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000779void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000780 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000781
Ted Kremenek05a23782008-02-26 19:05:15 +0000782 assert (B->getOpcode() == BinaryOperator::LAnd ||
783 B->getOpcode() == BinaryOperator::LOr);
784
785 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
786
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000787 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000788 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000789
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000790 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000791
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000792 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000793
794 assert (Ex);
795
796 if (Ex == B->getRHS()) {
797
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000798 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000799
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000800 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000801
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000802 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000803 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000804 return;
805 }
806
Ted Kremenek05a23782008-02-26 19:05:15 +0000807 // We took the RHS. Because the value of the '&&' or '||' expression must
808 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
809 // or 1. Alternatively, we could take a lazy approach, and calculate this
810 // value later when necessary. We don't have the machinery in place for
811 // this right now, and since most logical expressions are used for branches,
812 // the payoff is not likely to be large. Instead, we do eager evaluation.
813
814 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000815 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000816
817 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000818 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000819 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000820
821 isFeasible = false;
822 NewState = Assume(St, X, false, isFeasible);
823
824 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000825 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000826 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000827 }
828 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000829 // We took the LHS expression. Depending on whether we are '&&' or
830 // '||' we know what the value of the expression is via properties of
831 // the short-circuiting.
832
833 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000834 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000835 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000836}
Ted Kremenek05a23782008-02-26 19:05:15 +0000837
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000838//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000839// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000840//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000841
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000842void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
843 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000844
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000845 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000846
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000847 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000848
849 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
850
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000851 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000852
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000853 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000854 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000855 else
856 EvalLoad(Dst, Ex, Pred, St, V);
857 return;
858
859 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
860 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
861
862 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000863 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000864 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000865 return;
866
867 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekcd162cc2008-10-17 00:55:33 +0000868 // FIXME: Does this need to be revised? We were getting cases in
869 // real code that did this.
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000870
Ted Kremeneke580c1b2008-10-31 15:33:11 +0000871 // FIXME: This is not a valid assertion. Produce a test case that
872 // refutes it.
873 // assert(asLValue); // Can we assume this?
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000874
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000875 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000876 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000877 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000878 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000879
880 assert (false &&
881 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000882}
883
Ted Kremenek540cbe22008-04-22 04:56:29 +0000884/// VisitArraySubscriptExpr - Transfer function for array accesses
885void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000886 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000887
888 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000889 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000890 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000891 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000892
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000893 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000894 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000895 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000896
897 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000898 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000899 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000900
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000901 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000902 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000903 else
904 EvalLoad(Dst, A, *I2, St, V);
905 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000906 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000907}
908
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000909/// VisitMemberExpr - Transfer function for member expressions.
910void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000911 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000912
913 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000914 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000915
916 if (M->isArrow())
917 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
918 else
919 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
920
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000921 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000922 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000923 // FIXME: Should we insert some assumption logic in here to determine
924 // if "Base" is a valid piece of memory? Before we put this assumption
925 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000926 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000927
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000928 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000929 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000930 else
931 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000932 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000933}
934
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000935void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000936 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000937
938 assert (Builder && "GRStmtNodeBuilder must be defined.");
939
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000940 // Evaluate the location (checks for bad dereferences).
941 St = EvalLocation(Ex, Pred, St, location);
942
943 if (!St)
944 return;
945
946 // Proceed with the store.
947
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000948 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000949
Ted Kremenek186350f2008-04-23 20:12:28 +0000950 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000951 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000952 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000953
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000954 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000955 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000956
Ted Kremenek729a9a22008-07-17 23:15:45 +0000957 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000958
959 // Handle the case where no nodes where generated. Auto-generate that
960 // contains the updated state if we aren't generating sinks.
961
Ted Kremenekb0533962008-04-18 20:35:30 +0000962 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000963 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000964 location, Val);
965}
966
967void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000968 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000969 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000970
971 // Evaluate the location (checks for bad dereferences).
972
973 St = EvalLocation(Ex, Pred, St, location, true);
974
975 if (!St)
976 return;
977
978 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000979 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000980
981 // FIXME: Currently symbolic analysis "generates" new symbols
982 // for the contents of values. We need a better approach.
983
984 // FIXME: The "CheckOnly" option exists only because Array and Field
985 // loads aren't fully implemented. Eventually this option will go away.
Ted Kremenek982e6742008-08-28 18:43:46 +0000986
Ted Kremenek436f2b92008-04-30 04:23:07 +0000987 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000988 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000989 else if (location.isUnknown()) {
990 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000991 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000992 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000993 else
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000994 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, GetSVal(St, cast<Loc>(location),
995 Ex->getType())), K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000996}
997
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000998void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000999 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001000
1001 NodeSet TmpDst;
1002 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1003
1004 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1005 MakeNode(Dst, Ex, *I, (*I)->getState());
1006}
1007
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001008const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001009 const GRState* St,
1010 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001011
1012 // Check for loads/stores from/to undefined values.
1013 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001014 ProgramPoint::Kind K =
1015 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1016
1017 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001018 Succ->markAsSink();
1019 UndefDeref.insert(Succ);
1020 }
1021
1022 return NULL;
1023 }
1024
1025 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1026 if (location.isUnknown())
1027 return St;
1028
1029 // During a load, one of two possible situations arise:
1030 // (1) A crash, because the location (pointer) was NULL.
1031 // (2) The location (pointer) is not NULL, and the dereference works.
1032 //
1033 // We add these assumptions.
1034
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001035 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001036
1037 // "Assume" that the pointer is not NULL.
1038
1039 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001040 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001041
1042 // "Assume" that the pointer is NULL.
1043
1044 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001045 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1046 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001047
1048 if (isFeasibleNull) {
1049
Ted Kremenek7360fda2008-09-18 23:09:54 +00001050 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001051 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001052 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1053
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001054 // We don't use "MakeNode" here because the node will be a sink
1055 // and we have no intention of processing it later.
1056
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001057 ProgramPoint::Kind K =
1058 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1059
1060 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001061
1062 if (NullNode) {
1063
1064 NullNode->markAsSink();
1065
1066 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1067 else ExplicitNullDeref.insert(NullNode);
1068 }
1069 }
Zhongxing Xu60156f02008-11-08 03:45:42 +00001070
1071 // Check for out-of-bound array access.
1072 if (isFeasibleNotNull && isa<loc::MemRegionVal>(LV)) {
1073 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1074 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1075 // Get the index of the accessed element.
1076 SVal Idx = ER->getIndex();
1077 // Get the extent of the array.
1078 SVal NumElements = StateMgr.getStoreManager().getSizeInElements(StNotNull,
1079 ER->getSuperRegion());
1080
1081 bool isFeasibleInBound = false;
1082 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1083 true, isFeasibleInBound);
1084
1085 bool isFeasibleOutBound = false;
1086 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1087 false, isFeasibleOutBound);
Chris Lattner8341be72008-11-10 03:00:37 +00001088 StInBound = StOutBound = 0; // FIXME: squeltch warning.
Zhongxing Xu60156f02008-11-08 03:45:42 +00001089
1090 // Report warnings ...
1091 }
1092 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001093
1094 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001095}
1096
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001097//===----------------------------------------------------------------------===//
1098// Transfer function: Function calls.
1099//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001100void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001101 CallExpr::arg_iterator AI,
1102 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001103 NodeSet& Dst)
1104{
1105 // Determine the type of function we're calling (if available).
1106 const FunctionTypeProto *Proto = NULL;
1107 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1108 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1109 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1110
1111 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1112}
1113
1114void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1115 CallExpr::arg_iterator AI,
1116 CallExpr::arg_iterator AE,
1117 NodeSet& Dst, const FunctionTypeProto *Proto,
1118 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001119
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001120 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001121 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001122 // If the call argument is being bound to a reference parameter,
1123 // visit it as an lvalue, not an rvalue.
1124 bool VisitAsLvalue = false;
1125 if (Proto && ParamIdx < Proto->getNumArgs())
1126 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1127
1128 NodeSet DstTmp;
1129 if (VisitAsLvalue)
1130 VisitLValue(*AI, Pred, DstTmp);
1131 else
1132 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001133 ++AI;
1134
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001135 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001136 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001137
1138 return;
1139 }
1140
1141 // If we reach here we have processed all of the arguments. Evaluate
1142 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001143
Ted Kremenek994a09b2008-02-25 21:16:03 +00001144 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001145 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001146
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001147 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001148
Ted Kremenekde434242008-02-19 01:44:53 +00001149 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001150 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1151
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001152 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001153 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001154
Ted Kremeneka1354a52008-03-03 16:47:31 +00001155 // FIXME: Add support for symbolic function calls (calls involving
1156 // function pointer values that are symbolic).
1157
1158 // Check for undefined control-flow or calls to NULL.
1159
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001160 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001161 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001162
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001163 if (N) {
1164 N->markAsSink();
1165 BadCalls.insert(N);
1166 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001167
Ted Kremenekde434242008-02-19 01:44:53 +00001168 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001169 }
1170
1171 // Check for the "noreturn" attribute.
1172
1173 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1174
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001175 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001176
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001177 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001178
1179 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001180 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001181 else {
1182 // HACK: Some functions are not marked noreturn, and don't return.
1183 // Here are a few hardwired ones. If this takes too long, we can
1184 // potentially cache these results.
1185 const char* s = FD->getIdentifier()->getName();
1186 unsigned n = strlen(s);
1187
1188 switch (n) {
1189 default:
1190 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001191
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001192 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001193 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1194 break;
1195
1196 case 5:
1197 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001198 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001199 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001200 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001201 // FIXME: use Assume to inspect the possible symbolic value of
1202 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001203 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001204 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001205 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001206 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001207 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001208 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001209
1210 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001211 if (!memcmp(s, "Assert", 6)) {
1212 Builder->BuildSinks = true;
1213 break;
1214 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001215
1216 // FIXME: This is just a wrapper around throwing an exception.
1217 // Eventually inter-procedural analysis should handle this easily.
1218 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1219
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001220 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001221
1222 case 7:
1223 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1224 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001225
Ted Kremenekf47bb782008-04-30 17:54:04 +00001226 case 8:
1227 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1228 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001229
1230 case 12:
1231 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1232 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001233
Ted Kremenekf9683082008-09-19 02:30:47 +00001234 case 13:
1235 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1236 break;
1237
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001238 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001239 if (!memcmp(s, "dtrace_assfail", 14) ||
1240 !memcmp(s, "yy_fatal_error", 14))
1241 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001242 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001243
1244 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001245 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1246 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001247 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001248
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001249 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001250 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001251
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001252 }
1253 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001254
1255 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001256
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001257 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001258
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001259 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001260
Ted Kremenek186350f2008-04-23 20:12:28 +00001261 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001262 switch (id) {
1263 case Builtin::BI__builtin_expect: {
1264 // For __builtin_expect, just return the value of the subexpression.
1265 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001266 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001267 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001268 continue;
1269 }
1270
Ted Kremenekb3021332008-11-02 00:35:01 +00001271 case Builtin::BI__builtin_alloca: {
1272 // FIXME: Handle size.
1273 // FIXME: Refactor into StoreManager itself?
1274 MemRegionManager& RM = getStateManager().getRegionManager();
1275 const MemRegion* R =
1276 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
1277 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1278 continue;
1279 }
1280
Ted Kremenek55aea312008-03-05 22:59:42 +00001281 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001282 break;
1283 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001284 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001285
Ted Kremenek186350f2008-04-23 20:12:28 +00001286 // Check any arguments passed-by-value against being undefined.
1287
1288 bool badArg = false;
1289
1290 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1291 I != E; ++I) {
1292
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001293 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001294 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001295
Ted Kremenek186350f2008-04-23 20:12:28 +00001296 if (N) {
1297 N->markAsSink();
1298 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001299 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001300
Ted Kremenek186350f2008-04-23 20:12:28 +00001301 badArg = true;
1302 break;
1303 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001304 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001305
1306 if (badArg)
1307 continue;
1308
1309 // Dispatch to the plug-in transfer function.
1310
1311 unsigned size = Dst.size();
1312 SaveOr OldHasGen(Builder->HasGeneratedNode);
1313 EvalCall(Dst, CE, L, *DI);
1314
1315 // Handle the case where no nodes where generated. Auto-generate that
1316 // contains the updated state if we aren't generating sinks.
1317
1318 if (!Builder->BuildSinks && Dst.size() == size &&
1319 !Builder->HasGeneratedNode)
1320 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001321 }
1322}
1323
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001324//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001325// Transfer function: Objective-C ivar references.
1326//===----------------------------------------------------------------------===//
1327
1328void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1329 NodeTy* Pred, NodeSet& Dst,
1330 bool asLValue) {
1331
1332 Expr* Base = cast<Expr>(Ex->getBase());
1333 NodeSet Tmp;
1334 Visit(Base, Pred, Tmp);
1335
1336 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1337 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001338 SVal BaseVal = GetSVal(St, Base);
1339 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001340
1341 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001342 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001343 else
1344 EvalLoad(Dst, Ex, *I, St, location);
1345 }
1346}
1347
1348//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001349// Transfer function: Objective-C message expressions.
1350//===----------------------------------------------------------------------===//
1351
1352void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1353 NodeSet& Dst){
1354
1355 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1356 Pred, Dst);
1357}
1358
1359void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001360 ObjCMessageExpr::arg_iterator AI,
1361 ObjCMessageExpr::arg_iterator AE,
1362 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001363 if (AI == AE) {
1364
1365 // Process the receiver.
1366
1367 if (Expr* Receiver = ME->getReceiver()) {
1368 NodeSet Tmp;
1369 Visit(Receiver, Pred, Tmp);
1370
1371 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1372 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1373
1374 return;
1375 }
1376
1377 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1378 return;
1379 }
1380
1381 NodeSet Tmp;
1382 Visit(*AI, Pred, Tmp);
1383
1384 ++AI;
1385
1386 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1387 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1388}
1389
1390void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1391 NodeTy* Pred,
1392 NodeSet& Dst) {
1393
1394 // FIXME: More logic for the processing the method call.
1395
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001396 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001397 bool RaisesException = false;
1398
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001399
1400 if (Expr* Receiver = ME->getReceiver()) {
1401
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001402 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001403
1404 // Check for undefined control-flow or calls to NULL.
1405
1406 if (L.isUndef()) {
1407 NodeTy* N = Builder->generateNode(ME, St, Pred);
1408
1409 if (N) {
1410 N->markAsSink();
1411 UndefReceivers.insert(N);
1412 }
1413
1414 return;
1415 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001416
1417 // Check if the "raise" message was sent.
1418 if (ME->getSelector() == RaiseSel)
1419 RaisesException = true;
1420 }
1421 else {
1422
1423 IdentifierInfo* ClsName = ME->getClassName();
1424 Selector S = ME->getSelector();
1425
1426 // Check for special instance methods.
1427
1428 if (!NSExceptionII) {
1429 ASTContext& Ctx = getContext();
1430
1431 NSExceptionII = &Ctx.Idents.get("NSException");
1432 }
1433
1434 if (ClsName == NSExceptionII) {
1435
1436 enum { NUM_RAISE_SELECTORS = 2 };
1437
1438 // Lazily create a cache of the selectors.
1439
1440 if (!NSExceptionInstanceRaiseSelectors) {
1441
1442 ASTContext& Ctx = getContext();
1443
1444 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1445
1446 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1447 unsigned idx = 0;
1448
1449 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001450 II.push_back(&Ctx.Idents.get("raise"));
1451 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001452 NSExceptionInstanceRaiseSelectors[idx++] =
1453 Ctx.Selectors.getSelector(II.size(), &II[0]);
1454
1455 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001456 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001457 NSExceptionInstanceRaiseSelectors[idx++] =
1458 Ctx.Selectors.getSelector(II.size(), &II[0]);
1459 }
1460
1461 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1462 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1463 RaisesException = true; break;
1464 }
1465 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001466 }
1467
1468 // Check for any arguments that are uninitialized/undefined.
1469
1470 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1471 I != E; ++I) {
1472
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001473 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001474
1475 // Generate an error node for passing an uninitialized/undefined value
1476 // as an argument to a message expression. This node is a sink.
1477 NodeTy* N = Builder->generateNode(ME, St, Pred);
1478
1479 if (N) {
1480 N->markAsSink();
1481 MsgExprUndefArgs[N] = *I;
1482 }
1483
1484 return;
1485 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001486 }
1487
1488 // Check if we raise an exception. For now treat these as sinks. Eventually
1489 // we will want to handle exceptions properly.
1490
1491 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1492
1493 if (RaisesException)
1494 Builder->BuildSinks = true;
1495
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001496 // Dispatch to plug-in transfer function.
1497
1498 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001499 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001500
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001501 EvalObjCMessageExpr(Dst, ME, Pred);
1502
1503 // Handle the case where no nodes where generated. Auto-generate that
1504 // contains the updated state if we aren't generating sinks.
1505
Ted Kremenekb0533962008-04-18 20:35:30 +00001506 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001507 MakeNode(Dst, ME, Pred, St);
1508}
1509
1510//===----------------------------------------------------------------------===//
1511// Transfer functions: Miscellaneous statements.
1512//===----------------------------------------------------------------------===//
1513
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001514void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001515 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001516 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001517 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001518
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001519 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001520 T = ExCast->getTypeAsWritten();
1521
Zhongxing Xued340f72008-10-22 08:02:16 +00001522 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001523 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001524 else
1525 Visit(Ex, Pred, S1);
1526
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001527 // Check for casting to "void".
1528 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001529
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001530 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001531 Dst.Add(*I1);
1532
Ted Kremenek874d63f2008-01-24 02:02:54 +00001533 return;
1534 }
1535
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001536 // FIXME: The rest of this should probably just go into EvalCall, and
1537 // let the transfer function object be responsible for constructing
1538 // nodes.
1539
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001540 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001541 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001542 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001543 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001544
1545 // Unknown?
1546
1547 if (V.isUnknown()) {
1548 Dst.Add(N);
1549 continue;
1550 }
1551
1552 // Undefined?
1553
1554 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001555 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001556 continue;
1557 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001558
1559 // For const casts, just propagate the value.
1560 ASTContext& C = getContext();
1561
1562 if (C.getCanonicalType(T).getUnqualifiedType() ==
1563 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001564 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001565 continue;
1566 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001567
1568 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001569 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001570 unsigned bits = getContext().getTypeSize(ExTy);
1571
1572 // FIXME: Determine if the number of bits of the target type is
1573 // equal or exceeds the number of bits to store the pointer value.
1574 // If not, flag an error.
1575
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001576 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001577 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001578 continue;
1579 }
1580
1581 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001582 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1583 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001584 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001585 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001586 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001587 continue;
1588 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001589
1590 // StoreManager casts array to different values.
1591 if (ExTy->isArrayType()) {
Ted Kremenek0b50f5b2008-10-24 21:10:49 +00001592 assert(T->isPointerType() || T->isReferenceType());
Zhongxing Xue564b522008-10-23 04:19:25 +00001593
Zhongxing Xue1911af2008-10-23 03:10:39 +00001594 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001595 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001596 continue;
1597 }
1598
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001599 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001600 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001601 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001602}
1603
Ted Kremenek4f090272008-10-27 21:54:31 +00001604void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001605 NodeTy* Pred, NodeSet& Dst,
1606 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001607 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1608 NodeSet Tmp;
1609 Visit(ILE, Pred, Tmp);
1610
1611 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001612 const GRState* St = GetState(*I);
1613 SVal ILV = GetSVal(St, ILE);
1614 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001615
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001616 if (asLValue)
1617 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1618 else
1619 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001620 }
1621}
1622
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001623void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001624
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001625 // The CFG has one DeclStmt per Decl.
1626 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001627
1628 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001629 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001630
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001631 const VarDecl* VD = dyn_cast<VarDecl>(D);
1632
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001633 Expr* Ex = const_cast<Expr*>(VD->getInit());
1634
1635 // FIXME: static variables may have an initializer, but the second
1636 // time a function is called those values may not be current.
1637 NodeSet Tmp;
1638
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001639 if (Ex)
1640 Visit(Ex, Pred, Tmp);
1641
1642 if (Tmp.empty())
1643 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001644
1645 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001646 const GRState* St = GetState(*I);
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +00001647 St = StateMgr.BindDecl(St, VD, Ex, Builder->getCurrentBlockCount());
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001648 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001649 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001650}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001651
Ted Kremenekf75b1862008-10-30 17:47:32 +00001652namespace {
1653 // This class is used by VisitInitListExpr as an item in a worklist
1654 // for processing the values contained in an InitListExpr.
1655class VISIBILITY_HIDDEN InitListWLItem {
1656public:
1657 llvm::ImmutableList<SVal> Vals;
1658 GRExprEngine::NodeTy* N;
1659 InitListExpr::reverse_iterator Itr;
1660
1661 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1662 InitListExpr::reverse_iterator itr)
1663 : Vals(vals), N(n), Itr(itr) {}
1664};
1665}
1666
1667
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001668void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1669 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001670
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001671 const GRState* state = GetState(Pred);
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001672 QualType T = E->getType();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001673 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001674
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001675 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001676
Ted Kremeneka49e3672008-10-30 23:14:36 +00001677 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001678
Ted Kremeneka49e3672008-10-30 23:14:36 +00001679 // Handle base case where the initializer has no elements.
1680 // e.g: static int* myArray[] = {};
1681 if (NumInitElements == 0) {
1682 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1683 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1684 return;
1685 }
1686
1687 // Create a worklist to process the initializers.
1688 llvm::SmallVector<InitListWLItem, 10> WorkList;
1689 WorkList.reserve(NumInitElements);
1690 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001691 InitListExpr::reverse_iterator ItrEnd = E->rend();
1692
Ted Kremeneka49e3672008-10-30 23:14:36 +00001693 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001694 while (!WorkList.empty()) {
1695 InitListWLItem X = WorkList.back();
1696 WorkList.pop_back();
1697
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001698 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001699 Visit(*X.Itr, X.N, Tmp);
1700
1701 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001702
Ted Kremenekf75b1862008-10-30 17:47:32 +00001703 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1704 // Get the last initializer value.
1705 state = GetState(*NI);
1706 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1707
1708 // Construct the new list of values by prepending the new value to
1709 // the already constructed list.
1710 llvm::ImmutableList<SVal> NewVals =
1711 getBasicVals().consVals(InitV, X.Vals);
1712
1713 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001714 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001715 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001716
Ted Kremenekf75b1862008-10-30 17:47:32 +00001717 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001718 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001719 }
1720 else {
1721 // Still some initializer values to go. Push them onto the worklist.
1722 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1723 }
1724 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001725 }
Ted Kremenek87903072008-10-30 18:34:31 +00001726
1727 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001728 }
1729
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001730 if (Loc::IsLocType(T) || T->isIntegerType()) {
1731 assert (E->getNumInits() == 1);
1732 NodeSet Tmp;
1733 Expr* Init = E->getInit(0);
1734 Visit(Init, Pred, Tmp);
1735 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1736 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001737 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001738 }
1739 return;
1740 }
1741
1742 if (T->isUnionType()) {
1743 // FIXME: to be implemented.
1744 MakeNode(Dst, E, Pred, state);
1745 return;
1746 }
1747
1748 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1749 assert(0 && "unprocessed InitListExpr type");
1750}
Ted Kremenekf233d482008-02-05 00:26:40 +00001751
Sebastian Redl05189992008-11-11 17:56:53 +00001752/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1753void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1754 NodeTy* Pred,
1755 NodeSet& Dst) {
1756 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001757 uint64_t amt;
1758
1759 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001760
Ted Kremenek87e80342008-03-15 03:13:20 +00001761 // FIXME: Add support for VLAs.
1762 if (!T.getTypePtr()->isConstantSizeType())
1763 return;
1764
Ted Kremenekf342d182008-04-30 21:31:12 +00001765 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1766 // the compiler has laid out its representation. Just report Unknown
1767 // for these.
1768 if (T->isObjCInterfaceType())
1769 return;
1770
Ted Kremenek87e80342008-03-15 03:13:20 +00001771 amt = 1; // Handle sizeof(void)
1772
1773 if (T != getContext().VoidTy)
1774 amt = getContext().getTypeSize(T) / 8;
1775
1776 }
1777 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001778 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001779
Ted Kremenek0e561a32008-03-21 21:30:14 +00001780 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001781 BindExpr(GetState(Pred), Ex,
1782 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001783}
1784
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001785
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001786void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001787 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001788
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001789 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001790
1791 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001792 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001793
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001794 case UnaryOperator::Deref: {
1795
1796 Expr* Ex = U->getSubExpr()->IgnoreParens();
1797 NodeSet Tmp;
1798 Visit(Ex, Pred, Tmp);
1799
1800 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001801
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001802 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001803 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001804
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001805 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001806 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001807 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001808 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001809 }
1810
1811 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001812 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001813
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001814 case UnaryOperator::Real: {
1815
1816 Expr* Ex = U->getSubExpr()->IgnoreParens();
1817 NodeSet Tmp;
1818 Visit(Ex, Pred, Tmp);
1819
1820 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1821
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001822 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001823 if (Ex->getType()->isAnyComplexType()) {
1824 // Just report "Unknown."
1825 Dst.Add(*I);
1826 continue;
1827 }
1828
1829 // For all other types, UnaryOperator::Real is an identity operation.
1830 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001831 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001832 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001833 }
1834
1835 return;
1836 }
1837
1838 case UnaryOperator::Imag: {
1839
1840 Expr* Ex = U->getSubExpr()->IgnoreParens();
1841 NodeSet Tmp;
1842 Visit(Ex, Pred, Tmp);
1843
1844 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001845 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001846 if (Ex->getType()->isAnyComplexType()) {
1847 // Just report "Unknown."
1848 Dst.Add(*I);
1849 continue;
1850 }
1851
1852 // For all other types, UnaryOperator::Float returns 0.
1853 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001854 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001855 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001856 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001857 }
1858
1859 return;
1860 }
1861
1862 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001863 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001864 Dst.Add(Pred);
1865 return;
1866
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001867 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001868 case UnaryOperator::Extension: {
1869
1870 // Unary "+" is a no-op, similar to a parentheses. We still have places
1871 // where it may be a block-level expression, so we need to
1872 // generate an extra node that just propagates the value of the
1873 // subexpression.
1874
1875 Expr* Ex = U->getSubExpr()->IgnoreParens();
1876 NodeSet Tmp;
1877 Visit(Ex, Pred, Tmp);
1878
1879 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001880 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001881 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001882 }
1883
1884 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001885 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001886
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001887 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001888
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001889 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001890 Expr* Ex = U->getSubExpr()->IgnoreParens();
1891 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001892 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001893
1894 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001895 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001896 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001897 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001898 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001899 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001900
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001901 return;
1902 }
1903
1904 case UnaryOperator::LNot:
1905 case UnaryOperator::Minus:
1906 case UnaryOperator::Not: {
1907
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001908 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001909 Expr* Ex = U->getSubExpr()->IgnoreParens();
1910 NodeSet Tmp;
1911 Visit(Ex, Pred, Tmp);
1912
1913 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001914 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00001915
1916 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001917 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00001918
1919 // Perform promotions.
Ted Kremenek5a236cb2008-09-30 05:35:42 +00001920 // FIXME: This is the right thing to do, but it currently breaks
1921 // a bunch of tests.
1922 // V = EvalCast(V, U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001923
1924 if (V.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001925 MakeNode(Dst, U, *I, BindExpr(St, U, V));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001926 continue;
1927 }
1928
1929 switch (U->getOpcode()) {
1930 default:
1931 assert(false && "Invalid Opcode.");
1932 break;
1933
1934 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00001935 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001936 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001937 break;
1938
1939 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00001940 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001941 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001942 break;
1943
1944 case UnaryOperator::LNot:
1945
1946 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1947 //
1948 // Note: technically we do "E == 0", but this is the same in the
1949 // transfer functions as "0 == E".
1950
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001951 if (isa<Loc>(V)) {
1952 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
1953 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001954 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001955 }
1956 else {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001957 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001958#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001959 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
1960 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001961#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001962 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001963 continue;
1964#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001965 }
1966
1967 break;
1968 }
1969
1970 MakeNode(Dst, U, *I, St);
1971 }
1972
1973 return;
1974 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001975 }
1976
1977 // Handle ++ and -- (both pre- and post-increment).
1978
1979 assert (U->isIncrementDecrementOp());
1980 NodeSet Tmp;
1981 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001982 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001983
1984 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1985
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001986 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001987 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001988
1989 // Perform a load.
1990 NodeSet Tmp2;
1991 EvalLoad(Tmp2, Ex, *I, St, V1);
1992
1993 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1994
1995 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001996 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001997
1998 // Propagate unknown and undefined values.
1999 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002000 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002001 continue;
2002 }
2003
Ted Kremenek443003b2008-02-21 19:29:23 +00002004 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002005
2006 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2007 : BinaryOperator::Sub;
2008
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002009 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002010 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002011
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002012 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002013 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002014 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002015 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002016}
2017
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002018void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2019 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2020}
2021
2022void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2023 AsmStmt::outputs_iterator I,
2024 AsmStmt::outputs_iterator E,
2025 NodeTy* Pred, NodeSet& Dst) {
2026 if (I == E) {
2027 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2028 return;
2029 }
2030
2031 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002032 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002033
2034 ++I;
2035
2036 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2037 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2038}
2039
2040void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2041 AsmStmt::inputs_iterator I,
2042 AsmStmt::inputs_iterator E,
2043 NodeTy* Pred, NodeSet& Dst) {
2044 if (I == E) {
2045
2046 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002047 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002048
2049 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2050 // which interprets the inline asm and stores proper results in the
2051 // outputs.
2052
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002053 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002054
2055 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2056 OE = A->end_outputs(); OI != OE; ++OI) {
2057
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002058 SVal X = GetSVal(St, *OI);
2059 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002060
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002061 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002062 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002063 }
2064
Ted Kremenek0e561a32008-03-21 21:30:14 +00002065 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002066 return;
2067 }
2068
2069 NodeSet Tmp;
2070 Visit(*I, Pred, Tmp);
2071
2072 ++I;
2073
2074 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2075 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2076}
2077
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002078void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2079 assert (Builder && "GRStmtNodeBuilder must be defined.");
2080
2081 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002082
Ted Kremenek186350f2008-04-23 20:12:28 +00002083 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2084 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002085
Ted Kremenek729a9a22008-07-17 23:15:45 +00002086 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002087
Ted Kremenekb0533962008-04-18 20:35:30 +00002088 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002089
Ted Kremenekb0533962008-04-18 20:35:30 +00002090 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002091 MakeNode(Dst, S, Pred, GetState(Pred));
2092}
2093
Ted Kremenek02737ed2008-03-31 15:02:58 +00002094void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2095
2096 Expr* R = S->getRetValue();
2097
2098 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002099 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002100 return;
2101 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002102
2103 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00002104 QualType T = R->getType();
2105
Chris Lattner423a3c92008-04-02 17:45:06 +00002106 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002107
2108 // Check if any of the return values return the address of a stack variable.
2109
2110 NodeSet Tmp;
2111 Visit(R, Pred, Tmp);
2112
2113 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002114 SVal X = GetSVal((*I)->getState(), R);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002115
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002116 if (isa<loc::MemRegionVal>(X)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002117
Ted Kremenek9e240492008-10-04 05:50:14 +00002118 // Determine if the value is on the stack.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002119 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00002120
2121 if (R && getStateManager().hasStackStorage(R)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002122
2123 // Create a special node representing the v
2124
2125 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
2126
2127 if (RetStackNode) {
2128 RetStackNode->markAsSink();
2129 RetsStackAddr.insert(RetStackNode);
2130 }
2131
2132 continue;
2133 }
2134 }
2135
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002136 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002137 }
2138 }
2139 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002140 Visit(R, Pred, DstRet);
2141
2142 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2143 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002144}
Ted Kremenek55deb972008-03-25 00:34:37 +00002145
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002146//===----------------------------------------------------------------------===//
2147// Transfer functions: Binary operators.
2148//===----------------------------------------------------------------------===//
2149
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002150const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2151 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002152
2153 // Divide by undefined? (potentially zero)
2154
2155 if (Denom.isUndef()) {
2156 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2157
2158 if (DivUndef) {
2159 DivUndef->markAsSink();
2160 ExplicitBadDivides.insert(DivUndef);
2161 }
2162
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002163 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002164 }
2165
2166 // Check for divide/remainder-by-zero.
2167 // First, "assume" that the denominator is 0 or undefined.
2168
2169 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002170 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002171
2172 // Second, "assume" that the denominator cannot be 0.
2173
2174 bool isFeasibleNotZero = false;
2175 St = Assume(St, Denom, true, isFeasibleNotZero);
2176
2177 // Create the node for the divide-by-zero (if it occurred).
2178
2179 if (isFeasibleZero)
2180 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2181 DivZeroNode->markAsSink();
2182
2183 if (isFeasibleNotZero)
2184 ImplicitBadDivides.insert(DivZeroNode);
2185 else
2186 ExplicitBadDivides.insert(DivZeroNode);
2187
2188 }
2189
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002190 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002191}
2192
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002193void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002194 GRExprEngine::NodeTy* Pred,
2195 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002196
2197 NodeSet Tmp1;
2198 Expr* LHS = B->getLHS()->IgnoreParens();
2199 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002200
2201 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002202 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002203 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002204 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002205
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002206 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002207
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002208 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002209
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002210 // Process the RHS.
2211
2212 NodeSet Tmp2;
2213 Visit(RHS, *I1, Tmp2);
2214
2215 // With both the LHS and RHS evaluated, process the operation itself.
2216
2217 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002218
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002219 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002220 const GRState* OldSt = St;
2221
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002222 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002223 BinaryOperator::Opcode Op = B->getOpcode();
2224
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002225 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002226
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002227 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002228
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002229 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002230 // FIXME: Handle structs.
2231 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002232
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002233 if (RightV.isUnknown() && (T->isIntegerType() || Loc::IsLocType(T))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002234 unsigned Count = Builder->getCurrentBlockCount();
2235 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2236
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002237 RightV = Loc::IsLocType(B->getRHS()->getType())
2238 ? cast<SVal>(loc::SymbolVal(Sym))
2239 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002240 }
2241
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002242 // Simulate the effects of a "store": bind the value of the RHS
2243 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002244
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002245 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002246 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002247 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002248
2249 case BinaryOperator::Div:
2250 case BinaryOperator::Rem:
2251
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002252 // Special checking for integer denominators.
2253 if (RHS->getType()->isIntegerType()) {
2254 St = CheckDivideZero(B, St, *I2, RightV);
2255 if (!St) continue;
2256 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002257
2258 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002259
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002260 default: {
2261
2262 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002263 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002264
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002265 // Process non-assignements except commas or short-circuited
2266 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002267
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002268 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002269
2270 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002271 if (OldSt != St) {
2272 // Generate a new node if we have already created a new state.
2273 MakeNode(Dst, B, *I2, St);
2274 }
2275 else
2276 Dst.Add(*I2);
2277
Ted Kremenek89063af2008-02-21 19:15:37 +00002278 continue;
2279 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002280
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002281 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002282
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002283 // The operands were *not* undefined, but the result is undefined.
2284 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002285
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002286 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002287 UndefNode->markAsSink();
2288 UndefResults.insert(UndefNode);
2289 }
2290
2291 continue;
2292 }
2293
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002294 // Otherwise, create a new node.
2295
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002296 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002297 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002298 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002299 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002300
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002301 assert (B->isCompoundAssignmentOp());
2302
Ted Kremenek934e3e92008-10-27 23:02:39 +00002303 if (Op >= BinaryOperator::AndAssign) {
2304 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2305 BinaryOperator::And));
2306 }
2307 else {
2308 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2309 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002310
2311 // Perform a load (the LHS). This performs the checks for
2312 // null dereferences, and so on.
2313 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002314 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002315 EvalLoad(Tmp3, LHS, *I2, St, location);
2316
2317 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2318
2319 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002320 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002321
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002322 // Check for divide-by-zero.
2323 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2324 && RHS->getType()->isIntegerType()) {
2325
2326 // CheckDivideZero returns a new state where the denominator
2327 // is assumed to be non-zero.
2328 St = CheckDivideZero(B, St, *I3, RightV);
2329
2330 if (!St)
2331 continue;
2332 }
2333
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002334 // Propagate undefined values (left-side).
2335 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002336 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002337 continue;
2338 }
2339
2340 // Propagate unknown values (left and right-side).
2341 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002342 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002343 UnknownVal());
2344 continue;
2345 }
2346
2347 // At this point:
2348 //
2349 // The LHS is not Undef/Unknown.
2350 // The RHS is not Unknown.
2351
2352 // Get the computation type.
2353 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2354
2355 // Perform promotions.
2356 V = EvalCast(V, CTy);
2357 RightV = EvalCast(RightV, CTy);
2358
2359 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002360 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002361 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002362 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002363 continue;
2364 }
2365
2366 // Compute the result of the operation.
2367
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002368 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002369
2370 if (Result.isUndef()) {
2371
2372 // The operands were not undefined, but the result is undefined.
2373
2374 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2375 UndefNode->markAsSink();
2376 UndefResults.insert(UndefNode);
2377 }
2378
2379 continue;
2380 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002381
2382 // EXPERIMENTAL: "Conjured" symbols.
2383 // FIXME: Handle structs.
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002384 if (Result.isUnknown() &&
2385 (CTy->isIntegerType() || Loc::IsLocType(CTy))) {
2386
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002387 unsigned Count = Builder->getCurrentBlockCount();
2388 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2389
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002390 Result = Loc::IsLocType(CTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002391 ? cast<SVal>(loc::SymbolVal(Sym))
2392 : cast<SVal>(nonloc::SymbolVal(Sym));
2393 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002394
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002395 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002396 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002397 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002398 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002399}
Ted Kremenekee985462008-01-16 18:18:48 +00002400
2401//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002402// Transfer-function Helpers.
2403//===----------------------------------------------------------------------===//
2404
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002405void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002406 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002407 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002408 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002409
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002410 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002411 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2412
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002413 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002414 MakeNode(Dst, Ex, Pred, *I);
2415}
2416
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002417void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002418 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002419 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002420
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002421 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002422 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002423}
2424
2425//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002426// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002427//===----------------------------------------------------------------------===//
2428
Ted Kremenekaa66a322008-01-16 21:46:15 +00002429#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002430static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002431static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002432
Ted Kremenekaa66a322008-01-16 21:46:15 +00002433namespace llvm {
2434template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002435struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002436 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002437
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002438 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2439
2440 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002441 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002442 GraphPrintCheckerState->isUndefDeref(N) ||
2443 GraphPrintCheckerState->isUndefStore(N) ||
2444 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002445 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2446 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002447 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002448 GraphPrintCheckerState->isBadCall(N) ||
2449 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002450 return "color=\"red\",style=\"filled\"";
2451
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002452 if (GraphPrintCheckerState->isNoReturnCall(N))
2453 return "color=\"blue\",style=\"filled\"";
2454
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002455 return "";
2456 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002457
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002458 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002459 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002460
2461 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002462 ProgramPoint Loc = N->getLocation();
2463
2464 switch (Loc.getKind()) {
2465 case ProgramPoint::BlockEntranceKind:
2466 Out << "Block Entrance: B"
2467 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2468 break;
2469
2470 case ProgramPoint::BlockExitKind:
2471 assert (false);
2472 break;
2473
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002474 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002475 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002476 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002477 const PostStmt& L = cast<PostStmt>(Loc);
2478 Stmt* S = L.getStmt();
2479 SourceLocation SLoc = S->getLocStart();
2480
2481 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002482 llvm::raw_os_ostream OutS(Out);
2483 S->printPretty(OutS);
2484 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002485
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002486 if (SLoc.isFileID()) {
2487 Out << "\\lline="
2488 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002489 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002490 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002491
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002492 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002493 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002494 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002495 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002496 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002497 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002498 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002499 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002500 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2501 Out << "\\|Explicit divide-by zero or undefined value.";
2502 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2503 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002504 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002505 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002506 else if (GraphPrintCheckerState->isNoReturnCall(N))
2507 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002508 else if (GraphPrintCheckerState->isBadCall(N))
2509 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002510 else if (GraphPrintCheckerState->isUndefArg(N))
2511 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002512
Ted Kremenekaa66a322008-01-16 21:46:15 +00002513 break;
2514 }
2515
2516 default: {
2517 const BlockEdge& E = cast<BlockEdge>(Loc);
2518 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2519 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002520
2521 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002522
2523 SourceLocation SLoc = T->getLocStart();
2524
Ted Kremenekb38911f2008-01-30 23:03:39 +00002525 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002526
Ted Kremeneka95d3752008-09-13 05:16:45 +00002527 llvm::raw_os_ostream OutS(Out);
2528 E.getSrc()->printTerminator(OutS);
2529 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002530
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002531 if (SLoc.isFileID()) {
2532 Out << "\\lline="
2533 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2534 << GraphPrintSourceManager->getColumnNumber(SLoc);
2535 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002536
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002537 if (isa<SwitchStmt>(T)) {
2538 Stmt* Label = E.getDst()->getLabel();
2539
2540 if (Label) {
2541 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2542 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002543 llvm::raw_os_ostream OutS(Out);
2544 C->getLHS()->printPretty(OutS);
2545 OutS.flush();
2546
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002547 if (Stmt* RHS = C->getRHS()) {
2548 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002549 RHS->printPretty(OutS);
2550 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002551 }
2552
2553 Out << ":";
2554 }
2555 else {
2556 assert (isa<DefaultStmt>(Label));
2557 Out << "\\ldefault:";
2558 }
2559 }
2560 else
2561 Out << "\\l(implicit) default:";
2562 }
2563 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002564 // FIXME
2565 }
2566 else {
2567 Out << "\\lCondition: ";
2568 if (*E.getSrc()->succ_begin() == E.getDst())
2569 Out << "true";
2570 else
2571 Out << "false";
2572 }
2573
2574 Out << "\\l";
2575 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002576
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002577 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2578 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002579 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002580 }
2581 }
2582
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002583 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002584
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002585 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2586 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002587
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002588 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002589 return Out.str();
2590 }
2591};
2592} // end llvm namespace
2593#endif
2594
Ted Kremenekffe0f432008-03-07 22:58:01 +00002595#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002596
2597template <typename ITERATOR>
2598GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2599
2600template <>
2601GRExprEngine::NodeTy*
2602GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2603 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2604 return I->first;
2605}
2606
Ted Kremenekffe0f432008-03-07 22:58:01 +00002607template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002608static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002609 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002610
Ted Kremenekd4527582008-09-16 18:44:52 +00002611 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002612
2613 for ( ; I != E; ++I ) {
2614 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002615 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002616
Ted Kremenekd4527582008-09-16 18:44:52 +00002617 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002618 continue;
2619
Ted Kremenekd4527582008-09-16 18:44:52 +00002620 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002621 Sources.push_back(N);
2622 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002623}
2624#endif
2625
2626void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002627#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002628 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002629 std::vector<NodeTy*> Src;
2630
2631 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002632 AddSources(Src, null_derefs_begin(), null_derefs_end());
2633 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2634 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2635 AddSources(Src, undef_results_begin(), undef_results_end());
2636 AddSources(Src, bad_calls_begin(), bad_calls_end());
2637 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002638 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002639
Ted Kremenekcb612922008-04-18 19:23:43 +00002640 // The new way.
2641 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2642 (*I)->GetErrorNodes(Src);
2643
2644
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002645 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002646 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002647 else {
2648 GraphPrintCheckerState = this;
2649 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002650
Ted Kremenekffe0f432008-03-07 22:58:01 +00002651 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002652
2653 GraphPrintCheckerState = NULL;
2654 GraphPrintSourceManager = NULL;
2655 }
2656#endif
2657}
2658
2659void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2660#ifndef NDEBUG
2661 GraphPrintCheckerState = this;
2662 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002663
Ted Kremenek493d7a22008-03-11 18:25:33 +00002664 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2665
2666 if (!TrimmedG)
2667 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2668 else {
2669 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2670 delete TrimmedG;
2671 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002672
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002673 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002674 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002675#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002676}