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