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