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