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