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