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