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