blob: 2a43b9a89eaaa96ce7c57af97323559c470cb4aa [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 Kremenekb930d7a2009-04-01 06:52:48 +000016#include "clang/AST/ParentMap.h"
Ted Kremenek77349cb2008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
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 Kremenek8e5fb282009-04-09 16:46:55 +0000125 ValMgr(StateMgr.getValueManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000126 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000127 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
128 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000129 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000130 BR(BRD, *this),
131 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132
Ted Kremenek1a654b62008-06-20 21:45:25 +0000133GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000134 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000136}
137
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000138//===----------------------------------------------------------------------===//
139// Utility methods.
140//===----------------------------------------------------------------------===//
141
Ted Kremenek186350f2008-04-23 20:12:28 +0000142
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000143void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000144 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000146 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000147}
148
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000149void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
150 if (!BatchAuditor)
151 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
152
153 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000154}
155
Ted Kremenek536aa022009-03-30 17:53:05 +0000156void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
157 if (!BatchAuditor)
158 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
159
160 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
161}
162
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000163const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000164 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000165}
166
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000167//===----------------------------------------------------------------------===//
168// Top-level transfer function logic (Dispatcher).
169//===----------------------------------------------------------------------===//
170
171void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
172
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000173 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
174 S->getLocStart(),
175 "Error evaluating statement");
176
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000177 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000178 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000179
180 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000181 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000182 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000183
184 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000185 if (BatchAuditor)
186 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000187
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000188 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000189 SymbolReaper SymReaper(Liveness, SymMgr);
190 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
191 CurrentStmt, SymReaper)
192 : EntryNode->getState();
193
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000194 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000195 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196
Ted Kremenek241677a2009-01-21 22:26:05 +0000197 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000198 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000199 else {
200 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000201 SaveOr OldHasGen(Builder->HasGeneratedNode);
202
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000203 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
204 Builder->PurgingDeadSymbols = true;
205
Ted Kremenek729a9a22008-07-17 23:15:45 +0000206 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000207 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000208
209 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
210 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000211 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000212
213 bool HasAutoGenerated = false;
214
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000215 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000216
217 NodeSet Dst;
218
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000219 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000220 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
221
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000223 Visit(S, *I, Dst);
224
225 // Do we need to auto-generate a node? We only need to do this to generate
226 // a node with a "cleaned" state; GRCoreEngine will actually handle
227 // auto-transitions for other cases.
228 if (Dst.size() == 1 && *Dst.begin() == EntryNode
229 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
230 HasAutoGenerated = true;
231 builder.generateNode(S, GetState(EntryNode), *I);
232 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000233 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000234
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000235 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000236 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000237 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000238
239 // FIXME: Consolidate.
240 StateMgr.CurrentStmt = 0;
241 CurrentStmt = 0;
242
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000244}
245
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000246void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
247 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
248 S->getLocStart(),
249 "Error evaluating statement");
250
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000251 // FIXME: add metadata to the CFG so that we can disable
252 // this check when we KNOW that there is no block-level subexpression.
253 // The motivation is that this check requires a hashtable lookup.
254
255 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
256 Dst.Add(Pred);
257 return;
258 }
259
260 switch (S->getStmtClass()) {
261
262 default:
263 // Cases we intentionally have "default" handle:
264 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
265
266 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
267 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000268
269 case Stmt::ArraySubscriptExprClass:
270 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
271 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000272
273 case Stmt::AsmStmtClass:
274 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
275 break;
276
277 case Stmt::BinaryOperatorClass: {
278 BinaryOperator* B = cast<BinaryOperator>(S);
279
280 if (B->isLogicalOp()) {
281 VisitLogicalExpr(B, Pred, Dst);
282 break;
283 }
284 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000285 const GRState* state = GetState(Pred);
286 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000287 break;
288 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000289
Ted Kremenek48af2a92009-02-25 22:32:02 +0000290 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
291 NodeSet Tmp;
292 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000293 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000294 }
295 else
296 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
297
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000298 break;
299 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000300
Douglas Gregorb4609802008-11-14 16:09:21 +0000301 case Stmt::CallExprClass:
302 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000303 CallExpr* C = cast<CallExpr>(S);
304 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000305 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000306 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000307
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000308 // FIXME: ChooseExpr is really a constant. We need to fix
309 // the CFG do not model them as explicit control-flow.
310
311 case Stmt::ChooseExprClass: { // __builtin_choose_expr
312 ChooseExpr* C = cast<ChooseExpr>(S);
313 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
314 break;
315 }
316
317 case Stmt::CompoundAssignOperatorClass:
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
319 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000320
321 case Stmt::CompoundLiteralExprClass:
322 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
323 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324
325 case Stmt::ConditionalOperatorClass: { // '?' operator
326 ConditionalOperator* C = cast<ConditionalOperator>(S);
327 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
328 break;
329 }
330
331 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000332 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000333 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000334 break;
335
336 case Stmt::DeclStmtClass:
337 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
338 break;
339
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000340 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000341 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000342 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000343 VisitCast(C, C->getSubExpr(), Pred, Dst);
344 break;
345 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000346
347 case Stmt::InitListExprClass:
348 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
349 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000350
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000351 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000352 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
353 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000354
355 case Stmt::ObjCIvarRefExprClass:
356 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
357 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000358
359 case Stmt::ObjCForCollectionStmtClass:
360 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
361 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000362
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000363 case Stmt::ObjCMessageExprClass: {
364 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
365 break;
366 }
367
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000368 case Stmt::ObjCAtThrowStmtClass: {
369 // FIXME: This is not complete. We basically treat @throw as
370 // an abort.
371 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
372 Builder->BuildSinks = true;
373 MakeNode(Dst, S, Pred, GetState(Pred));
374 break;
375 }
376
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000377 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000378 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000379 break;
380
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000381 case Stmt::ReturnStmtClass:
382 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
383 break;
384
Sebastian Redl05189992008-11-11 17:56:53 +0000385 case Stmt::SizeOfAlignOfExprClass:
386 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000387 break;
388
389 case Stmt::StmtExprClass: {
390 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000391
392 if (SE->getSubStmt()->body_empty()) {
393 // Empty statement expression.
394 assert(SE->getType() == getContext().VoidTy
395 && "Empty statement expression must have void type.");
396 Dst.Add(Pred);
397 break;
398 }
399
400 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
401 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000402 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000403 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000404 else
405 Dst.Add(Pred);
406
407 break;
408 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000409
410 case Stmt::StringLiteralClass:
411 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
412 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000413
Ted Kremenek72374592009-03-18 23:49:26 +0000414 case Stmt::UnaryOperatorClass: {
415 UnaryOperator *U = cast<UnaryOperator>(S);
416 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
417 NodeSet Tmp;
418 VisitUnaryOperator(U, Pred, Tmp, false);
419 EvalEagerlyAssume(Dst, Tmp, U);
420 }
421 else
422 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000423 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000424 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000425 }
426}
427
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000428void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000429
430 Ex = Ex->IgnoreParens();
431
432 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
433 Dst.Add(Pred);
434 return;
435 }
436
437 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000438
439 case Stmt::ArraySubscriptExprClass:
440 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
441 return;
442
443 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000444 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000445 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
446 return;
447
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000448 case Stmt::ObjCIvarRefExprClass:
449 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
450 return;
451
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000452 case Stmt::UnaryOperatorClass:
453 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
454 return;
455
456 case Stmt::MemberExprClass:
457 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
458 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000459
Ted Kremenek4f090272008-10-27 21:54:31 +0000460 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000461 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000462 return;
463
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000464 case Stmt::ObjCPropertyRefExprClass:
465 // FIXME: Property assignments are lvalues, but not really "locations".
466 // e.g.: self.x = something;
467 // Here the "self.x" really can translate to a method call (setter) when
468 // the assignment is made. Moreover, the entire assignment expression
469 // evaluate to whatever "something" is, not calling the "getter" for
470 // the property (which would make sense since it can have side effects).
471 // We'll probably treat this as a location, but not one that we can
472 // take the address of. Perhaps we need a new SVal class for cases
473 // like thsis?
474 // Note that we have a similar problem for bitfields, since they don't
475 // have "locations" in the sense that we can take their address.
476 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000477 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000478
479 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000480 const GRState* state = GetState(Pred);
481 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
482 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000483 return;
484 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000485
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000486 default:
487 // Arbitrary subexpressions can return aggregate temporaries that
488 // can be used in a lvalue context. We need to enhance our support
489 // of such temporaries in both the environment and the store, so right
490 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000491 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000492 "Other kinds of expressions with non-aggregate/union types do"
493 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000494
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000495 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000496 }
497}
498
499//===----------------------------------------------------------------------===//
500// Block entrance. (Update counters).
501//===----------------------------------------------------------------------===//
502
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000503bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000504 GRBlockCounter BC) {
505
506 return BC.getNumVisited(B->getBlockID()) < 3;
507}
508
509//===----------------------------------------------------------------------===//
510// Branch processing.
511//===----------------------------------------------------------------------===//
512
Ted Kremeneka8538d92009-02-13 01:45:31 +0000513const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000514 Stmt* Terminator,
515 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000516
517 switch (Terminator->getStmtClass()) {
518 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000519 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000520
521 case Stmt::BinaryOperatorClass: { // '&&' and '||'
522
523 BinaryOperator* B = cast<BinaryOperator>(Terminator);
524 BinaryOperator::Opcode Op = B->getOpcode();
525
526 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
527
528 // For &&, if we take the true branch, then the value of the whole
529 // expression is that of the RHS expression.
530 //
531 // For ||, if we take the false branch, then the value of the whole
532 // expression is that of the RHS expression.
533
534 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
535 (Op == BinaryOperator::LOr && !branchTaken)
536 ? B->getRHS() : B->getLHS();
537
Ted Kremeneka8538d92009-02-13 01:45:31 +0000538 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000539 }
540
541 case Stmt::ConditionalOperatorClass: { // ?:
542
543 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
544
545 // For ?, if branchTaken == true then the value is either the LHS or
546 // the condition itself. (GNU extension).
547
548 Expr* Ex;
549
550 if (branchTaken)
551 Ex = C->getLHS() ? C->getLHS() : C->getCond();
552 else
553 Ex = C->getRHS();
554
Ted Kremeneka8538d92009-02-13 01:45:31 +0000555 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000556 }
557
558 case Stmt::ChooseExprClass: { // ?:
559
560 ChooseExpr* C = cast<ChooseExpr>(Terminator);
561
562 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000563 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000564 }
565 }
566}
567
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000568/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
569/// to try to recover some path-sensitivity for casts of symbolic
570/// integers that promote their values (which are currently not tracked well).
571/// This function returns the SVal bound to Condition->IgnoreCasts if all the
572// cast(s) did was sign-extend the original value.
573static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
574 Stmt* Condition, ASTContext& Ctx) {
575
576 Expr *Ex = dyn_cast<Expr>(Condition);
577 if (!Ex)
578 return UnknownVal();
579
580 uint64_t bits = 0;
581 bool bitsInit = false;
582
583 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
584 QualType T = CE->getType();
585
586 if (!T->isIntegerType())
587 return UnknownVal();
588
589 uint64_t newBits = Ctx.getTypeSize(T);
590 if (!bitsInit || newBits < bits) {
591 bitsInit = true;
592 bits = newBits;
593 }
594
595 Ex = CE->getSubExpr();
596 }
597
598 // We reached a non-cast. Is it a symbolic value?
599 QualType T = Ex->getType();
600
601 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
602 return UnknownVal();
603
604 return StateMgr.GetSVal(state, Ex);
605}
606
Ted Kremenekaf337412008-11-12 19:24:17 +0000607void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000608 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000609
Ted Kremeneke7d22112008-02-11 19:21:59 +0000610 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000611 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000612 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000613
Ted Kremenekb2331832008-02-15 22:29:00 +0000614 // Check for NULL conditions; e.g. "for(;;)"
615 if (!Condition) {
616 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000617 return;
618 }
619
Ted Kremenek21028dd2009-03-11 03:54:24 +0000620 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
621 Condition->getLocStart(),
622 "Error evaluating branch");
623
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000624 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000625
626 switch (V.getBaseKind()) {
627 default:
628 break;
629
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000630 case SVal::UnknownKind: {
631 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
632 if (Ex->getType()->isIntegerType()) {
633 // Try to recover some path-sensitivity. Right now casts of symbolic
634 // integers that promote their values are currently not tracked well.
635 // If 'Condition' is such an expression, try and recover the
636 // underlying value and use that instead.
637 SVal recovered = RecoverCastedSymbol(getStateManager(),
638 builder.getState(), Condition,
639 getContext());
640
641 if (!recovered.isUnknown()) {
642 V = recovered;
643 break;
644 }
645 }
646 }
647
Ted Kremenek58b33212008-02-26 19:40:44 +0000648 builder.generateNode(MarkBranch(PrevState, Term, true), true);
649 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000650 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000651 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000652
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000653 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000654 NodeTy* N = builder.generateNode(PrevState, true);
655
656 if (N) {
657 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000658 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000659 }
660
661 builder.markInfeasible(false);
662 return;
663 }
664 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000665
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000666 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000667
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000668 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000669 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000670
671 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000672 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000673 else
674 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000675
676 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000677
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000678 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000679 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000680
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000681 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000682 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000683 else
684 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000685}
686
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000687/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000688/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000689void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000690
Ted Kremeneka8538d92009-02-13 01:45:31 +0000691 const GRState* state = builder.getState();
692 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000693
694 // Three possibilities:
695 //
696 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000697 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000698 // (3) We have no clue about the label. Dispatch to all targets.
699 //
700
701 typedef IndirectGotoNodeBuilder::iterator iterator;
702
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000703 if (isa<loc::GotoLabel>(V)) {
704 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000705
706 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000707 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000708 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000709 return;
710 }
711 }
712
713 assert (false && "No block with label.");
714 return;
715 }
716
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000717 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000718 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000719 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000720 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000721 return;
722 }
723
724 // This is really a catch-all. We don't support symbolics yet.
725
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000726 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000727
728 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000729 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000730}
Ted Kremenekf233d482008-02-05 00:26:40 +0000731
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000732
733void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
734 NodeTy* Pred, NodeSet& Dst) {
735
736 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
737
Ted Kremeneka8538d92009-02-13 01:45:31 +0000738 const GRState* state = GetState(Pred);
739 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000740
741 assert (X.isUndef());
742
743 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
744
745 assert (SE);
746
Ted Kremeneka8538d92009-02-13 01:45:31 +0000747 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000748
749 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000750 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000751}
752
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000753/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
754/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000755void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
756 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000757 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000758 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000759 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000760
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000761 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000762 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000763 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000764 return;
765 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000766
Ted Kremeneka8538d92009-02-13 01:45:31 +0000767 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000768 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000769
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000770 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000771 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000772
773 // Evaluate the LHS of the case value.
774 Expr::EvalResult V1;
775 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000776
Ted Kremenek72afb372009-01-17 01:54:16 +0000777 // Sanity checks. These go away in Release builds.
778 assert(b && V1.Val.isInt() && !V1.HasSideEffects
779 && "Case condition must evaluate to an integer constant.");
780 b = b; // silence unused variable warning
781 assert(V1.Val.getInt().getBitWidth() ==
782 getContext().getTypeSize(CondE->getType()));
783
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000784 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000785 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000786
787 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000788 b = E->Evaluate(V2, getContext());
789 assert(b && V2.Val.isInt() && !V2.HasSideEffects
790 && "Case condition must evaluate to an integer constant.");
791 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000792 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000793 else
794 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000795
796 // FIXME: Eventually we should replace the logic below with a range
797 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000798 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000799
Ted Kremenek14a11402008-03-17 22:17:56 +0000800 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000801 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000802 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
803 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000804
Ted Kremenek72afb372009-01-17 01:54:16 +0000805 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000806 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000807 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000808
809 if (isFeasible) {
810 builder.generateCaseStmtNode(I, StNew);
811
812 // If CondV evaluates to a constant, then we know that this
813 // is the *only* case that we can take, so stop evaluating the
814 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000815 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000816 return;
817 }
818
819 // Now "assume" that the case doesn't match. Add this state
820 // to the default state (if it is feasible).
821
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000822 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000823 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000824
Ted Kremenek5014ab12008-04-23 05:03:18 +0000825 if (isFeasible) {
826 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000827 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000828 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000829
Ted Kremenek14a11402008-03-17 22:17:56 +0000830 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000831 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000832 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000833
Ted Kremenek72afb372009-01-17 01:54:16 +0000834 ++V1.Val.getInt();
835 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000836
837 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000838 }
839
840 // If we reach here, than we know that the default branch is
841 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000842 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000843}
844
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000845//===----------------------------------------------------------------------===//
846// Transfer functions: logical operations ('&&', '||').
847//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000848
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000849void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000850 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000851
Ted Kremenek05a23782008-02-26 19:05:15 +0000852 assert (B->getOpcode() == BinaryOperator::LAnd ||
853 B->getOpcode() == BinaryOperator::LOr);
854
855 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
856
Ted Kremeneka8538d92009-02-13 01:45:31 +0000857 const GRState* state = GetState(Pred);
858 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000859
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000860 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000861
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000862 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000863
864 assert (Ex);
865
866 if (Ex == B->getRHS()) {
867
Ted Kremeneka8538d92009-02-13 01:45:31 +0000868 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000869
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000870 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000871
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000872 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000873 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000874 return;
875 }
876
Ted Kremenek05a23782008-02-26 19:05:15 +0000877 // We took the RHS. Because the value of the '&&' or '||' expression must
878 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
879 // or 1. Alternatively, we could take a lazy approach, and calculate this
880 // value later when necessary. We don't have the machinery in place for
881 // this right now, and since most logical expressions are used for branches,
882 // the payoff is not likely to be large. Instead, we do eager evaluation.
883
884 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000885 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000886
887 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000888 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000889 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000890
891 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000892 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000893
894 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000895 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000896 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000897 }
898 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000899 // We took the LHS expression. Depending on whether we are '&&' or
900 // '||' we know what the value of the expression is via properties of
901 // the short-circuiting.
902
903 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000904 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000905 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000906}
Ted Kremenek05a23782008-02-26 19:05:15 +0000907
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000908//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000909// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000910//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000911
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000912void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
913 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000914
Ted Kremeneka8538d92009-02-13 01:45:31 +0000915 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000916
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000917 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000918
919 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
920
Ted Kremeneka8538d92009-02-13 01:45:31 +0000921 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000922
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000923 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000924 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000925 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000926 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000927 return;
928
929 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
930 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
931
932 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000933 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000934 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000935 return;
936
937 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000938 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000939 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000940 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000941 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000942 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000943
944 assert (false &&
945 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000946}
947
Ted Kremenek540cbe22008-04-22 04:56:29 +0000948/// VisitArraySubscriptExpr - Transfer function for array accesses
949void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000950 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000951
952 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000953 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000954 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000955
956 if (Base->getType()->isVectorType()) {
957 // For vector types get its lvalue.
958 // FIXME: This may not be correct. Is the rvalue of a vector its location?
959 // In fact, I think this is just a hack. We need to get the right
960 // semantics.
961 VisitLValue(Base, Pred, Tmp);
962 }
963 else
964 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000965
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000966 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000967 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000968 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000969
970 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000971 const GRState* state = GetState(*I2);
972 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
973 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000974
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000975 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000976 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000977 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000978 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000979 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000980 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000981}
982
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000983/// VisitMemberExpr - Transfer function for member expressions.
984void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000985 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000986
987 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000988 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000989
990 if (M->isArrow())
991 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
992 else
993 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
994
Douglas Gregor86f19402008-12-20 23:49:58 +0000995 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
996 if (!Field) // FIXME: skipping member expressions for non-fields
997 return;
998
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000999 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001000 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001001 // FIXME: Should we insert some assumption logic in here to determine
1002 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001003 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001004 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001005
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001006 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001007 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001008 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001009 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001010 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001011}
1012
Ted Kremeneka8538d92009-02-13 01:45:31 +00001013/// EvalBind - Handle the semantics of binding a value to a specific location.
1014/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1015void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1016 const GRState* state, SVal location, SVal Val) {
1017
Ted Kremenek41573eb2009-02-14 01:43:44 +00001018 const GRState* newState = 0;
1019
1020 if (location.isUnknown()) {
1021 // We know that the new state will be the same as the old state since
1022 // the location of the binding is "unknown". Consequently, there
1023 // is no reason to just create a new node.
1024 newState = state;
1025 }
1026 else {
1027 // We are binding to a value other than 'unknown'. Perform the binding
1028 // using the StoreManager.
1029 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1030 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001031
Ted Kremenek41573eb2009-02-14 01:43:44 +00001032 // The next thing to do is check if the GRTransferFuncs object wants to
1033 // update the state based on the new binding. If the GRTransferFunc object
1034 // doesn't do anything, just auto-propagate the current state.
1035 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1036 newState != state);
1037
1038 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001039}
1040
1041/// EvalStore - Handle the semantics of a store via an assignment.
1042/// @param Dst The node set to store generated state nodes
1043/// @param Ex The expression representing the location of the store
1044/// @param state The current simulation state
1045/// @param location The location to store the value
1046/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001047void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001048 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001049
1050 assert (Builder && "GRStmtNodeBuilder must be defined.");
1051
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001053 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001054
Ted Kremenek8c354752008-12-16 22:02:27 +00001055 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001056 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001057
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001058 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001059 state = GetState(Pred);
1060
1061 // Proceed with the store.
1062 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1063 Builder->PointKind = ProgramPoint::PostStoreKind;
1064 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001065}
1066
1067void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001068 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001069
Ted Kremenek8c354752008-12-16 22:02:27 +00001070 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001071 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001072
Ted Kremenek8c354752008-12-16 22:02:27 +00001073 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001074 return;
1075
Ted Kremeneka8538d92009-02-13 01:45:31 +00001076 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001077
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001078 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001079 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001080
1081 // FIXME: Currently symbolic analysis "generates" new symbols
1082 // for the contents of values. We need a better approach.
1083
Ted Kremenek8c354752008-12-16 22:02:27 +00001084 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001085 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001086 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001087 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001088 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001089 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
1090 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001091 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001092}
1093
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001094void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001095 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001096
1097 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001098 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001099
1100 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1101 MakeNode(Dst, Ex, *I, (*I)->getState());
1102}
1103
Ted Kremenek8c354752008-12-16 22:02:27 +00001104GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001105 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001106 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001107
1108 // Check for loads/stores from/to undefined values.
1109 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001110 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001111 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001112 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001113
Ted Kremenek8c354752008-12-16 22:02:27 +00001114 if (N) {
1115 N->markAsSink();
1116 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001117 }
1118
Ted Kremenek8c354752008-12-16 22:02:27 +00001119 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001120 }
1121
1122 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1123 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001124 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001125
1126 // During a load, one of two possible situations arise:
1127 // (1) A crash, because the location (pointer) was NULL.
1128 // (2) The location (pointer) is not NULL, and the dereference works.
1129 //
1130 // We add these assumptions.
1131
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001132 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001133
1134 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001135 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001136 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001137
1138 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001139 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001140 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001141 getStateManager());
Zhongxing Xua1718c72009-04-03 07:33:13 +00001142
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001143 if (isFeasibleNull) {
1144
Ted Kremenek7360fda2008-09-18 23:09:54 +00001145 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001146 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001147 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1148
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001149 // We don't use "MakeNode" here because the node will be a sink
1150 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001151 NodeTy* NullNode =
1152 Builder->generateNode(Ex, StNull, Pred,
1153 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001154
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001155 if (NullNode) {
1156
1157 NullNode->markAsSink();
1158
1159 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1160 else ExplicitNullDeref.insert(NullNode);
1161 }
1162 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001163
1164 if (!isFeasibleNotNull)
1165 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001166
1167 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001168 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001169 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1170 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1171 // Get the index of the accessed element.
1172 SVal Idx = ER->getIndex();
1173 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001174 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1175 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001176
1177 bool isFeasibleInBound = false;
1178 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1179 true, isFeasibleInBound);
1180
1181 bool isFeasibleOutBound = false;
1182 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1183 false, isFeasibleOutBound);
1184
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001185 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001186 // Report warning. Make sink node manually.
1187 NodeTy* OOBNode =
1188 Builder->generateNode(Ex, StOutBound, Pred,
1189 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001190
1191 if (OOBNode) {
1192 OOBNode->markAsSink();
1193
1194 if (isFeasibleInBound)
1195 ImplicitOOBMemAccesses.insert(OOBNode);
1196 else
1197 ExplicitOOBMemAccesses.insert(OOBNode);
1198 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001199 }
1200
Ted Kremenek8c354752008-12-16 22:02:27 +00001201 if (!isFeasibleInBound)
1202 return 0;
1203
1204 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001205 }
1206 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001207
Ted Kremenek8c354752008-12-16 22:02:27 +00001208 // Generate a new node indicating the checks succeed.
1209 return Builder->generateNode(Ex, StNotNull, Pred,
1210 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001211}
1212
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001213//===----------------------------------------------------------------------===//
1214// Transfer function: Function calls.
1215//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001216void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001217 CallExpr::arg_iterator AI,
1218 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001219 NodeSet& Dst)
1220{
1221 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001222 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001223 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1224 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001225 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001226
1227 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1228}
1229
1230void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1231 CallExpr::arg_iterator AI,
1232 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001233 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001234 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001235
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001236 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001237 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001238 // If the call argument is being bound to a reference parameter,
1239 // visit it as an lvalue, not an rvalue.
1240 bool VisitAsLvalue = false;
1241 if (Proto && ParamIdx < Proto->getNumArgs())
1242 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1243
1244 NodeSet DstTmp;
1245 if (VisitAsLvalue)
1246 VisitLValue(*AI, Pred, DstTmp);
1247 else
1248 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001249 ++AI;
1250
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001251 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001252 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001253
1254 return;
1255 }
1256
1257 // If we reach here we have processed all of the arguments. Evaluate
1258 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001259
Ted Kremenek994a09b2008-02-25 21:16:03 +00001260 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001261 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001262
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001263 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001264
Ted Kremenekde434242008-02-19 01:44:53 +00001265 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001266 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1267
Ted Kremeneka8538d92009-02-13 01:45:31 +00001268 const GRState* state = GetState(*DI);
1269 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001270
Ted Kremeneka1354a52008-03-03 16:47:31 +00001271 // FIXME: Add support for symbolic function calls (calls involving
1272 // function pointer values that are symbolic).
1273
1274 // Check for undefined control-flow or calls to NULL.
1275
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001276 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001277 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001278
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001279 if (N) {
1280 N->markAsSink();
1281 BadCalls.insert(N);
1282 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001283
Ted Kremenekde434242008-02-19 01:44:53 +00001284 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001285 }
1286
1287 // Check for the "noreturn" attribute.
1288
1289 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1290
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001291 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001292
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001293 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001294
1295 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001296 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001297 else {
1298 // HACK: Some functions are not marked noreturn, and don't return.
1299 // Here are a few hardwired ones. If this takes too long, we can
1300 // potentially cache these results.
1301 const char* s = FD->getIdentifier()->getName();
1302 unsigned n = strlen(s);
1303
1304 switch (n) {
1305 default:
1306 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001307
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001308 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001309 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1310 break;
1311
1312 case 5:
1313 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001314 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001315 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001316 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001317 // FIXME: use Assume to inspect the possible symbolic value of
1318 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001319 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001320 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001321 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001322 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001323 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001324 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001325
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001326 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001327 if (!memcmp(s, "Assert", 6)) {
1328 Builder->BuildSinks = true;
1329 break;
1330 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001331
1332 // FIXME: This is just a wrapper around throwing an exception.
1333 // Eventually inter-procedural analysis should handle this easily.
1334 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1335
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001336 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001337
1338 case 7:
1339 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1340 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001341
Ted Kremenekf47bb782008-04-30 17:54:04 +00001342 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001343 if (!memcmp(s ,"db_error", 8) ||
1344 !memcmp(s, "__assert", 8))
1345 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001346 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001347
1348 case 12:
1349 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1350 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001351
Ted Kremenekf9683082008-09-19 02:30:47 +00001352 case 13:
1353 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1354 break;
1355
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001356 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001357 if (!memcmp(s, "dtrace_assfail", 14) ||
1358 !memcmp(s, "yy_fatal_error", 14))
1359 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001360 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001361
1362 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001363 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001364 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1365 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001366 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001367
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001368 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001369 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001370
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001371 }
1372 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001373
1374 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001375
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001376 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001377
Douglas Gregor3c385e52009-02-14 18:57:46 +00001378 if (unsigned id
1379 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001380 switch (id) {
1381 case Builtin::BI__builtin_expect: {
1382 // For __builtin_expect, just return the value of the subexpression.
1383 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001384 SVal X = GetSVal(state, *(CE->arg_begin()));
1385 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001386 continue;
1387 }
1388
Ted Kremenekb3021332008-11-02 00:35:01 +00001389 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001390 // FIXME: Refactor into StoreManager itself?
1391 MemRegionManager& RM = getStateManager().getRegionManager();
1392 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001393 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001394
1395 // Set the extent of the region in bytes. This enables us to use the
1396 // SVal of the argument directly. If we save the extent in bits, we
1397 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001398 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1399 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001400
Ted Kremeneka8538d92009-02-13 01:45:31 +00001401 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001402 continue;
1403 }
1404
Ted Kremenek55aea312008-03-05 22:59:42 +00001405 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001406 break;
1407 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001408 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001409
Ted Kremenek186350f2008-04-23 20:12:28 +00001410 // Check any arguments passed-by-value against being undefined.
1411
1412 bool badArg = false;
1413
1414 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1415 I != E; ++I) {
1416
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001417 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001418 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001419
Ted Kremenek186350f2008-04-23 20:12:28 +00001420 if (N) {
1421 N->markAsSink();
1422 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001423 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001424
Ted Kremenek186350f2008-04-23 20:12:28 +00001425 badArg = true;
1426 break;
1427 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001428 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001429
1430 if (badArg)
1431 continue;
1432
1433 // Dispatch to the plug-in transfer function.
1434
1435 unsigned size = Dst.size();
1436 SaveOr OldHasGen(Builder->HasGeneratedNode);
1437 EvalCall(Dst, CE, L, *DI);
1438
1439 // Handle the case where no nodes where generated. Auto-generate that
1440 // contains the updated state if we aren't generating sinks.
1441
1442 if (!Builder->BuildSinks && Dst.size() == size &&
1443 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001444 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001445 }
1446}
1447
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001448//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001449// Transfer function: Objective-C ivar references.
1450//===----------------------------------------------------------------------===//
1451
Ted Kremenekf5cae632009-02-28 20:50:43 +00001452static std::pair<const void*,const void*> EagerlyAssumeTag
1453 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1454
Ted Kremenekb2939022009-02-25 23:32:10 +00001455void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001456 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1457 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001458
1459 // Test if the previous node was as the same expression. This can happen
1460 // when the expression fails to evaluate to anything meaningful and
1461 // (as an optimization) we don't generate a node.
1462 ProgramPoint P = Pred->getLocation();
1463 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1464 Dst.Add(Pred);
1465 continue;
1466 }
1467
Ted Kremenek48af2a92009-02-25 22:32:02 +00001468 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001469 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001470 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001471 // First assume that the condition is true.
1472 bool isFeasible = false;
1473 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1474 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001475 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1476 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001477 stateTrue, Pred));
1478 }
1479
1480 // Next, assume that the condition is false.
1481 isFeasible = false;
1482 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1483 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001484 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1485 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001486 stateFalse, Pred));
1487 }
1488 }
1489 else
1490 Dst.Add(Pred);
1491 }
1492}
1493
1494//===----------------------------------------------------------------------===//
1495// Transfer function: Objective-C ivar references.
1496//===----------------------------------------------------------------------===//
1497
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001498void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1499 NodeTy* Pred, NodeSet& Dst,
1500 bool asLValue) {
1501
1502 Expr* Base = cast<Expr>(Ex->getBase());
1503 NodeSet Tmp;
1504 Visit(Base, Pred, Tmp);
1505
1506 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001507 const GRState* state = GetState(*I);
1508 SVal BaseVal = GetSVal(state, Base);
1509 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001510
1511 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001512 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001513 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001514 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001515 }
1516}
1517
1518//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001519// Transfer function: Objective-C fast enumeration 'for' statements.
1520//===----------------------------------------------------------------------===//
1521
1522void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1523 NodeTy* Pred, NodeSet& Dst) {
1524
1525 // ObjCForCollectionStmts are processed in two places. This method
1526 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1527 // statements within a basic block. This transfer function does two things:
1528 //
1529 // (1) binds the next container value to 'element'. This creates a new
1530 // node in the ExplodedGraph.
1531 //
1532 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1533 // whether or not the container has any more elements. This value
1534 // will be tested in ProcessBranch. We need to explicitly bind
1535 // this value because a container can contain nil elements.
1536 //
1537 // FIXME: Eventually this logic should actually do dispatches to
1538 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1539 // This will require simulating a temporary NSFastEnumerationState, either
1540 // through an SVal or through the use of MemRegions. This value can
1541 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1542 // terminates we reclaim the temporary (it goes out of scope) and we
1543 // we can test if the SVal is 0 or if the MemRegion is null (depending
1544 // on what approach we take).
1545 //
1546 // For now: simulate (1) by assigning either a symbol or nil if the
1547 // container is empty. Thus this transfer function will by default
1548 // result in state splitting.
1549
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001550 Stmt* elem = S->getElement();
1551 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001552
1553 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001554 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001555 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001556 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1557 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1558 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001559 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001560
1561 NodeSet Tmp;
1562 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001563
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001564 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1565 const GRState* state = GetState(*I);
1566 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1567 }
1568}
1569
1570void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1571 NodeTy* Pred, NodeSet& Dst,
1572 SVal ElementV) {
1573
1574
Ted Kremenekaf337412008-11-12 19:24:17 +00001575
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001576 // Get the current state. Use 'EvalLocation' to determine if it is a null
1577 // pointer, etc.
1578 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001579
Ted Kremenek8c354752008-12-16 22:02:27 +00001580 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1581 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001582 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001583
1584 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001585
Ted Kremenekaf337412008-11-12 19:24:17 +00001586 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001587 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001588 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1589 GRStateRef hasElems = state.BindExpr(S, TrueV);
1590
Ted Kremenekaf337412008-11-12 19:24:17 +00001591 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001592 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1593 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001594
1595 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1596 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1597 // FIXME: The proper thing to do is to really iterate over the
1598 // container. We will do this with dispatch logic to the store.
1599 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001600 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001601 assert (Loc::IsLocType(T));
1602 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001603 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1604 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1605 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001606
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001607 // Bind the location to 'nil' on the false branch.
1608 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1609 noElems = noElems.BindLoc(ElementV, nilV);
1610 }
1611
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001612 // Create the new nodes.
1613 MakeNode(Dst, S, Pred, hasElems);
1614 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001615}
1616
1617//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001618// Transfer function: Objective-C message expressions.
1619//===----------------------------------------------------------------------===//
1620
1621void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1622 NodeSet& Dst){
1623
1624 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1625 Pred, Dst);
1626}
1627
1628void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001629 ObjCMessageExpr::arg_iterator AI,
1630 ObjCMessageExpr::arg_iterator AE,
1631 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001632 if (AI == AE) {
1633
1634 // Process the receiver.
1635
1636 if (Expr* Receiver = ME->getReceiver()) {
1637 NodeSet Tmp;
1638 Visit(Receiver, Pred, Tmp);
1639
1640 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1641 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1642
1643 return;
1644 }
1645
1646 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1647 return;
1648 }
1649
1650 NodeSet Tmp;
1651 Visit(*AI, Pred, Tmp);
1652
1653 ++AI;
1654
1655 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1656 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1657}
1658
1659void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1660 NodeTy* Pred,
1661 NodeSet& Dst) {
1662
1663 // FIXME: More logic for the processing the method call.
1664
Ted Kremeneka8538d92009-02-13 01:45:31 +00001665 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001666 bool RaisesException = false;
1667
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001668
1669 if (Expr* Receiver = ME->getReceiver()) {
1670
Ted Kremeneka8538d92009-02-13 01:45:31 +00001671 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001672
Ted Kremenek21fe8372009-02-19 04:06:22 +00001673 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001674 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001675 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001676
1677 if (N) {
1678 N->markAsSink();
1679 UndefReceivers.insert(N);
1680 }
1681
1682 return;
1683 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001684
Ted Kremenek21fe8372009-02-19 04:06:22 +00001685 // "Assume" that the receiver is not NULL.
1686 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001687 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001688
1689 // "Assume" that the receiver is NULL.
1690 bool isFeasibleNull = false;
1691 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1692
1693 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001694 QualType RetTy = ME->getType();
1695
Ted Kremenek21fe8372009-02-19 04:06:22 +00001696 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001697 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001698 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001699 // The [0 ...] expressions will return garbage. Flag either an
1700 // explicit or implicit error. Because of the structure of this
1701 // function we currently do not bifurfacte the state graph at
1702 // this point.
1703 // FIXME: We should bifurcate and fill the returned struct with
1704 // garbage.
1705 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1706 N->markAsSink();
1707 if (isFeasibleNotNull)
1708 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001709 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001710 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001711 }
1712 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001713 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001714 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001715 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001716 if (RetTy != Ctx.VoidTy) {
1717 if (BR.getParentMap().isConsumedExpr(ME)) {
1718 // sizeof(void *)
1719 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1720 // sizeof(return type)
1721 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001722
Ted Kremenekfe630b92009-04-09 05:45:56 +00001723 if(voidPtrSize < returnTypeSize) {
1724 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1725 N->markAsSink();
1726 if(isFeasibleNotNull)
1727 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001728 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001729 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001730 }
1731 }
1732 else if (!isFeasibleNotNull) {
1733 // Handle the safe cases where the return value is 0 if the
1734 // receiver is nil.
1735 //
1736 // FIXME: For now take the conservative approach that we only
1737 // return null values if we *know* that the receiver is nil.
1738 // This is because we can have surprises like:
1739 //
1740 // ... = [[NSScreens screens] objectAtIndex:0];
1741 //
1742 // What can happen is that [... screens] could return nil, but
1743 // it most likely isn't nil. We should assume the semantics
1744 // of this case unless we have *a lot* more knowledge.
1745 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001746 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001747 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001748 return;
1749 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001750 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001751 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001752 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001753 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001754 // of this method should assume that the receiver is not nil.
1755 if (!StNotNull)
1756 return;
1757
Ted Kremenekda9ae602009-04-08 18:51:08 +00001758 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001759 }
1760
Ted Kremeneke448ab42008-05-01 18:33:28 +00001761 // Check if the "raise" message was sent.
1762 if (ME->getSelector() == RaiseSel)
1763 RaisesException = true;
1764 }
1765 else {
1766
1767 IdentifierInfo* ClsName = ME->getClassName();
1768 Selector S = ME->getSelector();
1769
1770 // Check for special instance methods.
1771
1772 if (!NSExceptionII) {
1773 ASTContext& Ctx = getContext();
1774
1775 NSExceptionII = &Ctx.Idents.get("NSException");
1776 }
1777
1778 if (ClsName == NSExceptionII) {
1779
1780 enum { NUM_RAISE_SELECTORS = 2 };
1781
1782 // Lazily create a cache of the selectors.
1783
1784 if (!NSExceptionInstanceRaiseSelectors) {
1785
1786 ASTContext& Ctx = getContext();
1787
1788 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1789
1790 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1791 unsigned idx = 0;
1792
1793 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001794 II.push_back(&Ctx.Idents.get("raise"));
1795 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001796 NSExceptionInstanceRaiseSelectors[idx++] =
1797 Ctx.Selectors.getSelector(II.size(), &II[0]);
1798
1799 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001800 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001801 NSExceptionInstanceRaiseSelectors[idx++] =
1802 Ctx.Selectors.getSelector(II.size(), &II[0]);
1803 }
1804
1805 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1806 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1807 RaisesException = true; break;
1808 }
1809 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001810 }
1811
1812 // Check for any arguments that are uninitialized/undefined.
1813
1814 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1815 I != E; ++I) {
1816
Ted Kremeneka8538d92009-02-13 01:45:31 +00001817 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001818
1819 // Generate an error node for passing an uninitialized/undefined value
1820 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001821 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001822
1823 if (N) {
1824 N->markAsSink();
1825 MsgExprUndefArgs[N] = *I;
1826 }
1827
1828 return;
1829 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001830 }
1831
1832 // Check if we raise an exception. For now treat these as sinks. Eventually
1833 // we will want to handle exceptions properly.
1834
1835 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1836
1837 if (RaisesException)
1838 Builder->BuildSinks = true;
1839
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001840 // Dispatch to plug-in transfer function.
1841
1842 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001843 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001844
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001845 EvalObjCMessageExpr(Dst, ME, Pred);
1846
1847 // Handle the case where no nodes where generated. Auto-generate that
1848 // contains the updated state if we aren't generating sinks.
1849
Ted Kremenekb0533962008-04-18 20:35:30 +00001850 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001851 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001852}
1853
1854//===----------------------------------------------------------------------===//
1855// Transfer functions: Miscellaneous statements.
1856//===----------------------------------------------------------------------===//
1857
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001858void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1859 QualType PtrTy,
1860 Expr* CastE, NodeTy* Pred,
1861 NodeSet& Dst) {
1862 if (!V.isUnknownOrUndef()) {
1863 // FIXME: Determine if the number of bits of the target type is
1864 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00001865 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00001866 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
1867 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001868 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001869 else
1870 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001871}
1872
1873
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001874void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001875 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001876 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001877 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001878
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001879 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001880 T = ExCast->getTypeAsWritten();
1881
Zhongxing Xued340f72008-10-22 08:02:16 +00001882 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001883 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001884 else
1885 Visit(Ex, Pred, S1);
1886
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001887 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00001888 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001889 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001890 Dst.Add(*I1);
1891
Ted Kremenek874d63f2008-01-24 02:02:54 +00001892 return;
1893 }
1894
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001895 // FIXME: The rest of this should probably just go into EvalCall, and
1896 // let the transfer function object be responsible for constructing
1897 // nodes.
1898
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001899 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001900 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001901 const GRState* state = GetState(N);
1902 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00001903 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001904
1905 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001906 if (V.isUnknown()) {
1907 Dst.Add(N);
1908 continue;
1909 }
1910
1911 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00001912 if (V.isUndef())
1913 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001914
1915 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001916 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00001917 C.getCanonicalType(ExTy).getUnqualifiedType())
1918 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001919
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001920 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001921 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001922 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001923 continue;
1924 }
1925
1926 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00001927 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001928 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001929 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001930 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001931 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00001932 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001933 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001934
Ted Kremenekc5302912009-03-05 20:22:13 +00001935 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00001936 }
1937
1938 // Just pass through function and block pointers.
1939 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
1940 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00001941 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001942 }
1943
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001944 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001945 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001946 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00001947 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001948
1949 // Are we casting from an array to a pointer? If so just pass on
1950 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00001951 if (T->isPointerType())
1952 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001953
1954 // Are we casting from an array to an integer? If so, cast the decayed
1955 // pointer value to an integer.
1956 assert(T->isIntegerType());
1957 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1958 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001959 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001960 continue;
1961 }
1962
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001963 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001964 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
1965 // FIXME: For TypedViewRegions, we should handle the case where the
1966 // underlying symbolic pointer is a function pointer or
1967 // block pointer.
1968
1969 // FIXME: We should handle the case where we strip off view layers to get
1970 // to a desugared type.
1971
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001972 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00001973 // We get a symbolic function pointer for a dereference of a function
1974 // pointer, but it is of function type. Example:
1975
1976 // struct FPRec {
1977 // void (*my_func)(int * x);
1978 // };
1979 //
1980 // int bar(int x);
1981 //
1982 // int f1_a(struct FPRec* foo) {
1983 // int x;
1984 // (*foo->my_func)(&x);
1985 // return bar(x)+1; // no-warning
1986 // }
1987
1988 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001989
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001990 const MemRegion* R = RV->getRegion();
1991 StoreManager& StoreMgr = getStoreManager();
1992
1993 // Delegate to store manager to get the result of casting a region
1994 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001995 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001996
1997 // Inspect the result. If the MemRegion* returned is NULL, this
1998 // expression evaluates to UnknownVal.
1999 R = Res.getRegion();
2000 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2001
2002 // Generate the new node in the ExplodedGraph.
2003 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002004 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002005 }
2006
Ted Kremenekdc402902009-03-04 00:14:35 +00002007 // If we are casting a symbolic value, make a symbolic region and a
2008 // TypedViewRegion subregion.
2009 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) {
2010 SymbolRef Sym = SV->getSymbol();
Ted Kremenekc5302912009-03-05 20:22:13 +00002011 QualType SymTy = getSymbolManager().getType(Sym);
2012
2013 // Just pass through symbols that are function or block pointers.
2014 if (SymTy->isFunctionPointerType() || SymTy->isBlockPointerType())
2015 goto PassThrough;
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002016
2017 // Are we casting to a function or block pointer?
2018 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
2019 // FIXME: We should verify that the underlying type of the symbolic
2020 // pointer is a void* (or maybe char*). Other things are an abuse
2021 // of the type system.
2022 goto PassThrough;
2023 }
Ted Kremenekc5302912009-03-05 20:22:13 +00002024
Ted Kremenekdc402902009-03-04 00:14:35 +00002025 StoreManager& StoreMgr = getStoreManager();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002026 const MemRegion* R = StoreMgr.getRegionManager().getSymbolicRegion(Sym);
Ted Kremenekdc402902009-03-04 00:14:35 +00002027
2028 // Delegate to store manager to get the result of casting a region
2029 // to a different type.
2030 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
2031
2032 // Inspect the result. If the MemRegion* returned is NULL, this
2033 // expression evaluates to UnknownVal.
2034 R = Res.getRegion();
2035 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2036
2037 // Generate the new node in the ExplodedGraph.
2038 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
2039 continue;
2040 }
2041
Ted Kremenekc5302912009-03-05 20:22:13 +00002042 // All other cases.
2043 DispatchCast: {
2044 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2045 EvalCast(V, CastE->getType())));
2046 continue;
2047 }
2048
2049 PassThrough: {
2050 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2051 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002052 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002053}
2054
Ted Kremenek4f090272008-10-27 21:54:31 +00002055void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002056 NodeTy* Pred, NodeSet& Dst,
2057 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002058 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2059 NodeSet Tmp;
2060 Visit(ILE, Pred, Tmp);
2061
2062 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002063 const GRState* state = GetState(*I);
2064 SVal ILV = GetSVal(state, ILE);
2065 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002066
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002067 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002068 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002069 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002070 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002071 }
2072}
2073
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002074void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002075
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002076 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002077 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002078
2079 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002080 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002081
Ted Kremenekefd59942008-12-08 22:47:34 +00002082 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002083 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002084
2085 // FIXME: static variables may have an initializer, but the second
2086 // time a function is called those values may not be current.
2087 NodeSet Tmp;
2088
Ted Kremenekaf337412008-11-12 19:24:17 +00002089 if (InitEx)
2090 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002091
2092 if (Tmp.empty())
2093 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002094
2095 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002096 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002097 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002098
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002099 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2100 QualType T = getContext().getCanonicalType(VD->getType());
2101 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2102 // FIXME: Handle multi-dimensional VLAs.
2103
2104 Expr* SE = VLA->getSizeExpr();
2105 SVal Size = GetSVal(state, SE);
2106
2107 if (Size.isUndef()) {
2108 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2109 N->markAsSink();
2110 ExplicitBadSizedVLA.insert(N);
2111 }
2112 continue;
2113 }
2114
2115 bool isFeasibleZero = false;
2116 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2117
2118 bool isFeasibleNotZero = false;
2119 state = Assume(state, Size, true, isFeasibleNotZero);
2120
2121 if (isFeasibleZero) {
2122 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2123 N->markAsSink();
2124 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2125 else ExplicitBadSizedVLA.insert(N);
2126 }
2127 }
2128
2129 if (!isFeasibleNotZero)
2130 continue;
2131 }
2132
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002133 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002134 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002135 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002136 QualType T = VD->getType();
2137
2138 // Recover some path-sensitivity if a scalar value evaluated to
2139 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002140 if (InitVal.isUnknown() ||
2141 !getConstraintManager().canReasonAbout(InitVal)) {
Zhongxing Xucfe29912009-04-09 06:53:24 +00002142 InitVal = SVal::GetConjuredSymbolVal(SymMgr,
2143 getStoreManager().getRegionManager(), InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002144 }
2145
Ted Kremeneka8538d92009-02-13 01:45:31 +00002146 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002147
2148 // The next thing to do is check if the GRTransferFuncs object wants to
2149 // update the state based on the new binding. If the GRTransferFunc
2150 // object doesn't do anything, just auto-propagate the current state.
2151 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2152 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2153 InitVal);
2154 }
2155 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002156 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002157 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002158 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002159 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002160}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002161
Ted Kremenekf75b1862008-10-30 17:47:32 +00002162namespace {
2163 // This class is used by VisitInitListExpr as an item in a worklist
2164 // for processing the values contained in an InitListExpr.
2165class VISIBILITY_HIDDEN InitListWLItem {
2166public:
2167 llvm::ImmutableList<SVal> Vals;
2168 GRExprEngine::NodeTy* N;
2169 InitListExpr::reverse_iterator Itr;
2170
2171 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2172 InitListExpr::reverse_iterator itr)
2173 : Vals(vals), N(n), Itr(itr) {}
2174};
2175}
2176
2177
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002178void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2179 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002180
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002181 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002182 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002183 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002184
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002185 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002186
Ted Kremeneka49e3672008-10-30 23:14:36 +00002187 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002188
Ted Kremeneka49e3672008-10-30 23:14:36 +00002189 // Handle base case where the initializer has no elements.
2190 // e.g: static int* myArray[] = {};
2191 if (NumInitElements == 0) {
2192 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2193 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2194 return;
2195 }
2196
2197 // Create a worklist to process the initializers.
2198 llvm::SmallVector<InitListWLItem, 10> WorkList;
2199 WorkList.reserve(NumInitElements);
2200 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002201 InitListExpr::reverse_iterator ItrEnd = E->rend();
2202
Ted Kremeneka49e3672008-10-30 23:14:36 +00002203 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002204 while (!WorkList.empty()) {
2205 InitListWLItem X = WorkList.back();
2206 WorkList.pop_back();
2207
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002208 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002209 Visit(*X.Itr, X.N, Tmp);
2210
2211 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002212
Ted Kremenekf75b1862008-10-30 17:47:32 +00002213 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2214 // Get the last initializer value.
2215 state = GetState(*NI);
2216 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2217
2218 // Construct the new list of values by prepending the new value to
2219 // the already constructed list.
2220 llvm::ImmutableList<SVal> NewVals =
2221 getBasicVals().consVals(InitV, X.Vals);
2222
2223 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002224 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002225 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002226
Ted Kremenekf75b1862008-10-30 17:47:32 +00002227 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002228 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002229 }
2230 else {
2231 // Still some initializer values to go. Push them onto the worklist.
2232 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2233 }
2234 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002235 }
Ted Kremenek87903072008-10-30 18:34:31 +00002236
2237 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002238 }
2239
Ted Kremenek062e2f92008-11-13 06:10:40 +00002240 if (T->isUnionType() || T->isVectorType()) {
2241 // FIXME: to be implemented.
2242 // Note: That vectors can return true for T->isIntegerType()
2243 MakeNode(Dst, E, Pred, state);
2244 return;
2245 }
2246
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002247 if (Loc::IsLocType(T) || T->isIntegerType()) {
2248 assert (E->getNumInits() == 1);
2249 NodeSet Tmp;
2250 Expr* Init = E->getInit(0);
2251 Visit(Init, Pred, Tmp);
2252 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2253 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002254 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002255 }
2256 return;
2257 }
2258
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002259
2260 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2261 assert(0 && "unprocessed InitListExpr type");
2262}
Ted Kremenekf233d482008-02-05 00:26:40 +00002263
Sebastian Redl05189992008-11-11 17:56:53 +00002264/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2265void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2266 NodeTy* Pred,
2267 NodeSet& Dst) {
2268 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002269 uint64_t amt;
2270
2271 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002272 if (T == getContext().VoidTy) {
2273 // sizeof(void) == 1 byte.
2274 amt = 1;
2275 }
2276 else if (!T.getTypePtr()->isConstantSizeType()) {
2277 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002278 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002279 }
2280 else if (T->isObjCInterfaceType()) {
2281 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2282 // the compiler has laid out its representation. Just report Unknown
2283 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002284 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002285 }
2286 else {
2287 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002288 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002289 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002290 }
2291 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002292 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002293
Ted Kremenek0e561a32008-03-21 21:30:14 +00002294 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002295 BindExpr(GetState(Pred), Ex,
2296 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002297}
2298
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002299
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002300void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002301 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002302
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002303 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002304
2305 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002306 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002307
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002308 case UnaryOperator::Deref: {
2309
2310 Expr* Ex = U->getSubExpr()->IgnoreParens();
2311 NodeSet Tmp;
2312 Visit(Ex, Pred, Tmp);
2313
2314 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002315
Ted Kremeneka8538d92009-02-13 01:45:31 +00002316 const GRState* state = GetState(*I);
2317 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002318
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002319 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002320 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002321 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002322 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002323 }
2324
2325 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002326 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002327
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002328 case UnaryOperator::Real: {
2329
2330 Expr* Ex = U->getSubExpr()->IgnoreParens();
2331 NodeSet Tmp;
2332 Visit(Ex, Pred, Tmp);
2333
2334 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2335
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002336 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002337 if (Ex->getType()->isAnyComplexType()) {
2338 // Just report "Unknown."
2339 Dst.Add(*I);
2340 continue;
2341 }
2342
2343 // For all other types, UnaryOperator::Real is an identity operation.
2344 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002345 const GRState* state = GetState(*I);
2346 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002347 }
2348
2349 return;
2350 }
2351
2352 case UnaryOperator::Imag: {
2353
2354 Expr* Ex = U->getSubExpr()->IgnoreParens();
2355 NodeSet Tmp;
2356 Visit(Ex, Pred, Tmp);
2357
2358 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002359 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002360 if (Ex->getType()->isAnyComplexType()) {
2361 // Just report "Unknown."
2362 Dst.Add(*I);
2363 continue;
2364 }
2365
2366 // For all other types, UnaryOperator::Float returns 0.
2367 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002368 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002369 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002370 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002371 }
2372
2373 return;
2374 }
2375
2376 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002377 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002378 Dst.Add(Pred);
2379 return;
2380
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002381 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002382 case UnaryOperator::Extension: {
2383
2384 // Unary "+" is a no-op, similar to a parentheses. We still have places
2385 // where it may be a block-level expression, so we need to
2386 // generate an extra node that just propagates the value of the
2387 // subexpression.
2388
2389 Expr* Ex = U->getSubExpr()->IgnoreParens();
2390 NodeSet Tmp;
2391 Visit(Ex, Pred, Tmp);
2392
2393 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002394 const GRState* state = GetState(*I);
2395 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002396 }
2397
2398 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002399 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002400
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002401 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002402
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002403 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002404 Expr* Ex = U->getSubExpr()->IgnoreParens();
2405 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002406 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002407
2408 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002409 const GRState* state = GetState(*I);
2410 SVal V = GetSVal(state, Ex);
2411 state = BindExpr(state, U, V);
2412 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002413 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002414
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002415 return;
2416 }
2417
2418 case UnaryOperator::LNot:
2419 case UnaryOperator::Minus:
2420 case UnaryOperator::Not: {
2421
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002422 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002423 Expr* Ex = U->getSubExpr()->IgnoreParens();
2424 NodeSet Tmp;
2425 Visit(Ex, Pred, Tmp);
2426
2427 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002428 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002429
2430 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002431 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002432
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002433 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002434 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002435 continue;
2436 }
2437
Ted Kremenek60595da2008-11-15 04:01:56 +00002438// QualType DstT = getContext().getCanonicalType(U->getType());
2439// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2440//
2441// if (DstT != SrcT) // Perform promotions.
2442// V = EvalCast(V, DstT);
2443//
2444// if (V.isUnknownOrUndef()) {
2445// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2446// continue;
2447// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002448
2449 switch (U->getOpcode()) {
2450 default:
2451 assert(false && "Invalid Opcode.");
2452 break;
2453
2454 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002455 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002456 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002457 break;
2458
2459 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002460 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002461 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002462 break;
2463
2464 case UnaryOperator::LNot:
2465
2466 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2467 //
2468 // Note: technically we do "E == 0", but this is the same in the
2469 // transfer functions as "0 == E".
2470
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002471 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002472 Loc X = Loc::MakeNull(getBasicVals());
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002473 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2474 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002475 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002476 }
2477 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002478 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002479#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002480 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002481 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002482#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002483 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2484 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002485 continue;
2486#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002487 }
2488
2489 break;
2490 }
2491
Ted Kremeneka8538d92009-02-13 01:45:31 +00002492 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002493 }
2494
2495 return;
2496 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002497 }
2498
2499 // Handle ++ and -- (both pre- and post-increment).
2500
2501 assert (U->isIncrementDecrementOp());
2502 NodeSet Tmp;
2503 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002504 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002505
2506 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2507
Ted Kremeneka8538d92009-02-13 01:45:31 +00002508 const GRState* state = GetState(*I);
2509 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002510
2511 // Perform a load.
2512 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002513 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002514
2515 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2516
Ted Kremeneka8538d92009-02-13 01:45:31 +00002517 state = GetState(*I2);
2518 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002519
2520 // Propagate unknown and undefined values.
2521 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002522 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002523 continue;
2524 }
2525
Ted Kremenek21028dd2009-03-11 03:54:24 +00002526 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002527 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2528 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002529
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002530 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002531
2532 // Conjure a new symbol if necessary to recover precision.
2533 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result))
Zhongxing Xu867418f2009-04-09 05:57:11 +00002534 Result = SVal::GetConjuredSymbolVal(SymMgr,
2535 getStoreManager().getRegionManager(),Ex,
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002536 Builder->getCurrentBlockCount());
2537
Ted Kremeneka8538d92009-02-13 01:45:31 +00002538 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002539
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002540 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002541 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002542 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002543 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002544}
2545
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002546void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2547 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2548}
2549
2550void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2551 AsmStmt::outputs_iterator I,
2552 AsmStmt::outputs_iterator E,
2553 NodeTy* Pred, NodeSet& Dst) {
2554 if (I == E) {
2555 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2556 return;
2557 }
2558
2559 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002560 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002561
2562 ++I;
2563
2564 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2565 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2566}
2567
2568void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2569 AsmStmt::inputs_iterator I,
2570 AsmStmt::inputs_iterator E,
2571 NodeTy* Pred, NodeSet& Dst) {
2572 if (I == E) {
2573
2574 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002575 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002576
2577 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2578 // which interprets the inline asm and stores proper results in the
2579 // outputs.
2580
Ted Kremeneka8538d92009-02-13 01:45:31 +00002581 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002582
2583 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2584 OE = A->end_outputs(); OI != OE; ++OI) {
2585
Ted Kremeneka8538d92009-02-13 01:45:31 +00002586 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002587 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002588
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002589 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002590 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002591 }
2592
Ted Kremeneka8538d92009-02-13 01:45:31 +00002593 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002594 return;
2595 }
2596
2597 NodeSet Tmp;
2598 Visit(*I, Pred, Tmp);
2599
2600 ++I;
2601
2602 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2603 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2604}
2605
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002606void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2607 assert (Builder && "GRStmtNodeBuilder must be defined.");
2608
2609 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002610
Ted Kremenek186350f2008-04-23 20:12:28 +00002611 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2612 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002613
Ted Kremenek729a9a22008-07-17 23:15:45 +00002614 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002615
Ted Kremenekb0533962008-04-18 20:35:30 +00002616 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002617
Ted Kremenekb0533962008-04-18 20:35:30 +00002618 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002619 MakeNode(Dst, S, Pred, GetState(Pred));
2620}
2621
Ted Kremenek02737ed2008-03-31 15:02:58 +00002622void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2623
2624 Expr* R = S->getRetValue();
2625
2626 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002627 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002628 return;
2629 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002630
Ted Kremenek5917d782008-11-21 00:27:44 +00002631 NodeSet Tmp;
2632 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002633
Ted Kremenek5917d782008-11-21 00:27:44 +00002634 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2635 SVal X = GetSVal((*I)->getState(), R);
2636
2637 // Check if we return the address of a stack variable.
2638 if (isa<loc::MemRegionVal>(X)) {
2639 // Determine if the value is on the stack.
2640 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002641
Ted Kremenek5917d782008-11-21 00:27:44 +00002642 if (R && getStateManager().hasStackStorage(R)) {
2643 // Create a special node representing the error.
2644 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2645 N->markAsSink();
2646 RetsStackAddr.insert(N);
2647 }
2648 continue;
2649 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002650 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002651 // Check if we return an undefined value.
2652 else if (X.isUndef()) {
2653 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2654 N->markAsSink();
2655 RetsUndef.insert(N);
2656 }
2657 continue;
2658 }
2659
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002660 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002661 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002662}
Ted Kremenek55deb972008-03-25 00:34:37 +00002663
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002664//===----------------------------------------------------------------------===//
2665// Transfer functions: Binary operators.
2666//===----------------------------------------------------------------------===//
2667
Ted Kremeneka8538d92009-02-13 01:45:31 +00002668const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002669 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002670
2671 // Divide by undefined? (potentially zero)
2672
2673 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002674 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002675
2676 if (DivUndef) {
2677 DivUndef->markAsSink();
2678 ExplicitBadDivides.insert(DivUndef);
2679 }
2680
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002681 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002682 }
2683
2684 // Check for divide/remainder-by-zero.
2685 // First, "assume" that the denominator is 0 or undefined.
2686
2687 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002688 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002689
2690 // Second, "assume" that the denominator cannot be 0.
2691
2692 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002693 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002694
2695 // Create the node for the divide-by-zero (if it occurred).
2696
2697 if (isFeasibleZero)
2698 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2699 DivZeroNode->markAsSink();
2700
2701 if (isFeasibleNotZero)
2702 ImplicitBadDivides.insert(DivZeroNode);
2703 else
2704 ExplicitBadDivides.insert(DivZeroNode);
2705
2706 }
2707
Ted Kremeneka8538d92009-02-13 01:45:31 +00002708 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002709}
2710
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002711void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002712 GRExprEngine::NodeTy* Pred,
2713 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002714
2715 NodeSet Tmp1;
2716 Expr* LHS = B->getLHS()->IgnoreParens();
2717 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002718
Ted Kremenek759623e2008-12-06 02:39:30 +00002719 // FIXME: Add proper support for ObjCKVCRefExpr.
2720 if (isa<ObjCKVCRefExpr>(LHS)) {
2721 Visit(RHS, Pred, Dst);
2722 return;
2723 }
2724
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002725 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002726 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002727 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002728 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002729
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002730 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002731
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002732 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002733
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002734 // Process the RHS.
2735
2736 NodeSet Tmp2;
2737 Visit(RHS, *I1, Tmp2);
2738
2739 // With both the LHS and RHS evaluated, process the operation itself.
2740
2741 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002742
Ted Kremeneka8538d92009-02-13 01:45:31 +00002743 const GRState* state = GetState(*I2);
2744 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002745
Ted Kremeneka8538d92009-02-13 01:45:31 +00002746 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002747 BinaryOperator::Opcode Op = B->getOpcode();
2748
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002749 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002750
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002751 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002752
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002753 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002754 // FIXME: Handle structs.
2755 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002756
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002757 if ((RightV.isUnknown() ||
2758 !getConstraintManager().canReasonAbout(RightV))
2759 && (Loc::IsLocType(T) ||
2760 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002761 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002762
Zhongxing Xu9cafcd52009-04-09 06:56:25 +00002763 RightV = SVal::GetConjuredSymbolVal(SymMgr,
2764 getStoreManager().getRegionManager(), B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002765 }
2766
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002767 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002768 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002769 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2770 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002771 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002772 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002773
2774 case BinaryOperator::Div:
2775 case BinaryOperator::Rem:
2776
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002777 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002778 if (RHS->getType()->isIntegerType() &&
2779 RHS->getType()->isScalarType()) {
2780
Ted Kremeneka8538d92009-02-13 01:45:31 +00002781 state = CheckDivideZero(B, state, *I2, RightV);
2782 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002783 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002784
2785 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002786
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002787 default: {
2788
2789 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002790 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002791
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002792 // Process non-assignements except commas or short-circuited
2793 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002794
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002795 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002796
2797 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002798 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002799 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002800 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002801 }
2802 else
2803 Dst.Add(*I2);
2804
Ted Kremenek89063af2008-02-21 19:15:37 +00002805 continue;
2806 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002807
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002808 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002809
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002810 // The operands were *not* undefined, but the result is undefined.
2811 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002812
Ted Kremeneka8538d92009-02-13 01:45:31 +00002813 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002814 UndefNode->markAsSink();
2815 UndefResults.insert(UndefNode);
2816 }
2817
2818 continue;
2819 }
2820
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002821 // Otherwise, create a new node.
2822
Ted Kremeneka8538d92009-02-13 01:45:31 +00002823 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002824 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002825 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002826 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002827
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002828 assert (B->isCompoundAssignmentOp());
2829
Ted Kremenekcad29962009-02-07 00:52:24 +00002830 switch (Op) {
2831 default:
2832 assert(0 && "Invalid opcode for compound assignment.");
2833 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2834 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2835 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2836 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2837 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2838 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2839 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2840 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2841 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2842 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002843 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002844
2845 // Perform a load (the LHS). This performs the checks for
2846 // null dereferences, and so on.
2847 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002848 SVal location = GetSVal(state, LHS);
2849 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002850
2851 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2852
Ted Kremeneka8538d92009-02-13 01:45:31 +00002853 state = GetState(*I3);
2854 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002855
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002856 // Check for divide-by-zero.
2857 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002858 && RHS->getType()->isIntegerType()
2859 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002860
2861 // CheckDivideZero returns a new state where the denominator
2862 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002863 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002864
Ted Kremeneka8538d92009-02-13 01:45:31 +00002865 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002866 continue;
2867 }
2868
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002869 // Propagate undefined values (left-side).
2870 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002871 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002872 continue;
2873 }
2874
2875 // Propagate unknown values (left and right-side).
2876 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002877 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2878 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002879 continue;
2880 }
2881
2882 // At this point:
2883 //
2884 // The LHS is not Undef/Unknown.
2885 // The RHS is not Unknown.
2886
2887 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00002888 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002889 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002890
2891 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
2892 CLHSTy = getContext().getCanonicalType(CTy);
2893
Ted Kremenek60595da2008-11-15 04:01:56 +00002894 QualType LTy = getContext().getCanonicalType(LHS->getType());
2895 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00002896
2897 // Promote LHS.
2898 V = EvalCast(V, CLHSTy);
2899
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002900 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002901 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002902 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00002903 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00002904 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002905 continue;
2906 }
2907
Ted Kremenek60595da2008-11-15 04:01:56 +00002908 // Compute the result of the operation.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002909 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002910
2911 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002912 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002913 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002914 UndefNode->markAsSink();
2915 UndefResults.insert(UndefNode);
2916 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002917 continue;
2918 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002919
2920 // EXPERIMENTAL: "Conjured" symbols.
2921 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002922
2923 SVal LHSVal;
2924
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002925 if ((Result.isUnknown() ||
2926 !getConstraintManager().canReasonAbout(Result))
2927 && (Loc::IsLocType(CTy)
2928 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002929
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002930 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002931
Ted Kremenek60595da2008-11-15 04:01:56 +00002932 // The symbolic value is actually for the type of the left-hand side
2933 // expression, not the computation type, as this is the value the
2934 // LValue on the LHS will bind to.
Zhongxing Xucaf8ce12009-04-09 07:01:16 +00002935 LHSVal = SVal::GetConjuredSymbolVal(SymMgr,
2936 getStoreManager().getRegionManager(), B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002937
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002938 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002939 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002940 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002941 else {
2942 // The left-hand side may bind to a different value then the
2943 // computation type.
2944 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2945 }
2946
Ted Kremeneka8538d92009-02-13 01:45:31 +00002947 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2948 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002949 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002950 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002951 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002952}
Ted Kremenekee985462008-01-16 18:18:48 +00002953
2954//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002955// Transfer-function Helpers.
2956//===----------------------------------------------------------------------===//
2957
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002958void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002959 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002960 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002961 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002962
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002963 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002964 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002965
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002966 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002967 MakeNode(Dst, Ex, Pred, *I);
2968}
2969
Ted Kremeneka8538d92009-02-13 01:45:31 +00002970void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002971 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002972 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002973
Ted Kremeneka8538d92009-02-13 01:45:31 +00002974 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002975 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002976}
2977
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002978SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
2979 QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002980
2981 if (L.isUndef() || R.isUndef())
2982 return UndefinedVal();
2983
2984 if (L.isUnknown() || R.isUnknown())
2985 return UnknownVal();
2986
2987 if (isa<Loc>(L)) {
2988 if (isa<Loc>(R))
2989 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2990 else
2991 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2992 }
2993
2994 if (isa<Loc>(R)) {
2995 // Support pointer arithmetic where the increment/decrement operand
2996 // is on the left and the pointer on the right.
2997
2998 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2999
3000 // Commute the operands.
3001 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
3002 cast<NonLoc>(L));
3003 }
3004 else
3005 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003006 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003007}
3008
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003009//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003010// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003011//===----------------------------------------------------------------------===//
3012
Ted Kremenekaa66a322008-01-16 21:46:15 +00003013#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003014static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003015static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003016
Ted Kremenekaa66a322008-01-16 21:46:15 +00003017namespace llvm {
3018template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003019struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003020 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003021
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003022 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3023
3024 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003025 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003026 GraphPrintCheckerState->isUndefDeref(N) ||
3027 GraphPrintCheckerState->isUndefStore(N) ||
3028 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003029 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3030 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003031 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003032 GraphPrintCheckerState->isBadCall(N) ||
3033 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003034 return "color=\"red\",style=\"filled\"";
3035
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003036 if (GraphPrintCheckerState->isNoReturnCall(N))
3037 return "color=\"blue\",style=\"filled\"";
3038
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003039 return "";
3040 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003041
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003042 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003043 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003044
3045 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003046 ProgramPoint Loc = N->getLocation();
3047
3048 switch (Loc.getKind()) {
3049 case ProgramPoint::BlockEntranceKind:
3050 Out << "Block Entrance: B"
3051 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3052 break;
3053
3054 case ProgramPoint::BlockExitKind:
3055 assert (false);
3056 break;
3057
Ted Kremenekaa66a322008-01-16 21:46:15 +00003058 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003059 if (isa<PostStmt>(Loc)) {
3060 const PostStmt& L = cast<PostStmt>(Loc);
3061 Stmt* S = L.getStmt();
3062 SourceLocation SLoc = S->getLocStart();
3063
3064 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3065 llvm::raw_os_ostream OutS(Out);
3066 S->printPretty(OutS);
3067 OutS.flush();
3068
3069 if (SLoc.isFileID()) {
3070 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003071 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3072 << " col="
3073 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3074 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003075 }
3076
3077 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3078 Out << "\\|Implicit-Null Dereference.\\l";
3079 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3080 Out << "\\|Explicit-Null Dereference.\\l";
3081 else if (GraphPrintCheckerState->isUndefDeref(N))
3082 Out << "\\|Dereference of undefialied value.\\l";
3083 else if (GraphPrintCheckerState->isUndefStore(N))
3084 Out << "\\|Store to Undefined Loc.";
3085 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3086 Out << "\\|Explicit divide-by zero or undefined value.";
3087 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3088 Out << "\\|Implicit divide-by zero or undefined value.";
3089 else if (GraphPrintCheckerState->isUndefResult(N))
3090 Out << "\\|Result of operation is undefined.";
3091 else if (GraphPrintCheckerState->isNoReturnCall(N))
3092 Out << "\\|Call to function marked \"noreturn\".";
3093 else if (GraphPrintCheckerState->isBadCall(N))
3094 Out << "\\|Call to NULL/Undefined.";
3095 else if (GraphPrintCheckerState->isUndefArg(N))
3096 Out << "\\|Argument in call is undefined";
3097
3098 break;
3099 }
3100
Ted Kremenekaa66a322008-01-16 21:46:15 +00003101 const BlockEdge& E = cast<BlockEdge>(Loc);
3102 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3103 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003104
3105 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003106
3107 SourceLocation SLoc = T->getLocStart();
3108
Ted Kremenekb38911f2008-01-30 23:03:39 +00003109 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003110
Ted Kremeneka95d3752008-09-13 05:16:45 +00003111 llvm::raw_os_ostream OutS(Out);
3112 E.getSrc()->printTerminator(OutS);
3113 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003114
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003115 if (SLoc.isFileID()) {
3116 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003117 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3118 << " col="
3119 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003120 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003121
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003122 if (isa<SwitchStmt>(T)) {
3123 Stmt* Label = E.getDst()->getLabel();
3124
3125 if (Label) {
3126 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3127 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003128 llvm::raw_os_ostream OutS(Out);
3129 C->getLHS()->printPretty(OutS);
3130 OutS.flush();
3131
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003132 if (Stmt* RHS = C->getRHS()) {
3133 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003134 RHS->printPretty(OutS);
3135 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003136 }
3137
3138 Out << ":";
3139 }
3140 else {
3141 assert (isa<DefaultStmt>(Label));
3142 Out << "\\ldefault:";
3143 }
3144 }
3145 else
3146 Out << "\\l(implicit) default:";
3147 }
3148 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003149 // FIXME
3150 }
3151 else {
3152 Out << "\\lCondition: ";
3153 if (*E.getSrc()->succ_begin() == E.getDst())
3154 Out << "true";
3155 else
3156 Out << "false";
3157 }
3158
3159 Out << "\\l";
3160 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003161
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003162 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3163 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003164 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003165 }
3166 }
3167
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003168 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003169
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003170 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3171 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003172
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003173 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003174 return Out.str();
3175 }
3176};
3177} // end llvm namespace
3178#endif
3179
Ted Kremenekffe0f432008-03-07 22:58:01 +00003180#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003181template <typename ITERATOR>
3182GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3183
3184template <>
3185GRExprEngine::NodeTy*
3186GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3187 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3188 return I->first;
3189}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003190#endif
3191
3192void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003193#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003194 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003195 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003196
3197 // Flush any outstanding reports to make sure we cover all the nodes.
3198 // This does not cause them to get displayed.
3199 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3200 const_cast<BugType*>(*I)->FlushReports(BR);
3201
3202 // Iterate through the reports and get their nodes.
3203 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3204 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3205 const BugReportEquivClass& EQ = *I2;
3206 const BugReport &R = **EQ.begin();
3207 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3208 if (N) Src.push_back(N);
3209 }
3210 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003211
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003212 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003213 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003214 else {
3215 GraphPrintCheckerState = this;
3216 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003217
Ted Kremenekffe0f432008-03-07 22:58:01 +00003218 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003219
3220 GraphPrintCheckerState = NULL;
3221 GraphPrintSourceManager = NULL;
3222 }
3223#endif
3224}
3225
3226void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3227#ifndef NDEBUG
3228 GraphPrintCheckerState = this;
3229 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003230
Ted Kremenekcf118d42009-02-04 23:49:09 +00003231 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003232
Ted Kremenekcf118d42009-02-04 23:49:09 +00003233 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003234 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003235 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003236 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003237
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003238 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003239 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003240#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003241}