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