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