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