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