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