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