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