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