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