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