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