Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 1 | //== Environment.cpp - Map from Stmt* to Locations/Values -------*- C++ -*--==// |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defined the Environment and EnvironmentManager classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 13 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 21 | SVal Environment::GetSVal(const Stmt *E, ValueManager& ValMgr) const { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 22 | |
| 23 | for (;;) { |
| 24 | |
| 25 | switch (E->getStmtClass()) { |
| 26 | |
| 27 | case Stmt::AddrLabelExprClass: |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 28 | return ValMgr.makeLoc(cast<AddrLabelExpr>(E)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 29 | |
| 30 | // ParenExprs are no-ops. |
| 31 | |
| 32 | case Stmt::ParenExprClass: |
| 33 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 34 | continue; |
| 35 | |
| 36 | case Stmt::CharacterLiteralClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 37 | const CharacterLiteral* C = cast<CharacterLiteral>(E); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 38 | return ValMgr.makeIntVal(C->getValue(), C->getType()); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | case Stmt::IntegerLiteralClass: { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 42 | return ValMgr.makeIntVal(cast<IntegerLiteral>(E)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 45 | // Casts where the source and target type are the same |
| 46 | // are no-ops. We blast through these to get the descendant |
| 47 | // subexpression that has a value. |
| 48 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 49 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 50 | case Stmt::CStyleCastExprClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 51 | const CastExpr* C = cast<CastExpr>(E); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 52 | QualType CT = C->getType(); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 53 | |
| 54 | if (CT->isVoidType()) |
| 55 | return UnknownVal(); |
| 56 | |
| 57 | break; |
| 58 | } |
| 59 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 60 | // Handle all other Stmt* using a lookup. |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 61 | |
| 62 | default: |
| 63 | break; |
| 64 | }; |
| 65 | |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | return LookupExpr(E); |
| 70 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 71 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 72 | SVal Environment::GetBlkExprSVal(const Stmt *E, ValueManager& ValMgr) const { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 74 | while (1) { |
| 75 | switch (E->getStmtClass()) { |
| 76 | case Stmt::ParenExprClass: |
| 77 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 78 | continue; |
| 79 | |
| 80 | case Stmt::CharacterLiteralClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 81 | const CharacterLiteral* C = cast<CharacterLiteral>(E); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 82 | return ValMgr.makeIntVal(C->getValue(), C->getType()); |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | case Stmt::IntegerLiteralClass: { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame^] | 86 | return ValMgr.makeIntVal(cast<IntegerLiteral>(E)); |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | default: |
| 90 | return LookupBlkExpr(E); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 91 | } |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 95 | Environment EnvironmentManager::BindExpr(const Environment& Env, const Stmt* E, |
| 96 | SVal V, bool isBlkExpr, |
| 97 | bool Invalidate) { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 98 | assert (E); |
| 99 | |
| 100 | if (V.isUnknown()) { |
| 101 | if (Invalidate) |
| 102 | return isBlkExpr ? RemoveBlkExpr(Env, E) : RemoveSubExpr(Env, E); |
| 103 | else |
| 104 | return Env; |
| 105 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 107 | return isBlkExpr ? AddBlkExpr(Env, E, V) : AddSubExpr(Env, E, V); |
| 108 | } |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 110 | namespace { |
| 111 | class VISIBILITY_HIDDEN MarkLiveCallback : public SymbolVisitor { |
| 112 | SymbolReaper &SymReaper; |
| 113 | public: |
| 114 | MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {} |
| 115 | bool VisitSymbol(SymbolRef sym) { SymReaper.markLive(sym); return true; } |
| 116 | }; |
| 117 | } // end anonymous namespace |
| 118 | |
Zhongxing Xu | 9d8d0fc | 2009-03-12 07:54:17 +0000 | [diff] [blame] | 119 | // RemoveDeadBindings: |
| 120 | // - Remove subexpression bindings. |
| 121 | // - Remove dead block expression bindings. |
| 122 | // - Keep live block expression bindings: |
| 123 | // - Mark their reachable symbols live in SymbolReaper, |
| 124 | // see ScanReachableSymbols. |
| 125 | // - Mark the region in DRoots if the binding is a loc::MemRegionVal. |
| 126 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 127 | Environment |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 128 | EnvironmentManager::RemoveDeadBindings(Environment Env, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 129 | SymbolReaper& SymReaper, |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 130 | GRStateManager& StateMgr, |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 131 | const GRState *state, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 132 | llvm::SmallVectorImpl<const MemRegion*>& DRoots) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 134 | // Drop bindings for subexpressions. |
| 135 | Env = RemoveSubExprBindings(Env); |
| 136 | |
| 137 | // Iterate over the block-expr bindings. |
| 138 | for (Environment::beb_iterator I = Env.beb_begin(), E = Env.beb_end(); |
| 139 | I != E; ++I) { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 140 | const Stmt *BlkExpr = I.getKey(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 142 | if (SymReaper.isLive(Loc, BlkExpr)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 143 | SVal X = I.getData(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 145 | // If the block expr's value is a memory region, then mark that region. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 146 | if (isa<loc::MemRegionVal>(X)) |
| 147 | DRoots.push_back(cast<loc::MemRegionVal>(X).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 149 | // Mark all symbols in the block expr's value live. |
| 150 | MarkLiveCallback cb(SymReaper); |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 151 | state->scanReachableSymbols(X, cb); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 152 | } else { |
| 153 | // The block expr is dead. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 154 | SVal X = I.getData(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 155 | |
Zhongxing Xu | a950fe2 | 2008-08-21 23:00:21 +0000 | [diff] [blame] | 156 | // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the |
| 157 | // beginning of itself, but we need its UndefinedVal to determine its |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 158 | // SVal. |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 159 | |
| 160 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
| 161 | continue; |
| 162 | |
| 163 | Env = RemoveBlkExpr(Env, BlkExpr); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return Env; |
| 168 | } |