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