Ted Kremenek | 64de207 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | a0be826 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 6f4a9ef | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | de8d62b | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 64de207 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 10 | // 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 Kremenek | de8d62b | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 64de207 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | e68c0fc | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h" |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/Checker.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
| 20 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Builtins.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 6947a79 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 91076ca | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PrettyStackTrace.h" |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/ImmutableList.h" |
Douglas Gregor | f7b87cb | 2009-10-29 00:41:01 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | a7b8ffb | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | c025841 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 30 | #ifndef NDEBUG |
| 31 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | c025841 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Ted Kremenek | bd8957b | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using llvm::dyn_cast; |
| 36 | using llvm::cast; |
| 37 | using llvm::APSInt; |
Ted Kremenek | 0a8d376 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 40 | // Batch auditor. DEPRECATED. |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | |
| 45 | class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck { |
| 46 | typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks; |
| 47 | typedef llvm::DenseMap<void*,Checks> MapTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 49 | MapTy M; |
| 50 | Checks::Factory F; |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 51 | Checks AllStmts; |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 52 | |
| 53 | public: |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 54 | MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : |
| 55 | F(Alloc), AllStmts(F.GetEmptyList()) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 57 | virtual ~MappedBatchAuditor() { |
| 58 | llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 60 | for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
| 61 | for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){ |
| 62 | |
| 63 | GRSimpleAPICheck* check = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 65 | if (AlreadyVisited.count(check)) |
| 66 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 68 | AlreadyVisited.insert(check); |
| 69 | delete check; |
| 70 | } |
| 71 | } |
| 72 | |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 73 | void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) { |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 74 | assert (A && "Check cannot be null."); |
| 75 | void* key = reinterpret_cast<void*>((uintptr_t) C); |
| 76 | MapTy::iterator I = M.find(key); |
| 77 | M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second); |
| 78 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 80 | void AddCheck(GRSimpleAPICheck *A) { |
| 81 | assert (A && "Check cannot be null."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | AllStmts = F.Concat(A, AllStmts); |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 83 | } |
Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 84 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 85 | virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) { |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 86 | // First handle the auditors that accept all statements. |
| 87 | bool isSink = false; |
| 88 | for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I) |
| 89 | isSink |= (*I)->Audit(N, VMgr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 91 | // Next handle the auditors that accept only specific statements. |
Ted Kremenek | bfd28fd | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 92 | const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 93 | void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass()); |
| 94 | MapTy::iterator MI = M.find(key); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | if (MI != M.end()) { |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 96 | for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I) |
| 97 | isSink |= (*I)->Audit(N, VMgr); |
| 98 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
| 100 | return isSink; |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
| 104 | } // end anonymous namespace |
| 105 | |
| 106 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 107 | // Checker worklist routines. |
| 108 | //===----------------------------------------------------------------------===// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
| 110 | void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 111 | ExplodedNodeSet &Src, bool isPrevisit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 113 | if (Checkers.empty()) { |
| 114 | Dst = Src; |
| 115 | return; |
| 116 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 118 | ExplodedNodeSet Tmp; |
| 119 | ExplodedNodeSet *PrevSet = &Src; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 6f2a705 | 2009-10-30 17:47:32 +0000 | [diff] [blame] | 121 | for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I) |
| 122 | { |
| 123 | ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 124 | : (PrevSet == &Tmp) ? &Src : &Tmp; |
Ted Kremenek | 6f2a705 | 2009-10-30 17:47:32 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 126 | CurrSet->clear(); |
Zhongxing Xu | 6b8bfb3 | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 127 | void *tag = I->first; |
| 128 | Checker *checker = I->second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 130 | for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end(); |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 131 | NI != NE; ++NI) |
Zhongxing Xu | 6b8bfb3 | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 132 | checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, tag, isPrevisit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 134 | // Update which NodeSet is the current one. |
| 135 | PrevSet = CurrSet; |
| 136 | } |
| 137 | |
| 138 | // Don't autotransition. The CheckerContext objects should do this |
| 139 | // automatically. |
| 140 | } |
| 141 | |
| 142 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 143 | // Engine construction and deletion. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 146 | static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { |
| 147 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 148 | return Ctx.Selectors.getSelector(0, &II); |
| 149 | } |
| 150 | |
Ted Kremenek | 2d063be | 2008-03-09 18:05:48 +0000 | [diff] [blame] | 151 | |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 152 | GRExprEngine::GRExprEngine(AnalysisManager &mgr) |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 153 | : AMgr(mgr), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | CoreEngine(mgr.getASTContext(), *this), |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 155 | G(CoreEngine.getGraph()), |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 156 | Builder(NULL), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | StateMgr(G.getContext(), mgr.getStoreManagerCreator(), |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 158 | mgr.getConstraintManagerCreator(), G.getAllocator()), |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 159 | SymMgr(StateMgr.getSymbolManager()), |
Ted Kremenek | f8cb51c | 2009-04-09 16:46:55 +0000 | [diff] [blame] | 160 | ValMgr(StateMgr.getValueManager()), |
Ted Kremenek | f267a15 | 2009-07-16 01:32:00 +0000 | [diff] [blame] | 161 | SVator(ValMgr.getSValuator()), |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 162 | CurrentStmt(NULL), |
Zhongxing Xu | 4ee570a | 2008-12-22 08:30:52 +0000 | [diff] [blame] | 163 | NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | RaiseSel(GetNullarySelector("raise", G.getContext())), |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 165 | BR(mgr, *this) {} |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 166 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | GRExprEngine::~GRExprEngine() { |
Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 168 | BR.FlushReports(); |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 169 | delete [] NSExceptionInstanceRaiseSelectors; |
Ted Kremenek | 6f2a705 | 2009-10-30 17:47:32 +0000 | [diff] [blame] | 170 | for (CheckersOrdered::iterator I=Checkers.begin(), E=Checkers.end(); I!=E;++I) |
Zhongxing Xu | 6b8bfb3 | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 171 | delete I->second; |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 174 | //===----------------------------------------------------------------------===// |
| 175 | // Utility methods. |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 179 | void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) { |
Ted Kremenek | 9c32a1e | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 180 | StateMgr.TF = tf; |
Ted Kremenek | 0fbbb08 | 2009-11-03 23:30:34 +0000 | [diff] [blame] | 181 | tf->RegisterChecks(*this); |
Ted Kremenek | ceba6ea | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 182 | tf->RegisterPrinters(getStateManager().Printers); |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 185 | void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) { |
| 186 | if (!BatchAuditor) |
| 187 | BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 189 | ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C); |
Ted Kremenek | 7acc3a3 | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ted Kremenek | 4967c89 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 192 | void GRExprEngine::AddCheck(GRSimpleAPICheck *A) { |
| 193 | if (!BatchAuditor) |
| 194 | BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator())); |
| 195 | |
| 196 | ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A); |
| 197 | } |
| 198 | |
Zhongxing Xu | 5f078cb | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 199 | const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) { |
| 200 | const GRState *state = StateMgr.getInitialState(InitLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 84c6f0a | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 202 | // Preconditions. |
| 203 | |
Ted Kremenek | 5054663 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 204 | // FIXME: It would be nice if we had a more general mechanism to add |
| 205 | // such preconditions. Some day. |
Ted Kremenek | 84c6f0a | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 206 | const Decl *D = InitLoc->getDecl(); |
| 207 | |
| 208 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 209 | // Precondition: the first argument of 'main' is an integer guaranteed |
| 210 | // to be > 0. |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 211 | if (FD->getIdentifier()->getName() == "main" && |
Ted Kremenek | 5054663 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 212 | FD->getNumParams() > 0) { |
| 213 | const ParmVarDecl *PD = FD->getParamDecl(0); |
| 214 | QualType T = PD->getType(); |
| 215 | if (T->isIntegerType()) |
Ted Kremenek | 14536f6 | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 216 | if (const MemRegion *R = state->getRegion(PD, InitLoc)) { |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 217 | SVal V = state->getSVal(loc::MemRegionVal(R)); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 218 | SVal Constraint_untested = EvalBinOp(state, BinaryOperator::GT, V, |
| 219 | ValMgr.makeZeroVal(T), |
| 220 | getContext().IntTy); |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 222 | if (DefinedOrUnknownSVal *Constraint = |
| 223 | dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested)) { |
| 224 | if (const GRState *newState = state->Assume(*Constraint, true)) |
| 225 | state = newState; |
| 226 | } |
Ted Kremenek | 5054663 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Ted Kremenek | 84c6f0a | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 229 | } |
| 230 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 231 | // Precondition: 'self' is always non-null upon entry to an Objective-C |
| 232 | // method. |
| 233 | const ImplicitParamDecl *SelfD = MD->getSelfDecl(); |
| 234 | const MemRegion *R = state->getRegion(SelfD, InitLoc); |
| 235 | SVal V = state->getSVal(loc::MemRegionVal(R)); |
| 236 | |
| 237 | if (const Loc *LV = dyn_cast<Loc>(&V)) { |
| 238 | // Assume that the pointer value in 'self' is non-null. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 239 | state = state->Assume(*LV, true); |
Ted Kremenek | 84c6f0a | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 240 | assert(state && "'self' cannot be null"); |
| 241 | } |
| 242 | } |
| 243 | |
Ted Kremenek | 5054663 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 244 | return state; |
Ted Kremenek | 723fe3f | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 247 | //===----------------------------------------------------------------------===// |
| 248 | // Top-level transfer function logic (Dispatcher). |
| 249 | //===----------------------------------------------------------------------===// |
| 250 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 251 | void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | 91076ca | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 253 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), |
| 254 | S->getLocStart(), |
| 255 | "Error evaluating statement"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 257 | Builder = &builder; |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 258 | EntryNode = builder.getLastNode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | bc9118b | 2008-07-17 21:27:31 +0000 | [diff] [blame] | 260 | // FIXME: Consolidate. |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 261 | CurrentStmt = S; |
Ted Kremenek | bc9118b | 2008-07-17 21:27:31 +0000 | [diff] [blame] | 262 | StateMgr.CurrentStmt = S; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 264 | // Set up our simple checks. |
Ted Kremenek | c50e1a1 | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 265 | if (BatchAuditor) |
| 266 | Builder->setAuditor(BatchAuditor.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
| 268 | // Create the cleaned state. |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 269 | SymbolReaper SymReaper(Builder->getBasePredecessor()->getLiveVariables(), |
| 270 | SymMgr); |
Zhongxing Xu | 3ca89b9 | 2009-08-27 06:55:26 +0000 | [diff] [blame] | 271 | CleanedState = AMgr.shouldPurgeDead() |
| 272 | ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper) |
| 273 | : EntryNode->getState(); |
Ted Kremenek | 16fbfe6 | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 274 | |
Ted Kremenek | 3812b76 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 275 | // Process any special transfer function for dead symbols. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 276 | ExplodedNodeSet Tmp; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 16fbfe6 | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 278 | if (!SymReaper.hasDeadSymbols()) |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 279 | Tmp.Add(EntryNode); |
Ted Kremenek | 3812b76 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 280 | else { |
| 281 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 282 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
| 283 | |
Ted Kremenek | 9a935fb | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 284 | SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols); |
| 285 | Builder->PurgingDeadSymbols = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
| 287 | getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S, |
Ted Kremenek | 16fbfe6 | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 288 | CleanedState, SymReaper); |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 289 | |
| 290 | if (!Builder->BuildSinks && !Builder->HasGeneratedNode) |
| 291 | Tmp.Add(EntryNode); |
Ted Kremenek | 3812b76 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 292 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 294 | bool HasAutoGenerated = false; |
| 295 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 296 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 297 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 298 | ExplodedNodeSet Dst; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
| 300 | // Set the cleaned state. |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 301 | Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
| 303 | // Visit the statement. |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 304 | Visit(S, *I, Dst); |
| 305 | |
| 306 | // Do we need to auto-generate a node? We only need to do this to generate |
| 307 | // a node with a "cleaned" state; GRCoreEngine will actually handle |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | // auto-transitions for other cases. |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 309 | if (Dst.size() == 1 && *Dst.begin() == EntryNode |
| 310 | && !Builder->HasGeneratedNode && !HasAutoGenerated) { |
| 311 | HasAutoGenerated = true; |
| 312 | builder.generateNode(S, GetState(EntryNode), *I); |
| 313 | } |
Ted Kremenek | 3812b76 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 314 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 316 | // NULL out these variables to cleanup. |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 317 | CleanedState = NULL; |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 318 | EntryNode = NULL; |
Ted Kremenek | bc9118b | 2008-07-17 21:27:31 +0000 | [diff] [blame] | 319 | |
| 320 | // FIXME: Consolidate. |
| 321 | StateMgr.CurrentStmt = 0; |
| 322 | CurrentStmt = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | ae8014c | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 324 | Builder = NULL; |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 91076ca | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 328 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), |
| 329 | S->getLocStart(), |
| 330 | "Error evaluating statement"); |
| 331 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 332 | // FIXME: add metadata to the CFG so that we can disable |
| 333 | // this check when we KNOW that there is no block-level subexpression. |
| 334 | // The motivation is that this check requires a hashtable lookup. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Zhongxing Xu | 94ec649 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 336 | if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) { |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 337 | Dst.Add(Pred); |
| 338 | return; |
| 339 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 341 | switch (S->getStmtClass()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 343 | default: |
| 344 | // Cases we intentionally have "default" handle: |
| 345 | // AddrLabelExpr, IntegerLiteral, CharacterLiteral |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 347 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 348 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | da5cdda | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 350 | case Stmt::ArraySubscriptExprClass: |
| 351 | VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false); |
| 352 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 354 | case Stmt::AsmStmtClass: |
| 355 | VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst); |
| 356 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 358 | case Stmt::BinaryOperatorClass: { |
| 359 | BinaryOperator* B = cast<BinaryOperator>(S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 361 | if (B->isLogicalOp()) { |
| 362 | VisitLogicalExpr(B, Pred, Dst); |
| 363 | break; |
| 364 | } |
| 365 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 366 | const GRState* state = GetState(Pred); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 367 | MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS()))); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 368 | break; |
| 369 | } |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 370 | |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 371 | if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 372 | ExplodedNodeSet Tmp; |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 373 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S)); |
Ted Kremenek | dc3f50f | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 375 | } |
| 376 | else |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 377 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false); |
Ted Kremenek | dc3f50f | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 378 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 382 | case Stmt::CallExprClass: |
| 383 | case Stmt::CXXOperatorCallExprClass: { |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 384 | CallExpr* C = cast<CallExpr>(S); |
| 385 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 386 | break; |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 387 | } |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 389 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 390 | // the CFG do not model them as explicit control-flow. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 392 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 393 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 394 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 395 | break; |
| 396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 398 | case Stmt::CompoundAssignOperatorClass: |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 399 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 400 | break; |
Zhongxing Xu | 2c677c3 | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 401 | |
| 402 | case Stmt::CompoundLiteralExprClass: |
| 403 | VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false); |
| 404 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 406 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 407 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 408 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 409 | break; |
| 410 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 412 | case Stmt::DeclRefExprClass: |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 413 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 414 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 416 | case Stmt::DeclStmtClass: |
| 417 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 418 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Argyrios Kyrtzidis | 3bab3d2 | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 420 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 421 | case Stmt::CStyleCastExprClass: { |
Argyrios Kyrtzidis | 3bab3d2 | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 422 | CastExpr* C = cast<CastExpr>(S); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 423 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 424 | break; |
| 425 | } |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 426 | |
| 427 | case Stmt::InitListExprClass: |
| 428 | VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst); |
| 429 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 431 | case Stmt::MemberExprClass: |
Ted Kremenek | 38213f9 | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 432 | VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false); |
| 433 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 435 | case Stmt::ObjCIvarRefExprClass: |
| 436 | VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false); |
| 437 | break; |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 438 | |
| 439 | case Stmt::ObjCForCollectionStmtClass: |
| 440 | VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst); |
| 441 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 443 | case Stmt::ObjCMessageExprClass: { |
| 444 | VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst); |
| 445 | break; |
| 446 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | 1857ff4 | 2008-12-09 20:18:58 +0000 | [diff] [blame] | 448 | case Stmt::ObjCAtThrowStmtClass: { |
| 449 | // FIXME: This is not complete. We basically treat @throw as |
| 450 | // an abort. |
| 451 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 452 | Builder->BuildSinks = true; |
| 453 | MakeNode(Dst, S, Pred, GetState(Pred)); |
| 454 | break; |
| 455 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 457 | case Stmt::ParenExprClass: |
Ted Kremenek | da5cdda | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 458 | Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 459 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 461 | case Stmt::ReturnStmtClass: |
| 462 | VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst); |
| 463 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 465 | case Stmt::SizeOfAlignOfExprClass: |
| 466 | VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 467 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 469 | case Stmt::StmtExprClass: { |
| 470 | StmtExpr* SE = cast<StmtExpr>(S); |
Ted Kremenek | d25fb7a6 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 471 | |
| 472 | if (SE->getSubStmt()->body_empty()) { |
| 473 | // Empty statement expression. |
| 474 | assert(SE->getType() == getContext().VoidTy |
| 475 | && "Empty statement expression must have void type."); |
| 476 | Dst.Add(Pred); |
| 477 | break; |
| 478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Ted Kremenek | d25fb7a6 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 480 | if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) { |
| 481 | const GRState* state = GetState(Pred); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 482 | MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr))); |
Ted Kremenek | d25fb7a6 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 483 | } |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 484 | else |
| 485 | Dst.Add(Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 487 | break; |
| 488 | } |
Zhongxing Xu | d2fa1e0 | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 489 | |
| 490 | case Stmt::StringLiteralClass: |
| 491 | VisitLValue(cast<StringLiteral>(S), Pred, Dst); |
| 492 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Ted Kremenek | 891642e | 2009-03-18 23:49:26 +0000 | [diff] [blame] | 494 | case Stmt::UnaryOperatorClass: { |
| 495 | UnaryOperator *U = cast<UnaryOperator>(S); |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 496 | if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 497 | ExplodedNodeSet Tmp; |
Ted Kremenek | 891642e | 2009-03-18 23:49:26 +0000 | [diff] [blame] | 498 | VisitUnaryOperator(U, Pred, Tmp, false); |
| 499 | EvalEagerlyAssume(Dst, Tmp, U); |
| 500 | } |
| 501 | else |
| 502 | VisitUnaryOperator(U, Pred, Dst, false); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 503 | break; |
Ted Kremenek | 891642e | 2009-03-18 23:49:26 +0000 | [diff] [blame] | 504 | } |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 509 | ExplodedNodeSet& Dst) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 511 | Ex = Ex->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Zhongxing Xu | 94ec649 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 513 | if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) { |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 514 | Dst.Add(Pred); |
| 515 | return; |
| 516 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 518 | switch (Ex->getStmtClass()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 520 | case Stmt::ArraySubscriptExprClass: |
| 521 | VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true); |
| 522 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 524 | case Stmt::DeclRefExprClass: |
| 525 | VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true); |
| 526 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 528 | case Stmt::ObjCIvarRefExprClass: |
| 529 | VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true); |
| 530 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 532 | case Stmt::UnaryOperatorClass: |
| 533 | VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true); |
| 534 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 536 | case Stmt::MemberExprClass: |
| 537 | VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true); |
| 538 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 540 | case Stmt::CompoundLiteralExprClass: |
Zhongxing Xu | 2c677c3 | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 541 | VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true); |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 542 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | 5870046 | 2008-10-17 17:24:14 +0000 | [diff] [blame] | 544 | case Stmt::ObjCPropertyRefExprClass: |
Fariborz Jahanian | 9a84665 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 545 | case Stmt::ObjCImplicitSetterGetterRefExprClass: |
Ted Kremenek | 5870046 | 2008-10-17 17:24:14 +0000 | [diff] [blame] | 546 | // FIXME: Property assignments are lvalues, but not really "locations". |
| 547 | // e.g.: self.x = something; |
| 548 | // Here the "self.x" really can translate to a method call (setter) when |
| 549 | // the assignment is made. Moreover, the entire assignment expression |
| 550 | // evaluate to whatever "something" is, not calling the "getter" for |
| 551 | // the property (which would make sense since it can have side effects). |
| 552 | // We'll probably treat this as a location, but not one that we can |
| 553 | // take the address of. Perhaps we need a new SVal class for cases |
| 554 | // like thsis? |
| 555 | // Note that we have a similar problem for bitfields, since they don't |
| 556 | // have "locations" in the sense that we can take their address. |
| 557 | Dst.Add(Pred); |
Ted Kremenek | 8f5dc29 | 2008-10-18 04:08:49 +0000 | [diff] [blame] | 558 | return; |
Zhongxing Xu | 0d2706f | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 559 | |
| 560 | case Stmt::StringLiteralClass: { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 561 | const GRState* state = GetState(Pred); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 562 | SVal V = state->getLValue(cast<StringLiteral>(Ex)); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 563 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V)); |
Zhongxing Xu | 0d2706f | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 564 | return; |
| 565 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 567 | case Stmt::BinaryOperatorClass: |
| 568 | case Stmt::CompoundAssignOperatorClass: |
| 569 | VisitBinaryOperator(cast<BinaryOperator>(Ex), Pred, Dst, true); |
| 570 | return; |
| 571 | |
Ted Kremenek | 850422e | 2008-10-18 04:15:35 +0000 | [diff] [blame] | 572 | default: |
| 573 | // Arbitrary subexpressions can return aggregate temporaries that |
| 574 | // can be used in a lvalue context. We need to enhance our support |
| 575 | // of such temporaries in both the environment and the store, so right |
| 576 | // now we just do a regular visit. |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 577 | assert ((Ex->getType()->isAggregateType()) && |
Ted Kremenek | e69a1fa | 2008-10-25 20:09:21 +0000 | [diff] [blame] | 578 | "Other kinds of expressions with non-aggregate/union types do" |
| 579 | " not have lvalues."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Ted Kremenek | 850422e | 2008-10-18 04:15:35 +0000 | [diff] [blame] | 581 | Visit(Ex, Pred, Dst); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | //===----------------------------------------------------------------------===// |
| 586 | // Block entrance. (Update counters). |
| 587 | //===----------------------------------------------------------------------===// |
| 588 | |
Ted Kremenek | 5ab5a1b | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 589 | bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*, |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 590 | GRBlockCounter BC) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 592 | return BC.getNumVisited(B->getBlockID()) < 3; |
| 593 | } |
| 594 | |
| 595 | //===----------------------------------------------------------------------===// |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 596 | // Generic node creation. |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 599 | ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S, |
| 600 | ExplodedNode* Pred, const GRState* St, |
| 601 | ProgramPoint::Kind K, const void *tag) { |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 602 | assert (Builder && "GRStmtNodeBuilder not present."); |
| 603 | SaveAndRestore<const void*> OldTag(Builder->Tag); |
| 604 | Builder->Tag = tag; |
| 605 | return Builder->MakeNode(Dst, S, Pred, St, K); |
| 606 | } |
| 607 | |
| 608 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 609 | // Branch processing. |
| 610 | //===----------------------------------------------------------------------===// |
| 611 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 612 | const GRState* GRExprEngine::MarkBranch(const GRState* state, |
Ted Kremenek | a7b8ffb | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 613 | Stmt* Terminator, |
| 614 | bool branchTaken) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 616 | switch (Terminator->getStmtClass()) { |
| 617 | default: |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 618 | return state; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 620 | case Stmt::BinaryOperatorClass: { // '&&' and '||' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 622 | BinaryOperator* B = cast<BinaryOperator>(Terminator); |
| 623 | BinaryOperator::Opcode Op = B->getOpcode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 625 | assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 627 | // For &&, if we take the true branch, then the value of the whole |
| 628 | // expression is that of the RHS expression. |
| 629 | // |
| 630 | // For ||, if we take the false branch, then the value of the whole |
| 631 | // expression is that of the RHS expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 633 | Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | (Op == BinaryOperator::LOr && !branchTaken) |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 635 | ? B->getRHS() : B->getLHS(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 637 | return state->BindExpr(B, UndefinedVal(Ex)); |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 638 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 640 | case Stmt::ConditionalOperatorClass: { // ?: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 642 | ConditionalOperator* C = cast<ConditionalOperator>(Terminator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 644 | // For ?, if branchTaken == true then the value is either the LHS or |
| 645 | // the condition itself. (GNU extension). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
| 647 | Expr* Ex; |
| 648 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 649 | if (branchTaken) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | Ex = C->getLHS() ? C->getLHS() : C->getCond(); |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 651 | else |
| 652 | Ex = C->getRHS(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 654 | return state->BindExpr(C, UndefinedVal(Ex)); |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 655 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 657 | case Stmt::ChooseExprClass: { // ?: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 659 | ChooseExpr* C = cast<ChooseExpr>(Terminator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
| 661 | Expr* Ex = branchTaken ? C->getLHS() : C->getRHS(); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 662 | return state->BindExpr(C, UndefinedVal(Ex)); |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 667 | /// RecoverCastedSymbol - A helper function for ProcessBranch that is used |
| 668 | /// to try to recover some path-sensitivity for casts of symbolic |
| 669 | /// integers that promote their values (which are currently not tracked well). |
| 670 | /// This function returns the SVal bound to Condition->IgnoreCasts if all the |
| 671 | // cast(s) did was sign-extend the original value. |
| 672 | static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state, |
| 673 | Stmt* Condition, ASTContext& Ctx) { |
| 674 | |
| 675 | Expr *Ex = dyn_cast<Expr>(Condition); |
| 676 | if (!Ex) |
| 677 | return UnknownVal(); |
| 678 | |
| 679 | uint64_t bits = 0; |
| 680 | bool bitsInit = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 682 | while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) { |
| 683 | QualType T = CE->getType(); |
| 684 | |
| 685 | if (!T->isIntegerType()) |
| 686 | return UnknownVal(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 688 | uint64_t newBits = Ctx.getTypeSize(T); |
| 689 | if (!bitsInit || newBits < bits) { |
| 690 | bitsInit = true; |
| 691 | bits = newBits; |
| 692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 694 | Ex = CE->getSubExpr(); |
| 695 | } |
| 696 | |
| 697 | // We reached a non-cast. Is it a symbolic value? |
| 698 | QualType T = Ex->getType(); |
| 699 | |
| 700 | if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits) |
| 701 | return UnknownVal(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Ted Kremenek | 095f1a9 | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 703 | return state->getSVal(Ex); |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 706 | void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 707 | GRBranchNodeBuilder& builder) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Ted Kremenek | 8db4b11 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 709 | // Check for NULL conditions; e.g. "for(;;)" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | if (!Condition) { |
Ted Kremenek | 8db4b11 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 711 | builder.markInfeasible(false); |
Ted Kremenek | 8db4b11 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 712 | return; |
| 713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Ted Kremenek | 32c41ec | 2009-03-11 03:54:24 +0000 | [diff] [blame] | 715 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), |
| 716 | Condition->getLocStart(), |
| 717 | "Error evaluating branch"); |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 718 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | const GRState* PrevState = builder.getState(); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 720 | SVal X = PrevState->getSVal(Condition); |
| 721 | DefinedSVal *V = NULL; |
| 722 | |
| 723 | while (true) { |
| 724 | V = dyn_cast<DefinedSVal>(&X); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 726 | if (!V) { |
| 727 | if (X.isUnknown()) { |
| 728 | if (const Expr *Ex = dyn_cast<Expr>(Condition)) { |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 729 | if (Ex->getType()->isIntegerType()) { |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 730 | // Try to recover some path-sensitivity. Right now casts of symbolic |
| 731 | // integers that promote their values are currently not tracked well. |
| 732 | // If 'Condition' is such an expression, try and recover the |
| 733 | // underlying value and use that instead. |
| 734 | SVal recovered = RecoverCastedSymbol(getStateManager(), |
| 735 | builder.getState(), Condition, |
| 736 | getContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 738 | if (!recovered.isUnknown()) { |
| 739 | X = recovered; |
| 740 | continue; |
| 741 | } |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 742 | } |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | builder.generateNode(MarkBranch(PrevState, Term, true), true); |
| 746 | builder.generateNode(MarkBranch(PrevState, Term, false), false); |
| 747 | return; |
Ted Kremenek | 22358bd | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 748 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 750 | assert(X.isUndef()); |
| 751 | ExplodedNode *N = builder.generateNode(PrevState, true); |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 752 | |
| 753 | if (N) { |
| 754 | N->markAsSink(); |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 755 | UndefBranches.insert(N); |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 756 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 758 | builder.markInfeasible(false); |
| 759 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | } |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 761 | |
| 762 | break; |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | |
Ted Kremenek | 17f4dbd | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 765 | // Process the true branch. |
Ted Kremenek | af9f362 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 766 | if (builder.isFeasible(true)) { |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 767 | if (const GRState *state = PrevState->Assume(*V, true)) |
Ted Kremenek | af9f362 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 768 | builder.generateNode(MarkBranch(state, Term, true), true); |
| 769 | else |
| 770 | builder.markInfeasible(true); |
| 771 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
| 773 | // Process the false branch. |
Ted Kremenek | af9f362 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 774 | if (builder.isFeasible(false)) { |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 775 | if (const GRState *state = PrevState->Assume(*V, false)) |
Ted Kremenek | af9f362 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 776 | builder.generateNode(MarkBranch(state, Term, false), false); |
| 777 | else |
| 778 | builder.markInfeasible(false); |
| 779 | } |
Ted Kremenek | 7ff1893 | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 782 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 783 | /// nodes by processing the 'effects' of a computed goto jump. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 784 | void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 785 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | const GRState *state = builder.getState(); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 787 | SVal V = state->getSVal(builder.getTarget()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 789 | // Three possibilities: |
| 790 | // |
| 791 | // (1) We know the computed label. |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 792 | // (2) The label is NULL (or some other constant), or Undefined. |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 793 | // (3) We have no clue about the label. Dispatch to all targets. |
| 794 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 796 | typedef GRIndirectGotoNodeBuilder::iterator iterator; |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 797 | |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 798 | if (isa<loc::GotoLabel>(V)) { |
| 799 | LabelStmt* L = cast<loc::GotoLabel>(V).getLabel(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 801 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 2bba901 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 802 | if (I.getLabel() == L) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 803 | builder.generateNode(I, state); |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 804 | return; |
| 805 | } |
| 806 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 808 | assert (false && "No block with label."); |
| 809 | return; |
| 810 | } |
| 811 | |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 812 | if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) { |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 813 | // Dispatch to the first target and mark it as a sink. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 814 | ExplodedNode* N = builder.generateNode(builder.begin(), state, true); |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 815 | UndefBranches.insert(N); |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 816 | return; |
| 817 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 819 | // This is really a catch-all. We don't support symbolics yet. |
Ted Kremenek | 9c03f68 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 820 | // FIXME: Implement dispatch for symbolic pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 822 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 823 | builder.generateNode(I, state); |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 824 | } |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 825 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 826 | |
| 827 | void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 828 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Zhongxing Xu | 94ec649 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 830 | assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 832 | const GRState* state = GetState(Pred); |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 833 | SVal X = state->getSVal(Ex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 835 | assert (X.isUndef()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 837 | Expr *SE = (Expr*) cast<UndefinedVal>(X).getData(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | assert(SE); |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 839 | X = state->getSVal(SE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 841 | // Make sure that we invalidate the previous binding. |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 842 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true)); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 845 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 846 | /// nodes by processing the 'effects' of a switch statement. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) { |
| 848 | typedef GRSwitchNodeBuilder::iterator iterator; |
| 849 | const GRState* state = builder.getState(); |
Ted Kremenek | 346169f | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 850 | Expr* CondE = builder.getCondition(); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 851 | SVal CondV_untested = state->getSVal(CondE); |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 852 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 853 | if (CondV_untested.isUndef()) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 854 | ExplodedNode* N = builder.generateDefaultCaseNode(state, true); |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 855 | UndefBranches.insert(N); |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 856 | return; |
| 857 | } |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 858 | DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested); |
Ted Kremenek | 346169f | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 859 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 860 | const GRState *DefaultSt = state; |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 861 | bool defaultIsFeasible = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 863 | for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) { |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 864 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 865 | |
| 866 | // Evaluate the LHS of the case value. |
| 867 | Expr::EvalResult V1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | bool b = Case->getLHS()->Evaluate(V1, getContext()); |
| 869 | |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 870 | // Sanity checks. These go away in Release builds. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | assert(b && V1.Val.isInt() && !V1.HasSideEffects |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 872 | && "Case condition must evaluate to an integer constant."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | b = b; // silence unused variable warning |
| 874 | assert(V1.Val.getInt().getBitWidth() == |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 875 | getContext().getTypeSize(CondE->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 877 | // Get the RHS of the case, if it exists. |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 878 | Expr::EvalResult V2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 880 | if (Expr* E = Case->getRHS()) { |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 881 | b = E->Evaluate(V2, getContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | assert(b && V2.Val.isInt() && !V2.HasSideEffects |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 883 | && "Case condition must evaluate to an integer constant."); |
| 884 | b = b; // silence unused variable warning |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 885 | } |
Ted Kremenek | 9eae403 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 886 | else |
| 887 | V2 = V1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 889 | // FIXME: Eventually we should replace the logic below with a range |
| 890 | // comparison, rather than concretize the values within the range. |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 891 | // This should be easy once we have "ranges" for NonLVals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Ted Kremenek | 9eae403 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 893 | do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt())); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 895 | DefinedOrUnknownSVal Res = SVator.EvalEQ(DefaultSt, CondV, CaseVal); |
| 896 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | // Now "assume" that the case matches. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 898 | if (const GRState* stateNew = state->Assume(Res, true)) { |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 899 | builder.generateCaseStmtNode(I, stateNew); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 901 | // If CondV evaluates to a constant, then we know that this |
| 902 | // is the *only* case that we can take, so stop evaluating the |
| 903 | // others. |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 904 | if (isa<nonloc::ConcreteInt>(CondV)) |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 908 | // Now "assume" that the case doesn't match. Add this state |
| 909 | // to the default state (if it is feasible). |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 910 | if (const GRState *stateNew = DefaultSt->Assume(Res, false)) { |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 911 | defaultIsFeasible = true; |
| 912 | DefaultSt = stateNew; |
Ted Kremenek | d2419a0 | 2008-04-23 05:03:18 +0000 | [diff] [blame] | 913 | } |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 914 | |
Ted Kremenek | 9eae403 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 915 | // Concretize the next value in the range. |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 916 | if (V1.Val.getInt() == V2.Val.getInt()) |
Ted Kremenek | 9eae403 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 917 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | |
Ted Kremenek | 1ab188f | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 919 | ++V1.Val.getInt(); |
| 920 | assert (V1.Val.getInt() <= V2.Val.getInt()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Ted Kremenek | 9eae403 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 922 | } while (true); |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 925 | // If we reach here, than we know that the default branch is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | // possible. |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 927 | if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt); |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 930 | //===----------------------------------------------------------------------===// |
| 931 | // Transfer functions: logical operations ('&&', '||'). |
| 932 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 933 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 934 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred, |
| 935 | ExplodedNodeSet& Dst) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 937 | assert(B->getOpcode() == BinaryOperator::LAnd || |
| 938 | B->getOpcode() == BinaryOperator::LOr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | |
Zhongxing Xu | 94ec649 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 940 | assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 942 | const GRState* state = GetState(Pred); |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 943 | SVal X = state->getSVal(B); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 944 | assert(X.isUndef()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 946 | const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData(); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 947 | assert(Ex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 949 | if (Ex == B->getRHS()) { |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 950 | X = state->getSVal(Ex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 952 | // Handle undefined values. |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 953 | if (X.isUndef()) { |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 954 | MakeNode(Dst, B, Pred, state->BindExpr(B, X)); |
Ted Kremenek | bc54390 | 2008-02-26 19:40:44 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 957 | |
| 958 | DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 960 | // We took the RHS. Because the value of the '&&' or '||' expression must |
| 961 | // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0 |
| 962 | // or 1. Alternatively, we could take a lazy approach, and calculate this |
| 963 | // value later when necessary. We don't have the machinery in place for |
| 964 | // this right now, and since most logical expressions are used for branches, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | // the payoff is not likely to be large. Instead, we do eager evaluation. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 966 | if (const GRState *newState = state->Assume(XD, true)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | MakeNode(Dst, B, Pred, |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 968 | newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType()))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 970 | if (const GRState *newState = state->Assume(XD, false)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | MakeNode(Dst, B, Pred, |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 972 | newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType()))); |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 973 | } |
| 974 | else { |
Ted Kremenek | f3a4b96 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 975 | // We took the LHS expression. Depending on whether we are '&&' or |
| 976 | // '||' we know what the value of the expression is via properties of |
| 977 | // the short-circuiting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 979 | B->getType()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 980 | MakeNode(Dst, B, Pred, state->BindExpr(B, X)); |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 981 | } |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 982 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 984 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 90c7cb6 | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 985 | // Transfer functions: Loads and stores. |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 986 | //===----------------------------------------------------------------------===// |
Ted Kremenek | de8d62b | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 987 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred, |
Ted Kremenek | 14536f6 | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 989 | ExplodedNodeSet &Dst, bool asLValue) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 991 | const GRState *state = GetState(Pred); |
| 992 | const NamedDecl *D = Ex->getDecl(); |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 993 | |
| 994 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 995 | |
Ted Kremenek | 14536f6 | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 996 | SVal V = state->getLValue(VD, Pred->getLocationContext()); |
Zhongxing Xu | 252fe5c | 2008-10-17 02:20:14 +0000 | [diff] [blame] | 997 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 998 | if (asLValue) |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 999 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), |
Ted Kremenek | a6e0832 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 1000 | ProgramPoint::PostLValueKind); |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1001 | else |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1002 | EvalLoad(Dst, Ex, Pred, state, V); |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | |
| 1005 | } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) { |
| 1006 | assert(!asLValue && "EnumConstantDecl does not have lvalue."); |
| 1007 | |
Zhongxing Xu | d09b520 | 2009-06-23 06:13:19 +0000 | [diff] [blame] | 1008 | SVal V = ValMgr.makeIntVal(ED->getInitVal()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1009 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V)); |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | |
| 1012 | } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | 1624a47 | 2009-09-23 01:30:01 +0000 | [diff] [blame] | 1013 | // This code is valid regardless of the value of 'isLValue'. |
Zhongxing Xu | ac12943 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1014 | SVal V = ValMgr.getFunctionPointer(FD); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1015 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), |
Ted Kremenek | a6e0832 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 1016 | ProgramPoint::PostLValueKind); |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1017 | return; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1018 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1020 | assert (false && |
| 1021 | "ValueDecl support for this ValueDecl not implemented."); |
Ted Kremenek | 88da1de | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Ted Kremenek | da5cdda | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 1024 | /// VisitArraySubscriptExpr - Transfer function for array accesses |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1026 | ExplodedNode* Pred, |
| 1027 | ExplodedNodeSet& Dst, bool asLValue){ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Ted Kremenek | da5cdda | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 1029 | Expr* Base = A->getBase()->IgnoreParens(); |
Ted Kremenek | 10246e8 | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1030 | Expr* Idx = A->getIdx()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1031 | ExplodedNodeSet Tmp; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | cce27f5 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 1033 | if (Base->getType()->isVectorType()) { |
| 1034 | // For vector types get its lvalue. |
| 1035 | // FIXME: This may not be correct. Is the rvalue of a vector its location? |
| 1036 | // In fact, I think this is just a hack. We need to get the right |
| 1037 | // semantics. |
| 1038 | VisitLValue(Base, Pred, Tmp); |
| 1039 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | else |
Ted Kremenek | cce27f5 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 1041 | Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1043 | for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) { |
| 1044 | ExplodedNodeSet Tmp2; |
Ted Kremenek | 3ad391d | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 1045 | Visit(Idx, *I1, Tmp2); // Evaluate the index. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1047 | for (ExplodedNodeSet::iterator I2=Tmp2.begin(),E2=Tmp2.end();I2!=E2; ++I2) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1048 | const GRState* state = GetState(*I2); |
Zhongxing Xu | 7d6387b | 2009-10-14 03:33:08 +0000 | [diff] [blame] | 1049 | SVal V = state->getLValue(A->getType(), state->getSVal(Idx), |
| 1050 | state->getSVal(Base)); |
Ted Kremenek | 10246e8 | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1051 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1052 | if (asLValue) |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1053 | MakeNode(Dst, A, *I2, state->BindExpr(A, V), |
Ted Kremenek | a6e0832 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 1054 | ProgramPoint::PostLValueKind); |
Ted Kremenek | 10246e8 | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1055 | else |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1056 | EvalLoad(Dst, A, *I2, state, V); |
Ted Kremenek | 10246e8 | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1057 | } |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1058 | } |
Ted Kremenek | da5cdda | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Ted Kremenek | 38213f9 | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1061 | /// VisitMemberExpr - Transfer function for member expressions. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1062 | void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred, |
| 1063 | ExplodedNodeSet& Dst, bool asLValue) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Ted Kremenek | 38213f9 | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1065 | Expr* Base = M->getBase()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1066 | ExplodedNodeSet Tmp; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (M->isArrow()) |
Ted Kremenek | fef1f30 | 2008-10-18 03:28:48 +0000 | [diff] [blame] | 1069 | Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f |
| 1070 | else |
| 1071 | VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1073 | FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl()); |
| 1074 | if (!Field) // FIXME: skipping member expressions for non-fields |
| 1075 | return; |
| 1076 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1077 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1078 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ad391d | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 1079 | // FIXME: Should we insert some assumption logic in here to determine |
| 1080 | // if "Base" is a valid piece of memory? Before we put this assumption |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1081 | // later when using FieldOffset lvals (which we no longer have). |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 1082 | SVal L = state->getLValue(Field, state->getSVal(Base)); |
Ted Kremenek | 3ad391d | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 1083 | |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 1084 | if (asLValue) |
| 1085 | MakeNode(Dst, M, *I, state->BindExpr(M, L), ProgramPoint::PostLValueKind); |
| 1086 | else |
| 1087 | EvalLoad(Dst, M, *I, state, L); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1088 | } |
Ted Kremenek | 38213f9 | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1091 | /// EvalBind - Handle the semantics of binding a value to a specific location. |
| 1092 | /// This method is used by EvalStore and (soon) VisitDeclStmt, and others. |
Ted Kremenek | b006b82 | 2009-11-04 00:09:15 +0000 | [diff] [blame^] | 1093 | void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Stmt* Ex, ExplodedNode* Pred, |
| 1094 | const GRState* state, SVal location, SVal Val, |
| 1095 | bool atDeclInit) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1096 | |
Ted Kremenek | e68c0fc | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1097 | const GRState* newState = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Ted Kremenek | b006b82 | 2009-11-04 00:09:15 +0000 | [diff] [blame^] | 1099 | if (atDeclInit) { |
| 1100 | const VarRegion *VR = |
| 1101 | cast<VarRegion>(cast<loc::MemRegionVal>(location).getRegion()); |
| 1102 | |
| 1103 | newState = state->bindDecl(VR, Val); |
Ted Kremenek | e68c0fc | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1104 | } |
| 1105 | else { |
Ted Kremenek | b006b82 | 2009-11-04 00:09:15 +0000 | [diff] [blame^] | 1106 | if (location.isUnknown()) { |
| 1107 | // We know that the new state will be the same as the old state since |
| 1108 | // the location of the binding is "unknown". Consequently, there |
| 1109 | // is no reason to just create a new node. |
| 1110 | newState = state; |
| 1111 | } |
| 1112 | else { |
| 1113 | // We are binding to a value other than 'unknown'. Perform the binding |
| 1114 | // using the StoreManager. |
| 1115 | newState = state->bindLoc(cast<Loc>(location), Val); |
| 1116 | } |
Ted Kremenek | e68c0fc | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1117 | } |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1118 | |
Ted Kremenek | e68c0fc | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1119 | // The next thing to do is check if the GRTransferFuncs object wants to |
| 1120 | // update the state based on the new binding. If the GRTransferFunc object |
| 1121 | // doesn't do anything, just auto-propagate the current state. |
| 1122 | GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex, |
| 1123 | newState != state); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
Ted Kremenek | e68c0fc | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1125 | getTF().EvalBind(BuilderRef, location, Val); |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /// EvalStore - Handle the semantics of a store via an assignment. |
| 1129 | /// @param Dst The node set to store generated state nodes |
| 1130 | /// @param Ex The expression representing the location of the store |
| 1131 | /// @param state The current simulation state |
| 1132 | /// @param location The location to store the value |
| 1133 | /// @param Val The value to be stored |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1134 | void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred, |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1135 | const GRState* state, SVal location, SVal Val, |
| 1136 | const void *tag) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Ted Kremenek | 90c7cb6 | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 1138 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1140 | // Evaluate the location (checks for bad dereferences). |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1141 | Pred = EvalLocation(Ex, Pred, state, location, tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1143 | if (!Pred) |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1144 | return; |
Ted Kremenek | c072b82 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1145 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1146 | assert (!location.isUndef()); |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1147 | state = GetState(Pred); |
| 1148 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | // Proceed with the store. |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1150 | SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind); |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1151 | SaveAndRestore<const void*> OldTag(Builder->Tag); |
| 1152 | Builder->PointKind = ProgramPoint::PostStoreKind; |
| 1153 | Builder->Tag = tag; |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1154 | EvalBind(Dst, Ex, Pred, state, location, Val); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1157 | void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred, |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1158 | const GRState* state, SVal location, |
| 1159 | const void *tag) { |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1160 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | // Evaluate the location (checks for bad dereferences). |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1162 | Pred = EvalLocation(Ex, Pred, state, location, tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1164 | if (!Pred) |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1165 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1167 | state = GetState(Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1169 | // Proceed with the load. |
Ted Kremenek | f1b9209 | 2008-08-28 18:43:46 +0000 | [diff] [blame] | 1170 | ProgramPoint::Kind K = ProgramPoint::PostLoadKind; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1171 | |
| 1172 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 1173 | // for the contents of values. We need a better approach. |
| 1174 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1175 | if (location.isUnknown()) { |
Ted Kremenek | 5cc9e60 | 2008-04-30 04:23:07 +0000 | [diff] [blame] | 1176 | // This is important. We must nuke the old binding. |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1177 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, UnknownVal()), |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 1178 | K, tag); |
Ted Kremenek | 5cc9e60 | 2008-04-30 04:23:07 +0000 | [diff] [blame] | 1179 | } |
Zhongxing Xu | 33178a0 | 2008-11-28 08:34:30 +0000 | [diff] [blame] | 1180 | else { |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1181 | SVal V = state->getSVal(cast<Loc>(location), Ex->getType()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1182 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), K, tag); |
Zhongxing Xu | 33178a0 | 2008-11-28 08:34:30 +0000 | [diff] [blame] | 1183 | } |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, Expr* StoreE, |
| 1187 | ExplodedNode* Pred, const GRState* state, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1188 | SVal location, SVal Val, const void *tag) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1190 | ExplodedNodeSet TmpDst; |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1191 | EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag); |
Ted Kremenek | bc15d85 | 2008-09-20 01:50:34 +0000 | [diff] [blame] | 1192 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1193 | for (ExplodedNodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I) |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1194 | MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag); |
Ted Kremenek | bc15d85 | 2008-09-20 01:50:34 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1197 | ExplodedNode* GRExprEngine::EvalLocation(Stmt* Ex, ExplodedNode* Pred, |
| 1198 | const GRState* state, SVal location, |
| 1199 | const void *tag) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1201 | SaveAndRestore<const void*> OldTag(Builder->Tag); |
| 1202 | Builder->Tag = tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Ted Kremenek | fac290d | 2009-11-02 23:19:29 +0000 | [diff] [blame] | 1204 | if (location.isUnknown() || Checkers.empty()) |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1205 | return Pred; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Ted Kremenek | fac290d | 2009-11-02 23:19:29 +0000 | [diff] [blame] | 1207 | |
| 1208 | for (CheckersOrdered::iterator I=Checkers.begin(), E=Checkers.end(); I!=E;++I) |
| 1209 | { |
| 1210 | Pred = I->second->CheckLocation(Ex, Pred, state, location, *this); |
| 1211 | if (!Pred) |
| 1212 | break; |
| 1213 | } |
| 1214 | |
| 1215 | return Pred; |
Zhongxing Xu | 3d43015 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1216 | |
Ted Kremenek | eb01ba6 | 2009-08-01 05:59:39 +0000 | [diff] [blame] | 1217 | // FIXME: Temporarily disable out-of-bounds checking until we make |
| 1218 | // the logic reflect recent changes to CastRegion and friends. |
| 1219 | #if 0 |
Zhongxing Xu | 3d43015 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1220 | // Check for out-of-bound array access. |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1221 | if (isa<loc::MemRegionVal>(LV)) { |
Zhongxing Xu | 3d43015 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1222 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
| 1223 | if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { |
| 1224 | // Get the index of the accessed element. |
| 1225 | SVal Idx = ER->getIndex(); |
| 1226 | // Get the extent of the array. |
Zhongxing Xu | 6587c75 | 2008-11-24 07:02:06 +0000 | [diff] [blame] | 1227 | SVal NumElements = getStoreManager().getSizeInElements(StNotNull, |
| 1228 | ER->getSuperRegion()); |
Zhongxing Xu | 3d43015 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1229 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1230 | const GRState * StInBound = StNotNull->AssumeInBound(Idx, NumElements, |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1231 | true); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1232 | const GRState* StOutBound = StNotNull->AssumeInBound(Idx, NumElements, |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1233 | false); |
Zhongxing Xu | 3d43015 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1234 | |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1235 | if (StOutBound) { |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1236 | // Report warning. Make sink node manually. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1237 | ExplodedNode* OOBNode = |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1238 | Builder->generateNode(Ex, StOutBound, Pred, |
| 1239 | ProgramPoint::PostOutOfBoundsCheckFailedKind); |
Zhongxing Xu | aa86cff | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (OOBNode) { |
| 1242 | OOBNode->markAsSink(); |
| 1243 | |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1244 | if (StInBound) |
Zhongxing Xu | aa86cff | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 1245 | ImplicitOOBMemAccesses.insert(OOBNode); |
| 1246 | else |
| 1247 | ExplicitOOBMemAccesses.insert(OOBNode); |
| 1248 | } |
Zhongxing Xu | 4d45b34 | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1251 | if (!StInBound) |
| 1252 | return NULL; |
| 1253 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1254 | StNotNull = StInBound; |
Zhongxing Xu | 3d43015 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
Ted Kremenek | eb01ba6 | 2009-08-01 05:59:39 +0000 | [diff] [blame] | 1257 | #endif |
Ted Kremenek | 90c7cb6 | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1260 | //===----------------------------------------------------------------------===// |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1261 | // Transfer function: OSAtomics. |
| 1262 | // |
| 1263 | // FIXME: Eventually refactor into a more "plugin" infrastructure. |
| 1264 | //===----------------------------------------------------------------------===// |
| 1265 | |
| 1266 | // Mac OS X: |
| 1267 | // http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3 |
| 1268 | // atomic.3.html |
| 1269 | // |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1270 | static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst, |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1271 | GRExprEngine& Engine, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1272 | GRStmtNodeBuilder& Builder, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | CallExpr* CE, SVal L, |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1274 | ExplodedNode* Pred) { |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Not enough arguments to match OSAtomicCompareAndSwap? |
| 1277 | if (CE->getNumArgs() != 3) |
| 1278 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1280 | ASTContext &C = Engine.getContext(); |
| 1281 | Expr *oldValueExpr = CE->getArg(0); |
| 1282 | QualType oldValueType = C.getCanonicalType(oldValueExpr->getType()); |
| 1283 | |
| 1284 | Expr *newValueExpr = CE->getArg(1); |
| 1285 | QualType newValueType = C.getCanonicalType(newValueExpr->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1287 | // Do the types of 'oldValue' and 'newValue' match? |
| 1288 | if (oldValueType != newValueType) |
| 1289 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1291 | Expr *theValueExpr = CE->getArg(2); |
Ted Kremenek | eb01ba6 | 2009-08-01 05:59:39 +0000 | [diff] [blame] | 1292 | const PointerType *theValueType = |
| 1293 | theValueExpr->getType()->getAs<PointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1295 | // theValueType not a pointer? |
| 1296 | if (!theValueType) |
| 1297 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1299 | QualType theValueTypePointee = |
| 1300 | C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1302 | // The pointee must match newValueType and oldValueType. |
| 1303 | if (theValueTypePointee != newValueType) |
| 1304 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1306 | static unsigned magic_load = 0; |
| 1307 | static unsigned magic_store = 0; |
| 1308 | |
| 1309 | const void *OSAtomicLoadTag = &magic_load; |
| 1310 | const void *OSAtomicStoreTag = &magic_store; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1312 | // Load 'theValue'. |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1313 | const GRState *state = Pred->getState(); |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1314 | ExplodedNodeSet Tmp; |
Ted Kremenek | 095f1a9 | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 1315 | SVal location = state->getSVal(theValueExpr); |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1316 | Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag); |
| 1317 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1318 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1319 | I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1321 | ExplodedNode *N = *I; |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1322 | const GRState *stateLoad = N->getState(); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1323 | SVal theValueVal_untested = stateLoad->getSVal(theValueExpr); |
| 1324 | SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | |
Ted Kremenek | a1f9c7f | 2009-07-20 20:38:59 +0000 | [diff] [blame] | 1326 | // FIXME: Issue an error. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1327 | if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | return false; |
Ted Kremenek | a1f9c7f | 2009-07-20 20:38:59 +0000 | [diff] [blame] | 1329 | } |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1330 | |
| 1331 | DefinedOrUnknownSVal theValueVal = |
| 1332 | cast<DefinedOrUnknownSVal>(theValueVal_untested); |
| 1333 | DefinedOrUnknownSVal oldValueVal = |
| 1334 | cast<DefinedOrUnknownSVal>(oldValueVal_untested); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | 3a459dc | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 1336 | SValuator &SVator = Engine.getSValuator(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1338 | // Perform the comparison. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1339 | DefinedOrUnknownSVal Cmp = SVator.EvalEQ(stateLoad, theValueVal, |
| 1340 | oldValueVal); |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1341 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1342 | const GRState *stateEqual = stateLoad->Assume(Cmp, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1344 | // Were they equal? |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1345 | if (stateEqual) { |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1346 | // Perform the store. |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1347 | ExplodedNodeSet TmpStore; |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1348 | SVal val = stateEqual->getSVal(newValueExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1350 | // Handle implicit value casts. |
| 1351 | if (const TypedRegion *R = |
| 1352 | dyn_cast_or_null<TypedRegion>(location.getAsRegion())) { |
Ted Kremenek | 3a459dc | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 1353 | llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C), |
| 1354 | newValueExpr->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location, |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1358 | val, OSAtomicStoreTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1360 | // Now bind the result of the comparison. |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1361 | for (ExplodedNodeSet::iterator I2 = TmpStore.begin(), |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1362 | E2 = TmpStore.end(); I2 != E2; ++I2) { |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1363 | ExplodedNode *predNew = *I2; |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1364 | const GRState *stateNew = predNew->getState(); |
| 1365 | SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1366 | Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res)); |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1370 | // Were they not equal? |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1371 | if (const GRState *stateNotEqual = stateLoad->Assume(Cmp, false)) { |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1372 | SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1373 | Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res)); |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1377 | return true; |
| 1378 | } |
| 1379 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1380 | static bool EvalOSAtomic(ExplodedNodeSet& Dst, |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1381 | GRExprEngine& Engine, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1382 | GRStmtNodeBuilder& Builder, |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1383 | CallExpr* CE, SVal L, |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1384 | ExplodedNode* Pred) { |
Zhongxing Xu | ac12943 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1385 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
| 1386 | if (!FD) |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1387 | return false; |
Zhongxing Xu | ac12943 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1388 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1389 | const char *FName = FD->getNameAsCString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1391 | // Check for compare and swap. |
Ted Kremenek | 4531be1 | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 1392 | if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 || |
| 1393 | strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0) |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1394 | return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred); |
Ted Kremenek | 4531be1 | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 1395 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1396 | // FIXME: Other atomics. |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1401 | // Transfer function: Function calls. |
| 1402 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1748d8a | 2009-09-04 02:13:36 +0000 | [diff] [blame] | 1403 | static void MarkNoReturnFunction(const FunctionDecl *FD, CallExpr *CE, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | const GRState *state, |
Zhongxing Xu | 1748d8a | 2009-09-04 02:13:36 +0000 | [diff] [blame] | 1405 | GRStmtNodeBuilder *Builder) { |
Zhongxing Xu | bfb000f3 | 2009-09-04 02:17:35 +0000 | [diff] [blame] | 1406 | if (!FD) |
| 1407 | return; |
| 1408 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | if (FD->getAttr<NoReturnAttr>() || |
Zhongxing Xu | 1748d8a | 2009-09-04 02:13:36 +0000 | [diff] [blame] | 1410 | FD->getAttr<AnalyzerNoReturnAttr>()) |
| 1411 | Builder->BuildSinks = true; |
| 1412 | else { |
| 1413 | // HACK: Some functions are not marked noreturn, and don't return. |
| 1414 | // Here are a few hardwired ones. If this takes too long, we can |
| 1415 | // potentially cache these results. |
Douglas Gregor | f7b87cb | 2009-10-29 00:41:01 +0000 | [diff] [blame] | 1416 | using llvm::StringRef; |
| 1417 | bool BuildSinks |
| 1418 | = llvm::StringSwitch<bool>(StringRef(FD->getIdentifier()->getName())) |
| 1419 | .Case("exit", true) |
| 1420 | .Case("panic", true) |
| 1421 | .Case("error", true) |
| 1422 | .Case("Assert", true) |
| 1423 | // FIXME: This is just a wrapper around throwing an exception. |
| 1424 | // Eventually inter-procedural analysis should handle this easily. |
| 1425 | .Case("ziperr", true) |
| 1426 | .Case("assfail", true) |
| 1427 | .Case("db_error", true) |
| 1428 | .Case("__assert", true) |
| 1429 | .Case("__assert_rtn", true) |
| 1430 | .Case("__assert_fail", true) |
| 1431 | .Case("dtrace_assfail", true) |
| 1432 | .Case("yy_fatal_error", true) |
| 1433 | .Case("_XCAssertionFailureHandler", true) |
| 1434 | .Case("_DTAssertionFailureHandler", true) |
| 1435 | .Case("_TSAssertionFailureHandler", true) |
| 1436 | .Default(false); |
| 1437 | |
| 1438 | if (BuildSinks) |
| 1439 | Builder->BuildSinks = true; |
Zhongxing Xu | 1748d8a | 2009-09-04 02:13:36 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1442 | |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1443 | bool GRExprEngine::EvalBuiltinFunction(const FunctionDecl *FD, CallExpr *CE, |
| 1444 | ExplodedNode *Pred, |
| 1445 | ExplodedNodeSet &Dst) { |
| 1446 | if (!FD) |
| 1447 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1449 | unsigned id = FD->getBuiltinID(); |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1450 | if (!id) |
| 1451 | return false; |
| 1452 | |
| 1453 | const GRState *state = Pred->getState(); |
| 1454 | |
| 1455 | switch (id) { |
| 1456 | case Builtin::BI__builtin_expect: { |
| 1457 | // For __builtin_expect, just return the value of the subexpression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | assert (CE->arg_begin() != CE->arg_end()); |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1459 | SVal X = state->getSVal(*(CE->arg_begin())); |
| 1460 | MakeNode(Dst, CE, Pred, state->BindExpr(CE, X)); |
| 1461 | return true; |
| 1462 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1464 | case Builtin::BI__builtin_alloca: { |
| 1465 | // FIXME: Refactor into StoreManager itself? |
| 1466 | MemRegionManager& RM = getStateManager().getRegionManager(); |
| 1467 | const MemRegion* R = |
| 1468 | RM.getAllocaRegion(CE, Builder->getCurrentBlockCount()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1470 | // Set the extent of the region in bytes. This enables us to use the |
| 1471 | // SVal of the argument directly. If we save the extent in bits, we |
| 1472 | // cannot represent values like symbol*8. |
| 1473 | SVal Extent = state->getSVal(*(CE->arg_begin())); |
| 1474 | state = getStoreManager().setExtent(state, R, Extent); |
| 1475 | MakeNode(Dst, CE, Pred, state->BindExpr(CE, loc::MemRegionVal(R))); |
| 1476 | return true; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | return false; |
| 1481 | } |
| 1482 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | void GRExprEngine::EvalCall(ExplodedNodeSet& Dst, CallExpr* CE, SVal L, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1484 | ExplodedNode* Pred) { |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1485 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1487 | // FIXME: Allow us to chain together transfer functions. |
| 1488 | if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred)) |
| 1489 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1491 | getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred); |
| 1492 | } |
| 1493 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1494 | void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred, |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1495 | CallExpr::arg_iterator AI, |
| 1496 | CallExpr::arg_iterator AE, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | ExplodedNodeSet& Dst) { |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1498 | // Determine the type of function we're calling (if available). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1499 | const FunctionProtoType *Proto = NULL; |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1500 | QualType FnType = CE->getCallee()->IgnoreParens()->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1501 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1502 | Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1503 | |
| 1504 | VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0); |
| 1505 | } |
| 1506 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1507 | void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred, |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1508 | CallExpr::arg_iterator AI, |
| 1509 | CallExpr::arg_iterator AE, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | ExplodedNodeSet& Dst, |
| 1511 | const FunctionProtoType *Proto, |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1512 | unsigned ParamIdx) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1514 | // Process the arguments. |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1515 | if (AI != AE) { |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1516 | // If the call argument is being bound to a reference parameter, |
| 1517 | // visit it as an lvalue, not an rvalue. |
| 1518 | bool VisitAsLvalue = false; |
| 1519 | if (Proto && ParamIdx < Proto->getNumArgs()) |
| 1520 | VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType(); |
| 1521 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | ExplodedNodeSet DstTmp; |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1523 | if (VisitAsLvalue) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | VisitLValue(*AI, Pred, DstTmp); |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1525 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | Visit(*AI, Pred, DstTmp); |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1527 | ++AI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1529 | for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; |
| 1530 | ++DI) |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1531 | VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Ted Kremenek | e0188e6 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1533 | return; |
| 1534 | } |
| 1535 | |
| 1536 | // If we reach here we have processed all of the arguments. Evaluate |
| 1537 | // the callee expression. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1538 | ExplodedNodeSet DstTmp; |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1539 | Expr* Callee = CE->getCallee()->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 1541 | { // Enter new scope to make the lifetime of 'DstTmp2' bounded. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1542 | ExplodedNodeSet DstTmp2; |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 1543 | Visit(Callee, Pred, DstTmp2); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
Ted Kremenek | 49513cc | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 1545 | // Perform the previsit of the CallExpr, storing the results in DstTmp. |
| 1546 | CheckerVisit(CE, DstTmp, DstTmp2, true); |
| 1547 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Ted Kremenek | e0188e6 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1549 | // Finally, evaluate the function call. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1551 | DI != DE; ++DI) { |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1553 | const GRState* state = GetState(*DI); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1554 | SVal L = state->getSVal(Callee); |
Ted Kremenek | e0188e6 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1555 | |
Ted Kremenek | 8efd6b4e | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1556 | // FIXME: Add support for symbolic function calls (calls involving |
| 1557 | // function pointer values that are symbolic). |
Zhongxing Xu | 79affb7 | 2009-09-02 08:10:35 +0000 | [diff] [blame] | 1558 | |
Ted Kremenek | 7034236 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1559 | // Check for the "noreturn" attribute. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | |
Ted Kremenek | 7034236 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1561 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
Zhongxing Xu | ac12943 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1562 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
Zhongxing Xu | bfb000f3 | 2009-09-04 02:17:35 +0000 | [diff] [blame] | 1563 | |
| 1564 | MarkNoReturnFunction(FD, CE, state, Builder); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Ted Kremenek | 7034236 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1566 | // Evaluate the call. |
Zhongxing Xu | 1309a85 | 2009-09-05 06:46:12 +0000 | [diff] [blame] | 1567 | if (EvalBuiltinFunction(FD, CE, *DI, Dst)) |
Zhongxing Xu | 88f07cd | 2009-09-05 05:00:57 +0000 | [diff] [blame] | 1568 | continue; |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1569 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | // Dispatch to the plug-in transfer function. |
| 1571 | |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1572 | unsigned size = Dst.size(); |
| 1573 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
| 1574 | EvalCall(Dst, CE, L, *DI); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1576 | // Handle the case where no nodes where generated. Auto-generate that |
| 1577 | // contains the updated state if we aren't generating sinks. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1579 | if (!Builder->BuildSinks && Dst.size() == size && |
| 1580 | !Builder->HasGeneratedNode) |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1581 | MakeNode(Dst, CE, *DI, state); |
Ted Kremenek | e0188e6 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1585 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1586 | // Transfer function: Objective-C ivar references. |
| 1587 | //===----------------------------------------------------------------------===// |
| 1588 | |
Ted Kremenek | 111a6bd | 2009-02-28 20:50:43 +0000 | [diff] [blame] | 1589 | static std::pair<const void*,const void*> EagerlyAssumeTag |
| 1590 | = std::pair<const void*,const void*>(&EagerlyAssumeTag,0); |
| 1591 | |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1592 | void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, |
| 1593 | Expr *Ex) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1594 | for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) { |
| 1595 | ExplodedNode *Pred = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
Ted Kremenek | ff290ca | 2009-02-25 23:32:10 +0000 | [diff] [blame] | 1597 | // Test if the previous node was as the same expression. This can happen |
| 1598 | // when the expression fails to evaluate to anything meaningful and |
| 1599 | // (as an optimization) we don't generate a node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | ProgramPoint P = Pred->getLocation(); |
Ted Kremenek | ff290ca | 2009-02-25 23:32:10 +0000 | [diff] [blame] | 1601 | if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | Dst.Add(Pred); |
Ted Kremenek | ff290ca | 2009-02-25 23:32:10 +0000 | [diff] [blame] | 1603 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | } |
Ted Kremenek | ff290ca | 2009-02-25 23:32:10 +0000 | [diff] [blame] | 1605 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | const GRState* state = Pred->getState(); |
| 1607 | SVal V = state->getSVal(Ex); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1608 | if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) { |
Ted Kremenek | dc3f50f | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1609 | // First assume that the condition is true. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1610 | if (const GRState *stateTrue = state->Assume(*SEV, true)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | stateTrue = stateTrue->BindExpr(Ex, |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 1612 | ValMgr.makeIntVal(1U, Ex->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | Dst.Add(Builder->generateNode(PostStmtCustom(Ex, |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 1614 | &EagerlyAssumeTag, Pred->getLocationContext()), |
Ted Kremenek | dc3f50f | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1615 | stateTrue, Pred)); |
| 1616 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | |
Ted Kremenek | dc3f50f | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1618 | // Next, assume that the condition is false. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1619 | if (const GRState *stateFalse = state->Assume(*SEV, false)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | stateFalse = stateFalse->BindExpr(Ex, |
Ted Kremenek | 907a711 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 1621 | ValMgr.makeIntVal(0U, Ex->getType())); |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 1622 | Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag, |
| 1623 | Pred->getLocationContext()), |
Ted Kremenek | dc3f50f | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1624 | stateFalse, Pred)); |
| 1625 | } |
| 1626 | } |
| 1627 | else |
| 1628 | Dst.Add(Pred); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | //===----------------------------------------------------------------------===// |
| 1633 | // Transfer function: Objective-C ivar references. |
| 1634 | //===----------------------------------------------------------------------===// |
| 1635 | |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1636 | void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, ExplodedNode* Pred, |
| 1637 | ExplodedNodeSet& Dst, bool asLValue) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1639 | Expr* Base = cast<Expr>(Ex->getBase()); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1640 | ExplodedNodeSet Tmp; |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1641 | Visit(Base, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1643 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1644 | const GRState* state = GetState(*I); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1645 | SVal BaseVal = state->getSVal(Base); |
| 1646 | SVal location = state->getLValue(Ex->getDecl(), BaseVal); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1648 | if (asLValue) |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1649 | MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location)); |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1650 | else |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1651 | EvalLoad(Dst, Ex, *I, state, location); |
Ted Kremenek | 12dd55b | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1656 | // Transfer function: Objective-C fast enumeration 'for' statements. |
| 1657 | //===----------------------------------------------------------------------===// |
| 1658 | |
| 1659 | void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S, |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1660 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1662 | // ObjCForCollectionStmts are processed in two places. This method |
| 1663 | // handles the case where an ObjCForCollectionStmt* occurs as one of the |
| 1664 | // statements within a basic block. This transfer function does two things: |
| 1665 | // |
| 1666 | // (1) binds the next container value to 'element'. This creates a new |
| 1667 | // node in the ExplodedGraph. |
| 1668 | // |
| 1669 | // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating |
| 1670 | // whether or not the container has any more elements. This value |
| 1671 | // will be tested in ProcessBranch. We need to explicitly bind |
| 1672 | // this value because a container can contain nil elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | // |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1674 | // FIXME: Eventually this logic should actually do dispatches to |
| 1675 | // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration). |
| 1676 | // This will require simulating a temporary NSFastEnumerationState, either |
| 1677 | // through an SVal or through the use of MemRegions. This value can |
| 1678 | // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop |
| 1679 | // terminates we reclaim the temporary (it goes out of scope) and we |
| 1680 | // we can test if the SVal is 0 or if the MemRegion is null (depending |
| 1681 | // on what approach we take). |
| 1682 | // |
| 1683 | // For now: simulate (1) by assigning either a symbol or nil if the |
| 1684 | // container is empty. Thus this transfer function will by default |
| 1685 | // result in state splitting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1687 | Stmt* elem = S->getElement(); |
| 1688 | SVal ElementV; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1690 | if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) { |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1691 | VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1692 | assert (ElemD->getInit() == 0); |
Ted Kremenek | 14536f6 | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 1693 | ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext()); |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1694 | VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV); |
| 1695 | return; |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1696 | } |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1697 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1698 | ExplodedNodeSet Tmp; |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1699 | VisitLValue(cast<Expr>(elem), Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1701 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1702 | const GRState* state = GetState(*I); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1703 | VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem)); |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S, |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1708 | ExplodedNode* Pred, ExplodedNodeSet& Dst, |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1709 | SVal ElementV) { |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1710 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | |
| 1712 | |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1713 | // Get the current state. Use 'EvalLocation' to determine if it is a null |
| 1714 | // pointer, etc. |
| 1715 | Stmt* elem = S->getElement(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1717 | Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV); |
| 1718 | if (!Pred) |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1719 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Ted Kremenek | d93c6e3 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1721 | const GRState *state = GetState(Pred); |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1722 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1723 | // Handle the case where the container still has elements. |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1724 | SVal TrueV = ValMgr.makeTruthVal(1); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1725 | const GRState *hasElems = state->BindExpr(S, TrueV); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1727 | // Handle the case where the container has no elements. |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1728 | SVal FalseV = ValMgr.makeTruthVal(0); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1729 | const GRState *noElems = state->BindExpr(S, FalseV); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1730 | |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1731 | if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV)) |
| 1732 | if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) { |
| 1733 | // FIXME: The proper thing to do is to really iterate over the |
| 1734 | // container. We will do this with dispatch logic to the store. |
| 1735 | // For now, just 'conjure' up a symbolic value. |
Zhongxing Xu | 34d04b3 | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1736 | QualType T = R->getValueType(getContext()); |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1737 | assert (Loc::IsLocType(T)); |
| 1738 | unsigned Count = Builder->getCurrentBlockCount(); |
Zhongxing Xu | c48eb32 | 2009-04-09 06:49:52 +0000 | [diff] [blame] | 1739 | SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count); |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1740 | SVal V = ValMgr.makeLoc(Sym); |
Ted Kremenek | d93c6e3 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1741 | hasElems = hasElems->bindLoc(ElementV, V); |
Ted Kremenek | df31792 | 2008-11-12 21:12:46 +0000 | [diff] [blame] | 1742 | |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1743 | // Bind the location to 'nil' on the false branch. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | SVal nilV = ValMgr.makeIntVal(0, T); |
| 1745 | noElems = noElems->bindLoc(ElementV, nilV); |
Ted Kremenek | 537f638 | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1746 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Ted Kremenek | df31792 | 2008-11-12 21:12:46 +0000 | [diff] [blame] | 1748 | // Create the new nodes. |
| 1749 | MakeNode(Dst, S, Pred, hasElems); |
| 1750 | MakeNode(Dst, S, Pred, noElems); |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1754 | // Transfer function: Objective-C message expressions. |
| 1755 | //===----------------------------------------------------------------------===// |
| 1756 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1757 | void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred, |
| 1758 | ExplodedNodeSet& Dst){ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1760 | VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), |
| 1761 | Pred, Dst); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | } |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1763 | |
| 1764 | void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME, |
Zhongxing Xu | 4de1c85 | 2008-10-31 07:26:14 +0000 | [diff] [blame] | 1765 | ObjCMessageExpr::arg_iterator AI, |
| 1766 | ObjCMessageExpr::arg_iterator AE, |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1767 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1768 | if (AI == AE) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1770 | // Process the receiver. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1772 | if (Expr* Receiver = ME->getReceiver()) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1773 | ExplodedNodeSet Tmp; |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1774 | Visit(Receiver, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1776 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; |
| 1777 | ++NI) |
Daniel Dunbar | 8e31e77 | 2009-07-23 04:41:06 +0000 | [diff] [blame] | 1778 | VisitObjCMessageExprDispatchHelper(ME, *NI, Dst); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Daniel Dunbar | 8e31e77 | 2009-07-23 04:41:06 +0000 | [diff] [blame] | 1783 | VisitObjCMessageExprDispatchHelper(ME, Pred, Dst); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1784 | return; |
| 1785 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1787 | ExplodedNodeSet Tmp; |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1788 | Visit(*AI, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1790 | ++AI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1792 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI) |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1793 | VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst); |
| 1794 | } |
| 1795 | |
| 1796 | void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1797 | ExplodedNode* Pred, |
| 1798 | ExplodedNodeSet& Dst) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
| 1800 | // FIXME: More logic for the processing the method call. |
| 1801 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1802 | const GRState* state = GetState(Pred); |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1803 | bool RaisesException = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | |
| 1805 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1806 | if (Expr* Receiver = ME->getReceiver()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1808 | SVal L_untested = state->getSVal(Receiver); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
| 1810 | // Check for undefined control-flow. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1811 | if (L_untested.isUndef()) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1812 | ExplodedNode* N = Builder->generateNode(ME, state, Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1814 | if (N) { |
| 1815 | N->markAsSink(); |
| 1816 | UndefReceivers.insert(N); |
| 1817 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1819 | return; |
| 1820 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
| 1822 | // "Assume" that the receiver is not NULL. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1823 | DefinedOrUnknownSVal L = cast<DefinedOrUnknownSVal>(L_untested); |
| 1824 | const GRState *StNotNull = state->Assume(L, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
| 1826 | // "Assume" that the receiver is NULL. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 1827 | const GRState *StNull = state->Assume(L, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1829 | if (StNull) { |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1830 | QualType RetTy = ME->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Ted Kremenek | 66d9edc | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1832 | // Check if the receiver was nil and the return value a struct. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | if (RetTy->isRecordType()) { |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1834 | if (Pred->getParentMap().isConsumedExpr(ME)) { |
Ted Kremenek | 605fee8 | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1835 | // The [0 ...] expressions will return garbage. Flag either an |
| 1836 | // explicit or implicit error. Because of the structure of this |
| 1837 | // function we currently do not bifurfacte the state graph at |
| 1838 | // this point. |
| 1839 | // FIXME: We should bifurcate and fill the returned struct with |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | // garbage. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1841 | if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) { |
Ted Kremenek | 605fee8 | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1842 | N->markAsSink(); |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1843 | if (StNotNull) |
Ted Kremenek | 605fee8 | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1844 | NilReceiverStructRetImplicit.insert(N); |
Ted Kremenek | 2f3b0db | 2009-04-09 06:02:06 +0000 | [diff] [blame] | 1845 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | NilReceiverStructRetExplicit.insert(N); |
Ted Kremenek | 605fee8 | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1847 | } |
| 1848 | } |
Ted Kremenek | d937ed3 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1849 | } |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1850 | else { |
Ted Kremenek | d937ed3 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1851 | ASTContext& Ctx = getContext(); |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1852 | if (RetTy != Ctx.VoidTy) { |
Zhongxing Xu | 7e3431b | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 1853 | if (Pred->getParentMap().isConsumedExpr(ME)) { |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1854 | // sizeof(void *) |
| 1855 | const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); |
| 1856 | // sizeof(return type) |
| 1857 | const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType()); |
Ted Kremenek | d937ed3 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1858 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | if (voidPtrSize < returnTypeSize) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1860 | if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) { |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1861 | N->markAsSink(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | if (StNotNull) |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1863 | NilReceiverLargerThanVoidPtrRetImplicit.insert(N); |
Ted Kremenek | 2f3b0db | 2009-04-09 06:02:06 +0000 | [diff] [blame] | 1864 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | NilReceiverLargerThanVoidPtrRetExplicit.insert(N); |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1866 | } |
| 1867 | } |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1868 | else if (!StNotNull) { |
Ted Kremenek | f9f9420 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1869 | // Handle the safe cases where the return value is 0 if the |
| 1870 | // receiver is nil. |
| 1871 | // |
| 1872 | // FIXME: For now take the conservative approach that we only |
| 1873 | // return null values if we *know* that the receiver is nil. |
| 1874 | // This is because we can have surprises like: |
| 1875 | // |
| 1876 | // ... = [[NSScreens screens] objectAtIndex:0]; |
| 1877 | // |
| 1878 | // What can happen is that [... screens] could return nil, but |
| 1879 | // it most likely isn't nil. We should assume the semantics |
| 1880 | // of this case unless we have *a lot* more knowledge. |
| 1881 | // |
Ted Kremenek | f8cb51c | 2009-04-09 16:46:55 +0000 | [diff] [blame] | 1882 | SVal V = ValMgr.makeZeroVal(ME->getType()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1883 | MakeNode(Dst, ME, Pred, StNull->BindExpr(ME, V)); |
Ted Kremenek | c3f7c85 | 2009-04-09 04:06:51 +0000 | [diff] [blame] | 1884 | return; |
| 1885 | } |
Ted Kremenek | 605fee8 | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1886 | } |
Ted Kremenek | d937ed3 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1887 | } |
Ted Kremenek | 66d9edc | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1888 | } |
Ted Kremenek | 5451c60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 1889 | // We have handled the cases where the receiver is nil. The remainder |
Ted Kremenek | 2f3b0db | 2009-04-09 06:02:06 +0000 | [diff] [blame] | 1890 | // of this method should assume that the receiver is not nil. |
| 1891 | if (!StNotNull) |
| 1892 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Ted Kremenek | 5451c60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 1894 | state = StNotNull; |
Ted Kremenek | 66d9edc | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1895 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1897 | // Check if the "raise" message was sent. |
| 1898 | if (ME->getSelector() == RaiseSel) |
| 1899 | RaisesException = true; |
| 1900 | } |
| 1901 | else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1903 | IdentifierInfo* ClsName = ME->getClassName(); |
| 1904 | Selector S = ME->getSelector(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1906 | // Check for special instance methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | |
| 1908 | if (!NSExceptionII) { |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1909 | ASTContext& Ctx = getContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1911 | NSExceptionII = &Ctx.Idents.get("NSException"); |
| 1912 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1914 | if (ClsName == NSExceptionII) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1916 | enum { NUM_RAISE_SELECTORS = 2 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1918 | // Lazily create a cache of the selectors. |
| 1919 | |
| 1920 | if (!NSExceptionInstanceRaiseSelectors) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1922 | ASTContext& Ctx = getContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1924 | NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1926 | llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II; |
| 1927 | unsigned idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
| 1929 | // raise:format: |
Ted Kremenek | bb7386a | 2008-05-02 17:12:56 +0000 | [diff] [blame] | 1930 | II.push_back(&Ctx.Idents.get("raise")); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | II.push_back(&Ctx.Idents.get("format")); |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1932 | NSExceptionInstanceRaiseSelectors[idx++] = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | Ctx.Selectors.getSelector(II.size(), &II[0]); |
| 1934 | |
| 1935 | // raise:format::arguments: |
Ted Kremenek | bb7386a | 2008-05-02 17:12:56 +0000 | [diff] [blame] | 1936 | II.push_back(&Ctx.Idents.get("arguments")); |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1937 | NSExceptionInstanceRaiseSelectors[idx++] = |
| 1938 | Ctx.Selectors.getSelector(II.size(), &II[0]); |
| 1939 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1941 | for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) |
| 1942 | if (S == NSExceptionInstanceRaiseSelectors[i]) { |
| 1943 | RaisesException = true; break; |
| 1944 | } |
| 1945 | } |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1946 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1948 | // Check for any arguments that are uninitialized/undefined. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1950 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 1951 | I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1953 | if (state->getSVal(*I).isUndef()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1955 | // Generate an error node for passing an uninitialized/undefined value |
| 1956 | // as an argument to a message expression. This node is a sink. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1957 | ExplodedNode* N = Builder->generateNode(ME, state, Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1959 | if (N) { |
| 1960 | N->markAsSink(); |
| 1961 | MsgExprUndefArgs[N] = *I; |
| 1962 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1964 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | } |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1966 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 1968 | // Handle previsits checks. |
| 1969 | ExplodedNodeSet Src, DstTmp; |
| 1970 | Src.Add(Pred); |
| 1971 | CheckerVisit(ME, DstTmp, Src, true); |
| 1972 | |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1973 | // Check if we raise an exception. For now treat these as sinks. Eventually |
| 1974 | // we will want to handle exceptions properly. |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1975 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
Ted Kremenek | 7f82473 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1976 | if (RaisesException) |
| 1977 | Builder->BuildSinks = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1979 | // Dispatch to plug-in transfer function. |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1980 | unsigned size = Dst.size(); |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1981 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 1982 | |
| 1983 | for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); |
| 1984 | DI!=DE; ++DI) |
| 1985 | EvalObjCMessageExpr(Dst, ME, *DI); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1987 | // Handle the case where no nodes where generated. Auto-generate that |
| 1988 | // contains the updated state if we aren't generating sinks. |
Ted Kremenek | c072b82 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1989 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1990 | MakeNode(Dst, ME, Pred, state); |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | //===----------------------------------------------------------------------===// |
| 1994 | // Transfer functions: Miscellaneous statements. |
| 1995 | //===----------------------------------------------------------------------===// |
| 1996 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1997 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred, ExplodedNodeSet& Dst){ |
| 1998 | ExplodedNodeSet S1; |
Ted Kremenek | 9fd2531 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 1999 | QualType T = CastE->getType(); |
Zhongxing Xu | dab76fd | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 2000 | QualType ExTy = Ex->getType(); |
Zhongxing Xu | c272152 | 2008-10-22 08:02:16 +0000 | [diff] [blame] | 2001 | |
Zhongxing Xu | 4de1c85 | 2008-10-31 07:26:14 +0000 | [diff] [blame] | 2002 | if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE)) |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2003 | T = ExCast->getTypeAsWritten(); |
| 2004 | |
Zhongxing Xu | c272152 | 2008-10-22 08:02:16 +0000 | [diff] [blame] | 2005 | if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType()) |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2006 | VisitLValue(Ex, Pred, S1); |
Ted Kremenek | 9f3f827 | 2008-03-04 22:16:08 +0000 | [diff] [blame] | 2007 | else |
| 2008 | Visit(Ex, Pred, S1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Ted Kremenek | eccf3e5 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 2010 | // Check for casting to "void". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | if (T->isVoidType()) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2012 | for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 9fd2531 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 2013 | Dst.Add(*I1); |
| 2014 | |
Ted Kremenek | 33d8285 | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2017 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2018 | for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
| 2019 | ExplodedNode* N = *I1; |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2020 | const GRState* state = GetState(N); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2021 | SVal V = state->getSVal(Ex); |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2022 | const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2023 | state = Res.getState()->BindExpr(CastE, Res.getSVal()); |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2024 | MakeNode(Dst, CastE, N, state); |
Ted Kremenek | 33d8285 | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 2025 | } |
Ted Kremenek | 0535274 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2028 | void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | ExplodedNode* Pred, |
| 2030 | ExplodedNodeSet& Dst, |
Zhongxing Xu | 2c677c3 | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 2031 | bool asLValue) { |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2032 | InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens()); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2033 | ExplodedNodeSet Tmp; |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2034 | Visit(ILE, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2036 | for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2037 | const GRState* state = GetState(*I); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2038 | SVal ILV = state->getSVal(ILE); |
| 2039 | state = state->bindCompoundLiteral(CL, ILV); |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2040 | |
Zhongxing Xu | 2c677c3 | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 2041 | if (asLValue) |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2042 | MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL))); |
Zhongxing Xu | 2c677c3 | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 2043 | else |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2044 | MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV)); |
Ted Kremenek | bf26368 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
Ted Kremenek | 14536f6 | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2048 | void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | ExplodedNodeSet& Dst) { |
Ted Kremenek | 3b42715 | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2050 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | // The CFG has one DeclStmt per Decl. |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2052 | Decl* D = *DS->decl_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
Ted Kremenek | b45e6b9 | 2008-08-28 18:34:26 +0000 | [diff] [blame] | 2054 | if (!D || !isa<VarDecl>(D)) |
Ted Kremenek | 3b42715 | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2055 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
| 2057 | const VarDecl* VD = dyn_cast<VarDecl>(D); |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2058 | Expr* InitEx = const_cast<Expr*>(VD->getInit()); |
Ted Kremenek | 3b42715 | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2059 | |
| 2060 | // FIXME: static variables may have an initializer, but the second |
| 2061 | // time a function is called those values may not be current. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2062 | ExplodedNodeSet Tmp; |
Ted Kremenek | 3b42715 | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2063 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2064 | if (InitEx) |
| 2065 | Visit(InitEx, Pred, Tmp); |
Ted Kremenek | fc31129 | 2009-07-17 23:48:26 +0000 | [diff] [blame] | 2066 | else |
Ted Kremenek | b45e6b9 | 2008-08-28 18:34:26 +0000 | [diff] [blame] | 2067 | Tmp.Add(Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2069 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Zhongxing Xu | 27fee83 | 2009-11-03 12:13:38 +0000 | [diff] [blame] | 2070 | ExplodedNode *N = *I; |
| 2071 | const GRState *state; |
Zhongxing Xu | af7415f | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 2072 | |
Zhongxing Xu | 27fee83 | 2009-11-03 12:13:38 +0000 | [diff] [blame] | 2073 | for (CheckersOrdered::iterator CI = Checkers.begin(), CE = Checkers.end(); |
| 2074 | CI != CE; ++CI) { |
| 2075 | state = GetState(N); |
| 2076 | N = CI->second->CheckType(getContext().getCanonicalType(VD->getType()), |
| 2077 | N, state, DS, *this); |
| 2078 | if (!N) |
| 2079 | break; |
Ted Kremenek | 1336353 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2080 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
Zhongxing Xu | 27fee83 | 2009-11-03 12:13:38 +0000 | [diff] [blame] | 2082 | if (!N) |
| 2083 | continue; |
| 2084 | |
| 2085 | state = GetState(N); |
| 2086 | |
Zhongxing Xu | af7415f | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 2087 | // Decls without InitExpr are not initialized explicitly. |
Zhongxing Xu | 27fee83 | 2009-11-03 12:13:38 +0000 | [diff] [blame] | 2088 | const LocationContext *LC = N->getLocationContext(); |
Ted Kremenek | 14536f6 | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2089 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2090 | if (InitEx) { |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2091 | SVal InitVal = state->getSVal(InitEx); |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2092 | QualType T = VD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2094 | // Recover some path-sensitivity if a scalar value evaluated to |
| 2095 | // UnknownVal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | if (InitVal.isUnknown() || |
Ted Kremenek | 44c12ef | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2097 | !getConstraintManager().canReasonAbout(InitVal)) { |
Zhongxing Xu | 27fee83 | 2009-11-03 12:13:38 +0000 | [diff] [blame] | 2098 | InitVal = ValMgr.getConjuredSymbolVal(NULL, InitEx, |
| 2099 | Builder->getCurrentBlockCount()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | } |
Ted Kremenek | b006b82 | 2009-11-04 00:09:15 +0000 | [diff] [blame^] | 2101 | |
| 2102 | EvalBind(Dst, DS, *I, state, loc::MemRegionVal(state->getRegion(VD, LC)), |
| 2103 | InitVal, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | } |
Ted Kremenek | 1336353 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2105 | else { |
Ted Kremenek | b006b82 | 2009-11-04 00:09:15 +0000 | [diff] [blame^] | 2106 | state = state->bindDeclWithNoInit(state->getRegion(VD, LC)); |
Ted Kremenek | 1336353 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2107 | MakeNode(Dst, DS, *I, state); |
Ted Kremenek | 8f7afdd | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 2108 | } |
Ted Kremenek | 3b42715 | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2109 | } |
Ted Kremenek | 0535274 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 2110 | } |
Ted Kremenek | 33d8285 | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 2111 | |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2112 | namespace { |
| 2113 | // This class is used by VisitInitListExpr as an item in a worklist |
| 2114 | // for processing the values contained in an InitListExpr. |
| 2115 | class VISIBILITY_HIDDEN InitListWLItem { |
| 2116 | public: |
| 2117 | llvm::ImmutableList<SVal> Vals; |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2118 | ExplodedNode* N; |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2119 | InitListExpr::reverse_iterator Itr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2121 | InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals, |
| 2122 | InitListExpr::reverse_iterator itr) |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2123 | : Vals(vals), N(n), Itr(itr) {} |
| 2124 | }; |
| 2125 | } |
| 2126 | |
| 2127 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2129 | ExplodedNodeSet& Dst) { |
Ted Kremenek | 828e6df | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2130 | |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2131 | const GRState* state = GetState(Pred); |
Ted Kremenek | 45698bf | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 2132 | QualType T = getContext().getCanonicalType(E->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | unsigned NumInitElements = E->getNumInits(); |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2134 | |
Ted Kremenek | a41d9dd | 2009-07-28 20:46:55 +0000 | [diff] [blame] | 2135 | if (T->isArrayType() || T->isStructureType() || |
| 2136 | T->isUnionType() || T->isVectorType()) { |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2137 | |
Ted Kremenek | 828e6df | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2138 | llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | |
Ted Kremenek | 828e6df | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2140 | // Handle base case where the initializer has no elements. |
| 2141 | // e.g: static int* myArray[] = {}; |
| 2142 | if (NumInitElements == 0) { |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2143 | SVal V = ValMgr.makeCompoundVal(T, StartVals); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2144 | MakeNode(Dst, E, Pred, state->BindExpr(E, V)); |
Ted Kremenek | 828e6df | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2145 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Ted Kremenek | 828e6df | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2148 | // Create a worklist to process the initializers. |
| 2149 | llvm::SmallVector<InitListWLItem, 10> WorkList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | WorkList.reserve(NumInitElements); |
| 2151 | WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin())); |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2152 | InitListExpr::reverse_iterator ItrEnd = E->rend(); |
Ted Kremenek | 3003001 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 2153 | assert(!(E->rbegin() == E->rend())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | |
Ted Kremenek | 828e6df | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2155 | // Process the worklist until it is empty. |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2156 | while (!WorkList.empty()) { |
| 2157 | InitListWLItem X = WorkList.back(); |
| 2158 | WorkList.pop_back(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2160 | ExplodedNodeSet Tmp; |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2161 | Visit(*X.Itr, X.N, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2163 | InitListExpr::reverse_iterator NewItr = X.Itr + 1; |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2164 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2165 | for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) { |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2166 | // Get the last initializer value. |
| 2167 | state = GetState(*NI); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2168 | SVal InitV = state->getSVal(cast<Expr>(*X.Itr)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2170 | // Construct the new list of values by prepending the new value to |
| 2171 | // the already constructed list. |
| 2172 | llvm::ImmutableList<SVal> NewVals = |
| 2173 | getBasicVals().consVals(InitV, X.Vals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2175 | if (NewItr == ItrEnd) { |
Zhongxing Xu | 121a53a | 2008-10-31 03:01:26 +0000 | [diff] [blame] | 2176 | // Now we have a list holding all init values. Make CompoundValData. |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2177 | SVal V = ValMgr.makeCompoundVal(T, NewVals); |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2178 | |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2179 | // Make final state and node. |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2180 | MakeNode(Dst, E, *NI, state->BindExpr(E, V)); |
Ted Kremenek | f68bf63 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2181 | } |
| 2182 | else { |
| 2183 | // Still some initializer values to go. Push them onto the worklist. |
| 2184 | WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr)); |
| 2185 | } |
| 2186 | } |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Ted Kremenek | 28f41ba | 2008-10-30 18:34:31 +0000 | [diff] [blame] | 2189 | return; |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2192 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 2193 | assert (E->getNumInits() == 1); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2194 | ExplodedNodeSet Tmp; |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2195 | Expr* Init = E->getInit(0); |
| 2196 | Visit(Init, Pred, Tmp); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2197 | for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) { |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2198 | state = GetState(*I); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2199 | MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init))); |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2200 | } |
| 2201 | return; |
| 2202 | } |
| 2203 | |
Zhongxing Xu | b281cdd | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2204 | |
| 2205 | printf("InitListExpr type = %s\n", T.getAsString().c_str()); |
| 2206 | assert(0 && "unprocessed InitListExpr type"); |
| 2207 | } |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 2208 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2209 | /// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type). |
| 2210 | void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2211 | ExplodedNode* Pred, |
| 2212 | ExplodedNodeSet& Dst) { |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2213 | QualType T = Ex->getTypeOfArgument(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | uint64_t amt; |
| 2215 | |
Ted Kremenek | ae5b786 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2216 | if (Ex->isSizeOf()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | if (T == getContext().VoidTy) { |
Ted Kremenek | 4299d5d | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2218 | // sizeof(void) == 1 byte. |
| 2219 | amt = 1; |
| 2220 | } |
| 2221 | else if (!T.getTypePtr()->isConstantSizeType()) { |
| 2222 | // FIXME: Add support for VLAs. |
Ted Kremenek | ae5b786 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2223 | return; |
Ted Kremenek | 4299d5d | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2224 | } |
| 2225 | else if (T->isObjCInterfaceType()) { |
| 2226 | // Some code tries to take the sizeof an ObjCInterfaceType, relying that |
| 2227 | // the compiler has laid out its representation. Just report Unknown |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | // for these. |
Ted Kremenek | 9905746 | 2008-04-30 21:31:12 +0000 | [diff] [blame] | 2229 | return; |
Ted Kremenek | 4299d5d | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2230 | } |
| 2231 | else { |
| 2232 | // All other cases. |
Ted Kremenek | ae5b786 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2233 | amt = getContext().getTypeSize(T) / 8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2234 | } |
Ted Kremenek | ae5b786 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2235 | } |
| 2236 | else // Get alignment of the type. |
Ted Kremenek | 88ba750 | 2008-03-15 03:13:55 +0000 | [diff] [blame] | 2237 | amt = getContext().getTypeAlign(T) / 8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | |
Ted Kremenek | 181f723 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 2239 | MakeNode(Dst, Ex, Pred, |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2240 | GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType()))); |
Ted Kremenek | 002bf74 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Ted Kremenek | b597bb9 | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2243 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2244 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred, |
| 2245 | ExplodedNodeSet& Dst, bool asLValue) { |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2246 | |
Ted Kremenek | b597bb9 | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2247 | switch (U->getOpcode()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Ted Kremenek | b597bb9 | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2249 | default: |
Ted Kremenek | b597bb9 | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2250 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2252 | case UnaryOperator::Deref: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2254 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2255 | ExplodedNodeSet Tmp; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2256 | Visit(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2258 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2260 | const GRState* state = GetState(*I); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2261 | SVal location = state->getSVal(Ex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2263 | if (asLValue) |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2264 | MakeNode(Dst, U, *I, state->BindExpr(U, location), |
Ted Kremenek | a6e0832 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 2265 | ProgramPoint::PostLValueKind); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2266 | else |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2267 | EvalLoad(Dst, U, *I, state, location); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | } |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2269 | |
| 2270 | return; |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2271 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2273 | case UnaryOperator::Real: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2275 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2276 | ExplodedNodeSet Tmp; |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2277 | Visit(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2279 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2281 | // FIXME: We don't have complex SValues yet. |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2282 | if (Ex->getType()->isAnyComplexType()) { |
| 2283 | // Just report "Unknown." |
| 2284 | Dst.Add(*I); |
| 2285 | continue; |
| 2286 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2288 | // For all other types, UnaryOperator::Real is an identity operation. |
| 2289 | assert (U->getType() == Ex->getType()); |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2290 | const GRState* state = GetState(*I); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2291 | MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2294 | return; |
| 2295 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2297 | case UnaryOperator::Imag: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2299 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2300 | ExplodedNodeSet Tmp; |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2301 | Visit(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2303 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2304 | // FIXME: We don't have complex SValues yet. |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2305 | if (Ex->getType()->isAnyComplexType()) { |
| 2306 | // Just report "Unknown." |
| 2307 | Dst.Add(*I); |
| 2308 | continue; |
| 2309 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2311 | // For all other types, UnaryOperator::Float returns 0. |
| 2312 | assert (Ex->getType()->isIntegerType()); |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2313 | const GRState* state = GetState(*I); |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2314 | SVal X = ValMgr.makeZeroVal(Ex->getType()); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2315 | MakeNode(Dst, U, *I, state->BindExpr(U, X)); |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
Ted Kremenek | 46c82ab | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2318 | return; |
| 2319 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | |
Ted Kremenek | 2734713 | 2009-09-15 00:40:32 +0000 | [diff] [blame] | 2321 | case UnaryOperator::OffsetOf: { |
Ted Kremenek | b662294 | 2009-09-15 04:19:09 +0000 | [diff] [blame] | 2322 | Expr::EvalResult Res; |
| 2323 | if (U->Evaluate(Res, getContext()) && Res.Val.isInt()) { |
| 2324 | const APSInt &IV = Res.Val.getInt(); |
| 2325 | assert(IV.getBitWidth() == getContext().getTypeSize(U->getType())); |
| 2326 | assert(U->getType()->isIntegerType()); |
| 2327 | assert(IV.isSigned() == U->getType()->isSignedIntegerType()); |
| 2328 | SVal X = ValMgr.makeIntVal(IV); |
| 2329 | MakeNode(Dst, U, Pred, GetState(Pred)->BindExpr(U, X)); |
| 2330 | return; |
| 2331 | } |
| 2332 | // FIXME: Handle the case where __builtin_offsetof is not a constant. |
| 2333 | Dst.Add(Pred); |
Ted Kremenek | ca67cab | 2008-04-30 21:45:55 +0000 | [diff] [blame] | 2334 | return; |
Ted Kremenek | 2734713 | 2009-09-15 00:40:32 +0000 | [diff] [blame] | 2335 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2337 | case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH. |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2338 | case UnaryOperator::Extension: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2340 | // Unary "+" is a no-op, similar to a parentheses. We still have places |
| 2341 | // where it may be a block-level expression, so we need to |
| 2342 | // generate an extra node that just propagates the value of the |
| 2343 | // subexpression. |
| 2344 | |
| 2345 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2346 | ExplodedNodeSet Tmp; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2347 | Visit(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | |
| 2349 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2350 | const GRState* state = GetState(*I); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2351 | MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex))); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2352 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2354 | return; |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2355 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2357 | case UnaryOperator::AddrOf: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2359 | assert(!asLValue); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2360 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2361 | ExplodedNodeSet Tmp; |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2362 | VisitLValue(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | |
| 2364 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2365 | const GRState* state = GetState(*I); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2366 | SVal V = state->getSVal(Ex); |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2367 | state = state->BindExpr(U, V); |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2368 | MakeNode(Dst, U, *I, state); |
Ted Kremenek | 7f8ebb7 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 2369 | } |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2370 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | return; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2372 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2374 | case UnaryOperator::LNot: |
| 2375 | case UnaryOperator::Minus: |
| 2376 | case UnaryOperator::Not: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2378 | assert (!asLValue); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2379 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2380 | ExplodedNodeSet Tmp; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2381 | Visit(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | |
| 2383 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2384 | const GRState* state = GetState(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | |
Ted Kremenek | 76bccf6 | 2008-09-30 05:32:44 +0000 | [diff] [blame] | 2386 | // Get the value of the subexpression. |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2387 | SVal V = state->getSVal(Ex); |
Ted Kremenek | 76bccf6 | 2008-09-30 05:32:44 +0000 | [diff] [blame] | 2388 | |
Ted Kremenek | 1ca3346 | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 2389 | if (V.isUnknownOrUndef()) { |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2390 | MakeNode(Dst, U, *I, state->BindExpr(U, V)); |
Ted Kremenek | 1ca3346 | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 2391 | continue; |
| 2392 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2394 | // QualType DstT = getContext().getCanonicalType(U->getType()); |
| 2395 | // QualType SrcT = getContext().getCanonicalType(Ex->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | // |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2397 | // if (DstT != SrcT) // Perform promotions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | // V = EvalCast(V, DstT); |
| 2399 | // |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2400 | // if (V.isUnknownOrUndef()) { |
| 2401 | // MakeNode(Dst, U, *I, BindExpr(St, U, V)); |
| 2402 | // continue; |
| 2403 | // } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2405 | switch (U->getOpcode()) { |
| 2406 | default: |
| 2407 | assert(false && "Invalid Opcode."); |
| 2408 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2410 | case UnaryOperator::Not: |
Ted Kremenek | d331d09 | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 2411 | // FIXME: Do we need to handle promotions? |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2412 | state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | break; |
| 2414 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2415 | case UnaryOperator::Minus: |
Ted Kremenek | d331d09 | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 2416 | // FIXME: Do we need to handle promotions? |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2417 | state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | |
| 2420 | case UnaryOperator::LNot: |
| 2421 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2422 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 2423 | // |
| 2424 | // Note: technically we do "E == 0", but this is the same in the |
| 2425 | // transfer functions as "0 == E". |
Ted Kremenek | 1642bda | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2426 | SVal Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2428 | if (isa<Loc>(V)) { |
Zhongxing Xu | 7718ae4 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2429 | Loc X = ValMgr.makeNull(); |
Ted Kremenek | 1642bda | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2430 | Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X, |
| 2431 | U->getType()); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2432 | } |
| 2433 | else { |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2434 | nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType())); |
Ted Kremenek | 8ec5771 | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 2435 | Result = EvalBinOp(state, BinaryOperator::EQ, cast<NonLoc>(V), X, |
Ted Kremenek | 1642bda | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2436 | U->getType()); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2437 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2439 | state = state->BindExpr(U, Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2441 | break; |
| 2442 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2444 | MakeNode(Dst, U, *I, state); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2445 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2447 | return; |
| 2448 | } |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | // Handle ++ and -- (both pre- and post-increment). |
| 2452 | |
| 2453 | assert (U->isIncrementDecrementOp()); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2454 | ExplodedNodeSet Tmp; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2455 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2456 | VisitLValue(Ex, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2458 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2460 | const GRState* state = GetState(*I); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2461 | SVal V1 = state->getSVal(Ex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | |
| 2463 | // Perform a load. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2464 | ExplodedNodeSet Tmp2; |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2465 | EvalLoad(Tmp2, Ex, *I, state, V1); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2466 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2467 | for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2468 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2469 | state = GetState(*I2); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2470 | SVal V2_untested = state->getSVal(Ex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | |
| 2472 | // Propagate unknown and undefined values. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2473 | if (V2_untested.isUnknownOrUndef()) { |
| 2474 | MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested)); |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2475 | continue; |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2476 | } |
| 2477 | DefinedSVal V2 = cast<DefinedSVal>(V2_untested); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | |
| 2479 | // Handle all other values. |
Ted Kremenek | e81734b | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 2480 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 2481 | : BinaryOperator::Sub; |
Ted Kremenek | 32c41ec | 2009-03-11 03:54:24 +0000 | [diff] [blame] | 2482 | |
Zhongxing Xu | fe97165 | 2009-08-05 02:51:59 +0000 | [diff] [blame] | 2483 | // If the UnaryOperator has non-location type, use its type to create the |
| 2484 | // constant value. If the UnaryOperator has location type, create the |
| 2485 | // constant with int type and pointer width. |
| 2486 | SVal RHS; |
| 2487 | |
| 2488 | if (U->getType()->isAnyPointerType()) |
| 2489 | RHS = ValMgr.makeIntValWithPtrWidth(1, false); |
| 2490 | else |
| 2491 | RHS = ValMgr.makeIntVal(1, U->getType()); |
| 2492 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType()); |
| 2494 | |
Ted Kremenek | 6b31533 | 2009-03-20 20:10:45 +0000 | [diff] [blame] | 2495 | // Conjure a new symbol if necessary to recover precision. |
Ted Kremenek | 35f875c | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2496 | if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){ |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2497 | DefinedOrUnknownSVal SymVal = |
Ted Kremenek | e41b81e | 2009-09-27 20:45:21 +0000 | [diff] [blame] | 2498 | ValMgr.getConjuredSymbolVal(NULL, Ex, |
| 2499 | Builder->getCurrentBlockCount()); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2500 | Result = SymVal; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | |
Ted Kremenek | 35f875c | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2502 | // If the value is a location, ++/-- should always preserve |
Ted Kremenek | 1642bda | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2503 | // non-nullness. Check if the original value was non-null, and if so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | // propagate that constraint. |
Ted Kremenek | 35f875c | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2505 | if (Loc::IsLocType(U->getType())) { |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2506 | DefinedOrUnknownSVal Constraint = |
| 2507 | SVator.EvalEQ(state, V2, ValMgr.makeZeroVal(U->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2509 | if (!state->Assume(Constraint, true)) { |
Ted Kremenek | 35f875c | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2510 | // It isn't feasible for the original value to be null. |
| 2511 | // Propagate this constraint. |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2512 | Constraint = SVator.EvalEQ(state, SymVal, |
| 2513 | ValMgr.makeZeroVal(U->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2515 | |
| 2516 | state = state->Assume(Constraint, false); |
Ted Kremenek | f990684 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2517 | assert(state); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | } |
| 2519 | } |
Ted Kremenek | 35f875c | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | |
Ted Kremenek | 1d5f2f3 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2522 | state = state->BindExpr(U, U->isPostfix() ? V2 : Result); |
Ted Kremenek | cdd0be1 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 2523 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | // Perform the store. |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2525 | EvalStore(Dst, U, *I2, state, V1, Result); |
Ted Kremenek | 43523e0 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2526 | } |
Ted Kremenek | 38213f9 | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 2527 | } |
Ted Kremenek | 43523e0 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2530 | void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2531 | VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2532 | } |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2533 | |
| 2534 | void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A, |
| 2535 | AsmStmt::outputs_iterator I, |
| 2536 | AsmStmt::outputs_iterator E, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2537 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2538 | if (I == E) { |
| 2539 | VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst); |
| 2540 | return; |
| 2541 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2543 | ExplodedNodeSet Tmp; |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2544 | VisitLValue(*I, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2546 | ++I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2548 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2549 | VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst); |
| 2550 | } |
| 2551 | |
| 2552 | void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A, |
| 2553 | AsmStmt::inputs_iterator I, |
| 2554 | AsmStmt::inputs_iterator E, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2555 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2556 | if (I == E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2558 | // We have processed both the inputs and the outputs. All of the outputs |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2559 | // should evaluate to Locs. Nuke all of their values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2561 | // FIXME: Some day in the future it would be nice to allow a "plug-in" |
| 2562 | // which interprets the inline asm and stores proper results in the |
| 2563 | // outputs. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2565 | const GRState* state = GetState(Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2567 | for (AsmStmt::outputs_iterator OI = A->begin_outputs(), |
| 2568 | OE = A->end_outputs(); OI != OE; ++OI) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
| 2570 | SVal X = state->getSVal(*OI); |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2571 | assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | |
Zhongxing Xu | 27f1742 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2573 | if (isa<Loc>(X)) |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2574 | state = state->bindLoc(cast<Loc>(X), UnknownVal()); |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2575 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2577 | MakeNode(Dst, A, Pred, state); |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2578 | return; |
| 2579 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2581 | ExplodedNodeSet Tmp; |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2582 | Visit(*I, Pred, Tmp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2584 | ++I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | |
Ted Kremenek | 17a0296 | 2009-09-03 03:02:58 +0000 | [diff] [blame] | 2586 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI) |
Ted Kremenek | 7c7a331 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2587 | VisitAsmStmtHelperInputs(A, I, E, *NI, Dst); |
| 2588 | } |
| 2589 | |
Ted Kremenek | 17a0296 | 2009-09-03 03:02:58 +0000 | [diff] [blame] | 2590 | void GRExprEngine::EvalReturn(ExplodedNodeSet& Dst, ReturnStmt* S, |
| 2591 | ExplodedNode* Pred) { |
Ted Kremenek | 9c37515 | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2592 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | |
| 2594 | unsigned size = Dst.size(); |
Ted Kremenek | c072b82 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2595 | |
Ted Kremenek | dd43aee | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 2596 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 2597 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | c072b82 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2598 | |
Ted Kremenek | 9c32a1e | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 2599 | getTF().EvalReturn(Dst, *this, *Builder, S, Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Ted Kremenek | c072b82 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2601 | // Handle the case where no nodes where generated. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | |
Ted Kremenek | c072b82 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2603 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | 9c37515 | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2604 | MakeNode(Dst, S, Pred, GetState(Pred)); |
| 2605 | } |
| 2606 | |
Ted Kremenek | 17a0296 | 2009-09-03 03:02:58 +0000 | [diff] [blame] | 2607 | void GRExprEngine::VisitReturnStmt(ReturnStmt* S, ExplodedNode* Pred, |
| 2608 | ExplodedNodeSet& Dst) { |
Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2609 | |
| 2610 | Expr* R = S->getRetValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | |
Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2612 | if (!R) { |
Ted Kremenek | 9c37515 | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2613 | EvalReturn(Dst, S, Pred); |
Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
Ted Kremenek | 9c37515 | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2616 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2617 | ExplodedNodeSet Tmp; |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2618 | Visit(R, Pred, Tmp); |
Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2619 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2620 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) { |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2621 | SVal X = (*I)->getState()->getSVal(R); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2623 | // Check if we return the address of a stack variable. |
| 2624 | if (isa<loc::MemRegionVal>(X)) { |
| 2625 | // Determine if the value is on the stack. |
| 2626 | const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
Ted Kremenek | 404b132 | 2009-06-23 18:05:21 +0000 | [diff] [blame] | 2628 | if (R && R->hasStackStorage()) { |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2629 | // Create a special node representing the error. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2630 | if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) { |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2631 | N->markAsSink(); |
| 2632 | RetsStackAddr.insert(N); |
| 2633 | } |
| 2634 | continue; |
| 2635 | } |
Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2636 | } |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2637 | // Check if we return an undefined value. |
| 2638 | else if (X.isUndef()) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2639 | if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) { |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2640 | N->markAsSink(); |
| 2641 | RetsUndef.insert(N); |
| 2642 | } |
| 2643 | continue; |
| 2644 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | |
Ted Kremenek | 9c37515 | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2646 | EvalReturn(Dst, S, *I); |
Ted Kremenek | 0b63f96 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2647 | } |
Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2648 | } |
Ted Kremenek | 64100da | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 2649 | |
Ted Kremenek | 667cacb | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 2650 | //===----------------------------------------------------------------------===// |
| 2651 | // Transfer functions: Binary operators. |
| 2652 | //===----------------------------------------------------------------------===// |
| 2653 | |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2654 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2655 | ExplodedNode* Pred, |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 2656 | ExplodedNodeSet& Dst, bool asLValue) { |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2657 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2658 | ExplodedNodeSet Tmp1; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2659 | Expr* LHS = B->getLHS()->IgnoreParens(); |
| 2660 | Expr* RHS = B->getRHS()->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | |
Fariborz Jahanian | 9a84665 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2662 | // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr. |
| 2663 | if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | Visit(RHS, Pred, Dst); |
Ted Kremenek | 69d78b9 | 2008-12-06 02:39:30 +0000 | [diff] [blame] | 2665 | return; |
| 2666 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | |
Ted Kremenek | 43523e0 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2668 | if (B->isAssignmentOp()) |
Zhongxing Xu | 232c792 | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2669 | VisitLValue(LHS, Pred, Tmp1); |
Ted Kremenek | 43523e0 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2670 | else |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2671 | Visit(LHS, Pred, Tmp1); |
Ted Kremenek | fb55354 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 2672 | |
Ted Kremenek | 17a0296 | 2009-09-03 03:02:58 +0000 | [diff] [blame] | 2673 | for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) { |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2674 | SVal LeftV = (*I1)->getState()->getSVal(LHS); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2675 | ExplodedNodeSet Tmp2; |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2676 | Visit(RHS, *I1, Tmp2); |
Zhongxing Xu | 6e4232c | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 2677 | |
| 2678 | ExplodedNodeSet CheckedSet; |
| 2679 | CheckerVisit(B, CheckedSet, Tmp2, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2681 | // With both the LHS and RHS evaluated, process the operation itself. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | |
| 2683 | for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end(); |
Zhongxing Xu | 6e4232c | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 2684 | I2 != E2; ++I2) { |
Ted Kremenek | 7f0639b | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2685 | |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2686 | const GRState *state = GetState(*I2); |
| 2687 | const GRState *OldSt = state; |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2688 | SVal RightV = state->getSVal(RHS); |
Ted Kremenek | 7020eae | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 2689 | |
Ted Kremenek | cdd0be1 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 2690 | BinaryOperator::Opcode Op = B->getOpcode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | |
Zhongxing Xu | 2ebee13 | 2009-10-21 11:42:22 +0000 | [diff] [blame] | 2692 | if (Op == BinaryOperator::Assign) { |
| 2693 | // EXPERIMENTAL: "Conjured" symbols. |
| 2694 | // FIXME: Handle structs. |
| 2695 | QualType T = RHS->getType(); |
| 2696 | |
| 2697 | if ((RightV.isUnknown()||!getConstraintManager().canReasonAbout(RightV)) |
| 2698 | && (Loc::IsLocType(T) || (T->isScalarType()&&T->isIntegerType()))) { |
| 2699 | unsigned Count = Builder->getCurrentBlockCount(); |
| 2700 | RightV = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), Count); |
| 2701 | } |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 2702 | |
Ted Kremenek | 5c2040b1 | 2009-10-30 22:01:29 +0000 | [diff] [blame] | 2703 | SVal ExprVal = asLValue ? LeftV : RightV; |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 2704 | |
Zhongxing Xu | 2ebee13 | 2009-10-21 11:42:22 +0000 | [diff] [blame] | 2705 | // Simulate the effects of a "store": bind the value of the RHS |
| 2706 | // to the L-Value represented by the LHS. |
Zhongxing Xu | b9eda67 | 2009-10-30 07:19:39 +0000 | [diff] [blame] | 2707 | EvalStore(Dst, B, LHS, *I2, state->BindExpr(B, ExprVal), LeftV, RightV); |
Zhongxing Xu | 2ebee13 | 2009-10-21 11:42:22 +0000 | [diff] [blame] | 2708 | continue; |
| 2709 | } |
| 2710 | |
| 2711 | if (!B->isAssignmentOp()) { |
| 2712 | // Process non-assignments except commas or short-circuited |
| 2713 | // logical expressions (LAnd and LOr). |
| 2714 | SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType()); |
| 2715 | |
| 2716 | if (Result.isUnknown()) { |
| 2717 | if (OldSt != state) { |
| 2718 | // Generate a new node if we have already created a new state. |
| 2719 | MakeNode(Dst, B, *I2, state); |
Ted Kremenek | e2f6d6c | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 2720 | } |
Zhongxing Xu | 2ebee13 | 2009-10-21 11:42:22 +0000 | [diff] [blame] | 2721 | else |
| 2722 | Dst.Add(*I2); |
| 2723 | |
Ted Kremenek | 2044a51 | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 2724 | continue; |
Ted Kremenek | 0a8d376 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 2725 | } |
Zhongxing Xu | 2ebee13 | 2009-10-21 11:42:22 +0000 | [diff] [blame] | 2726 | |
| 2727 | state = state->BindExpr(B, Result); |
| 2728 | |
| 2729 | if (Result.isUndef()) { |
| 2730 | // The operands were *not* undefined, but the result is undefined. |
| 2731 | // This is a special node that should be flagged as an error. |
| 2732 | if (ExplodedNode *UndefNode = Builder->generateNode(B, state, *I2)){ |
| 2733 | UndefNode->markAsSink(); |
| 2734 | UndefResults.insert(UndefNode); |
Ted Kremenek | 7f8ebb7 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 2735 | } |
Ted Kremenek | 2044a51 | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 2736 | continue; |
Ted Kremenek | cdd0be1 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 2737 | } |
Zhongxing Xu | 2ebee13 | 2009-10-21 11:42:22 +0000 | [diff] [blame] | 2738 | |
| 2739 | // Otherwise, create a new node. |
| 2740 | MakeNode(Dst, B, *I2, state); |
| 2741 | continue; |
Ted Kremenek | 0a8d376 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 2742 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2744 | assert (B->isCompoundAssignmentOp()); |
| 2745 | |
Ted Kremenek | 5d7662c | 2009-02-07 00:52:24 +0000 | [diff] [blame] | 2746 | switch (Op) { |
| 2747 | default: |
| 2748 | assert(0 && "Invalid opcode for compound assignment."); |
| 2749 | case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break; |
| 2750 | case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break; |
| 2751 | case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break; |
| 2752 | case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break; |
| 2753 | case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break; |
| 2754 | case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break; |
| 2755 | case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break; |
| 2756 | case BinaryOperator::AndAssign: Op = BinaryOperator::And; break; |
| 2757 | case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break; |
| 2758 | case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break; |
Ted Kremenek | 54d399a | 2008-10-27 23:02:39 +0000 | [diff] [blame] | 2759 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2761 | // Perform a load (the LHS). This performs the checks for |
| 2762 | // null dereferences, and so on. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2763 | ExplodedNodeSet Tmp3; |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2764 | SVal location = state->getSVal(LHS); |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2765 | EvalLoad(Tmp3, LHS, *I2, state, location); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | |
| 2767 | for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; |
Zhongxing Xu | 6e4232c | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 2768 | ++I3) { |
Ted Kremenek | 17d541d | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2769 | state = GetState(*I3); |
Ted Kremenek | c55f0cd | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2770 | SVal V = state->getSVal(LHS); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2771 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2772 | // Get the computation type. |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2773 | QualType CTy = |
| 2774 | cast<CompoundAssignOperator>(B)->getComputationResultType(); |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2775 | CTy = getContext().getCanonicalType(CTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2776 | |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2777 | QualType CLHSTy = |
| 2778 | cast<CompoundAssignOperator>(B)->getComputationLHSType(); |
| 2779 | CLHSTy = getContext().getCanonicalType(CLHSTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2780 | |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2781 | QualType LTy = getContext().getCanonicalType(LHS->getType()); |
| 2782 | QualType RTy = getContext().getCanonicalType(RHS->getType()); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2783 | |
| 2784 | // Promote LHS. |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2785 | llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2786 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | // Compute the result of the operation. |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2788 | SVal Result; |
| 2789 | llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V, |
| 2790 | RightV, CTy), |
| 2791 | state, B->getType(), CTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2793 | if (Result.isUndef()) { |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2794 | // The operands were not undefined, but the result is undefined. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2795 | if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I3)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2796 | UndefNode->markAsSink(); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2797 | UndefResults.insert(UndefNode); |
| 2798 | } |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2799 | continue; |
| 2800 | } |
Ted Kremenek | 7f8a87f | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2801 | |
| 2802 | // EXPERIMENTAL: "Conjured" symbols. |
| 2803 | // FIXME: Handle structs. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2805 | SVal LHSVal; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
| 2807 | if ((Result.isUnknown() || |
Ted Kremenek | 44c12ef | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2808 | !getConstraintManager().canReasonAbout(Result)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | && (Loc::IsLocType(CTy) |
Ted Kremenek | 44c12ef | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2810 | || (CTy->isScalarType() && CTy->isIntegerType()))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | |
Ted Kremenek | 7f8a87f | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2812 | unsigned Count = Builder->getCurrentBlockCount(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2814 | // The symbolic value is actually for the type of the left-hand side |
| 2815 | // expression, not the computation type, as this is the value the |
| 2816 | // LValue on the LHS will bind to. |
Ted Kremenek | e41b81e | 2009-09-27 20:45:21 +0000 | [diff] [blame] | 2817 | LHSVal = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Zhongxing Xu | aa86cff | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 2819 | // However, we need to convert the symbol to the computation type. |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2820 | llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy); |
Ted Kremenek | 7f8a87f | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2821 | } |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2822 | else { |
| 2823 | // The left-hand side may bind to a different value then the |
| 2824 | // computation type. |
Ted Kremenek | ac7c724 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2825 | llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy); |
Ted Kremenek | 4413714 | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2826 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
| 2828 | EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, Result), |
Zhongxing Xu | 342950e | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 2829 | location, LHSVal); |
Ted Kremenek | fa5a3d0 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2830 | } |
Ted Kremenek | fb55354 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 2831 | } |
Ted Kremenek | de8d62b | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 2832 | } |
Ted Kremenek | de8d62b | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 2833 | } |
Ted Kremenek | 2e12c2e | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2834 | |
| 2835 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6f2a705 | 2009-10-30 17:47:32 +0000 | [diff] [blame] | 2836 | // Checker registration/lookup. |
| 2837 | //===----------------------------------------------------------------------===// |
| 2838 | |
| 2839 | Checker *GRExprEngine::lookupChecker(void *tag) const { |
| 2840 | CheckerMap::iterator I = CheckerM.find(tag); |
| 2841 | return (I == CheckerM.end()) ? NULL : Checkers[I->second].second; |
| 2842 | } |
| 2843 | |
| 2844 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3122cb | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 2845 | // Visualization. |
Ted Kremenek | 2e12c2e | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2846 | //===----------------------------------------------------------------------===// |
| 2847 | |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2848 | #ifndef NDEBUG |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2849 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 6947a79 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2850 | static SourceManager* GraphPrintSourceManager; |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2851 | |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2852 | namespace llvm { |
| 2853 | template<> |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2854 | struct VISIBILITY_HIDDEN DOTGraphTraits<ExplodedNode*> : |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2855 | public DefaultDOTGraphTraits { |
Zhongxing Xu | 6b8bfb3 | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 2856 | // FIXME: Since we do not cache error nodes in GRExprEngine now, this does not |
| 2857 | // work. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2858 | static std::string getNodeAttributes(const ExplodedNode* N, void*) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Ted Kremenek | 5b70a22 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2860 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 0f7130a | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 2861 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2862 | GraphPrintCheckerState->isUndefDeref(N) || |
| 2863 | GraphPrintCheckerState->isUndefStore(N) || |
| 2864 | GraphPrintCheckerState->isUndefControlFlow(N) || |
Ted Kremenek | 6f5fca7 | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2865 | GraphPrintCheckerState->isUndefResult(N) || |
Ted Kremenek | 51e87ea | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 2866 | GraphPrintCheckerState->isBadCall(N) || |
| 2867 | GraphPrintCheckerState->isUndefArg(N)) |
Ted Kremenek | 5b70a22 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2868 | return "color=\"red\",style=\"filled\""; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | |
Ted Kremenek | e0c7938 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 2870 | if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 2871 | return "color=\"blue\",style=\"filled\""; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | |
Ted Kremenek | 5b70a22 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2873 | return ""; |
| 2874 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2876 | static std::string getNodeLabel(const ExplodedNode* N, void*,bool ShortNames){ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | |
Ted Kremenek | 799bb6e | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 2878 | std::string sbuf; |
| 2879 | llvm::raw_string_ostream Out(sbuf); |
Ted Kremenek | 930191c | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2880 | |
| 2881 | // Program Location. |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2882 | ProgramPoint Loc = N->getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2884 | switch (Loc.getKind()) { |
| 2885 | case ProgramPoint::BlockEntranceKind: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | Out << "Block Entrance: B" |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2887 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 2888 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2890 | case ProgramPoint::BlockExitKind: |
| 2891 | assert (false); |
| 2892 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2893 | |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2894 | default: { |
Ted Kremenek | bfd28fd | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 2895 | if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) { |
| 2896 | const Stmt* S = L->getStmt(); |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2897 | SourceLocation SLoc = S->getLocStart(); |
| 2898 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2899 | Out << S->getStmtClassName() << ' ' << (void*) S << ' '; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2900 | LangOptions LO; // FIXME. |
| 2901 | S->printPretty(Out, 0, PrintingPolicy(LO)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | |
| 2903 | if (SLoc.isFileID()) { |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2904 | Out << "\\lline=" |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 2905 | << GraphPrintSourceManager->getInstantiationLineNumber(SLoc) |
| 2906 | << " col=" |
| 2907 | << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc) |
| 2908 | << "\\l"; |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2909 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Ted Kremenek | bfd28fd | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 2911 | if (isa<PreStmt>(Loc)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | Out << "\\lPreStmt\\l;"; |
Ted Kremenek | bfd28fd | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 2913 | else if (isa<PostLoad>(Loc)) |
Ted Kremenek | a6e0832 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 2914 | Out << "\\lPostLoad\\l;"; |
| 2915 | else if (isa<PostStore>(Loc)) |
| 2916 | Out << "\\lPostStore\\l"; |
| 2917 | else if (isa<PostLValue>(Loc)) |
| 2918 | Out << "\\lPostLValue\\l"; |
| 2919 | else if (isa<PostLocationChecksSucceed>(Loc)) |
| 2920 | Out << "\\lPostLocationChecksSucceed\\l"; |
| 2921 | else if (isa<PostNullCheckFailed>(Loc)) |
| 2922 | Out << "\\lPostNullCheckFailed\\l"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2924 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) |
| 2925 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 2926 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) |
| 2927 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 2928 | else if (GraphPrintCheckerState->isUndefDeref(N)) |
| 2929 | Out << "\\|Dereference of undefialied value.\\l"; |
| 2930 | else if (GraphPrintCheckerState->isUndefStore(N)) |
| 2931 | Out << "\\|Store to Undefined Loc."; |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2932 | else if (GraphPrintCheckerState->isUndefResult(N)) |
| 2933 | Out << "\\|Result of operation is undefined."; |
| 2934 | else if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 2935 | Out << "\\|Call to function marked \"noreturn\"."; |
| 2936 | else if (GraphPrintCheckerState->isBadCall(N)) |
| 2937 | Out << "\\|Call to NULL/Undefined."; |
| 2938 | else if (GraphPrintCheckerState->isUndefArg(N)) |
| 2939 | Out << "\\|Argument in call is undefined"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | |
Ted Kremenek | 9e08ff4 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2941 | break; |
| 2942 | } |
| 2943 | |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2944 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 2945 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 2946 | << E.getDst()->getBlockID() << ')'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2948 | if (Stmt* T = E.getSrc()->getTerminator()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | |
Ted Kremenek | 6947a79 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2950 | SourceLocation SLoc = T->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2952 | Out << "\\|Terminator: "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2953 | LangOptions LO; // FIXME. |
| 2954 | E.getSrc()->printTerminator(Out, LO); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Ted Kremenek | 03ab156 | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 2956 | if (SLoc.isFileID()) { |
| 2957 | Out << "\\lline=" |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 2958 | << GraphPrintSourceManager->getInstantiationLineNumber(SLoc) |
| 2959 | << " col=" |
| 2960 | << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc); |
Ted Kremenek | 03ab156 | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 2961 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2963 | if (isa<SwitchStmt>(T)) { |
| 2964 | Stmt* Label = E.getDst()->getLabel(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | |
| 2966 | if (Label) { |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2967 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 2968 | Out << "\\lcase "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2969 | LangOptions LO; // FIXME. |
| 2970 | C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2972 | if (Stmt* RHS = C->getRHS()) { |
| 2973 | Out << " .. "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2974 | RHS->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2975 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2977 | Out << ":"; |
| 2978 | } |
| 2979 | else { |
| 2980 | assert (isa<DefaultStmt>(Label)); |
| 2981 | Out << "\\ldefault:"; |
| 2982 | } |
| 2983 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | else |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2985 | Out << "\\l(implicit) default:"; |
| 2986 | } |
| 2987 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2988 | // FIXME |
| 2989 | } |
| 2990 | else { |
| 2991 | Out << "\\lCondition: "; |
| 2992 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 2993 | Out << "true"; |
| 2994 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | Out << "false"; |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2996 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2998 | Out << "\\l"; |
| 2999 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | |
Ted Kremenek | 93d1fed | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 3001 | if (GraphPrintCheckerState->isUndefControlFlow(N)) { |
| 3002 | Out << "\\|Control-flow based on\\lUndefined value.\\l"; |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 3003 | } |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 3004 | } |
| 3005 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
Ted Kremenek | 22c62c1 | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 3007 | Out << "\\|StateID: " << (void*) N->getState() << "\\|"; |
Ted Kremenek | b54312d | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 3008 | |
Ted Kremenek | d93c6e3 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3009 | const GRState *state = N->getState(); |
| 3010 | state->printDOT(Out); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | |
Ted Kremenek | 930191c | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 3012 | Out << "\\l"; |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 3013 | return Out.str(); |
| 3014 | } |
| 3015 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | } // end llvm namespace |
Ted Kremenek | ac886cb | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 3017 | #endif |
| 3018 | |
Ted Kremenek | 2bdd776 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3019 | #ifndef NDEBUG |
Ted Kremenek | 576b76a | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3020 | template <typename ITERATOR> |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3021 | ExplodedNode* GetGraphNode(ITERATOR I) { return *I; } |
Ted Kremenek | 576b76a | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3022 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3023 | template <> ExplodedNode* |
| 3024 | GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator> |
| 3025 | (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) { |
Ted Kremenek | 576b76a | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3026 | return I->first; |
| 3027 | } |
Ted Kremenek | 2bdd776 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3028 | #endif |
| 3029 | |
| 3030 | void GRExprEngine::ViewGraph(bool trim) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | #ifndef NDEBUG |
Ted Kremenek | 2bdd776 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3032 | if (trim) { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3033 | std::vector<ExplodedNode*> Src; |
Ted Kremenek | 9517505 | 2009-03-11 01:41:22 +0000 | [diff] [blame] | 3034 | |
| 3035 | // Flush any outstanding reports to make sure we cover all the nodes. |
| 3036 | // This does not cause them to get displayed. |
| 3037 | for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) |
| 3038 | const_cast<BugType*>(*I)->FlushReports(BR); |
| 3039 | |
| 3040 | // Iterate through the reports and get their nodes. |
| 3041 | for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) { |
Ted Kremenek | 17a0296 | 2009-09-03 03:02:58 +0000 | [diff] [blame] | 3042 | for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | I2!=E2; ++I2) { |
Ted Kremenek | 9517505 | 2009-03-11 01:41:22 +0000 | [diff] [blame] | 3044 | const BugReportEquivClass& EQ = *I2; |
| 3045 | const BugReport &R = **EQ.begin(); |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3046 | ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode()); |
Ted Kremenek | 9517505 | 2009-03-11 01:41:22 +0000 | [diff] [blame] | 3047 | if (N) Src.push_back(N); |
| 3048 | } |
| 3049 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | |
Ted Kremenek | 576b76a | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3051 | ViewGraph(&Src[0], &Src[0]+Src.size()); |
Ted Kremenek | 2bdd776 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3052 | } |
Ted Kremenek | a7178c7 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3053 | else { |
| 3054 | GraphPrintCheckerState = this; |
| 3055 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | 1630610 | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 3056 | |
Ted Kremenek | 2bdd776 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3057 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | |
Ted Kremenek | a7178c7 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3059 | GraphPrintCheckerState = NULL; |
| 3060 | GraphPrintSourceManager = NULL; |
| 3061 | } |
| 3062 | #endif |
| 3063 | } |
| 3064 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3065 | void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) { |
Ted Kremenek | a7178c7 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3066 | #ifndef NDEBUG |
| 3067 | GraphPrintCheckerState = this; |
| 3068 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3070 | std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first); |
Ted Kremenek | a7178c7 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3071 | |
Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 3072 | if (!TrimmedG.get()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 3073 | llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n"; |
Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 3074 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine"); |
| 3076 | |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 3077 | GraphPrintCheckerState = NULL; |
Ted Kremenek | 6947a79 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 3078 | GraphPrintSourceManager = NULL; |
Ted Kremenek | d3122cb | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 3079 | #endif |
Ted Kremenek | 2e12c2e | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 3080 | } |