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