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