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