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