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