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