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