blob: 6162592437e952c3bcab9e3d90a7c77ee513debd [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;
332
333 case Stmt::ConditionalOperatorClass: { // '?' operator
334 ConditionalOperator* C = cast<ConditionalOperator>(S);
335 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
336 break;
337 }
338
339 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000340 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000341 break;
342
343 case Stmt::DeclStmtClass:
344 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
345 break;
346
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000347 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000348 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000349 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000350 VisitCast(C, C->getSubExpr(), Pred, Dst);
351 break;
352 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000353
354 case Stmt::InitListExprClass:
355 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
356 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000357
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000358 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000359 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
360 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000361
362 case Stmt::ObjCIvarRefExprClass:
363 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
364 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000365
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000366 case Stmt::ObjCMessageExprClass: {
367 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
368 break;
369 }
370
371 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000372 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000373 break;
374
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000375 case Stmt::ReturnStmtClass:
376 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
377 break;
378
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000379 case Stmt::SizeOfAlignOfTypeExprClass:
380 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
381 break;
382
383 case Stmt::StmtExprClass: {
384 StmtExpr* SE = cast<StmtExpr>(S);
385
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000386 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000387
388 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
389 // probably just remove these from the CFG.
390 assert (!SE->getSubStmt()->body_empty());
391
392 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000393 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000394 else
395 Dst.Add(Pred);
396
397 break;
398 }
399
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000400 case Stmt::UnaryOperatorClass:
401 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000402 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000403 }
404}
405
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000406void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000407
408 Ex = Ex->IgnoreParens();
409
410 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
411 Dst.Add(Pred);
412 return;
413 }
414
415 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000416
417 case Stmt::ArraySubscriptExprClass:
418 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
419 return;
420
421 case Stmt::DeclRefExprClass:
422 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
423 return;
424
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000425 case Stmt::ObjCIvarRefExprClass:
426 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
427 return;
428
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000429 case Stmt::UnaryOperatorClass:
430 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
431 return;
432
433 case Stmt::MemberExprClass:
434 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
435 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000436
Ted Kremenek4f090272008-10-27 21:54:31 +0000437 case Stmt::CompoundLiteralExprClass:
438 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst);
439 return;
440
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000441 case Stmt::ObjCPropertyRefExprClass:
442 // FIXME: Property assignments are lvalues, but not really "locations".
443 // e.g.: self.x = something;
444 // Here the "self.x" really can translate to a method call (setter) when
445 // the assignment is made. Moreover, the entire assignment expression
446 // evaluate to whatever "something" is, not calling the "getter" for
447 // the property (which would make sense since it can have side effects).
448 // We'll probably treat this as a location, but not one that we can
449 // take the address of. Perhaps we need a new SVal class for cases
450 // like thsis?
451 // Note that we have a similar problem for bitfields, since they don't
452 // have "locations" in the sense that we can take their address.
453 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000454 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000455
456 case Stmt::StringLiteralClass: {
457 const GRState* St = GetState(Pred);
458 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000459 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000460 return;
461 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000462
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000463 default:
464 // Arbitrary subexpressions can return aggregate temporaries that
465 // can be used in a lvalue context. We need to enhance our support
466 // of such temporaries in both the environment and the store, so right
467 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000468 assert ((Ex->getType()->isAggregateType() ||
469 Ex->getType()->isUnionType()) &&
470 "Other kinds of expressions with non-aggregate/union types do"
471 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000472
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000473 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000474 }
475}
476
477//===----------------------------------------------------------------------===//
478// Block entrance. (Update counters).
479//===----------------------------------------------------------------------===//
480
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000481bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000482 GRBlockCounter BC) {
483
484 return BC.getNumVisited(B->getBlockID()) < 3;
485}
486
487//===----------------------------------------------------------------------===//
488// Branch processing.
489//===----------------------------------------------------------------------===//
490
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000491const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000492 Stmt* Terminator,
493 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000494
495 switch (Terminator->getStmtClass()) {
496 default:
497 return St;
498
499 case Stmt::BinaryOperatorClass: { // '&&' and '||'
500
501 BinaryOperator* B = cast<BinaryOperator>(Terminator);
502 BinaryOperator::Opcode Op = B->getOpcode();
503
504 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
505
506 // For &&, if we take the true branch, then the value of the whole
507 // expression is that of the RHS expression.
508 //
509 // For ||, if we take the false branch, then the value of the whole
510 // expression is that of the RHS expression.
511
512 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
513 (Op == BinaryOperator::LOr && !branchTaken)
514 ? B->getRHS() : B->getLHS();
515
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000516 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000517 }
518
519 case Stmt::ConditionalOperatorClass: { // ?:
520
521 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
522
523 // For ?, if branchTaken == true then the value is either the LHS or
524 // the condition itself. (GNU extension).
525
526 Expr* Ex;
527
528 if (branchTaken)
529 Ex = C->getLHS() ? C->getLHS() : C->getCond();
530 else
531 Ex = C->getRHS();
532
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000533 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000534 }
535
536 case Stmt::ChooseExprClass: { // ?:
537
538 ChooseExpr* C = cast<ChooseExpr>(Terminator);
539
540 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000541 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000542 }
543 }
544}
545
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000546void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000547 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000548
Ted Kremeneke7d22112008-02-11 19:21:59 +0000549 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000550 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000551 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000552
Ted Kremenekb2331832008-02-15 22:29:00 +0000553 // Check for NULL conditions; e.g. "for(;;)"
554 if (!Condition) {
555 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000556 return;
557 }
558
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000559 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000560
561 switch (V.getBaseKind()) {
562 default:
563 break;
564
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000565 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000566 builder.generateNode(MarkBranch(PrevState, Term, true), true);
567 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000568 return;
569
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000570 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000571 NodeTy* N = builder.generateNode(PrevState, true);
572
573 if (N) {
574 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000575 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000576 }
577
578 builder.markInfeasible(false);
579 return;
580 }
581 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000582
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000583 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000584
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000585 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000586 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000587
588 if (isFeasible)
589 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000590 else
591 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000592
593 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000594
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000595 isFeasible = false;
596 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000597
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000598 if (isFeasible)
599 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000600 else
601 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000602}
603
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000604/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000605/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000606void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000607
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000608 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000609 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000610
611 // Three possibilities:
612 //
613 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000614 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000615 // (3) We have no clue about the label. Dispatch to all targets.
616 //
617
618 typedef IndirectGotoNodeBuilder::iterator iterator;
619
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000620 if (isa<loc::GotoLabel>(V)) {
621 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000622
623 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000624 if (I.getLabel() == L) {
625 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000626 return;
627 }
628 }
629
630 assert (false && "No block with label.");
631 return;
632 }
633
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000634 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000635 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000636 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000637 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000638 return;
639 }
640
641 // This is really a catch-all. We don't support symbolics yet.
642
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000643 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000644
645 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000646 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000647}
Ted Kremenekf233d482008-02-05 00:26:40 +0000648
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000649
650void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
651 NodeTy* Pred, NodeSet& Dst) {
652
653 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
654
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000655 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000656 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000657
658 assert (X.isUndef());
659
660 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
661
662 assert (SE);
663
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000664 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000665
666 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000667 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000668}
669
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000670/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
671/// nodes by processing the 'effects' of a switch statement.
672void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
673
674 typedef SwitchNodeBuilder::iterator iterator;
675
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000676 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000677 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000678 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000680 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000681 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000682 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000683 return;
684 }
685
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000686 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000687
688 // While most of this can be assumed (such as the signedness), having it
689 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000690
Chris Lattner98be4942008-03-05 18:54:05 +0000691 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000692
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000693 APSInt V1(bits, false);
694 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000695 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000696
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000697 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000698
699 CaseStmt* Case = cast<CaseStmt>(I.getCase());
700
701 // Evaluate the case.
702 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
703 assert (false && "Case condition must evaluate to an integer constant.");
704 return;
705 }
706
707 // Get the RHS of the case, if it exists.
708
709 if (Expr* E = Case->getRHS()) {
710 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
711 assert (false &&
712 "Case condition (RHS) must evaluate to an integer constant.");
713 return ;
714 }
715
716 assert (V1 <= V2);
717 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000718 else
719 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000720
721 // FIXME: Eventually we should replace the logic below with a range
722 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000723 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000724
Ted Kremenek14a11402008-03-17 22:17:56 +0000725 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000726 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000728 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000729
730 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000731
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000732 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000733 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000734
735 if (isFeasible) {
736 builder.generateCaseStmtNode(I, StNew);
737
738 // If CondV evaluates to a constant, then we know that this
739 // is the *only* case that we can take, so stop evaluating the
740 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000741 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000742 return;
743 }
744
745 // Now "assume" that the case doesn't match. Add this state
746 // to the default state (if it is feasible).
747
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000748 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000749 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000750
Ted Kremenek5014ab12008-04-23 05:03:18 +0000751 if (isFeasible) {
752 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000753 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000754 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000755
Ted Kremenek14a11402008-03-17 22:17:56 +0000756 // Concretize the next value in the range.
757 if (V1 == V2)
758 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759
Ted Kremenek14a11402008-03-17 22:17:56 +0000760 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000761 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000762
763 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000764 }
765
766 // If we reach here, than we know that the default branch is
767 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000768 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000769}
770
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000771//===----------------------------------------------------------------------===//
772// Transfer functions: logical operations ('&&', '||').
773//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000774
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000775void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000776 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000777
Ted Kremenek05a23782008-02-26 19:05:15 +0000778 assert (B->getOpcode() == BinaryOperator::LAnd ||
779 B->getOpcode() == BinaryOperator::LOr);
780
781 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
782
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000783 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000784 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000785
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000786 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000787
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000788 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000789
790 assert (Ex);
791
792 if (Ex == B->getRHS()) {
793
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000794 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000795
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000796 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000797
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000798 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000799 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000800 return;
801 }
802
Ted Kremenek05a23782008-02-26 19:05:15 +0000803 // We took the RHS. Because the value of the '&&' or '||' expression must
804 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
805 // or 1. Alternatively, we could take a lazy approach, and calculate this
806 // value later when necessary. We don't have the machinery in place for
807 // this right now, and since most logical expressions are used for branches,
808 // the payoff is not likely to be large. Instead, we do eager evaluation.
809
810 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000811 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000812
813 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000814 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000815 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000816
817 isFeasible = false;
818 NewState = Assume(St, X, false, isFeasible);
819
820 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000821 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000822 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000823 }
824 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000825 // We took the LHS expression. Depending on whether we are '&&' or
826 // '||' we know what the value of the expression is via properties of
827 // the short-circuiting.
828
829 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000830 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000831 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000832}
Ted Kremenek05a23782008-02-26 19:05:15 +0000833
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000834//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000835// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000836//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000837
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000838void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
839 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000840
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000841 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000842
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000843 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000844
845 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
846
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000847 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000848
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000849 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000850 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000851 else
852 EvalLoad(Dst, Ex, Pred, St, V);
853 return;
854
855 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
856 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
857
858 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000859 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000860 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000861 return;
862
863 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekcd162cc2008-10-17 00:55:33 +0000864 // FIXME: Does this need to be revised? We were getting cases in
865 // real code that did this.
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000866
Ted Kremeneke580c1b2008-10-31 15:33:11 +0000867 // FIXME: This is not a valid assertion. Produce a test case that
868 // refutes it.
869 // assert(asLValue); // Can we assume this?
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000870
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000871 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000872 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000873 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000874 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000875
876 assert (false &&
877 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000878}
879
Ted Kremenek540cbe22008-04-22 04:56:29 +0000880/// VisitArraySubscriptExpr - Transfer function for array accesses
881void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000882 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000883
884 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000885 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000886 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000887 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000888
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000889 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000890 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000891 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000892
893 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000894 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000895 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000896
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000897 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000898 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000899 else
900 EvalLoad(Dst, A, *I2, St, V);
901 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000902 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000903}
904
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000905/// VisitMemberExpr - Transfer function for member expressions.
906void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000907 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000908
909 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000910 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000911
912 if (M->isArrow())
913 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
914 else
915 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
916
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000917 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000918 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000919 // FIXME: Should we insert some assumption logic in here to determine
920 // if "Base" is a valid piece of memory? Before we put this assumption
921 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000922 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000923
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000924 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000925 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000926 else
927 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000928 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000929}
930
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000931void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000932 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000933
934 assert (Builder && "GRStmtNodeBuilder must be defined.");
935
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000936 // Evaluate the location (checks for bad dereferences).
937 St = EvalLocation(Ex, Pred, St, location);
938
939 if (!St)
940 return;
941
942 // Proceed with the store.
943
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000944 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000945
Ted Kremenek186350f2008-04-23 20:12:28 +0000946 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000947 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000948 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000949
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000950 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000951 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000952
Ted Kremenek729a9a22008-07-17 23:15:45 +0000953 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000954
955 // Handle the case where no nodes where generated. Auto-generate that
956 // contains the updated state if we aren't generating sinks.
957
Ted Kremenekb0533962008-04-18 20:35:30 +0000958 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000959 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960 location, Val);
961}
962
963void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000964 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000965 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000966
967 // Evaluate the location (checks for bad dereferences).
968
969 St = EvalLocation(Ex, Pred, St, location, true);
970
971 if (!St)
972 return;
973
974 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000975 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000976
977 // FIXME: Currently symbolic analysis "generates" new symbols
978 // for the contents of values. We need a better approach.
979
980 // FIXME: The "CheckOnly" option exists only because Array and Field
981 // loads aren't fully implemented. Eventually this option will go away.
Ted Kremenek982e6742008-08-28 18:43:46 +0000982
Ted Kremenek436f2b92008-04-30 04:23:07 +0000983 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000984 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000985 else if (location.isUnknown()) {
986 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000987 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000988 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000989 else
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000990 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, GetSVal(St, cast<Loc>(location),
991 Ex->getType())), K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000992}
993
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000994void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000995 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000996
997 NodeSet TmpDst;
998 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
999
1000 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1001 MakeNode(Dst, Ex, *I, (*I)->getState());
1002}
1003
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001004const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001005 const GRState* St,
1006 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001007
1008 // Check for loads/stores from/to undefined values.
1009 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001010 ProgramPoint::Kind K =
1011 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1012
1013 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001014 Succ->markAsSink();
1015 UndefDeref.insert(Succ);
1016 }
1017
1018 return NULL;
1019 }
1020
1021 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1022 if (location.isUnknown())
1023 return St;
1024
1025 // During a load, one of two possible situations arise:
1026 // (1) A crash, because the location (pointer) was NULL.
1027 // (2) The location (pointer) is not NULL, and the dereference works.
1028 //
1029 // We add these assumptions.
1030
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001031 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001032
1033 // "Assume" that the pointer is not NULL.
1034
1035 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001036 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001037
1038 // "Assume" that the pointer is NULL.
1039
1040 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001041 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1042 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001043
1044 if (isFeasibleNull) {
1045
Ted Kremenek7360fda2008-09-18 23:09:54 +00001046 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001047 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001048 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1049
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001050 // We don't use "MakeNode" here because the node will be a sink
1051 // and we have no intention of processing it later.
1052
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001053 ProgramPoint::Kind K =
1054 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1055
1056 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001057
1058 if (NullNode) {
1059
1060 NullNode->markAsSink();
1061
1062 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1063 else ExplicitNullDeref.insert(NullNode);
1064 }
1065 }
1066
1067 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001068}
1069
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001070//===----------------------------------------------------------------------===//
1071// Transfer function: Function calls.
1072//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001073void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001074 CallExpr::arg_iterator AI,
1075 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001076 NodeSet& Dst)
1077{
1078 // Determine the type of function we're calling (if available).
1079 const FunctionTypeProto *Proto = NULL;
1080 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1081 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1082 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1083
1084 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1085}
1086
1087void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1088 CallExpr::arg_iterator AI,
1089 CallExpr::arg_iterator AE,
1090 NodeSet& Dst, const FunctionTypeProto *Proto,
1091 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001092
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001093 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001094 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001095 // If the call argument is being bound to a reference parameter,
1096 // visit it as an lvalue, not an rvalue.
1097 bool VisitAsLvalue = false;
1098 if (Proto && ParamIdx < Proto->getNumArgs())
1099 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1100
1101 NodeSet DstTmp;
1102 if (VisitAsLvalue)
1103 VisitLValue(*AI, Pred, DstTmp);
1104 else
1105 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001106 ++AI;
1107
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001108 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001109 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001110
1111 return;
1112 }
1113
1114 // If we reach here we have processed all of the arguments. Evaluate
1115 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001116
Ted Kremenek994a09b2008-02-25 21:16:03 +00001117 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001118 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001119
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001120 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001121
Ted Kremenekde434242008-02-19 01:44:53 +00001122 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001123 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1124
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001125 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001126 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001127
Ted Kremeneka1354a52008-03-03 16:47:31 +00001128 // FIXME: Add support for symbolic function calls (calls involving
1129 // function pointer values that are symbolic).
1130
1131 // Check for undefined control-flow or calls to NULL.
1132
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001133 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001134 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001135
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001136 if (N) {
1137 N->markAsSink();
1138 BadCalls.insert(N);
1139 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001140
Ted Kremenekde434242008-02-19 01:44:53 +00001141 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001142 }
1143
1144 // Check for the "noreturn" attribute.
1145
1146 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1147
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001148 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001149
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001150 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001151
1152 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001153 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001154 else {
1155 // HACK: Some functions are not marked noreturn, and don't return.
1156 // Here are a few hardwired ones. If this takes too long, we can
1157 // potentially cache these results.
1158 const char* s = FD->getIdentifier()->getName();
1159 unsigned n = strlen(s);
1160
1161 switch (n) {
1162 default:
1163 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001164
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001165 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001166 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1167 break;
1168
1169 case 5:
1170 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001171 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001172 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001173 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001174 // FIXME: use Assume to inspect the possible symbolic value of
1175 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001176 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001177 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001178 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001179 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001180 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001181 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001182
1183 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001184 if (!memcmp(s, "Assert", 6)) {
1185 Builder->BuildSinks = true;
1186 break;
1187 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001188
1189 // FIXME: This is just a wrapper around throwing an exception.
1190 // Eventually inter-procedural analysis should handle this easily.
1191 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1192
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001193 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001194
1195 case 7:
1196 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1197 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001198
Ted Kremenekf47bb782008-04-30 17:54:04 +00001199 case 8:
1200 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1201 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001202
1203 case 12:
1204 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1205 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001206
Ted Kremenekf9683082008-09-19 02:30:47 +00001207 case 13:
1208 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1209 break;
1210
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001211 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001212 if (!memcmp(s, "dtrace_assfail", 14) ||
1213 !memcmp(s, "yy_fatal_error", 14))
1214 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001215 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001216
1217 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001218 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1219 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001220 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001221
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001222 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001223 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001224
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001225 }
1226 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001227
1228 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001229
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001230 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001231
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001232 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001233
Ted Kremenek186350f2008-04-23 20:12:28 +00001234 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001235 switch (id) {
1236 case Builtin::BI__builtin_expect: {
1237 // For __builtin_expect, just return the value of the subexpression.
1238 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001239 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001240 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001241 continue;
1242 }
1243
Ted Kremenekb3021332008-11-02 00:35:01 +00001244 case Builtin::BI__builtin_alloca: {
1245 // FIXME: Handle size.
1246 // FIXME: Refactor into StoreManager itself?
1247 MemRegionManager& RM = getStateManager().getRegionManager();
1248 const MemRegion* R =
1249 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
1250 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1251 continue;
1252 }
1253
Ted Kremenek55aea312008-03-05 22:59:42 +00001254 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001255 break;
1256 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001257 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001258
Ted Kremenek186350f2008-04-23 20:12:28 +00001259 // Check any arguments passed-by-value against being undefined.
1260
1261 bool badArg = false;
1262
1263 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1264 I != E; ++I) {
1265
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001266 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001267 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001268
Ted Kremenek186350f2008-04-23 20:12:28 +00001269 if (N) {
1270 N->markAsSink();
1271 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001272 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001273
Ted Kremenek186350f2008-04-23 20:12:28 +00001274 badArg = true;
1275 break;
1276 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001277 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001278
1279 if (badArg)
1280 continue;
1281
1282 // Dispatch to the plug-in transfer function.
1283
1284 unsigned size = Dst.size();
1285 SaveOr OldHasGen(Builder->HasGeneratedNode);
1286 EvalCall(Dst, CE, L, *DI);
1287
1288 // Handle the case where no nodes where generated. Auto-generate that
1289 // contains the updated state if we aren't generating sinks.
1290
1291 if (!Builder->BuildSinks && Dst.size() == size &&
1292 !Builder->HasGeneratedNode)
1293 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001294 }
1295}
1296
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001297//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001298// Transfer function: Objective-C ivar references.
1299//===----------------------------------------------------------------------===//
1300
1301void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1302 NodeTy* Pred, NodeSet& Dst,
1303 bool asLValue) {
1304
1305 Expr* Base = cast<Expr>(Ex->getBase());
1306 NodeSet Tmp;
1307 Visit(Base, Pred, Tmp);
1308
1309 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1310 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001311 SVal BaseVal = GetSVal(St, Base);
1312 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001313
1314 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001315 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001316 else
1317 EvalLoad(Dst, Ex, *I, St, location);
1318 }
1319}
1320
1321//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001322// Transfer function: Objective-C message expressions.
1323//===----------------------------------------------------------------------===//
1324
1325void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1326 NodeSet& Dst){
1327
1328 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1329 Pred, Dst);
1330}
1331
1332void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001333 ObjCMessageExpr::arg_iterator AI,
1334 ObjCMessageExpr::arg_iterator AE,
1335 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001336 if (AI == AE) {
1337
1338 // Process the receiver.
1339
1340 if (Expr* Receiver = ME->getReceiver()) {
1341 NodeSet Tmp;
1342 Visit(Receiver, Pred, Tmp);
1343
1344 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1345 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1346
1347 return;
1348 }
1349
1350 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1351 return;
1352 }
1353
1354 NodeSet Tmp;
1355 Visit(*AI, Pred, Tmp);
1356
1357 ++AI;
1358
1359 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1360 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1361}
1362
1363void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1364 NodeTy* Pred,
1365 NodeSet& Dst) {
1366
1367 // FIXME: More logic for the processing the method call.
1368
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001369 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001370 bool RaisesException = false;
1371
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001372
1373 if (Expr* Receiver = ME->getReceiver()) {
1374
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001375 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001376
1377 // Check for undefined control-flow or calls to NULL.
1378
1379 if (L.isUndef()) {
1380 NodeTy* N = Builder->generateNode(ME, St, Pred);
1381
1382 if (N) {
1383 N->markAsSink();
1384 UndefReceivers.insert(N);
1385 }
1386
1387 return;
1388 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001389
1390 // Check if the "raise" message was sent.
1391 if (ME->getSelector() == RaiseSel)
1392 RaisesException = true;
1393 }
1394 else {
1395
1396 IdentifierInfo* ClsName = ME->getClassName();
1397 Selector S = ME->getSelector();
1398
1399 // Check for special instance methods.
1400
1401 if (!NSExceptionII) {
1402 ASTContext& Ctx = getContext();
1403
1404 NSExceptionII = &Ctx.Idents.get("NSException");
1405 }
1406
1407 if (ClsName == NSExceptionII) {
1408
1409 enum { NUM_RAISE_SELECTORS = 2 };
1410
1411 // Lazily create a cache of the selectors.
1412
1413 if (!NSExceptionInstanceRaiseSelectors) {
1414
1415 ASTContext& Ctx = getContext();
1416
1417 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1418
1419 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1420 unsigned idx = 0;
1421
1422 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001423 II.push_back(&Ctx.Idents.get("raise"));
1424 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001425 NSExceptionInstanceRaiseSelectors[idx++] =
1426 Ctx.Selectors.getSelector(II.size(), &II[0]);
1427
1428 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001429 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001430 NSExceptionInstanceRaiseSelectors[idx++] =
1431 Ctx.Selectors.getSelector(II.size(), &II[0]);
1432 }
1433
1434 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1435 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1436 RaisesException = true; break;
1437 }
1438 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001439 }
1440
1441 // Check for any arguments that are uninitialized/undefined.
1442
1443 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1444 I != E; ++I) {
1445
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001446 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001447
1448 // Generate an error node for passing an uninitialized/undefined value
1449 // as an argument to a message expression. This node is a sink.
1450 NodeTy* N = Builder->generateNode(ME, St, Pred);
1451
1452 if (N) {
1453 N->markAsSink();
1454 MsgExprUndefArgs[N] = *I;
1455 }
1456
1457 return;
1458 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001459 }
1460
1461 // Check if we raise an exception. For now treat these as sinks. Eventually
1462 // we will want to handle exceptions properly.
1463
1464 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1465
1466 if (RaisesException)
1467 Builder->BuildSinks = true;
1468
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001469 // Dispatch to plug-in transfer function.
1470
1471 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001472 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001473
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001474 EvalObjCMessageExpr(Dst, ME, Pred);
1475
1476 // Handle the case where no nodes where generated. Auto-generate that
1477 // contains the updated state if we aren't generating sinks.
1478
Ted Kremenekb0533962008-04-18 20:35:30 +00001479 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001480 MakeNode(Dst, ME, Pred, St);
1481}
1482
1483//===----------------------------------------------------------------------===//
1484// Transfer functions: Miscellaneous statements.
1485//===----------------------------------------------------------------------===//
1486
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001487void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001488 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001489 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001490 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001491
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001492 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001493 T = ExCast->getTypeAsWritten();
1494
Zhongxing Xued340f72008-10-22 08:02:16 +00001495 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001496 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001497 else
1498 Visit(Ex, Pred, S1);
1499
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001500 // Check for casting to "void".
1501 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001502
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001503 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001504 Dst.Add(*I1);
1505
Ted Kremenek874d63f2008-01-24 02:02:54 +00001506 return;
1507 }
1508
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001509 // FIXME: The rest of this should probably just go into EvalCall, and
1510 // let the transfer function object be responsible for constructing
1511 // nodes.
1512
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001513 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001514 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001515 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001516 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001517
1518 // Unknown?
1519
1520 if (V.isUnknown()) {
1521 Dst.Add(N);
1522 continue;
1523 }
1524
1525 // Undefined?
1526
1527 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001528 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001529 continue;
1530 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001531
1532 // For const casts, just propagate the value.
1533 ASTContext& C = getContext();
1534
1535 if (C.getCanonicalType(T).getUnqualifiedType() ==
1536 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001537 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001538 continue;
1539 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001540
1541 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001542 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001543 unsigned bits = getContext().getTypeSize(ExTy);
1544
1545 // FIXME: Determine if the number of bits of the target type is
1546 // equal or exceeds the number of bits to store the pointer value.
1547 // If not, flag an error.
1548
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001549 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001550 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001551 continue;
1552 }
1553
1554 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001555 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1556 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001557 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001558 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001559 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001560 continue;
1561 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001562
1563 // StoreManager casts array to different values.
1564 if (ExTy->isArrayType()) {
Ted Kremenek0b50f5b2008-10-24 21:10:49 +00001565 assert(T->isPointerType() || T->isReferenceType());
Zhongxing Xue564b522008-10-23 04:19:25 +00001566
Zhongxing Xue1911af2008-10-23 03:10:39 +00001567 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001568 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001569 continue;
1570 }
1571
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001572 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001573 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001574 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001575}
1576
Ted Kremenek4f090272008-10-27 21:54:31 +00001577void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
1578 NodeTy* Pred, NodeSet& Dst) {
1579
1580 // FIXME: Can getInitializer() be NULL?
1581 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1582 NodeSet Tmp;
1583 Visit(ILE, Pred, Tmp);
1584
1585 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
1586 // Retrieve the initializer values from the environment and store them
1587 // into a vector that will then be handed off to the Store.
1588 const GRState* St = GetState(*I);
1589 llvm::SmallVector<SVal, 10> IVals;
1590 IVals.reserve(ILE->getNumInits());
1591
1592 for (Stmt::child_iterator J=ILE->child_begin(), EJ=ILE->child_end();
1593 J!=EJ; ++J)
1594 IVals.push_back(GetSVal(St, cast<Expr>(*J)));
1595
1596 const CompoundLiteralRegion* R =
1597 StateMgr.getRegionManager().getCompoundLiteralRegion(CL);
1598
1599 assert (!IVals.empty() && "Initializer cannot be empty.");
1600
1601 St = StateMgr.BindCompoundLiteral(St, R, &IVals[0], &IVals[0]+IVals.size());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001602 MakeNode(Dst, CL, *I, BindExpr(St, CL, loc::MemRegionVal(R)));
Ted Kremenek4f090272008-10-27 21:54:31 +00001603 }
1604}
1605
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001606void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001607
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001608 // The CFG has one DeclStmt per Decl.
1609 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001610
1611 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001612 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001613
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001614 const VarDecl* VD = dyn_cast<VarDecl>(D);
1615
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001616 Expr* Ex = const_cast<Expr*>(VD->getInit());
1617
1618 // FIXME: static variables may have an initializer, but the second
1619 // time a function is called those values may not be current.
1620 NodeSet Tmp;
1621
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001622 if (Ex)
1623 Visit(Ex, Pred, Tmp);
1624
1625 if (Tmp.empty())
1626 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001627
1628 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001629 const GRState* St = GetState(*I);
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +00001630 St = StateMgr.BindDecl(St, VD, Ex, Builder->getCurrentBlockCount());
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001631 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001632 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001633}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001634
Ted Kremenekf75b1862008-10-30 17:47:32 +00001635namespace {
1636 // This class is used by VisitInitListExpr as an item in a worklist
1637 // for processing the values contained in an InitListExpr.
1638class VISIBILITY_HIDDEN InitListWLItem {
1639public:
1640 llvm::ImmutableList<SVal> Vals;
1641 GRExprEngine::NodeTy* N;
1642 InitListExpr::reverse_iterator Itr;
1643
1644 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1645 InitListExpr::reverse_iterator itr)
1646 : Vals(vals), N(n), Itr(itr) {}
1647};
1648}
1649
1650
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001651void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1652 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001653
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001654 const GRState* state = GetState(Pred);
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001655 QualType T = E->getType();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001656 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001657
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001658 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001659
Ted Kremeneka49e3672008-10-30 23:14:36 +00001660 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001661
Ted Kremeneka49e3672008-10-30 23:14:36 +00001662 // Handle base case where the initializer has no elements.
1663 // e.g: static int* myArray[] = {};
1664 if (NumInitElements == 0) {
1665 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1666 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1667 return;
1668 }
1669
1670 // Create a worklist to process the initializers.
1671 llvm::SmallVector<InitListWLItem, 10> WorkList;
1672 WorkList.reserve(NumInitElements);
1673 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001674 InitListExpr::reverse_iterator ItrEnd = E->rend();
1675
Ted Kremeneka49e3672008-10-30 23:14:36 +00001676 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001677 while (!WorkList.empty()) {
1678 InitListWLItem X = WorkList.back();
1679 WorkList.pop_back();
1680
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001681 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001682 Visit(*X.Itr, X.N, Tmp);
1683
1684 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001685
Ted Kremenekf75b1862008-10-30 17:47:32 +00001686 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1687 // Get the last initializer value.
1688 state = GetState(*NI);
1689 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1690
1691 // Construct the new list of values by prepending the new value to
1692 // the already constructed list.
1693 llvm::ImmutableList<SVal> NewVals =
1694 getBasicVals().consVals(InitV, X.Vals);
1695
1696 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001697 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001698 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001699
Ted Kremenekf75b1862008-10-30 17:47:32 +00001700 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001701 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001702 }
1703 else {
1704 // Still some initializer values to go. Push them onto the worklist.
1705 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1706 }
1707 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001708 }
Ted Kremenek87903072008-10-30 18:34:31 +00001709
1710 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001711 }
1712
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001713 if (Loc::IsLocType(T) || T->isIntegerType()) {
1714 assert (E->getNumInits() == 1);
1715 NodeSet Tmp;
1716 Expr* Init = E->getInit(0);
1717 Visit(Init, Pred, Tmp);
1718 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1719 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001720 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001721 }
1722 return;
1723 }
1724
1725 if (T->isUnionType()) {
1726 // FIXME: to be implemented.
1727 MakeNode(Dst, E, Pred, state);
1728 return;
1729 }
1730
1731 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1732 assert(0 && "unprocessed InitListExpr type");
1733}
Ted Kremenekf233d482008-02-05 00:26:40 +00001734
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001735/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001736void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1737 NodeTy* Pred,
1738 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001739 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001740 uint64_t amt;
1741
1742 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001743
Ted Kremenek87e80342008-03-15 03:13:20 +00001744 // FIXME: Add support for VLAs.
1745 if (!T.getTypePtr()->isConstantSizeType())
1746 return;
1747
Ted Kremenekf342d182008-04-30 21:31:12 +00001748 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1749 // the compiler has laid out its representation. Just report Unknown
1750 // for these.
1751 if (T->isObjCInterfaceType())
1752 return;
1753
Ted Kremenek87e80342008-03-15 03:13:20 +00001754 amt = 1; // Handle sizeof(void)
1755
1756 if (T != getContext().VoidTy)
1757 amt = getContext().getTypeSize(T) / 8;
1758
1759 }
1760 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001761 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001762
Ted Kremenek0e561a32008-03-21 21:30:14 +00001763 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001764 BindExpr(GetState(Pred), Ex,
1765 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001766}
1767
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001768
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001769void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001770 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001771
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001772 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001773
1774 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001775 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001776
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001777 case UnaryOperator::Deref: {
1778
1779 Expr* Ex = U->getSubExpr()->IgnoreParens();
1780 NodeSet Tmp;
1781 Visit(Ex, Pred, Tmp);
1782
1783 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001784
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001785 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001786 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001787
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001788 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001789 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001790 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001791 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001792 }
1793
1794 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001795 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001796
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001797 case UnaryOperator::Real: {
1798
1799 Expr* Ex = U->getSubExpr()->IgnoreParens();
1800 NodeSet Tmp;
1801 Visit(Ex, Pred, Tmp);
1802
1803 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1804
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001805 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001806 if (Ex->getType()->isAnyComplexType()) {
1807 // Just report "Unknown."
1808 Dst.Add(*I);
1809 continue;
1810 }
1811
1812 // For all other types, UnaryOperator::Real is an identity operation.
1813 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001814 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001815 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001816 }
1817
1818 return;
1819 }
1820
1821 case UnaryOperator::Imag: {
1822
1823 Expr* Ex = U->getSubExpr()->IgnoreParens();
1824 NodeSet Tmp;
1825 Visit(Ex, Pred, Tmp);
1826
1827 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001828 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001829 if (Ex->getType()->isAnyComplexType()) {
1830 // Just report "Unknown."
1831 Dst.Add(*I);
1832 continue;
1833 }
1834
1835 // For all other types, UnaryOperator::Float returns 0.
1836 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001837 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001838 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001839 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001840 }
1841
1842 return;
1843 }
1844
1845 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001846 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001847 Dst.Add(Pred);
1848 return;
1849
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001850 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001851 case UnaryOperator::Extension: {
1852
1853 // Unary "+" is a no-op, similar to a parentheses. We still have places
1854 // where it may be a block-level expression, so we need to
1855 // generate an extra node that just propagates the value of the
1856 // subexpression.
1857
1858 Expr* Ex = U->getSubExpr()->IgnoreParens();
1859 NodeSet Tmp;
1860 Visit(Ex, Pred, Tmp);
1861
1862 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001863 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001864 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001865 }
1866
1867 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001868 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001869
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001870 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001871
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001872 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001873 Expr* Ex = U->getSubExpr()->IgnoreParens();
1874 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001875 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001876
1877 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001878 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001879 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001880 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001881 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001882 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001883
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001884 return;
1885 }
1886
1887 case UnaryOperator::LNot:
1888 case UnaryOperator::Minus:
1889 case UnaryOperator::Not: {
1890
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001891 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001892 Expr* Ex = U->getSubExpr()->IgnoreParens();
1893 NodeSet Tmp;
1894 Visit(Ex, Pred, Tmp);
1895
1896 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001897 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00001898
1899 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001900 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00001901
1902 // Perform promotions.
Ted Kremenek5a236cb2008-09-30 05:35:42 +00001903 // FIXME: This is the right thing to do, but it currently breaks
1904 // a bunch of tests.
1905 // V = EvalCast(V, U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001906
1907 if (V.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001908 MakeNode(Dst, U, *I, BindExpr(St, U, V));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001909 continue;
1910 }
1911
1912 switch (U->getOpcode()) {
1913 default:
1914 assert(false && "Invalid Opcode.");
1915 break;
1916
1917 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00001918 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001919 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001920 break;
1921
1922 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00001923 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001924 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001925 break;
1926
1927 case UnaryOperator::LNot:
1928
1929 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1930 //
1931 // Note: technically we do "E == 0", but this is the same in the
1932 // transfer functions as "0 == E".
1933
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001934 if (isa<Loc>(V)) {
1935 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
1936 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001937 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001938 }
1939 else {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001940 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001941#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001942 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
1943 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001944#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001945 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001946 continue;
1947#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001948 }
1949
1950 break;
1951 }
1952
1953 MakeNode(Dst, U, *I, St);
1954 }
1955
1956 return;
1957 }
Ted Kremenek6dfe2f52008-10-18 22:20:20 +00001958
1959 case UnaryOperator::AlignOf: {
1960
1961 QualType T = U->getSubExpr()->getType();
1962
1963 // FIXME: Add support for VLAs.
1964
1965 if (!T.getTypePtr()->isConstantSizeType())
1966 return;
1967
1968 uint64_t size = getContext().getTypeAlign(T) / 8;
1969 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001970 St = BindExpr(St, U, NonLoc::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek6dfe2f52008-10-18 22:20:20 +00001971
1972 MakeNode(Dst, U, Pred, St);
1973 return;
1974 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001975
1976 case UnaryOperator::SizeOf: {
1977
1978 QualType T = U->getSubExpr()->getType();
1979
1980 // FIXME: Add support for VLAs.
1981
1982 if (!T.getTypePtr()->isConstantSizeType())
1983 return;
1984
1985 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001986 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001987 St = BindExpr(St, U, NonLoc::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001988
1989 MakeNode(Dst, U, Pred, St);
1990 return;
1991 }
1992 }
1993
1994 // Handle ++ and -- (both pre- and post-increment).
1995
1996 assert (U->isIncrementDecrementOp());
1997 NodeSet Tmp;
1998 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001999 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002000
2001 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2002
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002003 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002004 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002005
2006 // Perform a load.
2007 NodeSet Tmp2;
2008 EvalLoad(Tmp2, Ex, *I, St, V1);
2009
2010 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2011
2012 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002013 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002014
2015 // Propagate unknown and undefined values.
2016 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002017 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002018 continue;
2019 }
2020
Ted Kremenek443003b2008-02-21 19:29:23 +00002021 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002022
2023 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2024 : BinaryOperator::Sub;
2025
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002026 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002027 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002028
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002029 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002030 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002031 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002032 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002033}
2034
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002035void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2036 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2037}
2038
2039void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2040 AsmStmt::outputs_iterator I,
2041 AsmStmt::outputs_iterator E,
2042 NodeTy* Pred, NodeSet& Dst) {
2043 if (I == E) {
2044 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2045 return;
2046 }
2047
2048 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002049 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002050
2051 ++I;
2052
2053 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2054 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2055}
2056
2057void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2058 AsmStmt::inputs_iterator I,
2059 AsmStmt::inputs_iterator E,
2060 NodeTy* Pred, NodeSet& Dst) {
2061 if (I == E) {
2062
2063 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002064 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002065
2066 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2067 // which interprets the inline asm and stores proper results in the
2068 // outputs.
2069
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002070 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002071
2072 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2073 OE = A->end_outputs(); OI != OE; ++OI) {
2074
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002075 SVal X = GetSVal(St, *OI);
2076 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002077
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002078 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002079 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002080 }
2081
Ted Kremenek0e561a32008-03-21 21:30:14 +00002082 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002083 return;
2084 }
2085
2086 NodeSet Tmp;
2087 Visit(*I, Pred, Tmp);
2088
2089 ++I;
2090
2091 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2092 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2093}
2094
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002095void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2096 assert (Builder && "GRStmtNodeBuilder must be defined.");
2097
2098 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002099
Ted Kremenek186350f2008-04-23 20:12:28 +00002100 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2101 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002102
Ted Kremenek729a9a22008-07-17 23:15:45 +00002103 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002104
Ted Kremenekb0533962008-04-18 20:35:30 +00002105 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002106
Ted Kremenekb0533962008-04-18 20:35:30 +00002107 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002108 MakeNode(Dst, S, Pred, GetState(Pred));
2109}
2110
Ted Kremenek02737ed2008-03-31 15:02:58 +00002111void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2112
2113 Expr* R = S->getRetValue();
2114
2115 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002116 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002117 return;
2118 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002119
2120 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00002121 QualType T = R->getType();
2122
Chris Lattner423a3c92008-04-02 17:45:06 +00002123 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002124
2125 // Check if any of the return values return the address of a stack variable.
2126
2127 NodeSet Tmp;
2128 Visit(R, Pred, Tmp);
2129
2130 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002131 SVal X = GetSVal((*I)->getState(), R);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002132
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002133 if (isa<loc::MemRegionVal>(X)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002134
Ted Kremenek9e240492008-10-04 05:50:14 +00002135 // Determine if the value is on the stack.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002136 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00002137
2138 if (R && getStateManager().hasStackStorage(R)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002139
2140 // Create a special node representing the v
2141
2142 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
2143
2144 if (RetStackNode) {
2145 RetStackNode->markAsSink();
2146 RetsStackAddr.insert(RetStackNode);
2147 }
2148
2149 continue;
2150 }
2151 }
2152
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002153 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002154 }
2155 }
2156 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002157 Visit(R, Pred, DstRet);
2158
2159 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2160 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002161}
Ted Kremenek55deb972008-03-25 00:34:37 +00002162
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002163//===----------------------------------------------------------------------===//
2164// Transfer functions: Binary operators.
2165//===----------------------------------------------------------------------===//
2166
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002167const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2168 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002169
2170 // Divide by undefined? (potentially zero)
2171
2172 if (Denom.isUndef()) {
2173 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2174
2175 if (DivUndef) {
2176 DivUndef->markAsSink();
2177 ExplicitBadDivides.insert(DivUndef);
2178 }
2179
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002180 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002181 }
2182
2183 // Check for divide/remainder-by-zero.
2184 // First, "assume" that the denominator is 0 or undefined.
2185
2186 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002187 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002188
2189 // Second, "assume" that the denominator cannot be 0.
2190
2191 bool isFeasibleNotZero = false;
2192 St = Assume(St, Denom, true, isFeasibleNotZero);
2193
2194 // Create the node for the divide-by-zero (if it occurred).
2195
2196 if (isFeasibleZero)
2197 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2198 DivZeroNode->markAsSink();
2199
2200 if (isFeasibleNotZero)
2201 ImplicitBadDivides.insert(DivZeroNode);
2202 else
2203 ExplicitBadDivides.insert(DivZeroNode);
2204
2205 }
2206
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002207 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002208}
2209
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002210void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002211 GRExprEngine::NodeTy* Pred,
2212 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002213
2214 NodeSet Tmp1;
2215 Expr* LHS = B->getLHS()->IgnoreParens();
2216 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002217
2218 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002219 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002220 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002221 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002222
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002223 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002224
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002225 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002226
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002227 // Process the RHS.
2228
2229 NodeSet Tmp2;
2230 Visit(RHS, *I1, Tmp2);
2231
2232 // With both the LHS and RHS evaluated, process the operation itself.
2233
2234 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002235
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002236 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002237 const GRState* OldSt = St;
2238
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002239 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002240 BinaryOperator::Opcode Op = B->getOpcode();
2241
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002242 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002243
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002244 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002245
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002246 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002247 // FIXME: Handle structs.
2248 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002249
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002250 if (RightV.isUnknown() && (T->isIntegerType() || Loc::IsLocType(T))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002251 unsigned Count = Builder->getCurrentBlockCount();
2252 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2253
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002254 RightV = Loc::IsLocType(B->getRHS()->getType())
2255 ? cast<SVal>(loc::SymbolVal(Sym))
2256 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002257 }
2258
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002259 // Simulate the effects of a "store": bind the value of the RHS
2260 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002261
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002262 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002263 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002264 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002265
2266 case BinaryOperator::Div:
2267 case BinaryOperator::Rem:
2268
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002269 // Special checking for integer denominators.
2270 if (RHS->getType()->isIntegerType()) {
2271 St = CheckDivideZero(B, St, *I2, RightV);
2272 if (!St) continue;
2273 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002274
2275 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002276
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002277 default: {
2278
2279 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002280 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002281
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002282 // Process non-assignements except commas or short-circuited
2283 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002284
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002285 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002286
2287 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002288 if (OldSt != St) {
2289 // Generate a new node if we have already created a new state.
2290 MakeNode(Dst, B, *I2, St);
2291 }
2292 else
2293 Dst.Add(*I2);
2294
Ted Kremenek89063af2008-02-21 19:15:37 +00002295 continue;
2296 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002297
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002298 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002299
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002300 // The operands were *not* undefined, but the result is undefined.
2301 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002302
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002303 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002304 UndefNode->markAsSink();
2305 UndefResults.insert(UndefNode);
2306 }
2307
2308 continue;
2309 }
2310
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002311 // Otherwise, create a new node.
2312
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002313 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002314 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002315 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002316 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002317
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002318 assert (B->isCompoundAssignmentOp());
2319
Ted Kremenek934e3e92008-10-27 23:02:39 +00002320 if (Op >= BinaryOperator::AndAssign) {
2321 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2322 BinaryOperator::And));
2323 }
2324 else {
2325 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2326 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002327
2328 // Perform a load (the LHS). This performs the checks for
2329 // null dereferences, and so on.
2330 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002331 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002332 EvalLoad(Tmp3, LHS, *I2, St, location);
2333
2334 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2335
2336 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002337 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002338
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002339 // Check for divide-by-zero.
2340 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2341 && RHS->getType()->isIntegerType()) {
2342
2343 // CheckDivideZero returns a new state where the denominator
2344 // is assumed to be non-zero.
2345 St = CheckDivideZero(B, St, *I3, RightV);
2346
2347 if (!St)
2348 continue;
2349 }
2350
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002351 // Propagate undefined values (left-side).
2352 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002353 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002354 continue;
2355 }
2356
2357 // Propagate unknown values (left and right-side).
2358 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002359 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002360 UnknownVal());
2361 continue;
2362 }
2363
2364 // At this point:
2365 //
2366 // The LHS is not Undef/Unknown.
2367 // The RHS is not Unknown.
2368
2369 // Get the computation type.
2370 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2371
2372 // Perform promotions.
2373 V = EvalCast(V, CTy);
2374 RightV = EvalCast(RightV, CTy);
2375
2376 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002377 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002378 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002379 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002380 continue;
2381 }
2382
2383 // Compute the result of the operation.
2384
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002385 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002386
2387 if (Result.isUndef()) {
2388
2389 // The operands were not undefined, but the result is undefined.
2390
2391 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2392 UndefNode->markAsSink();
2393 UndefResults.insert(UndefNode);
2394 }
2395
2396 continue;
2397 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002398
2399 // EXPERIMENTAL: "Conjured" symbols.
2400 // FIXME: Handle structs.
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002401 if (Result.isUnknown() &&
2402 (CTy->isIntegerType() || Loc::IsLocType(CTy))) {
2403
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002404 unsigned Count = Builder->getCurrentBlockCount();
2405 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2406
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002407 Result = Loc::IsLocType(CTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002408 ? cast<SVal>(loc::SymbolVal(Sym))
2409 : cast<SVal>(nonloc::SymbolVal(Sym));
2410 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002411
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002412 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002413 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002414 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002415 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002416}
Ted Kremenekee985462008-01-16 18:18:48 +00002417
2418//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002419// Transfer-function Helpers.
2420//===----------------------------------------------------------------------===//
2421
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002422void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002423 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002424 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002425 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002426
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002427 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002428 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2429
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002430 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002431 MakeNode(Dst, Ex, Pred, *I);
2432}
2433
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002434void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002435 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002436 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002437
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002438 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002439 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002440}
2441
2442//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002443// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002444//===----------------------------------------------------------------------===//
2445
Ted Kremenekaa66a322008-01-16 21:46:15 +00002446#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002447static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002448static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002449
Ted Kremenekaa66a322008-01-16 21:46:15 +00002450namespace llvm {
2451template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002452struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002453 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002454
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002455 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2456
2457 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002458 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002459 GraphPrintCheckerState->isUndefDeref(N) ||
2460 GraphPrintCheckerState->isUndefStore(N) ||
2461 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002462 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2463 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002464 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002465 GraphPrintCheckerState->isBadCall(N) ||
2466 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002467 return "color=\"red\",style=\"filled\"";
2468
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002469 if (GraphPrintCheckerState->isNoReturnCall(N))
2470 return "color=\"blue\",style=\"filled\"";
2471
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002472 return "";
2473 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002474
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002475 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002476 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002477
2478 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002479 ProgramPoint Loc = N->getLocation();
2480
2481 switch (Loc.getKind()) {
2482 case ProgramPoint::BlockEntranceKind:
2483 Out << "Block Entrance: B"
2484 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2485 break;
2486
2487 case ProgramPoint::BlockExitKind:
2488 assert (false);
2489 break;
2490
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002491 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002492 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002493 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002494 const PostStmt& L = cast<PostStmt>(Loc);
2495 Stmt* S = L.getStmt();
2496 SourceLocation SLoc = S->getLocStart();
2497
2498 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002499 llvm::raw_os_ostream OutS(Out);
2500 S->printPretty(OutS);
2501 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002502
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002503 if (SLoc.isFileID()) {
2504 Out << "\\lline="
2505 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002506 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002507 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002508
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002509 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002510 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002511 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002512 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002513 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002514 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002515 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002516 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002517 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2518 Out << "\\|Explicit divide-by zero or undefined value.";
2519 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2520 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002521 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002522 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002523 else if (GraphPrintCheckerState->isNoReturnCall(N))
2524 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002525 else if (GraphPrintCheckerState->isBadCall(N))
2526 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002527 else if (GraphPrintCheckerState->isUndefArg(N))
2528 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002529
Ted Kremenekaa66a322008-01-16 21:46:15 +00002530 break;
2531 }
2532
2533 default: {
2534 const BlockEdge& E = cast<BlockEdge>(Loc);
2535 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2536 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002537
2538 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002539
2540 SourceLocation SLoc = T->getLocStart();
2541
Ted Kremenekb38911f2008-01-30 23:03:39 +00002542 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002543
Ted Kremeneka95d3752008-09-13 05:16:45 +00002544 llvm::raw_os_ostream OutS(Out);
2545 E.getSrc()->printTerminator(OutS);
2546 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002547
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002548 if (SLoc.isFileID()) {
2549 Out << "\\lline="
2550 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2551 << GraphPrintSourceManager->getColumnNumber(SLoc);
2552 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002553
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002554 if (isa<SwitchStmt>(T)) {
2555 Stmt* Label = E.getDst()->getLabel();
2556
2557 if (Label) {
2558 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2559 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002560 llvm::raw_os_ostream OutS(Out);
2561 C->getLHS()->printPretty(OutS);
2562 OutS.flush();
2563
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002564 if (Stmt* RHS = C->getRHS()) {
2565 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002566 RHS->printPretty(OutS);
2567 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002568 }
2569
2570 Out << ":";
2571 }
2572 else {
2573 assert (isa<DefaultStmt>(Label));
2574 Out << "\\ldefault:";
2575 }
2576 }
2577 else
2578 Out << "\\l(implicit) default:";
2579 }
2580 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002581 // FIXME
2582 }
2583 else {
2584 Out << "\\lCondition: ";
2585 if (*E.getSrc()->succ_begin() == E.getDst())
2586 Out << "true";
2587 else
2588 Out << "false";
2589 }
2590
2591 Out << "\\l";
2592 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002593
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002594 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2595 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002596 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002597 }
2598 }
2599
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002600 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002601
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002602 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2603 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002604
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002605 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002606 return Out.str();
2607 }
2608};
2609} // end llvm namespace
2610#endif
2611
Ted Kremenekffe0f432008-03-07 22:58:01 +00002612#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002613
2614template <typename ITERATOR>
2615GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2616
2617template <>
2618GRExprEngine::NodeTy*
2619GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2620 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2621 return I->first;
2622}
2623
Ted Kremenekffe0f432008-03-07 22:58:01 +00002624template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002625static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002626 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002627
Ted Kremenekd4527582008-09-16 18:44:52 +00002628 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002629
2630 for ( ; I != E; ++I ) {
2631 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002632 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002633
Ted Kremenekd4527582008-09-16 18:44:52 +00002634 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002635 continue;
2636
Ted Kremenekd4527582008-09-16 18:44:52 +00002637 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002638 Sources.push_back(N);
2639 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002640}
2641#endif
2642
2643void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002644#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002645 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002646 std::vector<NodeTy*> Src;
2647
2648 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002649 AddSources(Src, null_derefs_begin(), null_derefs_end());
2650 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2651 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2652 AddSources(Src, undef_results_begin(), undef_results_end());
2653 AddSources(Src, bad_calls_begin(), bad_calls_end());
2654 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002655 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002656
Ted Kremenekcb612922008-04-18 19:23:43 +00002657 // The new way.
2658 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2659 (*I)->GetErrorNodes(Src);
2660
2661
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002662 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002663 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002664 else {
2665 GraphPrintCheckerState = this;
2666 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002667
Ted Kremenekffe0f432008-03-07 22:58:01 +00002668 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002669
2670 GraphPrintCheckerState = NULL;
2671 GraphPrintSourceManager = NULL;
2672 }
2673#endif
2674}
2675
2676void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2677#ifndef NDEBUG
2678 GraphPrintCheckerState = this;
2679 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002680
Ted Kremenek493d7a22008-03-11 18:25:33 +00002681 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2682
2683 if (!TrimmedG)
2684 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2685 else {
2686 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2687 delete TrimmedG;
2688 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002689
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002690 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002691 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002692#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002693}