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