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