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