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