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