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