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