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