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