blob: 2b5808f562e9c3faa96729f297b06f88bfab314a [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 Kremenek41573eb2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
18
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000021#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000022#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000023#include "llvm/ADT/ImmutableList.h"
24#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000026
Ted Kremenek0f5f0592008-02-27 06:07:00 +000027#ifndef NDEBUG
28#include "llvm/Support/GraphWriter.h"
29#include <sstream>
30#endif
31
Ted Kremenekb387a3f2008-02-14 22:16:04 +000032using namespace clang;
33using llvm::dyn_cast;
34using llvm::cast;
35using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036
Ted Kremeneke695e1c2008-04-15 23:06:53 +000037//===----------------------------------------------------------------------===//
38// Engine construction and deletion.
39//===----------------------------------------------------------------------===//
40
Ted Kremenekbdb435d2008-07-11 18:37:32 +000041namespace {
42
43class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
44 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
45 typedef llvm::DenseMap<void*,Checks> MapTy;
46
47 MapTy M;
48 Checks::Factory F;
Ted Kremenek536aa022009-03-30 17:53:05 +000049 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000050
51public:
Ted Kremenek536aa022009-03-30 17:53:05 +000052 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
53 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenekbdb435d2008-07-11 18:37:32 +000054
55 virtual ~MappedBatchAuditor() {
56 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
57
58 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
59 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
60
61 GRSimpleAPICheck* check = *I;
62
63 if (AlreadyVisited.count(check))
64 continue;
65
66 AlreadyVisited.insert(check);
67 delete check;
68 }
69 }
70
Ted Kremenek536aa022009-03-30 17:53:05 +000071 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000072 assert (A && "Check cannot be null.");
73 void* key = reinterpret_cast<void*>((uintptr_t) C);
74 MapTy::iterator I = M.find(key);
75 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
76 }
Ted Kremenek536aa022009-03-30 17:53:05 +000077
78 void AddCheck(GRSimpleAPICheck *A) {
79 assert (A && "Check cannot be null.");
80 AllStmts = F.Concat(A, AllStmts);
81 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000082
Ted Kremenek4adc81e2008-08-13 04:27:00 +000083 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-03-30 17:53:05 +000084 // First handle the auditors that accept all statements.
85 bool isSink = false;
86 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
87 isSink |= (*I)->Audit(N, VMgr);
88
89 // Next handle the auditors that accept only specific statements.
Ted Kremenekbdb435d2008-07-11 18:37:32 +000090 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
91 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
92 MapTy::iterator MI = M.find(key);
Ted Kremenek536aa022009-03-30 17:53:05 +000093 if (MI != M.end()) {
94 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
95 isSink |= (*I)->Audit(N, VMgr);
96 }
Ted Kremenekbdb435d2008-07-11 18:37:32 +000097
Ted Kremenekbdb435d2008-07-11 18:37:32 +000098 return isSink;
99 }
100};
101
102} // end anonymous namespace
103
104//===----------------------------------------------------------------------===//
105// Engine construction and deletion.
106//===----------------------------------------------------------------------===//
107
Ted Kremeneke448ab42008-05-01 18:33:28 +0000108static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
109 IdentifierInfo* II = &Ctx.Idents.get(name);
110 return Ctx.Selectors.getSelector(0, &II);
111}
112
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000113
Ted Kremenek8b233612008-07-02 20:13:38 +0000114GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000115 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000116 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000117 StoreManagerCreator SMC,
118 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000123 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000125 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000126 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
127 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000128 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000129 BR(BRD, *this),
130 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000131
Ted Kremenek1a654b62008-06-20 21:45:25 +0000132GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000133 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000134 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000135}
136
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000137//===----------------------------------------------------------------------===//
138// Utility methods.
139//===----------------------------------------------------------------------===//
140
Ted Kremenek186350f2008-04-23 20:12:28 +0000141
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000142void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000143 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000144 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000145 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000146}
147
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000148void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
149 if (!BatchAuditor)
150 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
151
152 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000153}
154
Ted Kremenek536aa022009-03-30 17:53:05 +0000155void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
156 if (!BatchAuditor)
157 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
158
159 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
160}
161
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000162const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000163 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000164}
165
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000166//===----------------------------------------------------------------------===//
167// Top-level transfer function logic (Dispatcher).
168//===----------------------------------------------------------------------===//
169
170void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
171
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000172 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
173 S->getLocStart(),
174 "Error evaluating statement");
175
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000176 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000177 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000178
179 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000180 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000181 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000182
183 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000184 if (BatchAuditor)
185 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000186
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000187 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000188 SymbolReaper SymReaper(Liveness, SymMgr);
189 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
190 CurrentStmt, SymReaper)
191 : EntryNode->getState();
192
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000193 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000194 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000195
Ted Kremenek241677a2009-01-21 22:26:05 +0000196 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000197 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000198 else {
199 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000200 SaveOr OldHasGen(Builder->HasGeneratedNode);
201
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000202 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
203 Builder->PurgingDeadSymbols = true;
204
Ted Kremenek729a9a22008-07-17 23:15:45 +0000205 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000206 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000207
208 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
209 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000210 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000211
212 bool HasAutoGenerated = false;
213
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000214 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000215
216 NodeSet Dst;
217
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000219 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
220
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000222 Visit(S, *I, Dst);
223
224 // Do we need to auto-generate a node? We only need to do this to generate
225 // a node with a "cleaned" state; GRCoreEngine will actually handle
226 // auto-transitions for other cases.
227 if (Dst.size() == 1 && *Dst.begin() == EntryNode
228 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
229 HasAutoGenerated = true;
230 builder.generateNode(S, GetState(EntryNode), *I);
231 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000232 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000233
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000234 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000235 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000236 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000237
238 // FIXME: Consolidate.
239 StateMgr.CurrentStmt = 0;
240 CurrentStmt = 0;
241
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000243}
244
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000245void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
246 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
247 S->getLocStart(),
248 "Error evaluating statement");
249
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000250 // FIXME: add metadata to the CFG so that we can disable
251 // this check when we KNOW that there is no block-level subexpression.
252 // The motivation is that this check requires a hashtable lookup.
253
254 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
255 Dst.Add(Pred);
256 return;
257 }
258
259 switch (S->getStmtClass()) {
260
261 default:
262 // Cases we intentionally have "default" handle:
263 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
264
265 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
266 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000267
268 case Stmt::ArraySubscriptExprClass:
269 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
270 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000271
272 case Stmt::AsmStmtClass:
273 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
274 break;
275
276 case Stmt::BinaryOperatorClass: {
277 BinaryOperator* B = cast<BinaryOperator>(S);
278
279 if (B->isLogicalOp()) {
280 VisitLogicalExpr(B, Pred, Dst);
281 break;
282 }
283 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000284 const GRState* state = GetState(Pred);
285 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000286 break;
287 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000288
Ted Kremenek48af2a92009-02-25 22:32:02 +0000289 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
290 NodeSet Tmp;
291 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000292 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000293 }
294 else
295 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
296
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000297 break;
298 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000299
Douglas Gregorb4609802008-11-14 16:09:21 +0000300 case Stmt::CallExprClass:
301 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000302 CallExpr* C = cast<CallExpr>(S);
303 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000304 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000305 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000306
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000307 // FIXME: ChooseExpr is really a constant. We need to fix
308 // the CFG do not model them as explicit control-flow.
309
310 case Stmt::ChooseExprClass: { // __builtin_choose_expr
311 ChooseExpr* C = cast<ChooseExpr>(S);
312 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
313 break;
314 }
315
316 case Stmt::CompoundAssignOperatorClass:
317 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
318 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000319
320 case Stmt::CompoundLiteralExprClass:
321 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
322 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000323
324 case Stmt::ConditionalOperatorClass: { // '?' operator
325 ConditionalOperator* C = cast<ConditionalOperator>(S);
326 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
327 break;
328 }
329
330 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000331 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000332 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000333 break;
334
335 case Stmt::DeclStmtClass:
336 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
337 break;
338
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000339 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000340 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000341 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000342 VisitCast(C, C->getSubExpr(), Pred, Dst);
343 break;
344 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000345
346 case Stmt::InitListExprClass:
347 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
348 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000349
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000350 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000351 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
352 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000353
354 case Stmt::ObjCIvarRefExprClass:
355 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
356 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000357
358 case Stmt::ObjCForCollectionStmtClass:
359 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
360 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000361
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000362 case Stmt::ObjCMessageExprClass: {
363 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
364 break;
365 }
366
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000367 case Stmt::ObjCAtThrowStmtClass: {
368 // FIXME: This is not complete. We basically treat @throw as
369 // an abort.
370 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
371 Builder->BuildSinks = true;
372 MakeNode(Dst, S, Pred, GetState(Pred));
373 break;
374 }
375
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000376 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000377 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000378 break;
379
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000380 case Stmt::ReturnStmtClass:
381 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
382 break;
383
Sebastian Redl05189992008-11-11 17:56:53 +0000384 case Stmt::SizeOfAlignOfExprClass:
385 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000386 break;
387
388 case Stmt::StmtExprClass: {
389 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000390
391 if (SE->getSubStmt()->body_empty()) {
392 // Empty statement expression.
393 assert(SE->getType() == getContext().VoidTy
394 && "Empty statement expression must have void type.");
395 Dst.Add(Pred);
396 break;
397 }
398
399 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
400 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000401 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000402 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 else
404 Dst.Add(Pred);
405
406 break;
407 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000408
409 case Stmt::StringLiteralClass:
410 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
411 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000412
Ted Kremenek72374592009-03-18 23:49:26 +0000413 case Stmt::UnaryOperatorClass: {
414 UnaryOperator *U = cast<UnaryOperator>(S);
415 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
416 NodeSet Tmp;
417 VisitUnaryOperator(U, Pred, Tmp, false);
418 EvalEagerlyAssume(Dst, Tmp, U);
419 }
420 else
421 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000422 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000423 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000424 }
425}
426
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000427void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000428
429 Ex = Ex->IgnoreParens();
430
431 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
432 Dst.Add(Pred);
433 return;
434 }
435
436 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000437
438 case Stmt::ArraySubscriptExprClass:
439 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
440 return;
441
442 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000443 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000444 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
445 return;
446
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000447 case Stmt::ObjCIvarRefExprClass:
448 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
449 return;
450
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 case Stmt::UnaryOperatorClass:
452 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
453 return;
454
455 case Stmt::MemberExprClass:
456 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
457 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000458
Ted Kremenek4f090272008-10-27 21:54:31 +0000459 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000460 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000461 return;
462
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000463 case Stmt::ObjCPropertyRefExprClass:
464 // FIXME: Property assignments are lvalues, but not really "locations".
465 // e.g.: self.x = something;
466 // Here the "self.x" really can translate to a method call (setter) when
467 // the assignment is made. Moreover, the entire assignment expression
468 // evaluate to whatever "something" is, not calling the "getter" for
469 // the property (which would make sense since it can have side effects).
470 // We'll probably treat this as a location, but not one that we can
471 // take the address of. Perhaps we need a new SVal class for cases
472 // like thsis?
473 // Note that we have a similar problem for bitfields, since they don't
474 // have "locations" in the sense that we can take their address.
475 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000476 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000477
478 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000479 const GRState* state = GetState(Pred);
480 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
481 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000482 return;
483 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000484
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000485 default:
486 // Arbitrary subexpressions can return aggregate temporaries that
487 // can be used in a lvalue context. We need to enhance our support
488 // of such temporaries in both the environment and the store, so right
489 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000490 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000491 "Other kinds of expressions with non-aggregate/union types do"
492 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000493
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000494 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000495 }
496}
497
498//===----------------------------------------------------------------------===//
499// Block entrance. (Update counters).
500//===----------------------------------------------------------------------===//
501
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000502bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000503 GRBlockCounter BC) {
504
505 return BC.getNumVisited(B->getBlockID()) < 3;
506}
507
508//===----------------------------------------------------------------------===//
509// Branch processing.
510//===----------------------------------------------------------------------===//
511
Ted Kremeneka8538d92009-02-13 01:45:31 +0000512const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000513 Stmt* Terminator,
514 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000515
516 switch (Terminator->getStmtClass()) {
517 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000518 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000519
520 case Stmt::BinaryOperatorClass: { // '&&' and '||'
521
522 BinaryOperator* B = cast<BinaryOperator>(Terminator);
523 BinaryOperator::Opcode Op = B->getOpcode();
524
525 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
526
527 // For &&, if we take the true branch, then the value of the whole
528 // expression is that of the RHS expression.
529 //
530 // For ||, if we take the false branch, then the value of the whole
531 // expression is that of the RHS expression.
532
533 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
534 (Op == BinaryOperator::LOr && !branchTaken)
535 ? B->getRHS() : B->getLHS();
536
Ted Kremeneka8538d92009-02-13 01:45:31 +0000537 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000538 }
539
540 case Stmt::ConditionalOperatorClass: { // ?:
541
542 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
543
544 // For ?, if branchTaken == true then the value is either the LHS or
545 // the condition itself. (GNU extension).
546
547 Expr* Ex;
548
549 if (branchTaken)
550 Ex = C->getLHS() ? C->getLHS() : C->getCond();
551 else
552 Ex = C->getRHS();
553
Ted Kremeneka8538d92009-02-13 01:45:31 +0000554 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000555 }
556
557 case Stmt::ChooseExprClass: { // ?:
558
559 ChooseExpr* C = cast<ChooseExpr>(Terminator);
560
561 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000563 }
564 }
565}
566
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000567/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
568/// to try to recover some path-sensitivity for casts of symbolic
569/// integers that promote their values (which are currently not tracked well).
570/// This function returns the SVal bound to Condition->IgnoreCasts if all the
571// cast(s) did was sign-extend the original value.
572static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
573 Stmt* Condition, ASTContext& Ctx) {
574
575 Expr *Ex = dyn_cast<Expr>(Condition);
576 if (!Ex)
577 return UnknownVal();
578
579 uint64_t bits = 0;
580 bool bitsInit = false;
581
582 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
583 QualType T = CE->getType();
584
585 if (!T->isIntegerType())
586 return UnknownVal();
587
588 uint64_t newBits = Ctx.getTypeSize(T);
589 if (!bitsInit || newBits < bits) {
590 bitsInit = true;
591 bits = newBits;
592 }
593
594 Ex = CE->getSubExpr();
595 }
596
597 // We reached a non-cast. Is it a symbolic value?
598 QualType T = Ex->getType();
599
600 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
601 return UnknownVal();
602
603 return StateMgr.GetSVal(state, Ex);
604}
605
Ted Kremenekaf337412008-11-12 19:24:17 +0000606void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000607 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000608
Ted Kremeneke7d22112008-02-11 19:21:59 +0000609 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000610 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000611 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000612
Ted Kremenekb2331832008-02-15 22:29:00 +0000613 // Check for NULL conditions; e.g. "for(;;)"
614 if (!Condition) {
615 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000616 return;
617 }
618
Ted Kremenek21028dd2009-03-11 03:54:24 +0000619 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
620 Condition->getLocStart(),
621 "Error evaluating branch");
622
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000623 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000624
625 switch (V.getBaseKind()) {
626 default:
627 break;
628
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000629 case SVal::UnknownKind: {
630 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
631 if (Ex->getType()->isIntegerType()) {
632 // Try to recover some path-sensitivity. Right now casts of symbolic
633 // integers that promote their values are currently not tracked well.
634 // If 'Condition' is such an expression, try and recover the
635 // underlying value and use that instead.
636 SVal recovered = RecoverCastedSymbol(getStateManager(),
637 builder.getState(), Condition,
638 getContext());
639
640 if (!recovered.isUnknown()) {
641 V = recovered;
642 break;
643 }
644 }
645 }
646
Ted Kremenek58b33212008-02-26 19:40:44 +0000647 builder.generateNode(MarkBranch(PrevState, Term, true), true);
648 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000649 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000650 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000651
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000652 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000653 NodeTy* N = builder.generateNode(PrevState, true);
654
655 if (N) {
656 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000657 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000658 }
659
660 builder.markInfeasible(false);
661 return;
662 }
663 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000664
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000665 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000666
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000667 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000668 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000669
670 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000671 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000672 else
673 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000674
675 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000676
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000677 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000678 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000679
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000680 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000681 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000682 else
683 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000684}
685
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000686/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000687/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000688void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000689
Ted Kremeneka8538d92009-02-13 01:45:31 +0000690 const GRState* state = builder.getState();
691 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000692
693 // Three possibilities:
694 //
695 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000696 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000697 // (3) We have no clue about the label. Dispatch to all targets.
698 //
699
700 typedef IndirectGotoNodeBuilder::iterator iterator;
701
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000702 if (isa<loc::GotoLabel>(V)) {
703 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000704
705 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000706 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000707 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000708 return;
709 }
710 }
711
712 assert (false && "No block with label.");
713 return;
714 }
715
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000716 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000717 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000718 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000719 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000720 return;
721 }
722
723 // This is really a catch-all. We don't support symbolics yet.
724
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000725 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000726
727 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000728 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000729}
Ted Kremenekf233d482008-02-05 00:26:40 +0000730
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000731
732void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
733 NodeTy* Pred, NodeSet& Dst) {
734
735 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
736
Ted Kremeneka8538d92009-02-13 01:45:31 +0000737 const GRState* state = GetState(Pred);
738 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000739
740 assert (X.isUndef());
741
742 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
743
744 assert (SE);
745
Ted Kremeneka8538d92009-02-13 01:45:31 +0000746 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000747
748 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000749 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000750}
751
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000752/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
753/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000754void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
755 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000756 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000757 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000758 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000760 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000761 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000762 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000763 return;
764 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000765
Ted Kremeneka8538d92009-02-13 01:45:31 +0000766 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000767 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000768
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000769 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000770 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000771
772 // Evaluate the LHS of the case value.
773 Expr::EvalResult V1;
774 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000775
Ted Kremenek72afb372009-01-17 01:54:16 +0000776 // Sanity checks. These go away in Release builds.
777 assert(b && V1.Val.isInt() && !V1.HasSideEffects
778 && "Case condition must evaluate to an integer constant.");
779 b = b; // silence unused variable warning
780 assert(V1.Val.getInt().getBitWidth() ==
781 getContext().getTypeSize(CondE->getType()));
782
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000783 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000784 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000785
786 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000787 b = E->Evaluate(V2, getContext());
788 assert(b && V2.Val.isInt() && !V2.HasSideEffects
789 && "Case condition must evaluate to an integer constant.");
790 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000791 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000792 else
793 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000794
795 // FIXME: Eventually we should replace the logic below with a range
796 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000797 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000798
Ted Kremenek14a11402008-03-17 22:17:56 +0000799 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000800 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000801 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
802 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000803
Ted Kremenek72afb372009-01-17 01:54:16 +0000804 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000805 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000806 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000807
808 if (isFeasible) {
809 builder.generateCaseStmtNode(I, StNew);
810
811 // If CondV evaluates to a constant, then we know that this
812 // is the *only* case that we can take, so stop evaluating the
813 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000814 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000815 return;
816 }
817
818 // Now "assume" that the case doesn't match. Add this state
819 // to the default state (if it is feasible).
820
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000821 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000822 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000823
Ted Kremenek5014ab12008-04-23 05:03:18 +0000824 if (isFeasible) {
825 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000826 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000827 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000828
Ted Kremenek14a11402008-03-17 22:17:56 +0000829 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000830 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000831 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000832
Ted Kremenek72afb372009-01-17 01:54:16 +0000833 ++V1.Val.getInt();
834 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000835
836 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000837 }
838
839 // If we reach here, than we know that the default branch is
840 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000841 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000842}
843
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000844//===----------------------------------------------------------------------===//
845// Transfer functions: logical operations ('&&', '||').
846//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000847
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000848void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000849 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000850
Ted Kremenek05a23782008-02-26 19:05:15 +0000851 assert (B->getOpcode() == BinaryOperator::LAnd ||
852 B->getOpcode() == BinaryOperator::LOr);
853
854 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
855
Ted Kremeneka8538d92009-02-13 01:45:31 +0000856 const GRState* state = GetState(Pred);
857 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000858
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000859 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000860
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000861 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000862
863 assert (Ex);
864
865 if (Ex == B->getRHS()) {
866
Ted Kremeneka8538d92009-02-13 01:45:31 +0000867 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000868
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000869 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000870
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000871 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000872 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000873 return;
874 }
875
Ted Kremenek05a23782008-02-26 19:05:15 +0000876 // We took the RHS. Because the value of the '&&' or '||' expression must
877 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
878 // or 1. Alternatively, we could take a lazy approach, and calculate this
879 // value later when necessary. We don't have the machinery in place for
880 // this right now, and since most logical expressions are used for branches,
881 // the payoff is not likely to be large. Instead, we do eager evaluation.
882
883 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000884 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000885
886 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000887 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000888 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000889
890 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000891 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000892
893 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000894 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000895 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000896 }
897 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000898 // We took the LHS expression. Depending on whether we are '&&' or
899 // '||' we know what the value of the expression is via properties of
900 // the short-circuiting.
901
902 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000903 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000904 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000905}
Ted Kremenek05a23782008-02-26 19:05:15 +0000906
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000907//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000908// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000909//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000910
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000911void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
912 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000913
Ted Kremeneka8538d92009-02-13 01:45:31 +0000914 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000915
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000916 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000917
918 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
919
Ted Kremeneka8538d92009-02-13 01:45:31 +0000920 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000921
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000922 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000923 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000924 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000925 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000926 return;
927
928 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
929 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
930
931 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000932 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000933 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000934 return;
935
936 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000937 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000938 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000939 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000940 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000941 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000942
943 assert (false &&
944 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000945}
946
Ted Kremenek540cbe22008-04-22 04:56:29 +0000947/// VisitArraySubscriptExpr - Transfer function for array accesses
948void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000949 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000950
951 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000952 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000954
955 if (Base->getType()->isVectorType()) {
956 // For vector types get its lvalue.
957 // FIXME: This may not be correct. Is the rvalue of a vector its location?
958 // In fact, I think this is just a hack. We need to get the right
959 // semantics.
960 VisitLValue(Base, Pred, Tmp);
961 }
962 else
963 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000964
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000965 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000966 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000967 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000968
969 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000970 const GRState* state = GetState(*I2);
971 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
972 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000973
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000974 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000975 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000976 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000977 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000978 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000979 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000980}
981
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000982/// VisitMemberExpr - Transfer function for member expressions.
983void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000984 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000985
986 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000987 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000988
989 if (M->isArrow())
990 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
991 else
992 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
993
Douglas Gregor86f19402008-12-20 23:49:58 +0000994 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
995 if (!Field) // FIXME: skipping member expressions for non-fields
996 return;
997
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000998 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000999 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001000 // FIXME: Should we insert some assumption logic in here to determine
1001 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001002 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001003 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001004
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001005 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001006 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001007 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001008 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001009 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001010}
1011
Ted Kremeneka8538d92009-02-13 01:45:31 +00001012/// EvalBind - Handle the semantics of binding a value to a specific location.
1013/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1014void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1015 const GRState* state, SVal location, SVal Val) {
1016
Ted Kremenek41573eb2009-02-14 01:43:44 +00001017 const GRState* newState = 0;
1018
1019 if (location.isUnknown()) {
1020 // We know that the new state will be the same as the old state since
1021 // the location of the binding is "unknown". Consequently, there
1022 // is no reason to just create a new node.
1023 newState = state;
1024 }
1025 else {
1026 // We are binding to a value other than 'unknown'. Perform the binding
1027 // using the StoreManager.
1028 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1029 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001030
Ted Kremenek41573eb2009-02-14 01:43:44 +00001031 // The next thing to do is check if the GRTransferFuncs object wants to
1032 // update the state based on the new binding. If the GRTransferFunc object
1033 // doesn't do anything, just auto-propagate the current state.
1034 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1035 newState != state);
1036
1037 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001038}
1039
1040/// EvalStore - Handle the semantics of a store via an assignment.
1041/// @param Dst The node set to store generated state nodes
1042/// @param Ex The expression representing the location of the store
1043/// @param state The current simulation state
1044/// @param location The location to store the value
1045/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001047 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001048
1049 assert (Builder && "GRStmtNodeBuilder must be defined.");
1050
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001051 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001052 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053
Ted Kremenek8c354752008-12-16 22:02:27 +00001054 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001055 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001056
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001057 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001058 state = GetState(Pred);
1059
1060 // Proceed with the store.
1061 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1062 Builder->PointKind = ProgramPoint::PostStoreKind;
1063 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001064}
1065
1066void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001067 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001068
Ted Kremenek8c354752008-12-16 22:02:27 +00001069 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001070 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001071
Ted Kremenek8c354752008-12-16 22:02:27 +00001072 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001073 return;
1074
Ted Kremeneka8538d92009-02-13 01:45:31 +00001075 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001076
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001077 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001078 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001079
1080 // FIXME: Currently symbolic analysis "generates" new symbols
1081 // for the contents of values. We need a better approach.
1082
Ted Kremenek8c354752008-12-16 22:02:27 +00001083 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001084 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001085 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001086 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001087 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001088 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
1089 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001090 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001091}
1092
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001093void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001094 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001095
1096 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001097 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001098
1099 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1100 MakeNode(Dst, Ex, *I, (*I)->getState());
1101}
1102
Ted Kremenek8c354752008-12-16 22:02:27 +00001103GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001104 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001105 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001106
1107 // Check for loads/stores from/to undefined values.
1108 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001109 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001110 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001111 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001112
Ted Kremenek8c354752008-12-16 22:02:27 +00001113 if (N) {
1114 N->markAsSink();
1115 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116 }
1117
Ted Kremenek8c354752008-12-16 22:02:27 +00001118 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001119 }
1120
1121 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1122 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001123 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001124
1125 // During a load, one of two possible situations arise:
1126 // (1) A crash, because the location (pointer) was NULL.
1127 // (2) The location (pointer) is not NULL, and the dereference works.
1128 //
1129 // We add these assumptions.
1130
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001131 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001132
1133 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001134 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001135 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001136
1137 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001138 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001139 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001140 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001141
1142 if (isFeasibleNull) {
1143
Ted Kremenek7360fda2008-09-18 23:09:54 +00001144 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001145 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001146 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1147
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001148 // We don't use "MakeNode" here because the node will be a sink
1149 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001150 NodeTy* NullNode =
1151 Builder->generateNode(Ex, StNull, Pred,
1152 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001153
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001154 if (NullNode) {
1155
1156 NullNode->markAsSink();
1157
1158 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1159 else ExplicitNullDeref.insert(NullNode);
1160 }
1161 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001162
1163 if (!isFeasibleNotNull)
1164 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001165
1166 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001167 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001168 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1169 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1170 // Get the index of the accessed element.
1171 SVal Idx = ER->getIndex();
1172 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001173 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1174 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001175
1176 bool isFeasibleInBound = false;
1177 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1178 true, isFeasibleInBound);
1179
1180 bool isFeasibleOutBound = false;
1181 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1182 false, isFeasibleOutBound);
1183
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001184 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001185 // Report warning. Make sink node manually.
1186 NodeTy* OOBNode =
1187 Builder->generateNode(Ex, StOutBound, Pred,
1188 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001189
1190 if (OOBNode) {
1191 OOBNode->markAsSink();
1192
1193 if (isFeasibleInBound)
1194 ImplicitOOBMemAccesses.insert(OOBNode);
1195 else
1196 ExplicitOOBMemAccesses.insert(OOBNode);
1197 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001198 }
1199
Ted Kremenek8c354752008-12-16 22:02:27 +00001200 if (!isFeasibleInBound)
1201 return 0;
1202
1203 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001204 }
1205 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001206
Ted Kremenek8c354752008-12-16 22:02:27 +00001207 // Generate a new node indicating the checks succeed.
1208 return Builder->generateNode(Ex, StNotNull, Pred,
1209 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001210}
1211
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001212//===----------------------------------------------------------------------===//
1213// Transfer function: Function calls.
1214//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001215void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001216 CallExpr::arg_iterator AI,
1217 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001218 NodeSet& Dst)
1219{
1220 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001221 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001222 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1223 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001224 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001225
1226 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1227}
1228
1229void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1230 CallExpr::arg_iterator AI,
1231 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001232 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001233 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001234
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001235 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001236 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001237 // If the call argument is being bound to a reference parameter,
1238 // visit it as an lvalue, not an rvalue.
1239 bool VisitAsLvalue = false;
1240 if (Proto && ParamIdx < Proto->getNumArgs())
1241 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1242
1243 NodeSet DstTmp;
1244 if (VisitAsLvalue)
1245 VisitLValue(*AI, Pred, DstTmp);
1246 else
1247 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001248 ++AI;
1249
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001250 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001251 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001252
1253 return;
1254 }
1255
1256 // If we reach here we have processed all of the arguments. Evaluate
1257 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001258
Ted Kremenek994a09b2008-02-25 21:16:03 +00001259 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001260 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001261
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001262 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001263
Ted Kremenekde434242008-02-19 01:44:53 +00001264 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001265 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1266
Ted Kremeneka8538d92009-02-13 01:45:31 +00001267 const GRState* state = GetState(*DI);
1268 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001269
Ted Kremeneka1354a52008-03-03 16:47:31 +00001270 // FIXME: Add support for symbolic function calls (calls involving
1271 // function pointer values that are symbolic).
1272
1273 // Check for undefined control-flow or calls to NULL.
1274
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001275 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001276 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001277
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001278 if (N) {
1279 N->markAsSink();
1280 BadCalls.insert(N);
1281 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001282
Ted Kremenekde434242008-02-19 01:44:53 +00001283 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001284 }
1285
1286 // Check for the "noreturn" attribute.
1287
1288 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1289
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001290 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001291
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001292 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001293
1294 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001295 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001296 else {
1297 // HACK: Some functions are not marked noreturn, and don't return.
1298 // Here are a few hardwired ones. If this takes too long, we can
1299 // potentially cache these results.
1300 const char* s = FD->getIdentifier()->getName();
1301 unsigned n = strlen(s);
1302
1303 switch (n) {
1304 default:
1305 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001306
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001307 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001308 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1309 break;
1310
1311 case 5:
1312 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001313 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001314 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001315 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001316 // FIXME: use Assume to inspect the possible symbolic value of
1317 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001318 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001319 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001320 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001321 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001322 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001323 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001324
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001325 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001326 if (!memcmp(s, "Assert", 6)) {
1327 Builder->BuildSinks = true;
1328 break;
1329 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001330
1331 // FIXME: This is just a wrapper around throwing an exception.
1332 // Eventually inter-procedural analysis should handle this easily.
1333 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1334
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001335 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001336
1337 case 7:
1338 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1339 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001340
Ted Kremenekf47bb782008-04-30 17:54:04 +00001341 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001342 if (!memcmp(s ,"db_error", 8) ||
1343 !memcmp(s, "__assert", 8))
1344 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001345 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001346
1347 case 12:
1348 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1349 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001350
Ted Kremenekf9683082008-09-19 02:30:47 +00001351 case 13:
1352 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1353 break;
1354
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001355 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001356 if (!memcmp(s, "dtrace_assfail", 14) ||
1357 !memcmp(s, "yy_fatal_error", 14))
1358 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001359 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001360
1361 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001362 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001363 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1364 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001365 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001366
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001367 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001368 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001369
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001370 }
1371 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001372
1373 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001374
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001375 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001376
Douglas Gregor3c385e52009-02-14 18:57:46 +00001377 if (unsigned id
1378 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001379 switch (id) {
1380 case Builtin::BI__builtin_expect: {
1381 // For __builtin_expect, just return the value of the subexpression.
1382 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001383 SVal X = GetSVal(state, *(CE->arg_begin()));
1384 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001385 continue;
1386 }
1387
Ted Kremenekb3021332008-11-02 00:35:01 +00001388 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001389 // FIXME: Refactor into StoreManager itself?
1390 MemRegionManager& RM = getStateManager().getRegionManager();
1391 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001392 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001393
1394 // Set the extent of the region in bytes. This enables us to use the
1395 // SVal of the argument directly. If we save the extent in bits, we
1396 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001397 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1398 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001399
Ted Kremeneka8538d92009-02-13 01:45:31 +00001400 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001401 continue;
1402 }
1403
Ted Kremenek55aea312008-03-05 22:59:42 +00001404 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001405 break;
1406 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001407 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001408
Ted Kremenek186350f2008-04-23 20:12:28 +00001409 // Check any arguments passed-by-value against being undefined.
1410
1411 bool badArg = false;
1412
1413 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1414 I != E; ++I) {
1415
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001416 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001417 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001418
Ted Kremenek186350f2008-04-23 20:12:28 +00001419 if (N) {
1420 N->markAsSink();
1421 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001422 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001423
Ted Kremenek186350f2008-04-23 20:12:28 +00001424 badArg = true;
1425 break;
1426 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001427 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001428
1429 if (badArg)
1430 continue;
1431
1432 // Dispatch to the plug-in transfer function.
1433
1434 unsigned size = Dst.size();
1435 SaveOr OldHasGen(Builder->HasGeneratedNode);
1436 EvalCall(Dst, CE, L, *DI);
1437
1438 // Handle the case where no nodes where generated. Auto-generate that
1439 // contains the updated state if we aren't generating sinks.
1440
1441 if (!Builder->BuildSinks && Dst.size() == size &&
1442 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001443 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001444 }
1445}
1446
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001447//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001448// Transfer function: Objective-C ivar references.
1449//===----------------------------------------------------------------------===//
1450
Ted Kremenekf5cae632009-02-28 20:50:43 +00001451static std::pair<const void*,const void*> EagerlyAssumeTag
1452 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1453
Ted Kremenekb2939022009-02-25 23:32:10 +00001454void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001455 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1456 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001457
1458 // Test if the previous node was as the same expression. This can happen
1459 // when the expression fails to evaluate to anything meaningful and
1460 // (as an optimization) we don't generate a node.
1461 ProgramPoint P = Pred->getLocation();
1462 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1463 Dst.Add(Pred);
1464 continue;
1465 }
1466
Ted Kremenek48af2a92009-02-25 22:32:02 +00001467 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001468 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001469 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001470 // First assume that the condition is true.
1471 bool isFeasible = false;
1472 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1473 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001474 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1475 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001476 stateTrue, Pred));
1477 }
1478
1479 // Next, assume that the condition is false.
1480 isFeasible = false;
1481 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1482 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001483 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1484 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001485 stateFalse, Pred));
1486 }
1487 }
1488 else
1489 Dst.Add(Pred);
1490 }
1491}
1492
1493//===----------------------------------------------------------------------===//
1494// Transfer function: Objective-C ivar references.
1495//===----------------------------------------------------------------------===//
1496
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001497void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1498 NodeTy* Pred, NodeSet& Dst,
1499 bool asLValue) {
1500
1501 Expr* Base = cast<Expr>(Ex->getBase());
1502 NodeSet Tmp;
1503 Visit(Base, Pred, Tmp);
1504
1505 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001506 const GRState* state = GetState(*I);
1507 SVal BaseVal = GetSVal(state, Base);
1508 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001509
1510 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001511 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001512 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001513 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001514 }
1515}
1516
1517//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001518// Transfer function: Objective-C fast enumeration 'for' statements.
1519//===----------------------------------------------------------------------===//
1520
1521void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1522 NodeTy* Pred, NodeSet& Dst) {
1523
1524 // ObjCForCollectionStmts are processed in two places. This method
1525 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1526 // statements within a basic block. This transfer function does two things:
1527 //
1528 // (1) binds the next container value to 'element'. This creates a new
1529 // node in the ExplodedGraph.
1530 //
1531 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1532 // whether or not the container has any more elements. This value
1533 // will be tested in ProcessBranch. We need to explicitly bind
1534 // this value because a container can contain nil elements.
1535 //
1536 // FIXME: Eventually this logic should actually do dispatches to
1537 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1538 // This will require simulating a temporary NSFastEnumerationState, either
1539 // through an SVal or through the use of MemRegions. This value can
1540 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1541 // terminates we reclaim the temporary (it goes out of scope) and we
1542 // we can test if the SVal is 0 or if the MemRegion is null (depending
1543 // on what approach we take).
1544 //
1545 // For now: simulate (1) by assigning either a symbol or nil if the
1546 // container is empty. Thus this transfer function will by default
1547 // result in state splitting.
1548
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001549 Stmt* elem = S->getElement();
1550 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001551
1552 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001553 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001554 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001555 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1556 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1557 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001558 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001559
1560 NodeSet Tmp;
1561 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001562
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001563 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1564 const GRState* state = GetState(*I);
1565 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1566 }
1567}
1568
1569void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1570 NodeTy* Pred, NodeSet& Dst,
1571 SVal ElementV) {
1572
1573
Ted Kremenekaf337412008-11-12 19:24:17 +00001574
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001575 // Get the current state. Use 'EvalLocation' to determine if it is a null
1576 // pointer, etc.
1577 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001578
Ted Kremenek8c354752008-12-16 22:02:27 +00001579 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1580 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001581 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001582
1583 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001584
Ted Kremenekaf337412008-11-12 19:24:17 +00001585 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001586 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001587 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1588 GRStateRef hasElems = state.BindExpr(S, TrueV);
1589
Ted Kremenekaf337412008-11-12 19:24:17 +00001590 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001591 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1592 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001593
1594 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1595 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1596 // FIXME: The proper thing to do is to really iterate over the
1597 // container. We will do this with dispatch logic to the store.
1598 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001599 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001600 assert (Loc::IsLocType(T));
1601 unsigned Count = Builder->getCurrentBlockCount();
1602 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1603 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001604
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001605 // Bind the location to 'nil' on the false branch.
1606 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1607 noElems = noElems.BindLoc(ElementV, nilV);
1608 }
1609
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001610 // Create the new nodes.
1611 MakeNode(Dst, S, Pred, hasElems);
1612 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001613}
1614
1615//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001616// Transfer function: Objective-C message expressions.
1617//===----------------------------------------------------------------------===//
1618
1619void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1620 NodeSet& Dst){
1621
1622 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1623 Pred, Dst);
1624}
1625
1626void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001627 ObjCMessageExpr::arg_iterator AI,
1628 ObjCMessageExpr::arg_iterator AE,
1629 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001630 if (AI == AE) {
1631
1632 // Process the receiver.
1633
1634 if (Expr* Receiver = ME->getReceiver()) {
1635 NodeSet Tmp;
1636 Visit(Receiver, Pred, Tmp);
1637
1638 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1639 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1640
1641 return;
1642 }
1643
1644 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1645 return;
1646 }
1647
1648 NodeSet Tmp;
1649 Visit(*AI, Pred, Tmp);
1650
1651 ++AI;
1652
1653 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1654 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1655}
1656
1657void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1658 NodeTy* Pred,
1659 NodeSet& Dst) {
1660
1661 // FIXME: More logic for the processing the method call.
1662
Ted Kremeneka8538d92009-02-13 01:45:31 +00001663 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001664 bool RaisesException = false;
1665
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001666
1667 if (Expr* Receiver = ME->getReceiver()) {
1668
Ted Kremeneka8538d92009-02-13 01:45:31 +00001669 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001670
Ted Kremenek21fe8372009-02-19 04:06:22 +00001671 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001672 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001673 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001674
1675 if (N) {
1676 N->markAsSink();
1677 UndefReceivers.insert(N);
1678 }
1679
1680 return;
1681 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001682
Ted Kremenek21fe8372009-02-19 04:06:22 +00001683 // "Assume" that the receiver is not NULL.
1684 bool isFeasibleNotNull = false;
1685 Assume(state, L, true, isFeasibleNotNull);
1686
1687 // "Assume" that the receiver is NULL.
1688 bool isFeasibleNull = false;
1689 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1690
1691 if (isFeasibleNull) {
1692 // Check if the receiver was nil and the return value a struct.
1693 if (ME->getType()->isRecordType()) {
1694 // The [0 ...] expressions will return garbage. Flag either an
1695 // explicit or implicit error. Because of the structure of this
1696 // function we currently do not bifurfacte the state graph at
1697 // this point.
1698 // FIXME: We should bifurcate and fill the returned struct with
1699 // garbage.
1700 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1701 N->markAsSink();
1702 if (isFeasibleNotNull)
1703 NilReceiverStructRetImplicit.insert(N);
1704 else
1705 NilReceiverStructRetExplicit.insert(N);
1706 }
1707 }
1708 }
1709
Ted Kremeneke448ab42008-05-01 18:33:28 +00001710 // Check if the "raise" message was sent.
1711 if (ME->getSelector() == RaiseSel)
1712 RaisesException = true;
1713 }
1714 else {
1715
1716 IdentifierInfo* ClsName = ME->getClassName();
1717 Selector S = ME->getSelector();
1718
1719 // Check for special instance methods.
1720
1721 if (!NSExceptionII) {
1722 ASTContext& Ctx = getContext();
1723
1724 NSExceptionII = &Ctx.Idents.get("NSException");
1725 }
1726
1727 if (ClsName == NSExceptionII) {
1728
1729 enum { NUM_RAISE_SELECTORS = 2 };
1730
1731 // Lazily create a cache of the selectors.
1732
1733 if (!NSExceptionInstanceRaiseSelectors) {
1734
1735 ASTContext& Ctx = getContext();
1736
1737 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1738
1739 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1740 unsigned idx = 0;
1741
1742 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001743 II.push_back(&Ctx.Idents.get("raise"));
1744 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001745 NSExceptionInstanceRaiseSelectors[idx++] =
1746 Ctx.Selectors.getSelector(II.size(), &II[0]);
1747
1748 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001749 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001750 NSExceptionInstanceRaiseSelectors[idx++] =
1751 Ctx.Selectors.getSelector(II.size(), &II[0]);
1752 }
1753
1754 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1755 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1756 RaisesException = true; break;
1757 }
1758 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001759 }
1760
1761 // Check for any arguments that are uninitialized/undefined.
1762
1763 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1764 I != E; ++I) {
1765
Ted Kremeneka8538d92009-02-13 01:45:31 +00001766 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001767
1768 // Generate an error node for passing an uninitialized/undefined value
1769 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001770 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001771
1772 if (N) {
1773 N->markAsSink();
1774 MsgExprUndefArgs[N] = *I;
1775 }
1776
1777 return;
1778 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001779 }
1780
1781 // Check if we raise an exception. For now treat these as sinks. Eventually
1782 // we will want to handle exceptions properly.
1783
1784 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1785
1786 if (RaisesException)
1787 Builder->BuildSinks = true;
1788
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001789 // Dispatch to plug-in transfer function.
1790
1791 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001792 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001793
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001794 EvalObjCMessageExpr(Dst, ME, Pred);
1795
1796 // Handle the case where no nodes where generated. Auto-generate that
1797 // contains the updated state if we aren't generating sinks.
1798
Ted Kremenekb0533962008-04-18 20:35:30 +00001799 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001800 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001801}
1802
1803//===----------------------------------------------------------------------===//
1804// Transfer functions: Miscellaneous statements.
1805//===----------------------------------------------------------------------===//
1806
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001807void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1808 QualType PtrTy,
1809 Expr* CastE, NodeTy* Pred,
1810 NodeSet& Dst) {
1811 if (!V.isUnknownOrUndef()) {
1812 // FIXME: Determine if the number of bits of the target type is
1813 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00001814 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00001815 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
1816 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001817 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001818 else
1819 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001820}
1821
1822
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001823void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001824 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001825 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001826 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001827
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001828 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001829 T = ExCast->getTypeAsWritten();
1830
Zhongxing Xued340f72008-10-22 08:02:16 +00001831 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001832 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001833 else
1834 Visit(Ex, Pred, S1);
1835
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001836 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00001837 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001838 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001839 Dst.Add(*I1);
1840
Ted Kremenek874d63f2008-01-24 02:02:54 +00001841 return;
1842 }
1843
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001844 // FIXME: The rest of this should probably just go into EvalCall, and
1845 // let the transfer function object be responsible for constructing
1846 // nodes.
1847
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001848 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001849 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001850 const GRState* state = GetState(N);
1851 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00001852 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001853
1854 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001855 if (V.isUnknown()) {
1856 Dst.Add(N);
1857 continue;
1858 }
1859
1860 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00001861 if (V.isUndef())
1862 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001863
1864 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001865 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00001866 C.getCanonicalType(ExTy).getUnqualifiedType())
1867 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001868
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001869 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001870 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001871 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001872 continue;
1873 }
1874
1875 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00001876 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001877 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001878 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001879 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001880 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00001881 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001882 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001883
Ted Kremenekc5302912009-03-05 20:22:13 +00001884 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00001885 }
1886
1887 // Just pass through function and block pointers.
1888 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
1889 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00001890 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001891 }
1892
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001893 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001894 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001895 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00001896 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001897
1898 // Are we casting from an array to a pointer? If so just pass on
1899 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00001900 if (T->isPointerType())
1901 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001902
1903 // Are we casting from an array to an integer? If so, cast the decayed
1904 // pointer value to an integer.
1905 assert(T->isIntegerType());
1906 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1907 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001908 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001909 continue;
1910 }
1911
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001912 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001913 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
1914 // FIXME: For TypedViewRegions, we should handle the case where the
1915 // underlying symbolic pointer is a function pointer or
1916 // block pointer.
1917
1918 // FIXME: We should handle the case where we strip off view layers to get
1919 // to a desugared type.
1920
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001921 assert(Loc::IsLocType(T));
1922 assert(Loc::IsLocType(ExTy));
1923
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001924 const MemRegion* R = RV->getRegion();
1925 StoreManager& StoreMgr = getStoreManager();
1926
1927 // Delegate to store manager to get the result of casting a region
1928 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001929 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001930
1931 // Inspect the result. If the MemRegion* returned is NULL, this
1932 // expression evaluates to UnknownVal.
1933 R = Res.getRegion();
1934 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1935
1936 // Generate the new node in the ExplodedGraph.
1937 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001938 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001939 }
1940
Ted Kremenekdc402902009-03-04 00:14:35 +00001941 // If we are casting a symbolic value, make a symbolic region and a
1942 // TypedViewRegion subregion.
1943 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) {
1944 SymbolRef Sym = SV->getSymbol();
Ted Kremenekc5302912009-03-05 20:22:13 +00001945 QualType SymTy = getSymbolManager().getType(Sym);
1946
1947 // Just pass through symbols that are function or block pointers.
1948 if (SymTy->isFunctionPointerType() || SymTy->isBlockPointerType())
1949 goto PassThrough;
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001950
1951 // Are we casting to a function or block pointer?
1952 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
1953 // FIXME: We should verify that the underlying type of the symbolic
1954 // pointer is a void* (or maybe char*). Other things are an abuse
1955 // of the type system.
1956 goto PassThrough;
1957 }
Ted Kremenekc5302912009-03-05 20:22:13 +00001958
Ted Kremenekdc402902009-03-04 00:14:35 +00001959 StoreManager& StoreMgr = getStoreManager();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001960 const MemRegion* R = StoreMgr.getRegionManager().getSymbolicRegion(Sym);
Ted Kremenekdc402902009-03-04 00:14:35 +00001961
1962 // Delegate to store manager to get the result of casting a region
1963 // to a different type.
1964 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
1965
1966 // Inspect the result. If the MemRegion* returned is NULL, this
1967 // expression evaluates to UnknownVal.
1968 R = Res.getRegion();
1969 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1970
1971 // Generate the new node in the ExplodedGraph.
1972 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
1973 continue;
1974 }
1975
Ted Kremenekc5302912009-03-05 20:22:13 +00001976 // All other cases.
1977 DispatchCast: {
1978 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
1979 EvalCast(V, CastE->getType())));
1980 continue;
1981 }
1982
1983 PassThrough: {
1984 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
1985 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00001986 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001987}
1988
Ted Kremenek4f090272008-10-27 21:54:31 +00001989void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001990 NodeTy* Pred, NodeSet& Dst,
1991 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001992 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1993 NodeSet Tmp;
1994 Visit(ILE, Pred, Tmp);
1995
1996 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001997 const GRState* state = GetState(*I);
1998 SVal ILV = GetSVal(state, ILE);
1999 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002000
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002001 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002002 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002003 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002004 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002005 }
2006}
2007
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002008void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002009
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002010 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002011 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002012
2013 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002014 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002015
Ted Kremenekefd59942008-12-08 22:47:34 +00002016 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002017 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002018
2019 // FIXME: static variables may have an initializer, but the second
2020 // time a function is called those values may not be current.
2021 NodeSet Tmp;
2022
Ted Kremenekaf337412008-11-12 19:24:17 +00002023 if (InitEx)
2024 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002025
2026 if (Tmp.empty())
2027 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002028
2029 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002030 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002031 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002032
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002033 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2034 QualType T = getContext().getCanonicalType(VD->getType());
2035 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2036 // FIXME: Handle multi-dimensional VLAs.
2037
2038 Expr* SE = VLA->getSizeExpr();
2039 SVal Size = GetSVal(state, SE);
2040
2041 if (Size.isUndef()) {
2042 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2043 N->markAsSink();
2044 ExplicitBadSizedVLA.insert(N);
2045 }
2046 continue;
2047 }
2048
2049 bool isFeasibleZero = false;
2050 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2051
2052 bool isFeasibleNotZero = false;
2053 state = Assume(state, Size, true, isFeasibleNotZero);
2054
2055 if (isFeasibleZero) {
2056 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2057 N->markAsSink();
2058 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2059 else ExplicitBadSizedVLA.insert(N);
2060 }
2061 }
2062
2063 if (!isFeasibleNotZero)
2064 continue;
2065 }
2066
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002067 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002068 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002069 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002070 QualType T = VD->getType();
2071
2072 // Recover some path-sensitivity if a scalar value evaluated to
2073 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002074 if (InitVal.isUnknown() ||
2075 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenekaf337412008-11-12 19:24:17 +00002076 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002077 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002078 InitVal = loc::SymbolVal(Sym);
2079 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00002080 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002081 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002082 InitVal = nonloc::SymbolVal(Sym);
2083 }
2084 }
2085
Ted Kremeneka8538d92009-02-13 01:45:31 +00002086 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002087
2088 // The next thing to do is check if the GRTransferFuncs object wants to
2089 // update the state based on the new binding. If the GRTransferFunc
2090 // object doesn't do anything, just auto-propagate the current state.
2091 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2092 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2093 InitVal);
2094 }
2095 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002096 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002097 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002098 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002099 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002100}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002101
Ted Kremenekf75b1862008-10-30 17:47:32 +00002102namespace {
2103 // This class is used by VisitInitListExpr as an item in a worklist
2104 // for processing the values contained in an InitListExpr.
2105class VISIBILITY_HIDDEN InitListWLItem {
2106public:
2107 llvm::ImmutableList<SVal> Vals;
2108 GRExprEngine::NodeTy* N;
2109 InitListExpr::reverse_iterator Itr;
2110
2111 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2112 InitListExpr::reverse_iterator itr)
2113 : Vals(vals), N(n), Itr(itr) {}
2114};
2115}
2116
2117
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002118void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2119 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002120
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002121 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002122 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002123 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002124
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002125 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002126
Ted Kremeneka49e3672008-10-30 23:14:36 +00002127 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002128
Ted Kremeneka49e3672008-10-30 23:14:36 +00002129 // Handle base case where the initializer has no elements.
2130 // e.g: static int* myArray[] = {};
2131 if (NumInitElements == 0) {
2132 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2133 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2134 return;
2135 }
2136
2137 // Create a worklist to process the initializers.
2138 llvm::SmallVector<InitListWLItem, 10> WorkList;
2139 WorkList.reserve(NumInitElements);
2140 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002141 InitListExpr::reverse_iterator ItrEnd = E->rend();
2142
Ted Kremeneka49e3672008-10-30 23:14:36 +00002143 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002144 while (!WorkList.empty()) {
2145 InitListWLItem X = WorkList.back();
2146 WorkList.pop_back();
2147
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002148 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002149 Visit(*X.Itr, X.N, Tmp);
2150
2151 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002152
Ted Kremenekf75b1862008-10-30 17:47:32 +00002153 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2154 // Get the last initializer value.
2155 state = GetState(*NI);
2156 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2157
2158 // Construct the new list of values by prepending the new value to
2159 // the already constructed list.
2160 llvm::ImmutableList<SVal> NewVals =
2161 getBasicVals().consVals(InitV, X.Vals);
2162
2163 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002164 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002165 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002166
Ted Kremenekf75b1862008-10-30 17:47:32 +00002167 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002168 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002169 }
2170 else {
2171 // Still some initializer values to go. Push them onto the worklist.
2172 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2173 }
2174 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002175 }
Ted Kremenek87903072008-10-30 18:34:31 +00002176
2177 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002178 }
2179
Ted Kremenek062e2f92008-11-13 06:10:40 +00002180 if (T->isUnionType() || T->isVectorType()) {
2181 // FIXME: to be implemented.
2182 // Note: That vectors can return true for T->isIntegerType()
2183 MakeNode(Dst, E, Pred, state);
2184 return;
2185 }
2186
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002187 if (Loc::IsLocType(T) || T->isIntegerType()) {
2188 assert (E->getNumInits() == 1);
2189 NodeSet Tmp;
2190 Expr* Init = E->getInit(0);
2191 Visit(Init, Pred, Tmp);
2192 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2193 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002194 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002195 }
2196 return;
2197 }
2198
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002199
2200 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2201 assert(0 && "unprocessed InitListExpr type");
2202}
Ted Kremenekf233d482008-02-05 00:26:40 +00002203
Sebastian Redl05189992008-11-11 17:56:53 +00002204/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2205void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2206 NodeTy* Pred,
2207 NodeSet& Dst) {
2208 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002209 uint64_t amt;
2210
2211 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002212 if (T == getContext().VoidTy) {
2213 // sizeof(void) == 1 byte.
2214 amt = 1;
2215 }
2216 else if (!T.getTypePtr()->isConstantSizeType()) {
2217 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002218 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002219 }
2220 else if (T->isObjCInterfaceType()) {
2221 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2222 // the compiler has laid out its representation. Just report Unknown
2223 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002224 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002225 }
2226 else {
2227 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002228 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002229 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002230 }
2231 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002232 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002233
Ted Kremenek0e561a32008-03-21 21:30:14 +00002234 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002235 BindExpr(GetState(Pred), Ex,
2236 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002237}
2238
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002239
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002240void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002241 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002242
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002243 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002244
2245 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002246 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002247
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002248 case UnaryOperator::Deref: {
2249
2250 Expr* Ex = U->getSubExpr()->IgnoreParens();
2251 NodeSet Tmp;
2252 Visit(Ex, Pred, Tmp);
2253
2254 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002255
Ted Kremeneka8538d92009-02-13 01:45:31 +00002256 const GRState* state = GetState(*I);
2257 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002258
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002259 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002260 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002261 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002262 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002263 }
2264
2265 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002266 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002267
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002268 case UnaryOperator::Real: {
2269
2270 Expr* Ex = U->getSubExpr()->IgnoreParens();
2271 NodeSet Tmp;
2272 Visit(Ex, Pred, Tmp);
2273
2274 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2275
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002276 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002277 if (Ex->getType()->isAnyComplexType()) {
2278 // Just report "Unknown."
2279 Dst.Add(*I);
2280 continue;
2281 }
2282
2283 // For all other types, UnaryOperator::Real is an identity operation.
2284 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002285 const GRState* state = GetState(*I);
2286 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002287 }
2288
2289 return;
2290 }
2291
2292 case UnaryOperator::Imag: {
2293
2294 Expr* Ex = U->getSubExpr()->IgnoreParens();
2295 NodeSet Tmp;
2296 Visit(Ex, Pred, Tmp);
2297
2298 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002299 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002300 if (Ex->getType()->isAnyComplexType()) {
2301 // Just report "Unknown."
2302 Dst.Add(*I);
2303 continue;
2304 }
2305
2306 // For all other types, UnaryOperator::Float returns 0.
2307 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002308 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002309 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002310 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002311 }
2312
2313 return;
2314 }
2315
2316 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002317 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002318 Dst.Add(Pred);
2319 return;
2320
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002321 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002322 case UnaryOperator::Extension: {
2323
2324 // Unary "+" is a no-op, similar to a parentheses. We still have places
2325 // where it may be a block-level expression, so we need to
2326 // generate an extra node that just propagates the value of the
2327 // subexpression.
2328
2329 Expr* Ex = U->getSubExpr()->IgnoreParens();
2330 NodeSet Tmp;
2331 Visit(Ex, Pred, Tmp);
2332
2333 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002334 const GRState* state = GetState(*I);
2335 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002336 }
2337
2338 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002339 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002340
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002341 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002342
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002343 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002344 Expr* Ex = U->getSubExpr()->IgnoreParens();
2345 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002346 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002347
2348 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002349 const GRState* state = GetState(*I);
2350 SVal V = GetSVal(state, Ex);
2351 state = BindExpr(state, U, V);
2352 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002353 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002354
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002355 return;
2356 }
2357
2358 case UnaryOperator::LNot:
2359 case UnaryOperator::Minus:
2360 case UnaryOperator::Not: {
2361
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002362 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002363 Expr* Ex = U->getSubExpr()->IgnoreParens();
2364 NodeSet Tmp;
2365 Visit(Ex, Pred, Tmp);
2366
2367 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002368 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002369
2370 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002371 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002372
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002373 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002374 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002375 continue;
2376 }
2377
Ted Kremenek60595da2008-11-15 04:01:56 +00002378// QualType DstT = getContext().getCanonicalType(U->getType());
2379// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2380//
2381// if (DstT != SrcT) // Perform promotions.
2382// V = EvalCast(V, DstT);
2383//
2384// if (V.isUnknownOrUndef()) {
2385// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2386// continue;
2387// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002388
2389 switch (U->getOpcode()) {
2390 default:
2391 assert(false && "Invalid Opcode.");
2392 break;
2393
2394 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002395 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002396 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002397 break;
2398
2399 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002400 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002401 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002402 break;
2403
2404 case UnaryOperator::LNot:
2405
2406 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2407 //
2408 // Note: technically we do "E == 0", but this is the same in the
2409 // transfer functions as "0 == E".
2410
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002411 if (isa<Loc>(V)) {
2412 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002413 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2414 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002415 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002416 }
2417 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002418 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002419#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002420 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002421 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002422#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002423 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2424 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002425 continue;
2426#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002427 }
2428
2429 break;
2430 }
2431
Ted Kremeneka8538d92009-02-13 01:45:31 +00002432 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002433 }
2434
2435 return;
2436 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002437 }
2438
2439 // Handle ++ and -- (both pre- and post-increment).
2440
2441 assert (U->isIncrementDecrementOp());
2442 NodeSet Tmp;
2443 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002444 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002445
2446 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2447
Ted Kremeneka8538d92009-02-13 01:45:31 +00002448 const GRState* state = GetState(*I);
2449 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002450
2451 // Perform a load.
2452 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002453 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002454
2455 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2456
Ted Kremeneka8538d92009-02-13 01:45:31 +00002457 state = GetState(*I2);
2458 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002459
2460 // Propagate unknown and undefined values.
2461 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002462 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002463 continue;
2464 }
2465
Ted Kremenek21028dd2009-03-11 03:54:24 +00002466 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002467 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2468 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002469
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002470 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002471
2472 // Conjure a new symbol if necessary to recover precision.
2473 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result))
2474 Result = SVal::GetConjuredSymbolVal(SymMgr, Ex,
2475 Builder->getCurrentBlockCount());
2476
Ted Kremeneka8538d92009-02-13 01:45:31 +00002477 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002478
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002479 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002480 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002481 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002482 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002483}
2484
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002485void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2486 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2487}
2488
2489void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2490 AsmStmt::outputs_iterator I,
2491 AsmStmt::outputs_iterator E,
2492 NodeTy* Pred, NodeSet& Dst) {
2493 if (I == E) {
2494 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2495 return;
2496 }
2497
2498 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002499 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002500
2501 ++I;
2502
2503 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2504 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2505}
2506
2507void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2508 AsmStmt::inputs_iterator I,
2509 AsmStmt::inputs_iterator E,
2510 NodeTy* Pred, NodeSet& Dst) {
2511 if (I == E) {
2512
2513 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002514 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002515
2516 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2517 // which interprets the inline asm and stores proper results in the
2518 // outputs.
2519
Ted Kremeneka8538d92009-02-13 01:45:31 +00002520 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002521
2522 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2523 OE = A->end_outputs(); OI != OE; ++OI) {
2524
Ted Kremeneka8538d92009-02-13 01:45:31 +00002525 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002526 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002527
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002528 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002529 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002530 }
2531
Ted Kremeneka8538d92009-02-13 01:45:31 +00002532 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002533 return;
2534 }
2535
2536 NodeSet Tmp;
2537 Visit(*I, Pred, Tmp);
2538
2539 ++I;
2540
2541 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2542 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2543}
2544
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002545void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2546 assert (Builder && "GRStmtNodeBuilder must be defined.");
2547
2548 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002549
Ted Kremenek186350f2008-04-23 20:12:28 +00002550 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2551 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002552
Ted Kremenek729a9a22008-07-17 23:15:45 +00002553 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002554
Ted Kremenekb0533962008-04-18 20:35:30 +00002555 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002556
Ted Kremenekb0533962008-04-18 20:35:30 +00002557 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002558 MakeNode(Dst, S, Pred, GetState(Pred));
2559}
2560
Ted Kremenek02737ed2008-03-31 15:02:58 +00002561void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2562
2563 Expr* R = S->getRetValue();
2564
2565 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002566 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002567 return;
2568 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002569
Ted Kremenek5917d782008-11-21 00:27:44 +00002570 NodeSet Tmp;
2571 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002572
Ted Kremenek5917d782008-11-21 00:27:44 +00002573 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2574 SVal X = GetSVal((*I)->getState(), R);
2575
2576 // Check if we return the address of a stack variable.
2577 if (isa<loc::MemRegionVal>(X)) {
2578 // Determine if the value is on the stack.
2579 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002580
Ted Kremenek5917d782008-11-21 00:27:44 +00002581 if (R && getStateManager().hasStackStorage(R)) {
2582 // Create a special node representing the error.
2583 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2584 N->markAsSink();
2585 RetsStackAddr.insert(N);
2586 }
2587 continue;
2588 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002589 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002590 // Check if we return an undefined value.
2591 else if (X.isUndef()) {
2592 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2593 N->markAsSink();
2594 RetsUndef.insert(N);
2595 }
2596 continue;
2597 }
2598
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002599 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002600 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002601}
Ted Kremenek55deb972008-03-25 00:34:37 +00002602
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002603//===----------------------------------------------------------------------===//
2604// Transfer functions: Binary operators.
2605//===----------------------------------------------------------------------===//
2606
Ted Kremeneka8538d92009-02-13 01:45:31 +00002607const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002608 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002609
2610 // Divide by undefined? (potentially zero)
2611
2612 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002613 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002614
2615 if (DivUndef) {
2616 DivUndef->markAsSink();
2617 ExplicitBadDivides.insert(DivUndef);
2618 }
2619
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002620 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002621 }
2622
2623 // Check for divide/remainder-by-zero.
2624 // First, "assume" that the denominator is 0 or undefined.
2625
2626 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002627 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002628
2629 // Second, "assume" that the denominator cannot be 0.
2630
2631 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002632 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002633
2634 // Create the node for the divide-by-zero (if it occurred).
2635
2636 if (isFeasibleZero)
2637 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2638 DivZeroNode->markAsSink();
2639
2640 if (isFeasibleNotZero)
2641 ImplicitBadDivides.insert(DivZeroNode);
2642 else
2643 ExplicitBadDivides.insert(DivZeroNode);
2644
2645 }
2646
Ted Kremeneka8538d92009-02-13 01:45:31 +00002647 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002648}
2649
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002650void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002651 GRExprEngine::NodeTy* Pred,
2652 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002653
2654 NodeSet Tmp1;
2655 Expr* LHS = B->getLHS()->IgnoreParens();
2656 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002657
Ted Kremenek759623e2008-12-06 02:39:30 +00002658 // FIXME: Add proper support for ObjCKVCRefExpr.
2659 if (isa<ObjCKVCRefExpr>(LHS)) {
2660 Visit(RHS, Pred, Dst);
2661 return;
2662 }
2663
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002664 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002665 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002666 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002667 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002668
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002669 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002670
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002671 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002672
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002673 // Process the RHS.
2674
2675 NodeSet Tmp2;
2676 Visit(RHS, *I1, Tmp2);
2677
2678 // With both the LHS and RHS evaluated, process the operation itself.
2679
2680 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002681
Ted Kremeneka8538d92009-02-13 01:45:31 +00002682 const GRState* state = GetState(*I2);
2683 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002684
Ted Kremeneka8538d92009-02-13 01:45:31 +00002685 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002686 BinaryOperator::Opcode Op = B->getOpcode();
2687
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002688 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002689
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002690 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002691
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002692 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002693 // FIXME: Handle structs.
2694 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002695
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002696 if ((RightV.isUnknown() ||
2697 !getConstraintManager().canReasonAbout(RightV))
2698 && (Loc::IsLocType(T) ||
2699 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002700 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002701 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002702
Ted Kremenek062e2f92008-11-13 06:10:40 +00002703 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002704 ? cast<SVal>(loc::SymbolVal(Sym))
2705 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002706 }
2707
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002708 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002709 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002710 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2711 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002712 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002713 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002714
2715 case BinaryOperator::Div:
2716 case BinaryOperator::Rem:
2717
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002718 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002719 if (RHS->getType()->isIntegerType() &&
2720 RHS->getType()->isScalarType()) {
2721
Ted Kremeneka8538d92009-02-13 01:45:31 +00002722 state = CheckDivideZero(B, state, *I2, RightV);
2723 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002724 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002725
2726 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002727
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002728 default: {
2729
2730 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002731 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002732
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002733 // Process non-assignements except commas or short-circuited
2734 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002735
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002736 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002737
2738 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002739 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002740 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002741 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002742 }
2743 else
2744 Dst.Add(*I2);
2745
Ted Kremenek89063af2008-02-21 19:15:37 +00002746 continue;
2747 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002748
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002749 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002750
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002751 // The operands were *not* undefined, but the result is undefined.
2752 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002753
Ted Kremeneka8538d92009-02-13 01:45:31 +00002754 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002755 UndefNode->markAsSink();
2756 UndefResults.insert(UndefNode);
2757 }
2758
2759 continue;
2760 }
2761
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002762 // Otherwise, create a new node.
2763
Ted Kremeneka8538d92009-02-13 01:45:31 +00002764 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002765 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002766 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002767 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002768
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002769 assert (B->isCompoundAssignmentOp());
2770
Ted Kremenekcad29962009-02-07 00:52:24 +00002771 switch (Op) {
2772 default:
2773 assert(0 && "Invalid opcode for compound assignment.");
2774 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2775 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2776 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2777 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2778 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2779 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2780 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2781 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2782 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2783 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002784 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002785
2786 // Perform a load (the LHS). This performs the checks for
2787 // null dereferences, and so on.
2788 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002789 SVal location = GetSVal(state, LHS);
2790 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002791
2792 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2793
Ted Kremeneka8538d92009-02-13 01:45:31 +00002794 state = GetState(*I3);
2795 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002796
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002797 // Check for divide-by-zero.
2798 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002799 && RHS->getType()->isIntegerType()
2800 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002801
2802 // CheckDivideZero returns a new state where the denominator
2803 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002804 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002805
Ted Kremeneka8538d92009-02-13 01:45:31 +00002806 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002807 continue;
2808 }
2809
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002810 // Propagate undefined values (left-side).
2811 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002812 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002813 continue;
2814 }
2815
2816 // Propagate unknown values (left and right-side).
2817 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002818 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2819 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002820 continue;
2821 }
2822
2823 // At this point:
2824 //
2825 // The LHS is not Undef/Unknown.
2826 // The RHS is not Unknown.
2827
2828 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00002829 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002830 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002831
2832 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
2833 CLHSTy = getContext().getCanonicalType(CTy);
2834
Ted Kremenek60595da2008-11-15 04:01:56 +00002835 QualType LTy = getContext().getCanonicalType(LHS->getType());
2836 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00002837
2838 // Promote LHS.
2839 V = EvalCast(V, CLHSTy);
2840
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002841 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002842 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002843 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00002844 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00002845 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002846 continue;
2847 }
2848
Ted Kremenek60595da2008-11-15 04:01:56 +00002849 // Compute the result of the operation.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002850 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002851
2852 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002853 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002854 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002855 UndefNode->markAsSink();
2856 UndefResults.insert(UndefNode);
2857 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002858 continue;
2859 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002860
2861 // EXPERIMENTAL: "Conjured" symbols.
2862 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002863
2864 SVal LHSVal;
2865
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002866 if ((Result.isUnknown() ||
2867 !getConstraintManager().canReasonAbout(Result))
2868 && (Loc::IsLocType(CTy)
2869 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002870
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002871 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002872
Ted Kremenek60595da2008-11-15 04:01:56 +00002873 // The symbolic value is actually for the type of the left-hand side
2874 // expression, not the computation type, as this is the value the
2875 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002876 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002877 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002878 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002879 : cast<SVal>(nonloc::SymbolVal(Sym));
2880
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002881 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002882 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002883 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002884 else {
2885 // The left-hand side may bind to a different value then the
2886 // computation type.
2887 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2888 }
2889
Ted Kremeneka8538d92009-02-13 01:45:31 +00002890 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2891 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002892 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002893 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002894 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002895}
Ted Kremenekee985462008-01-16 18:18:48 +00002896
2897//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002898// Transfer-function Helpers.
2899//===----------------------------------------------------------------------===//
2900
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002901void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002902 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002903 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002904 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002905
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002906 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002907 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002908
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002909 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002910 MakeNode(Dst, Ex, Pred, *I);
2911}
2912
Ted Kremeneka8538d92009-02-13 01:45:31 +00002913void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002914 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002915 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002916
Ted Kremeneka8538d92009-02-13 01:45:31 +00002917 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002918 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002919}
2920
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002921SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
2922 QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002923
2924 if (L.isUndef() || R.isUndef())
2925 return UndefinedVal();
2926
2927 if (L.isUnknown() || R.isUnknown())
2928 return UnknownVal();
2929
2930 if (isa<Loc>(L)) {
2931 if (isa<Loc>(R))
2932 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2933 else
2934 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2935 }
2936
2937 if (isa<Loc>(R)) {
2938 // Support pointer arithmetic where the increment/decrement operand
2939 // is on the left and the pointer on the right.
2940
2941 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2942
2943 // Commute the operands.
2944 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2945 cast<NonLoc>(L));
2946 }
2947 else
2948 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002949 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002950}
2951
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002952//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002953// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002954//===----------------------------------------------------------------------===//
2955
Ted Kremenekaa66a322008-01-16 21:46:15 +00002956#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002957static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002958static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002959
Ted Kremenekaa66a322008-01-16 21:46:15 +00002960namespace llvm {
2961template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002962struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002963 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002964
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002965 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2966
2967 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002968 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002969 GraphPrintCheckerState->isUndefDeref(N) ||
2970 GraphPrintCheckerState->isUndefStore(N) ||
2971 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002972 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2973 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002974 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002975 GraphPrintCheckerState->isBadCall(N) ||
2976 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002977 return "color=\"red\",style=\"filled\"";
2978
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002979 if (GraphPrintCheckerState->isNoReturnCall(N))
2980 return "color=\"blue\",style=\"filled\"";
2981
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002982 return "";
2983 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002984
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002985 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002986 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002987
2988 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002989 ProgramPoint Loc = N->getLocation();
2990
2991 switch (Loc.getKind()) {
2992 case ProgramPoint::BlockEntranceKind:
2993 Out << "Block Entrance: B"
2994 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2995 break;
2996
2997 case ProgramPoint::BlockExitKind:
2998 assert (false);
2999 break;
3000
Ted Kremenekaa66a322008-01-16 21:46:15 +00003001 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003002 if (isa<PostStmt>(Loc)) {
3003 const PostStmt& L = cast<PostStmt>(Loc);
3004 Stmt* S = L.getStmt();
3005 SourceLocation SLoc = S->getLocStart();
3006
3007 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3008 llvm::raw_os_ostream OutS(Out);
3009 S->printPretty(OutS);
3010 OutS.flush();
3011
3012 if (SLoc.isFileID()) {
3013 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003014 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3015 << " col="
3016 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3017 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003018 }
3019
3020 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3021 Out << "\\|Implicit-Null Dereference.\\l";
3022 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3023 Out << "\\|Explicit-Null Dereference.\\l";
3024 else if (GraphPrintCheckerState->isUndefDeref(N))
3025 Out << "\\|Dereference of undefialied value.\\l";
3026 else if (GraphPrintCheckerState->isUndefStore(N))
3027 Out << "\\|Store to Undefined Loc.";
3028 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3029 Out << "\\|Explicit divide-by zero or undefined value.";
3030 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3031 Out << "\\|Implicit divide-by zero or undefined value.";
3032 else if (GraphPrintCheckerState->isUndefResult(N))
3033 Out << "\\|Result of operation is undefined.";
3034 else if (GraphPrintCheckerState->isNoReturnCall(N))
3035 Out << "\\|Call to function marked \"noreturn\".";
3036 else if (GraphPrintCheckerState->isBadCall(N))
3037 Out << "\\|Call to NULL/Undefined.";
3038 else if (GraphPrintCheckerState->isUndefArg(N))
3039 Out << "\\|Argument in call is undefined";
3040
3041 break;
3042 }
3043
Ted Kremenekaa66a322008-01-16 21:46:15 +00003044 const BlockEdge& E = cast<BlockEdge>(Loc);
3045 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3046 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003047
3048 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003049
3050 SourceLocation SLoc = T->getLocStart();
3051
Ted Kremenekb38911f2008-01-30 23:03:39 +00003052 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003053
Ted Kremeneka95d3752008-09-13 05:16:45 +00003054 llvm::raw_os_ostream OutS(Out);
3055 E.getSrc()->printTerminator(OutS);
3056 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003057
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003058 if (SLoc.isFileID()) {
3059 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003060 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3061 << " col="
3062 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003063 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003064
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003065 if (isa<SwitchStmt>(T)) {
3066 Stmt* Label = E.getDst()->getLabel();
3067
3068 if (Label) {
3069 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3070 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003071 llvm::raw_os_ostream OutS(Out);
3072 C->getLHS()->printPretty(OutS);
3073 OutS.flush();
3074
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003075 if (Stmt* RHS = C->getRHS()) {
3076 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003077 RHS->printPretty(OutS);
3078 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003079 }
3080
3081 Out << ":";
3082 }
3083 else {
3084 assert (isa<DefaultStmt>(Label));
3085 Out << "\\ldefault:";
3086 }
3087 }
3088 else
3089 Out << "\\l(implicit) default:";
3090 }
3091 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003092 // FIXME
3093 }
3094 else {
3095 Out << "\\lCondition: ";
3096 if (*E.getSrc()->succ_begin() == E.getDst())
3097 Out << "true";
3098 else
3099 Out << "false";
3100 }
3101
3102 Out << "\\l";
3103 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003104
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003105 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3106 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003107 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003108 }
3109 }
3110
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003111 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003112
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003113 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3114 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003115
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003116 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003117 return Out.str();
3118 }
3119};
3120} // end llvm namespace
3121#endif
3122
Ted Kremenekffe0f432008-03-07 22:58:01 +00003123#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003124template <typename ITERATOR>
3125GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3126
3127template <>
3128GRExprEngine::NodeTy*
3129GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3130 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3131 return I->first;
3132}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003133#endif
3134
3135void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003136#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003137 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003138 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003139
3140 // Flush any outstanding reports to make sure we cover all the nodes.
3141 // This does not cause them to get displayed.
3142 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3143 const_cast<BugType*>(*I)->FlushReports(BR);
3144
3145 // Iterate through the reports and get their nodes.
3146 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3147 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3148 const BugReportEquivClass& EQ = *I2;
3149 const BugReport &R = **EQ.begin();
3150 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3151 if (N) Src.push_back(N);
3152 }
3153 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003154
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003155 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003156 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003157 else {
3158 GraphPrintCheckerState = this;
3159 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003160
Ted Kremenekffe0f432008-03-07 22:58:01 +00003161 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003162
3163 GraphPrintCheckerState = NULL;
3164 GraphPrintSourceManager = NULL;
3165 }
3166#endif
3167}
3168
3169void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3170#ifndef NDEBUG
3171 GraphPrintCheckerState = this;
3172 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003173
Ted Kremenekcf118d42009-02-04 23:49:09 +00003174 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003175
Ted Kremenekcf118d42009-02-04 23:49:09 +00003176 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003177 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003178 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003179 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003180
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003181 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003182 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003183#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003184}