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