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