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