Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 10 | // This file defines a meta-engine for path-sensitive dataflow analysis that |
| 11 | // is built on GREngine, but provides the boilerplate to execute transfer |
| 12 | // functions and build the ExplodedGraph at the expression level. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Streams.h" |
Ted Kremenek | b387a3f | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 20 | |
Ted Kremenek | 0f5f059 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 21 | #ifndef NDEBUG |
| 22 | #include "llvm/Support/GraphWriter.h" |
| 23 | #include <sstream> |
| 24 | #endif |
| 25 | |
Ted Kremenek | b387a3f | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using llvm::dyn_cast; |
| 28 | using llvm::cast; |
| 29 | using llvm::APSInt; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Engine construction and deletion. |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Ted Kremenek | daa497e | 2008-03-09 18:05:48 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 36 | GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx) |
| 37 | : CoreEngine(cfg, CD, Ctx, *this), |
| 38 | G(CoreEngine.getGraph()), |
| 39 | Liveness(G.getCFG()), |
| 40 | Builder(NULL), |
| 41 | StateMgr(G.getContext(), G.getAllocator()), |
| 42 | BasicVals(StateMgr.getBasicValueFactory()), |
| 43 | TF(NULL), // FIXME |
| 44 | SymMgr(StateMgr.getSymbolManager()), |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 45 | CurrentStmt(NULL) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 46 | |
| 47 | // Compute liveness information. |
| 48 | Liveness.runOnCFG(G.getCFG()); |
| 49 | Liveness.runOnAllBlocks(G.getCFG(), NULL, true); |
| 50 | } |
| 51 | |
| 52 | GRExprEngine::~GRExprEngine() { |
| 53 | for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I) |
| 54 | delete *I; |
| 55 | |
| 56 | for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end(); |
| 57 | I != E; ++I) |
| 58 | delete *I; |
| 59 | |
| 60 | for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end(); |
| 61 | I != E; ++I) |
| 62 | delete *I; |
| 63 | } |
| 64 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 65 | //===----------------------------------------------------------------------===// |
| 66 | // Utility methods. |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
| 69 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 70 | // the value of a variable. |
| 71 | template<typename T> |
| 72 | struct VISIBILITY_HIDDEN SaveAndRestore { |
| 73 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 74 | ~SaveAndRestore() { X = old_value; } |
| 75 | T get() { return old_value; } |
| 76 | |
| 77 | T& X; |
| 78 | T old_value; |
| 79 | }; |
| 80 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 81 | // SaveOr - Similar to SaveAndRestore. Operates only on bools; the old |
| 82 | // value of a variable is saved, and during the dstor the old value is |
| 83 | // or'ed with the new value. |
| 84 | struct VISIBILITY_HIDDEN SaveOr { |
| 85 | SaveOr(bool& x) : X(x), old_value(x) { x = false; } |
| 86 | ~SaveOr() { X |= old_value; } |
| 87 | |
| 88 | bool& X; |
| 89 | bool old_value; |
| 90 | }; |
| 91 | |
| 92 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 93 | void GRExprEngine::EmitWarnings(Diagnostic& Diag, PathDiagnosticClient* PD) { |
| 94 | for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){ |
| 95 | BugReporter BR(Diag, PD, getContext(), *this); |
| 96 | (*I)->EmitWarnings(BR); |
| 97 | } |
| 98 | |
| 99 | for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end(); |
| 100 | I != E; ++I) { |
| 101 | BugReporter BR(Diag, PD, getContext(), *this); |
| 102 | (*I)->EmitWarnings(BR); |
| 103 | } |
| 104 | |
| 105 | for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end(); |
| 106 | I != E; ++I) { |
| 107 | BugReporter BR(Diag, PD, getContext(), *this); |
| 108 | (*I)->EmitWarnings(BR); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) { |
| 113 | TF = tf; |
| 114 | TF->RegisterChecks(*this); |
| 115 | } |
| 116 | |
| 117 | void GRExprEngine::AddCallCheck(GRSimpleAPICheck* A) { |
| 118 | CallChecks.push_back(A); |
| 119 | } |
| 120 | |
| 121 | void GRExprEngine::AddObjCMessageExprCheck(GRSimpleAPICheck* A) { |
| 122 | MsgExprChecks.push_back(A); |
| 123 | } |
| 124 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 125 | ValueState* GRExprEngine::getInitialState() { |
Ted Kremenek | 0793263 | 2008-02-27 06:47:26 +0000 | [diff] [blame] | 126 | |
| 127 | // The LiveVariables information already has a compilation of all VarDecls |
| 128 | // used in the function. Iterate through this set, and "symbolicate" |
| 129 | // any VarDecl whose value originally comes from outside the function. |
| 130 | |
| 131 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 132 | LVDataTy& D = Liveness.getAnalysisData(); |
| 133 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 134 | ValueState StateImpl = *StateMgr.getInitialState(); |
Ted Kremenek | 0793263 | 2008-02-27 06:47:26 +0000 | [diff] [blame] | 135 | |
| 136 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
| 137 | |
| 138 | VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first)); |
| 139 | |
| 140 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) { |
| 141 | RVal X = RVal::GetSymbolValue(SymMgr, VD); |
| 142 | StateMgr.BindVar(StateImpl, VD, X); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return StateMgr.getPersistentState(StateImpl); |
| 147 | } |
| 148 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 149 | ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 151 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 153 | if (Ex == CurrentStmt) { |
| 154 | isBlkExpr = getCFG().isBlkExpr(Ex); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 155 | |
| 156 | if (!isBlkExpr) |
| 157 | return St; |
| 158 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 5a7b382 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 160 | return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 163 | //===----------------------------------------------------------------------===// |
| 164 | // Top-level transfer function logic (Dispatcher). |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
| 168 | |
| 169 | Builder = &builder; |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 170 | EntryNode = builder.getLastNode(); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 171 | CurrentStmt = S; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 172 | |
| 173 | // Set up our simple checks. |
| 174 | |
| 175 | // FIXME: This can probably be installed directly in GRCoreEngine, obviating |
| 176 | // the need to do a copy every time we hit a block-level statement. |
| 177 | |
| 178 | if (!MsgExprChecks.empty()) |
| 179 | Builder->setObjCMsgExprAuditors((GRAuditor<ValueState>**) &MsgExprChecks[0], |
| 180 | (GRAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size())); |
| 181 | |
| 182 | |
| 183 | if (!CallChecks.empty()) |
| 184 | Builder->setCallExprAuditors((GRAuditor<ValueState>**) &CallChecks[0], |
| 185 | (GRAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size())); |
| 186 | |
| 187 | // Create the cleaned state. |
| 188 | |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 189 | CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, |
| 190 | Liveness, DeadSymbols); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 192 | // Process any special transfer function for dead symbols. |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 194 | NodeSet Tmp; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 196 | if (DeadSymbols.empty()) |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 197 | Tmp.Add(EntryNode); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 198 | else { |
| 199 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 200 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
| 201 | |
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(); |
| 783 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 784 | // Always visit the base as an LVal expression. This computes the |
| 785 | // abstract address of the base object. |
| 786 | NodeSet Tmp; |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 787 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 788 | if (Base->getType()->isPointerType()) // Base always is an LVal. |
| 789 | Visit(Base, Pred, Tmp); |
| 790 | else |
| 791 | VisitLVal(Base, Pred, Tmp); |
| 792 | |
| 793 | // TODO: Compute the LVal for the entry. This will enable array sensitivity |
| 794 | // for the analysis. |
| 795 | |
| 796 | // Return the LVal of the array entry. |
| 797 | if (asLVal) { |
| 798 | |
| 799 | // This is a redunant copy; we do this as a placeholder for future logic. |
| 800 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 801 | |
| 802 | ValueState* St = GetState(*I); |
| 803 | RVal V = GetRVal(St, Base); |
| 804 | |
| 805 | // TODO: Compute the LVal for the entry. This will enable array sensitivity |
| 806 | // for the analysis. |
| 807 | |
| 808 | if (!(V.isUndef() || V.isUnknown() || isa<lval::ConcreteInt>(V))) |
| 809 | V = UnknownVal(); |
| 810 | |
| 811 | MakeNode(Dst, A, *I, SetRVal(St, A, V)); |
| 812 | } |
| 813 | |
| 814 | return; |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 817 | // We are doing a load. Check for a bad dereference. In the future we |
| 818 | // will check the actual entry lval; for now, check the Base LVal. For now |
| 819 | // the load will just return "UnknownVal" (since we don't have array |
| 820 | // sensitivity), but it will perform a null check. |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 821 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 822 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 823 | |
| 824 | ValueState* St = GetState(*I); |
| 825 | RVal V = GetRVal(St, Base); |
| 826 | |
| 827 | // TODO: Compute the LVal for the entry. This will enable array sensitivity |
| 828 | // for the analysis. |
| 829 | |
| 830 | if (!(V.isUndef() || V.isUnknown() || isa<lval::ConcreteInt>(V))) |
| 831 | V = UnknownVal(); |
| 832 | |
| 833 | EvalLoad(Dst, A, *I, St, GetRVal(St, Base), true); |
| 834 | } |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 837 | /// VisitMemberExpr - Transfer function for member expressions. |
| 838 | void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred, |
| 839 | NodeSet& Dst, bool asLVal) { |
| 840 | |
| 841 | Expr* Base = M->getBase()->IgnoreParens(); |
| 842 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 843 | // Always visit the base as an LVal expression. This computes the |
| 844 | // abstract address of the base object. |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 845 | NodeSet Tmp; |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 846 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 847 | if (Base->getType()->isPointerType()) // Base always is an LVal. |
| 848 | Visit(Base, Pred, Tmp); |
| 849 | else |
| 850 | VisitLVal(Base, Pred, Tmp); |
| 851 | |
| 852 | |
| 853 | // Return the LVal of the field access. |
| 854 | if (asLVal) { |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 855 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 856 | // This is a redunant copy; we do this as a placeholder for future logic. |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 857 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 858 | ValueState* St = GetState(*I); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 859 | RVal BaseV = GetRVal(St, Base); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 861 | RVal V = lval::FieldOffset::Make(BasicVals, GetRVal(St, Base), |
| 862 | M->getMemberDecl()); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 863 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 864 | MakeNode(Dst, M, *I, SetRVal(St, M, V)); |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 865 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 866 | |
| 867 | return; |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 868 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 869 | |
| 870 | // We are doing a load. Check for a bad dereference. In the future we |
| 871 | // will check the actual field lval; for now, check the Base LVal. For now |
| 872 | // the load will just return "UnknownVal" (since we don't have field |
| 873 | // sensitivity), but it will perform a null check. |
| 874 | |
| 875 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 876 | ValueState* St = GetState(*I); |
| 877 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 878 | RVal V = lval::FieldOffset::Make(BasicVals, GetRVal(St, Base), |
| 879 | M->getMemberDecl()); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 880 | |
| 881 | EvalLoad(Dst, M, *I, St, V, true); |
| 882 | } |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 885 | void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred, |
| 886 | ValueState* St, RVal location, RVal Val) { |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 887 | |
| 888 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
| 889 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 890 | // Evaluate the location (checks for bad dereferences). |
| 891 | St = EvalLocation(Ex, Pred, St, location); |
| 892 | |
| 893 | if (!St) |
| 894 | return; |
| 895 | |
| 896 | // Proceed with the store. |
| 897 | |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 898 | unsigned size = Dst.size(); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 899 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 900 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 901 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 902 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 903 | assert (!location.isUndef()); |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 905 | TF->EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val); |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 906 | |
| 907 | // Handle the case where no nodes where generated. Auto-generate that |
| 908 | // contains the updated state if we aren't generating sinks. |
| 909 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 910 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 911 | TF->GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St, |
| 912 | location, Val); |
| 913 | } |
| 914 | |
| 915 | void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred, |
| 916 | ValueState* St, RVal location, bool CheckOnly) { |
| 917 | |
| 918 | // Evaluate the location (checks for bad dereferences). |
| 919 | |
| 920 | St = EvalLocation(Ex, Pred, St, location, true); |
| 921 | |
| 922 | if (!St) |
| 923 | return; |
| 924 | |
| 925 | // Proceed with the load. |
| 926 | |
| 927 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 928 | // for the contents of values. We need a better approach. |
| 929 | |
| 930 | // FIXME: The "CheckOnly" option exists only because Array and Field |
| 931 | // loads aren't fully implemented. Eventually this option will go away. |
| 932 | |
| 933 | if (location.isUnknown() || CheckOnly) |
| 934 | MakeNode(Dst, Ex, Pred, St); |
| 935 | else |
| 936 | MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location), |
| 937 | Ex->getType()))); |
| 938 | } |
| 939 | |
| 940 | ValueState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred, |
| 941 | ValueState* St, RVal location, |
| 942 | bool isLoad) { |
| 943 | |
| 944 | // Check for loads/stores from/to undefined values. |
| 945 | if (location.isUndef()) { |
| 946 | if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, isLoad)) { |
| 947 | Succ->markAsSink(); |
| 948 | UndefDeref.insert(Succ); |
| 949 | } |
| 950 | |
| 951 | return NULL; |
| 952 | } |
| 953 | |
| 954 | // Check for loads/stores from/to unknown locations. Treat as No-Ops. |
| 955 | if (location.isUnknown()) |
| 956 | return St; |
| 957 | |
| 958 | // During a load, one of two possible situations arise: |
| 959 | // (1) A crash, because the location (pointer) was NULL. |
| 960 | // (2) The location (pointer) is not NULL, and the dereference works. |
| 961 | // |
| 962 | // We add these assumptions. |
| 963 | |
| 964 | LVal LV = cast<LVal>(location); |
| 965 | |
| 966 | // "Assume" that the pointer is not NULL. |
| 967 | |
| 968 | bool isFeasibleNotNull = false; |
| 969 | ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull); |
| 970 | |
| 971 | // "Assume" that the pointer is NULL. |
| 972 | |
| 973 | bool isFeasibleNull = false; |
| 974 | ValueState* StNull = Assume(St, LV, false, isFeasibleNull); |
| 975 | |
| 976 | if (isFeasibleNull) { |
| 977 | |
| 978 | // We don't use "MakeNode" here because the node will be a sink |
| 979 | // and we have no intention of processing it later. |
| 980 | |
| 981 | NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, isLoad); |
| 982 | |
| 983 | if (NullNode) { |
| 984 | |
| 985 | NullNode->markAsSink(); |
| 986 | |
| 987 | if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode); |
| 988 | else ExplicitNullDeref.insert(NullNode); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | return isFeasibleNotNull ? StNotNull : NULL; |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 995 | //===----------------------------------------------------------------------===// |
| 996 | // Transfer function: Function calls. |
| 997 | //===----------------------------------------------------------------------===// |
| 998 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 999 | void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1000 | CallExpr::arg_iterator AI, |
| 1001 | CallExpr::arg_iterator AE, |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1002 | NodeSet& Dst) { |
| 1003 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1004 | // Process the arguments. |
| 1005 | |
| 1006 | if (AI != AE) { |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1007 | |
Ted Kremenek | ed2d2ed | 2008-03-04 00:56:45 +0000 | [diff] [blame] | 1008 | NodeSet DstTmp; |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1009 | Visit(*AI, Pred, DstTmp); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1010 | ++AI; |
| 1011 | |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1012 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1013 | VisitCall(CE, *DI, AI, AE, Dst); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1014 | |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | // If we reach here we have processed all of the arguments. Evaluate |
| 1019 | // the callee expression. |
Ted Kremenek | a1354a5 | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1020 | |
Ted Kremenek | 994a09b | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 1021 | NodeSet DstTmp; |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1022 | Expr* Callee = CE->getCallee()->IgnoreParens(); |
Ted Kremenek | a1354a5 | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1023 | |
Ted Kremenek | 994a09b | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 1024 | VisitLVal(Callee, Pred, DstTmp); |
Ted Kremenek | a1354a5 | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1025 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1026 | // Finally, evaluate the function call. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1027 | for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) { |
| 1028 | |
Ted Kremenek | 0d093d3 | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1029 | ValueState* St = GetState(*DI); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1030 | RVal L = GetRVal(St, Callee); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | a1354a5 | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1032 | // FIXME: Add support for symbolic function calls (calls involving |
| 1033 | // function pointer values that are symbolic). |
| 1034 | |
| 1035 | // Check for undefined control-flow or calls to NULL. |
| 1036 | |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1037 | if (L.isUndef() || isa<lval::ConcreteInt>(L)) { |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1038 | NodeTy* N = Builder->generateNode(CE, St, *DI); |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1039 | |
Ted Kremenek | 2ded35a | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 1040 | if (N) { |
| 1041 | N->markAsSink(); |
| 1042 | BadCalls.insert(N); |
| 1043 | } |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1044 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1045 | continue; |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // Check for the "noreturn" attribute. |
| 1049 | |
| 1050 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 1051 | |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1052 | if (isa<lval::FuncVal>(L)) { |
| 1053 | |
| 1054 | FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl(); |
| 1055 | |
| 1056 | if (FD->getAttr<NoReturnAttr>()) |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1057 | Builder->BuildSinks = true; |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1058 | else { |
| 1059 | // HACK: Some functions are not marked noreturn, and don't return. |
| 1060 | // Here are a few hardwired ones. If this takes too long, we can |
| 1061 | // potentially cache these results. |
| 1062 | const char* s = FD->getIdentifier()->getName(); |
| 1063 | unsigned n = strlen(s); |
| 1064 | |
| 1065 | switch (n) { |
| 1066 | default: |
| 1067 | break; |
Ted Kremenek | 76fdbde | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 1068 | |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1069 | case 4: |
Ted Kremenek | 76fdbde | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 1070 | if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true; |
| 1071 | break; |
| 1072 | |
| 1073 | case 5: |
| 1074 | if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true; |
| 1075 | break; |
Ted Kremenek | 9a094cb | 2008-04-22 05:37:33 +0000 | [diff] [blame] | 1076 | |
| 1077 | case 6: |
| 1078 | if (!memcmp(s, "Assert", 6)) Builder->BuildSinks = true; |
| 1079 | break; |
Ted Kremenek | 688738f | 2008-04-23 00:41:25 +0000 | [diff] [blame] | 1080 | |
| 1081 | case 7: |
| 1082 | if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true; |
| 1083 | break; |
Ted Kremenek | 9a108ae | 2008-04-22 06:09:33 +0000 | [diff] [blame] | 1084 | |
| 1085 | case 14: |
| 1086 | if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true; |
| 1087 | break; |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1088 | } |
Ted Kremenek | 9a108ae | 2008-04-22 06:09:33 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Evaluate the call. |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (isa<lval::FuncVal>(L)) { |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1096 | |
| 1097 | IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier(); |
| 1098 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1099 | if (unsigned id = Info->getBuiltinID()) |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1100 | switch (id) { |
| 1101 | case Builtin::BI__builtin_expect: { |
| 1102 | // For __builtin_expect, just return the value of the subexpression. |
| 1103 | assert (CE->arg_begin() != CE->arg_end()); |
| 1104 | RVal X = GetRVal(St, *(CE->arg_begin())); |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1105 | MakeNode(Dst, CE, *DI, SetRVal(St, CE, X)); |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1106 | continue; |
| 1107 | } |
| 1108 | |
| 1109 | default: |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1110 | break; |
| 1111 | } |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1112 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1113 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1114 | // Check any arguments passed-by-value against being undefined. |
| 1115 | |
| 1116 | bool badArg = false; |
| 1117 | |
| 1118 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 1119 | I != E; ++I) { |
| 1120 | |
| 1121 | if (GetRVal(GetState(*DI), *I).isUndef()) { |
| 1122 | NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI); |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1123 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1124 | if (N) { |
| 1125 | N->markAsSink(); |
| 1126 | UndefArgs[N] = *I; |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1127 | } |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1128 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1129 | badArg = true; |
| 1130 | break; |
| 1131 | } |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1132 | } |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (badArg) |
| 1135 | continue; |
| 1136 | |
| 1137 | // Dispatch to the plug-in transfer function. |
| 1138 | |
| 1139 | unsigned size = Dst.size(); |
| 1140 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
| 1141 | EvalCall(Dst, CE, L, *DI); |
| 1142 | |
| 1143 | // Handle the case where no nodes where generated. Auto-generate that |
| 1144 | // contains the updated state if we aren't generating sinks. |
| 1145 | |
| 1146 | if (!Builder->BuildSinks && Dst.size() == size && |
| 1147 | !Builder->HasGeneratedNode) |
| 1148 | MakeNode(Dst, CE, *DI, St); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1152 | //===----------------------------------------------------------------------===// |
| 1153 | // Transfer function: Objective-C message expressions. |
| 1154 | //===----------------------------------------------------------------------===// |
| 1155 | |
| 1156 | void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred, |
| 1157 | NodeSet& Dst){ |
| 1158 | |
| 1159 | VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), |
| 1160 | Pred, Dst); |
| 1161 | } |
| 1162 | |
| 1163 | void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME, |
| 1164 | ObjCMessageExpr::arg_iterator AI, |
| 1165 | ObjCMessageExpr::arg_iterator AE, |
| 1166 | NodeTy* Pred, NodeSet& Dst) { |
| 1167 | if (AI == AE) { |
| 1168 | |
| 1169 | // Process the receiver. |
| 1170 | |
| 1171 | if (Expr* Receiver = ME->getReceiver()) { |
| 1172 | NodeSet Tmp; |
| 1173 | Visit(Receiver, Pred, Tmp); |
| 1174 | |
| 1175 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1176 | VisitObjCMessageExprDispatchHelper(ME, *NI, Dst); |
| 1177 | |
| 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | VisitObjCMessageExprDispatchHelper(ME, Pred, Dst); |
| 1182 | return; |
| 1183 | } |
| 1184 | |
| 1185 | NodeSet Tmp; |
| 1186 | Visit(*AI, Pred, Tmp); |
| 1187 | |
| 1188 | ++AI; |
| 1189 | |
| 1190 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1191 | VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst); |
| 1192 | } |
| 1193 | |
| 1194 | void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME, |
| 1195 | NodeTy* Pred, |
| 1196 | NodeSet& Dst) { |
| 1197 | |
| 1198 | // FIXME: More logic for the processing the method call. |
| 1199 | |
| 1200 | ValueState* St = GetState(Pred); |
| 1201 | |
| 1202 | if (Expr* Receiver = ME->getReceiver()) { |
| 1203 | |
| 1204 | RVal L = GetRVal(St, Receiver); |
| 1205 | |
| 1206 | // Check for undefined control-flow or calls to NULL. |
| 1207 | |
| 1208 | if (L.isUndef()) { |
| 1209 | NodeTy* N = Builder->generateNode(ME, St, Pred); |
| 1210 | |
| 1211 | if (N) { |
| 1212 | N->markAsSink(); |
| 1213 | UndefReceivers.insert(N); |
| 1214 | } |
| 1215 | |
| 1216 | return; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | // Check for any arguments that are uninitialized/undefined. |
| 1221 | |
| 1222 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 1223 | I != E; ++I) { |
| 1224 | |
| 1225 | if (GetRVal(St, *I).isUndef()) { |
| 1226 | |
| 1227 | // Generate an error node for passing an uninitialized/undefined value |
| 1228 | // as an argument to a message expression. This node is a sink. |
| 1229 | NodeTy* N = Builder->generateNode(ME, St, Pred); |
| 1230 | |
| 1231 | if (N) { |
| 1232 | N->markAsSink(); |
| 1233 | MsgExprUndefArgs[N] = *I; |
| 1234 | } |
| 1235 | |
| 1236 | return; |
| 1237 | } |
| 1238 | } |
| 1239 | // Dispatch to plug-in transfer function. |
| 1240 | |
| 1241 | unsigned size = Dst.size(); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1242 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1243 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 1244 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1245 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1246 | EvalObjCMessageExpr(Dst, ME, Pred); |
| 1247 | |
| 1248 | // Handle the case where no nodes where generated. Auto-generate that |
| 1249 | // contains the updated state if we aren't generating sinks. |
| 1250 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1251 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1252 | MakeNode(Dst, ME, Pred, St); |
| 1253 | } |
| 1254 | |
| 1255 | //===----------------------------------------------------------------------===// |
| 1256 | // Transfer functions: Miscellaneous statements. |
| 1257 | //===----------------------------------------------------------------------===// |
| 1258 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1259 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1260 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 1261 | NodeSet S1; |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 1262 | QualType T = CastE->getType(); |
| 1263 | |
Ted Kremenek | 65cfb73 | 2008-03-04 22:16:08 +0000 | [diff] [blame] | 1264 | if (T->isReferenceType()) |
| 1265 | VisitLVal(Ex, Pred, S1); |
| 1266 | else |
| 1267 | Visit(Ex, Pred, S1); |
| 1268 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 1269 | // Check for casting to "void". |
| 1270 | if (T->isVoidType()) { |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 1271 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1272 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 1273 | Dst.Add(*I1); |
| 1274 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1275 | return; |
| 1276 | } |
| 1277 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 1278 | // FIXME: The rest of this should probably just go into EvalCall, and |
| 1279 | // let the transfer function object be responsible for constructing |
| 1280 | // nodes. |
| 1281 | |
| 1282 | QualType ExTy = Ex->getType(); |
| 1283 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1284 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1285 | NodeTy* N = *I1; |
Ted Kremenek | 0d093d3 | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1286 | ValueState* St = GetState(N); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1287 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 1288 | |
| 1289 | // Unknown? |
| 1290 | |
| 1291 | if (V.isUnknown()) { |
| 1292 | Dst.Add(N); |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | // Undefined? |
| 1297 | |
| 1298 | if (V.isUndef()) { |
| 1299 | MakeNode(Dst, CastE, N, SetRVal(St, CastE, V)); |
| 1300 | continue; |
| 1301 | } |
| 1302 | |
| 1303 | // Check for casts from pointers to integers. |
| 1304 | if (T->isIntegerType() && ExTy->isPointerType()) { |
| 1305 | unsigned bits = getContext().getTypeSize(ExTy); |
| 1306 | |
| 1307 | // FIXME: Determine if the number of bits of the target type is |
| 1308 | // equal or exceeds the number of bits to store the pointer value. |
| 1309 | // If not, flag an error. |
| 1310 | |
| 1311 | V = nonlval::LValAsInteger::Make(BasicVals, cast<LVal>(V), bits); |
| 1312 | MakeNode(Dst, CastE, N, SetRVal(St, CastE, V)); |
| 1313 | continue; |
| 1314 | } |
| 1315 | |
| 1316 | // Check for casts from integers to pointers. |
| 1317 | if (T->isPointerType() && ExTy->isIntegerType()) |
| 1318 | if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) { |
| 1319 | // Just unpackage the lval and return it. |
| 1320 | V = LV->getLVal(); |
| 1321 | MakeNode(Dst, CastE, N, SetRVal(St, CastE, V)); |
| 1322 | continue; |
| 1323 | } |
| 1324 | |
| 1325 | // All other cases. |
Ted Kremenek | 65cfb73 | 2008-03-04 22:16:08 +0000 | [diff] [blame] | 1326 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1327 | MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType()))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1328 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1331 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) { |
| 1332 | VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst); |
| 1333 | } |
| 1334 | |
| 1335 | void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D, |
| 1336 | NodeTy* Pred, NodeSet& Dst) { |
| 1337 | |
| 1338 | if (!D) |
| 1339 | return; |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1340 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1341 | if (!isa<VarDecl>(D)) { |
| 1342 | VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst); |
| 1343 | return; |
| 1344 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1346 | const VarDecl* VD = dyn_cast<VarDecl>(D); |
| 1347 | |
| 1348 | // FIXME: Add support for local arrays. |
| 1349 | if (VD->getType()->isArrayType()) { |
| 1350 | VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst); |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | Expr* Ex = const_cast<Expr*>(VD->getInit()); |
| 1355 | |
| 1356 | // FIXME: static variables may have an initializer, but the second |
| 1357 | // time a function is called those values may not be current. |
| 1358 | NodeSet Tmp; |
| 1359 | |
| 1360 | if (Ex) Visit(Ex, Pred, Tmp); |
| 1361 | if (Tmp.empty()) Tmp.Add(Pred); |
| 1362 | |
| 1363 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | c2c95b0 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 1364 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1365 | ValueState* St = GetState(*I); |
| 1366 | |
| 1367 | if (!Ex && VD->hasGlobalStorage()) { |
Ted Kremenek | c2c95b0 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 1368 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1369 | // Handle variables with global storage and no initializers. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1370 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1371 | // FIXME: static variables may have an initializer, but the second |
| 1372 | // time a function is called those values may not be current. |
| 1373 | |
| 1374 | |
| 1375 | // In this context, Static => Local variable. |
| 1376 | |
| 1377 | assert (!VD->getStorageClass() == VarDecl::Static || |
| 1378 | !VD->isFileVarDecl()); |
| 1379 | |
| 1380 | // If there is no initializer, set the value of the |
| 1381 | // variable to "Undefined". |
| 1382 | |
| 1383 | if (VD->getStorageClass() == VarDecl::Static) { |
| 1384 | |
| 1385 | // C99: 6.7.8 Initialization |
| 1386 | // If an object that has static storage duration is not initialized |
| 1387 | // explicitly, then: |
| 1388 | // —if it has pointer type, it is initialized to a null pointer; |
| 1389 | // —if it has arithmetic type, it is initialized to (positive or |
| 1390 | // unsigned) zero; |
Ted Kremenek | b0ab212 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 1391 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1392 | // FIXME: Handle structs. Now we treat their values as unknown. |
Ted Kremenek | fcb092b | 2008-03-04 20:40:11 +0000 | [diff] [blame] | 1393 | |
| 1394 | QualType T = VD->getType(); |
Ted Kremenek | b0ab212 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 1395 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1396 | if (T->isPointerType()) |
| 1397 | St = SetRVal(St, lval::DeclVal(VD), |
| 1398 | lval::ConcreteInt(BasicVals.getValue(0, T))); |
| 1399 | else if (T->isIntegerType()) |
| 1400 | St = SetRVal(St, lval::DeclVal(VD), |
| 1401 | nonlval::ConcreteInt(BasicVals.getValue(0, T))); |
Ted Kremenek | 16d8156 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1403 | // FIXME: Handle structs. Now we treat them as unknown. What |
| 1404 | // we need to do is treat their members as unknown. |
Ted Kremenek | b0ab212 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 1405 | } |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 1406 | } |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1407 | else { |
| 1408 | |
| 1409 | // FIXME: Handle structs. Now we treat them as unknown. What |
| 1410 | // we need to do is treat their members as unknown. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1411 | |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 1412 | QualType T = VD->getType(); |
| 1413 | |
| 1414 | if (T->isPointerType() || T->isIntegerType()) |
| 1415 | St = SetRVal(St, lval::DeclVal(VD), |
| 1416 | Ex ? GetRVal(St, Ex) : UndefinedVal()); |
| 1417 | } |
| 1418 | |
| 1419 | // Create a new node. We don't really need to create a new NodeSet |
| 1420 | // here, but it simplifies things and doesn't cost much. |
| 1421 | NodeSet Tmp2; |
| 1422 | MakeNode(Tmp2, DS, *I, St); |
| 1423 | if (Tmp2.empty()) Tmp2.Add(*I); |
| 1424 | |
| 1425 | for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) |
| 1426 | VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst); |
| 1427 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1428 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1429 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1430 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1431 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1432 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex, |
| 1433 | NodeTy* Pred, |
| 1434 | NodeSet& Dst) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1435 | QualType T = Ex->getArgumentType(); |
Ted Kremenek | 87e8034 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 1436 | uint64_t amt; |
| 1437 | |
| 1438 | if (Ex->isSizeOf()) { |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1439 | |
Ted Kremenek | 87e8034 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 1440 | // FIXME: Add support for VLAs. |
| 1441 | if (!T.getTypePtr()->isConstantSizeType()) |
| 1442 | return; |
| 1443 | |
| 1444 | amt = 1; // Handle sizeof(void) |
| 1445 | |
| 1446 | if (T != getContext().VoidTy) |
| 1447 | amt = getContext().getTypeSize(T) / 8; |
| 1448 | |
| 1449 | } |
| 1450 | else // Get alignment of the type. |
Ted Kremenek | 897781a | 2008-03-15 03:13:55 +0000 | [diff] [blame] | 1451 | amt = getContext().getTypeAlign(T) / 8; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1452 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1453 | MakeNode(Dst, Ex, Pred, |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1454 | SetRVal(GetState(Pred), Ex, |
| 1455 | NonLVal::MakeVal(BasicVals, amt, Ex->getType()))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1458 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1459 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred, |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1460 | NodeSet& Dst, bool asLVal) { |
| 1461 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1462 | switch (U->getOpcode()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1463 | |
| 1464 | default: |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1465 | break; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1466 | |
| 1467 | case UnaryOperator::Deref: { |
| 1468 | |
| 1469 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
| 1470 | NodeSet Tmp; |
| 1471 | Visit(Ex, Pred, Tmp); |
| 1472 | |
| 1473 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1474 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1475 | ValueState* St = GetState(*I); |
| 1476 | RVal location = GetRVal(St, Ex); |
| 1477 | |
| 1478 | if (asLVal) |
| 1479 | MakeNode(Dst, U, *I, SetRVal(St, U, location)); |
| 1480 | else |
| 1481 | EvalLoad(Dst, Ex, *I, St, location); |
| 1482 | } |
| 1483 | |
| 1484 | return; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1487 | case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH. |
| 1488 | case UnaryOperator::Extension: { |
| 1489 | |
| 1490 | // Unary "+" is a no-op, similar to a parentheses. We still have places |
| 1491 | // where it may be a block-level expression, so we need to |
| 1492 | // generate an extra node that just propagates the value of the |
| 1493 | // subexpression. |
| 1494 | |
| 1495 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
| 1496 | NodeSet Tmp; |
| 1497 | Visit(Ex, Pred, Tmp); |
| 1498 | |
| 1499 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 1500 | ValueState* St = GetState(*I); |
| 1501 | MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex))); |
| 1502 | } |
| 1503 | |
| 1504 | return; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1505 | } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1506 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1507 | case UnaryOperator::AddrOf: { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1508 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1509 | assert (!asLVal); |
| 1510 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
| 1511 | NodeSet Tmp; |
| 1512 | VisitLVal(Ex, Pred, Tmp); |
| 1513 | |
| 1514 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 1515 | ValueState* St = GetState(*I); |
| 1516 | RVal V = GetRVal(St, Ex); |
| 1517 | St = SetRVal(St, U, V); |
| 1518 | MakeNode(Dst, U, *I, St); |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1519 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1520 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | case UnaryOperator::LNot: |
| 1525 | case UnaryOperator::Minus: |
| 1526 | case UnaryOperator::Not: { |
| 1527 | |
| 1528 | assert (!asLVal); |
| 1529 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
| 1530 | NodeSet Tmp; |
| 1531 | Visit(Ex, Pred, Tmp); |
| 1532 | |
| 1533 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 1534 | ValueState* St = GetState(*I); |
| 1535 | RVal V = GetRVal(St, Ex); |
| 1536 | |
| 1537 | if (V.isUnknownOrUndef()) { |
| 1538 | MakeNode(Dst, U, *I, SetRVal(St, U, V)); |
| 1539 | continue; |
| 1540 | } |
| 1541 | |
| 1542 | switch (U->getOpcode()) { |
| 1543 | default: |
| 1544 | assert(false && "Invalid Opcode."); |
| 1545 | break; |
| 1546 | |
| 1547 | case UnaryOperator::Not: |
| 1548 | St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V))); |
| 1549 | break; |
| 1550 | |
| 1551 | case UnaryOperator::Minus: |
| 1552 | St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V))); |
| 1553 | break; |
| 1554 | |
| 1555 | case UnaryOperator::LNot: |
| 1556 | |
| 1557 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 1558 | // |
| 1559 | // Note: technically we do "E == 0", but this is the same in the |
| 1560 | // transfer functions as "0 == E". |
| 1561 | |
| 1562 | if (isa<LVal>(V)) { |
| 1563 | lval::ConcreteInt X(BasicVals.getZeroWithPtrWidth()); |
| 1564 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X); |
| 1565 | St = SetRVal(St, U, Result); |
| 1566 | } |
| 1567 | else { |
| 1568 | nonlval::ConcreteInt X(BasicVals.getValue(0, Ex->getType())); |
| 1569 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X); |
| 1570 | St = SetRVal(St, U, Result); |
| 1571 | } |
| 1572 | |
| 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | MakeNode(Dst, U, *I, St); |
| 1577 | } |
| 1578 | |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | case UnaryOperator::SizeOf: { |
| 1583 | |
| 1584 | QualType T = U->getSubExpr()->getType(); |
| 1585 | |
| 1586 | // FIXME: Add support for VLAs. |
| 1587 | |
| 1588 | if (!T.getTypePtr()->isConstantSizeType()) |
| 1589 | return; |
| 1590 | |
| 1591 | uint64_t size = getContext().getTypeSize(T) / 8; |
| 1592 | ValueState* St = GetState(Pred); |
| 1593 | St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType())); |
| 1594 | |
| 1595 | MakeNode(Dst, U, Pred, St); |
| 1596 | return; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | // Handle ++ and -- (both pre- and post-increment). |
| 1601 | |
| 1602 | assert (U->isIncrementDecrementOp()); |
| 1603 | NodeSet Tmp; |
| 1604 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
| 1605 | VisitLVal(Ex, Pred, Tmp); |
| 1606 | |
| 1607 | for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) { |
| 1608 | |
| 1609 | ValueState* St = GetState(*I); |
| 1610 | RVal V1 = GetRVal(St, Ex); |
| 1611 | |
| 1612 | // Perform a load. |
| 1613 | NodeSet Tmp2; |
| 1614 | EvalLoad(Tmp2, Ex, *I, St, V1); |
| 1615 | |
| 1616 | for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) { |
| 1617 | |
| 1618 | St = GetState(*I2); |
| 1619 | RVal V2 = GetRVal(St, Ex); |
| 1620 | |
| 1621 | // Propagate unknown and undefined values. |
| 1622 | if (V2.isUnknownOrUndef()) { |
| 1623 | MakeNode(Dst, U, *I2, SetRVal(St, U, V2)); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1624 | continue; |
| 1625 | } |
| 1626 | |
Ted Kremenek | 443003b | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 1627 | // Handle all other values. |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 1628 | |
| 1629 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 1630 | : BinaryOperator::Sub; |
| 1631 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1632 | RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U)); |
| 1633 | St = SetRVal(St, U, U->isPostfix() ? V2 : Result); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1634 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1635 | // Perform the store. |
| 1636 | EvalStore(Dst, U, *I, St, V1, Result); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1637 | } |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1638 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 1641 | void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) { |
| 1642 | VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst); |
| 1643 | } |
| 1644 | |
| 1645 | void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A, |
| 1646 | AsmStmt::outputs_iterator I, |
| 1647 | AsmStmt::outputs_iterator E, |
| 1648 | NodeTy* Pred, NodeSet& Dst) { |
| 1649 | if (I == E) { |
| 1650 | VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst); |
| 1651 | return; |
| 1652 | } |
| 1653 | |
| 1654 | NodeSet Tmp; |
| 1655 | VisitLVal(*I, Pred, Tmp); |
| 1656 | |
| 1657 | ++I; |
| 1658 | |
| 1659 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1660 | VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst); |
| 1661 | } |
| 1662 | |
| 1663 | void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A, |
| 1664 | AsmStmt::inputs_iterator I, |
| 1665 | AsmStmt::inputs_iterator E, |
| 1666 | NodeTy* Pred, NodeSet& Dst) { |
| 1667 | if (I == E) { |
| 1668 | |
| 1669 | // We have processed both the inputs and the outputs. All of the outputs |
| 1670 | // should evaluate to LVals. Nuke all of their values. |
| 1671 | |
| 1672 | // FIXME: Some day in the future it would be nice to allow a "plug-in" |
| 1673 | // which interprets the inline asm and stores proper results in the |
| 1674 | // outputs. |
| 1675 | |
| 1676 | ValueState* St = GetState(Pred); |
| 1677 | |
| 1678 | for (AsmStmt::outputs_iterator OI = A->begin_outputs(), |
| 1679 | OE = A->end_outputs(); OI != OE; ++OI) { |
| 1680 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1681 | RVal X = GetRVal(St, *OI); |
| 1682 | assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef. |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 1683 | |
| 1684 | if (isa<LVal>(X)) |
| 1685 | St = SetRVal(St, cast<LVal>(X), UnknownVal()); |
| 1686 | } |
| 1687 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1688 | MakeNode(Dst, A, Pred, St); |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
| 1691 | |
| 1692 | NodeSet Tmp; |
| 1693 | Visit(*I, Pred, Tmp); |
| 1694 | |
| 1695 | ++I; |
| 1696 | |
| 1697 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1698 | VisitAsmStmtHelperInputs(A, I, E, *NI, Dst); |
| 1699 | } |
| 1700 | |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1701 | void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) { |
| 1702 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
| 1703 | |
| 1704 | unsigned size = Dst.size(); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1705 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1706 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 1707 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1708 | |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1709 | TF->EvalReturn(Dst, *this, *Builder, S, Pred); |
| 1710 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1711 | // Handle the case where no nodes where generated. |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1712 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1713 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1714 | MakeNode(Dst, S, Pred, GetState(Pred)); |
| 1715 | } |
| 1716 | |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 1717 | void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) { |
| 1718 | |
| 1719 | Expr* R = S->getRetValue(); |
| 1720 | |
| 1721 | if (!R) { |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1722 | EvalReturn(Dst, S, Pred); |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 1723 | return; |
| 1724 | } |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1725 | |
| 1726 | NodeSet DstRet; |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 1727 | QualType T = R->getType(); |
| 1728 | |
Chris Lattner | 423a3c9 | 2008-04-02 17:45:06 +0000 | [diff] [blame] | 1729 | if (T->isPointerLikeType()) { |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 1730 | |
| 1731 | // Check if any of the return values return the address of a stack variable. |
| 1732 | |
| 1733 | NodeSet Tmp; |
| 1734 | Visit(R, Pred, Tmp); |
| 1735 | |
| 1736 | for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
| 1737 | RVal X = GetRVal((*I)->getState(), R); |
| 1738 | |
| 1739 | if (isa<lval::DeclVal>(X)) { |
| 1740 | |
| 1741 | if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) { |
| 1742 | |
| 1743 | // Create a special node representing the v |
| 1744 | |
| 1745 | NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I); |
| 1746 | |
| 1747 | if (RetStackNode) { |
| 1748 | RetStackNode->markAsSink(); |
| 1749 | RetsStackAddr.insert(RetStackNode); |
| 1750 | } |
| 1751 | |
| 1752 | continue; |
| 1753 | } |
| 1754 | } |
| 1755 | |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1756 | DstRet.Add(*I); |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | else |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 1760 | Visit(R, Pred, DstRet); |
| 1761 | |
| 1762 | for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I) |
| 1763 | EvalReturn(Dst, S, *I); |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 1764 | } |
Ted Kremenek | 55deb97 | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1765 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1766 | //===----------------------------------------------------------------------===// |
| 1767 | // Transfer functions: Binary operators. |
| 1768 | //===----------------------------------------------------------------------===// |
| 1769 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1770 | bool GRExprEngine::CheckDivideZero(Expr* Ex, ValueState* St, |
| 1771 | NodeTy* Pred, RVal Denom) { |
| 1772 | |
| 1773 | // Divide by undefined? (potentially zero) |
| 1774 | |
| 1775 | if (Denom.isUndef()) { |
| 1776 | NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred); |
| 1777 | |
| 1778 | if (DivUndef) { |
| 1779 | DivUndef->markAsSink(); |
| 1780 | ExplicitBadDivides.insert(DivUndef); |
| 1781 | } |
| 1782 | |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
| 1786 | // Check for divide/remainder-by-zero. |
| 1787 | // First, "assume" that the denominator is 0 or undefined. |
| 1788 | |
| 1789 | bool isFeasibleZero = false; |
| 1790 | ValueState* ZeroSt = Assume(St, Denom, false, isFeasibleZero); |
| 1791 | |
| 1792 | // Second, "assume" that the denominator cannot be 0. |
| 1793 | |
| 1794 | bool isFeasibleNotZero = false; |
| 1795 | St = Assume(St, Denom, true, isFeasibleNotZero); |
| 1796 | |
| 1797 | // Create the node for the divide-by-zero (if it occurred). |
| 1798 | |
| 1799 | if (isFeasibleZero) |
| 1800 | if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) { |
| 1801 | DivZeroNode->markAsSink(); |
| 1802 | |
| 1803 | if (isFeasibleNotZero) |
| 1804 | ImplicitBadDivides.insert(DivZeroNode); |
| 1805 | else |
| 1806 | ExplicitBadDivides.insert(DivZeroNode); |
| 1807 | |
| 1808 | } |
| 1809 | |
| 1810 | return !isFeasibleNotZero; |
| 1811 | } |
| 1812 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1813 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1814 | GRExprEngine::NodeTy* Pred, |
| 1815 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1816 | |
| 1817 | NodeSet Tmp1; |
| 1818 | Expr* LHS = B->getLHS()->IgnoreParens(); |
| 1819 | Expr* RHS = B->getRHS()->IgnoreParens(); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1820 | |
| 1821 | if (B->isAssignmentOp()) |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1822 | VisitLVal(LHS, Pred, Tmp1); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1823 | else |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1824 | Visit(LHS, Pred, Tmp1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1825 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1826 | for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1827 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1828 | RVal LeftV = GetRVal((*I1)->getState(), LHS); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1829 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1830 | // Process the RHS. |
| 1831 | |
| 1832 | NodeSet Tmp2; |
| 1833 | Visit(RHS, *I1, Tmp2); |
| 1834 | |
| 1835 | // With both the LHS and RHS evaluated, process the operation itself. |
| 1836 | |
| 1837 | for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1838 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1839 | ValueState* St = GetState(*I2); |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1840 | RVal RightV = GetRVal(St, RHS); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1841 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 1842 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1843 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1844 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1845 | case BinaryOperator::Assign: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1846 | |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 1847 | // EXPERIMENTAL: "Conjured" symbols. |
| 1848 | |
| 1849 | if (RightV.isUnknown()) { |
| 1850 | unsigned Count = Builder->getCurrentBlockCount(); |
| 1851 | SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count); |
| 1852 | |
| 1853 | RightV = B->getRHS()->getType()->isPointerType() |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1854 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 1855 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 1858 | // Simulate the effects of a "store": bind the value of the RHS |
| 1859 | // to the L-Value represented by the LHS. |
Ted Kremenek | e38718e | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 1860 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1861 | EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV); |
Ted Kremenek | e38718e | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 1862 | continue; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1863 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1864 | |
| 1865 | case BinaryOperator::Div: |
| 1866 | case BinaryOperator::Rem: |
| 1867 | |
| 1868 | // Special checking for integer denominators. |
| 1869 | |
| 1870 | if (RHS->getType()->isIntegerType() |
| 1871 | && CheckDivideZero(B, St, *I2, RightV)) |
| 1872 | continue; |
| 1873 | |
| 1874 | // FALL-THROUGH. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1875 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1876 | default: { |
| 1877 | |
| 1878 | if (B->isAssignmentOp()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1879 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1880 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1881 | // Process non-assignements except commas or short-circuited |
| 1882 | // logical expressions (LAnd and LOr). |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1883 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1884 | RVal Result = EvalBinOp(Op, LeftV, RightV); |
| 1885 | |
| 1886 | if (Result.isUnknown()) { |
| 1887 | Dst.Add(*I2); |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1888 | continue; |
| 1889 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1890 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1891 | if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1892 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1893 | // The operands were *not* undefined, but the result is undefined. |
| 1894 | // This is a special node that should be flagged as an error. |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1895 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1896 | if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) { |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1897 | UndefNode->markAsSink(); |
| 1898 | UndefResults.insert(UndefNode); |
| 1899 | } |
| 1900 | |
| 1901 | continue; |
| 1902 | } |
| 1903 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1904 | // Otherwise, create a new node. |
| 1905 | |
| 1906 | MakeNode(Dst, B, *I2, SetRVal(St, B, Result)); |
Ted Kremenek | e38718e | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 1907 | continue; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1908 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1909 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1910 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1911 | assert (B->isCompoundAssignmentOp()); |
| 1912 | |
| 1913 | if (Op >= BinaryOperator::AndAssign) |
| 1914 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 1915 | else |
| 1916 | ((int&) Op) -= BinaryOperator::MulAssign; |
| 1917 | |
| 1918 | // Perform a load (the LHS). This performs the checks for |
| 1919 | // null dereferences, and so on. |
| 1920 | NodeSet Tmp3; |
| 1921 | RVal location = GetRVal(St, LHS); |
| 1922 | EvalLoad(Tmp3, LHS, *I2, St, location); |
| 1923 | |
| 1924 | for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) { |
| 1925 | |
| 1926 | St = GetState(*I3); |
| 1927 | RVal V = GetRVal(St, LHS); |
| 1928 | |
| 1929 | // Propagate undefined values (left-side). |
| 1930 | if (V.isUndef()) { |
| 1931 | EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V); |
| 1932 | continue; |
| 1933 | } |
| 1934 | |
| 1935 | // Propagate unknown values (left and right-side). |
| 1936 | if (RightV.isUnknown() || V.isUnknown()) { |
| 1937 | EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location, |
| 1938 | UnknownVal()); |
| 1939 | continue; |
| 1940 | } |
| 1941 | |
| 1942 | // At this point: |
| 1943 | // |
| 1944 | // The LHS is not Undef/Unknown. |
| 1945 | // The RHS is not Unknown. |
| 1946 | |
| 1947 | // Get the computation type. |
| 1948 | QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType(); |
| 1949 | |
| 1950 | // Perform promotions. |
| 1951 | V = EvalCast(V, CTy); |
| 1952 | RightV = EvalCast(RightV, CTy); |
| 1953 | |
| 1954 | // Evaluate operands and promote to result type. |
| 1955 | |
| 1956 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1957 | && RHS->getType()->isIntegerType()) { |
| 1958 | |
| 1959 | if (CheckDivideZero(B, St, *I3, RightV)) |
| 1960 | continue; |
| 1961 | } |
| 1962 | else if (RightV.isUndef()) { |
| 1963 | |
| 1964 | // Propagate undefined values (right-side). |
| 1965 | |
| 1966 | EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV); |
| 1967 | continue; |
| 1968 | } |
| 1969 | |
| 1970 | // Compute the result of the operation. |
| 1971 | |
| 1972 | RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType()); |
| 1973 | |
| 1974 | if (Result.isUndef()) { |
| 1975 | |
| 1976 | // The operands were not undefined, but the result is undefined. |
| 1977 | |
| 1978 | if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) { |
| 1979 | UndefNode->markAsSink(); |
| 1980 | UndefResults.insert(UndefNode); |
| 1981 | } |
| 1982 | |
| 1983 | continue; |
| 1984 | } |
| 1985 | |
| 1986 | EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result); |
| 1987 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1988 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1989 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1990 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1991 | |
| 1992 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1993 | // "Assume" logic. |
| 1994 | //===----------------------------------------------------------------------===// |
| 1995 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1996 | ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond, |
Ted Kremenek | 550a0f9 | 2008-04-18 17:20:23 +0000 | [diff] [blame] | 1997 | bool Assumption, bool& isFeasible) { |
| 1998 | |
| 1999 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2000 | |
| 2001 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 2002 | : St; |
Ted Kremenek | 550a0f9 | 2008-04-18 17:20:23 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | ValueState* GRExprEngine::AssumeAux(ValueState* St, LVal Cond, |
| 2006 | bool Assumption, bool& isFeasible) { |
| 2007 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 2008 | switch (Cond.getSubKind()) { |
| 2009 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2010 | assert (false && "'Assume' not implemented for this LVal."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 2011 | return St; |
Ted Kremenek | 550a0f9 | 2008-04-18 17:20:23 +0000 | [diff] [blame] | 2012 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2013 | case lval::SymbolValKind: |
| 2014 | if (Assumption) |
| 2015 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 2016 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2017 | else |
| 2018 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 2019 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
Ted Kremenek | 550a0f9 | 2008-04-18 17:20:23 +0000 | [diff] [blame] | 2020 | |
| 2021 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 2022 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 2023 | case lval::FuncValKind: |
| 2024 | case lval::GotoLabelKind: |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 2025 | case lval::StringLiteralValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 2026 | isFeasible = Assumption; |
| 2027 | return St; |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 2028 | |
| 2029 | case lval::FieldOffsetKind: |
| 2030 | return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(), |
| 2031 | Assumption, isFeasible); |
| 2032 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 2033 | case lval::ConcreteIntKind: { |
| 2034 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 2035 | isFeasible = b ? Assumption : !Assumption; |
| 2036 | return St; |
| 2037 | } |
| 2038 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2041 | ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond, |
Ted Kremenek | 550a0f9 | 2008-04-18 17:20:23 +0000 | [diff] [blame] | 2042 | bool Assumption, bool& isFeasible) { |
| 2043 | |
| 2044 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2045 | |
| 2046 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 2047 | : St; |
Ted Kremenek | 550a0f9 | 2008-04-18 17:20:23 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | ValueState* GRExprEngine::AssumeAux(ValueState* St, NonLVal Cond, |
| 2051 | bool Assumption, bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2052 | switch (Cond.getSubKind()) { |
| 2053 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2054 | assert (false && "'Assume' not implemented for this NonLVal."); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2055 | return St; |
| 2056 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 2057 | |
| 2058 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 2059 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 2060 | SymbolID sym = SV.getSymbol(); |
| 2061 | |
| 2062 | if (Assumption) |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 2063 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 2064 | isFeasible); |
| 2065 | else |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 2066 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 2067 | isFeasible); |
| 2068 | } |
| 2069 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 2070 | case nonlval::SymIntConstraintValKind: |
| 2071 | return |
| 2072 | AssumeSymInt(St, Assumption, |
| 2073 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 2074 | isFeasible); |
| 2075 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 2076 | case nonlval::ConcreteIntKind: { |
| 2077 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2078 | isFeasible = b ? Assumption : !Assumption; |
| 2079 | return St; |
| 2080 | } |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 2081 | |
| 2082 | case nonlval::LValAsIntegerKind: { |
| 2083 | return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(), |
| 2084 | Assumption, isFeasible); |
| 2085 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2089 | ValueState* |
| 2090 | GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2091 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 2092 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2093 | // First, determine if sym == X, where X != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2094 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2095 | isFeasible = *X != V; |
| 2096 | return St; |
| 2097 | } |
| 2098 | |
| 2099 | // Second, determine if sym != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2100 | if (St->isNotEqual(sym, V)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2101 | isFeasible = true; |
| 2102 | return St; |
| 2103 | } |
| 2104 | |
| 2105 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 2106 | // Make that assumption. |
| 2107 | |
| 2108 | isFeasible = true; |
| 2109 | return StateMgr.AddNE(St, sym, V); |
| 2110 | } |
| 2111 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2112 | ValueState* |
| 2113 | GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2114 | const llvm::APSInt& V, bool& isFeasible) { |
| 2115 | |
| 2116 | // First, determine if sym == X, where X != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2117 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2118 | isFeasible = *X == V; |
| 2119 | return St; |
| 2120 | } |
| 2121 | |
| 2122 | // Second, determine if sym != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2123 | if (St->isNotEqual(sym, V)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 2124 | isFeasible = false; |
| 2125 | return St; |
| 2126 | } |
| 2127 | |
| 2128 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 2129 | // Make that assumption. |
| 2130 | |
| 2131 | isFeasible = true; |
| 2132 | return StateMgr.AddEQ(St, sym, V); |
| 2133 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2134 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2135 | ValueState* |
| 2136 | GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 2137 | const SymIntConstraint& C, bool& isFeasible) { |
| 2138 | |
| 2139 | switch (C.getOpcode()) { |
| 2140 | default: |
| 2141 | // No logic yet for other operators. |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 2142 | isFeasible = true; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 2143 | return St; |
| 2144 | |
| 2145 | case BinaryOperator::EQ: |
| 2146 | if (Assumption) |
| 2147 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 2148 | else |
| 2149 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 2150 | |
| 2151 | case BinaryOperator::NE: |
| 2152 | if (Assumption) |
| 2153 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 2154 | else |
| 2155 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 2156 | } |
| 2157 | } |
| 2158 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2159 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 2160 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2161 | //===----------------------------------------------------------------------===// |
| 2162 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2163 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2164 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2165 | static SourceManager* GraphPrintSourceManager; |
Ted Kremenek | 75da3e8 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2166 | static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2167 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2168 | namespace llvm { |
| 2169 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2170 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2171 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2172 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2173 | static void PrintVarBindings(std::ostream& Out, ValueState* St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2174 | |
| 2175 | Out << "Variables:\\l"; |
| 2176 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 2177 | bool isFirst = true; |
| 2178 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2179 | 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] | 2180 | |
| 2181 | if (isFirst) |
| 2182 | isFirst = false; |
| 2183 | else |
| 2184 | Out << "\\l"; |
| 2185 | |
| 2186 | Out << ' ' << I.getKey()->getName() << " : "; |
| 2187 | I.getData().print(Out); |
| 2188 | } |
| 2189 | |
| 2190 | } |
| 2191 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 2192 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2193 | static void PrintSubExprBindings(std::ostream& Out, ValueState* St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 2194 | |
| 2195 | bool isFirst = true; |
| 2196 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2197 | 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] | 2198 | |
| 2199 | if (isFirst) { |
| 2200 | Out << "\\l\\lSub-Expressions:\\l"; |
| 2201 | isFirst = false; |
| 2202 | } |
| 2203 | else |
| 2204 | Out << "\\l"; |
| 2205 | |
| 2206 | Out << " (" << (void*) I.getKey() << ") "; |
| 2207 | I.getKey()->printPretty(Out); |
| 2208 | Out << " : "; |
| 2209 | I.getData().print(Out); |
| 2210 | } |
| 2211 | } |
| 2212 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2213 | static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 2214 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2215 | bool isFirst = true; |
| 2216 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2217 | 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] | 2218 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 2219 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 2220 | isFirst = false; |
| 2221 | } |
| 2222 | else |
| 2223 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2224 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 2225 | Out << " (" << (void*) I.getKey() << ") "; |
| 2226 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 2227 | Out << " : "; |
| 2228 | I.getData().print(Out); |
| 2229 | } |
| 2230 | } |
| 2231 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2232 | static void PrintEQ(std::ostream& Out, ValueState* St) { |
| 2233 | ValueState::ConstEqTy CE = St->ConstEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 2234 | |
| 2235 | if (CE.isEmpty()) |
| 2236 | return; |
| 2237 | |
| 2238 | Out << "\\l\\|'==' constraints:"; |
| 2239 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2240 | 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] | 2241 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 2242 | } |
| 2243 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2244 | static void PrintNE(std::ostream& Out, ValueState* St) { |
| 2245 | ValueState::ConstNotEqTy NE = St->ConstNotEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 2246 | |
| 2247 | if (NE.isEmpty()) |
| 2248 | return; |
| 2249 | |
| 2250 | Out << "\\l\\|'!=' constraints:"; |
| 2251 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2252 | for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 2253 | I != EI; ++I){ |
| 2254 | |
| 2255 | Out << "\\l $" << I.getKey() << " : "; |
| 2256 | bool isFirst = true; |
| 2257 | |
| 2258 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 2259 | EJ=I.getData().end(); |
| 2260 | for ( ; J != EJ; ++J) { |
| 2261 | if (isFirst) isFirst = false; |
| 2262 | else Out << ", "; |
| 2263 | |
| 2264 | Out << (*J)->toString(); |
| 2265 | } |
| 2266 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 2270 | |
| 2271 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 2272 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2273 | GraphPrintCheckerState->isUndefDeref(N) || |
| 2274 | GraphPrintCheckerState->isUndefStore(N) || |
| 2275 | GraphPrintCheckerState->isUndefControlFlow(N) || |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 2276 | GraphPrintCheckerState->isExplicitBadDivide(N) || |
| 2277 | GraphPrintCheckerState->isImplicitBadDivide(N) || |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2278 | GraphPrintCheckerState->isUndefResult(N) || |
Ted Kremenek | 2ded35a | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 2279 | GraphPrintCheckerState->isBadCall(N) || |
| 2280 | GraphPrintCheckerState->isUndefArg(N)) |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2281 | return "color=\"red\",style=\"filled\""; |
| 2282 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 2283 | if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 2284 | return "color=\"blue\",style=\"filled\""; |
| 2285 | |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2286 | return ""; |
| 2287 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 2288 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2289 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2290 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2291 | |
| 2292 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2293 | ProgramPoint Loc = N->getLocation(); |
| 2294 | |
| 2295 | switch (Loc.getKind()) { |
| 2296 | case ProgramPoint::BlockEntranceKind: |
| 2297 | Out << "Block Entrance: B" |
| 2298 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 2299 | break; |
| 2300 | |
| 2301 | case ProgramPoint::BlockExitKind: |
| 2302 | assert (false); |
| 2303 | break; |
| 2304 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2305 | case ProgramPoint::PostLoadKind: |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2306 | case ProgramPoint::PostStmtKind: { |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2307 | const PostStmt& L = cast<PostStmt>(Loc); |
| 2308 | Stmt* S = L.getStmt(); |
| 2309 | SourceLocation SLoc = S->getLocStart(); |
| 2310 | |
| 2311 | Out << S->getStmtClassName() << ' ' << (void*) S << ' '; |
| 2312 | S->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 2313 | |
Ted Kremenek | 9b5551d | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 2314 | if (SLoc.isFileID()) { |
| 2315 | Out << "\\lline=" |
| 2316 | << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" |
| 2317 | << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l"; |
| 2318 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 2319 | |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2320 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 2321 | Out << "\\|Implicit-Null Dereference.\\l"; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2322 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 2323 | Out << "\\|Explicit-Null Dereference.\\l"; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2324 | else if (GraphPrintCheckerState->isUndefDeref(N)) |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2325 | Out << "\\|Dereference of undefialied value.\\l"; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2326 | else if (GraphPrintCheckerState->isUndefStore(N)) |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2327 | Out << "\\|Store to Undefined LVal."; |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 2328 | else if (GraphPrintCheckerState->isExplicitBadDivide(N)) |
| 2329 | Out << "\\|Explicit divide-by zero or undefined value."; |
| 2330 | else if (GraphPrintCheckerState->isImplicitBadDivide(N)) |
| 2331 | Out << "\\|Implicit divide-by zero or undefined value."; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2332 | else if (GraphPrintCheckerState->isUndefResult(N)) |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 2333 | Out << "\\|Result of operation is undefined."; |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 2334 | else if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 2335 | Out << "\\|Call to function marked \"noreturn\"."; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2336 | else if (GraphPrintCheckerState->isBadCall(N)) |
| 2337 | Out << "\\|Call to NULL/Undefined."; |
Ted Kremenek | 2ded35a | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 2338 | else if (GraphPrintCheckerState->isUndefArg(N)) |
| 2339 | Out << "\\|Argument in call is undefined"; |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 2340 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | |
| 2344 | default: { |
| 2345 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 2346 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 2347 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2348 | |
| 2349 | if (Stmt* T = E.getSrc()->getTerminator()) { |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2350 | |
| 2351 | SourceLocation SLoc = T->getLocStart(); |
| 2352 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2353 | Out << "\\|Terminator: "; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2354 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2355 | E.getSrc()->printTerminator(Out); |
| 2356 | |
Ted Kremenek | 9b5551d | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 2357 | if (SLoc.isFileID()) { |
| 2358 | Out << "\\lline=" |
| 2359 | << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" |
| 2360 | << GraphPrintSourceManager->getColumnNumber(SLoc); |
| 2361 | } |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2362 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 2363 | if (isa<SwitchStmt>(T)) { |
| 2364 | Stmt* Label = E.getDst()->getLabel(); |
| 2365 | |
| 2366 | if (Label) { |
| 2367 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 2368 | Out << "\\lcase "; |
| 2369 | C->getLHS()->printPretty(Out); |
| 2370 | |
| 2371 | if (Stmt* RHS = C->getRHS()) { |
| 2372 | Out << " .. "; |
| 2373 | RHS->printPretty(Out); |
| 2374 | } |
| 2375 | |
| 2376 | Out << ":"; |
| 2377 | } |
| 2378 | else { |
| 2379 | assert (isa<DefaultStmt>(Label)); |
| 2380 | Out << "\\ldefault:"; |
| 2381 | } |
| 2382 | } |
| 2383 | else |
| 2384 | Out << "\\l(implicit) default:"; |
| 2385 | } |
| 2386 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2387 | // FIXME |
| 2388 | } |
| 2389 | else { |
| 2390 | Out << "\\lCondition: "; |
| 2391 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 2392 | Out << "true"; |
| 2393 | else |
| 2394 | Out << "false"; |
| 2395 | } |
| 2396 | |
| 2397 | Out << "\\l"; |
| 2398 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2399 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2400 | if (GraphPrintCheckerState->isUndefControlFlow(N)) { |
| 2401 | Out << "\\|Control-flow based on\\lUndefined value.\\l"; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2402 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2403 | } |
| 2404 | } |
| 2405 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2406 | Out << "\\|StateID: " << (void*) N->getState() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2407 | |
Ted Kremenek | 75da3e8 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2408 | N->getState()->printDOT(Out, GraphCheckerStatePrinter); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2409 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2410 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2411 | return Out.str(); |
| 2412 | } |
| 2413 | }; |
| 2414 | } // end llvm namespace |
| 2415 | #endif |
| 2416 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2417 | #ifndef NDEBUG |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2418 | |
| 2419 | template <typename ITERATOR> |
| 2420 | GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; } |
| 2421 | |
| 2422 | template <> |
| 2423 | GRExprEngine::NodeTy* |
| 2424 | GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator> |
| 2425 | (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) { |
| 2426 | return I->first; |
| 2427 | } |
| 2428 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2429 | template <typename ITERATOR> |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2430 | static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources, |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2431 | ITERATOR I, ITERATOR E) { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2432 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2433 | llvm::SmallPtrSet<void*,10> CachedSources; |
| 2434 | |
| 2435 | for ( ; I != E; ++I ) { |
| 2436 | GRExprEngine::NodeTy* N = GetGraphNode(I); |
| 2437 | void* p = N->getLocation().getRawData(); |
| 2438 | |
| 2439 | if (CachedSources.count(p)) |
| 2440 | continue; |
| 2441 | |
| 2442 | CachedSources.insert(p); |
| 2443 | |
| 2444 | Sources.push_back(N); |
| 2445 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2446 | } |
| 2447 | #endif |
| 2448 | |
| 2449 | void GRExprEngine::ViewGraph(bool trim) { |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2450 | #ifndef NDEBUG |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2451 | if (trim) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2452 | std::vector<NodeTy*> Src; |
| 2453 | |
| 2454 | // Fixme: Migrate over to the new way of adding nodes. |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2455 | AddSources(Src, null_derefs_begin(), null_derefs_end()); |
| 2456 | AddSources(Src, undef_derefs_begin(), undef_derefs_end()); |
| 2457 | AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end()); |
| 2458 | AddSources(Src, undef_results_begin(), undef_results_end()); |
| 2459 | AddSources(Src, bad_calls_begin(), bad_calls_end()); |
| 2460 | AddSources(Src, undef_arg_begin(), undef_arg_end()); |
Ted Kremenek | 1b9df4c | 2008-03-14 18:14:50 +0000 | [diff] [blame] | 2461 | AddSources(Src, undef_branches_begin(), undef_branches_end()); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2462 | |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2463 | // The new way. |
| 2464 | for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 2465 | (*I)->GetErrorNodes(Src); |
| 2466 | |
| 2467 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2468 | ViewGraph(&Src[0], &Src[0]+Src.size()); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2469 | } |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2470 | else { |
| 2471 | GraphPrintCheckerState = this; |
| 2472 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | 75da3e8 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2473 | GraphCheckerStatePrinter = TF->getCheckerStatePrinter(); |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2474 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2475 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2476 | |
| 2477 | GraphPrintCheckerState = NULL; |
| 2478 | GraphPrintSourceManager = NULL; |
Ted Kremenek | 75da3e8 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2479 | GraphCheckerStatePrinter = NULL; |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2480 | } |
| 2481 | #endif |
| 2482 | } |
| 2483 | |
| 2484 | void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) { |
| 2485 | #ifndef NDEBUG |
| 2486 | GraphPrintCheckerState = this; |
| 2487 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | 75da3e8 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2488 | GraphCheckerStatePrinter = TF->getCheckerStatePrinter(); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2489 | |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2490 | GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End); |
| 2491 | |
| 2492 | if (!TrimmedG) |
| 2493 | llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n"; |
| 2494 | else { |
| 2495 | llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine"); |
| 2496 | delete TrimmedG; |
| 2497 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2498 | |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2499 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2500 | GraphPrintSourceManager = NULL; |
Ted Kremenek | 75da3e8 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2501 | GraphCheckerStatePrinter = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 2502 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2503 | } |