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