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