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 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 21 | SVal Environment::GetSVal(Stmt* E, BasicValueFactory& BasicVals) 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 | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 28 | return Loc::MakeVal(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: { |
| 37 | CharacterLiteral* C = cast<CharacterLiteral>(E); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 38 | return NonLoc::MakeVal(BasicVals, C->getValue(), C->getType()); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | case Stmt::IntegerLiteralClass: { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 42 | return NonLoc::MakeVal(BasicVals, 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 | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 51 | CastExpr* C = cast<CastExpr>(E); |
| 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 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 72 | SVal Environment::GetBlkExprSVal(Stmt* E, BasicValueFactory& BasicVals) 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: { |
| 81 | CharacterLiteral* C = cast<CharacterLiteral>(E); |
| 82 | return NonLoc::MakeVal(BasicVals, C->getValue(), C->getType()); |
| 83 | } |
| 84 | |
| 85 | case Stmt::IntegerLiteralClass: { |
| 86 | return NonLoc::MakeVal(BasicVals, cast<IntegerLiteral>(E)); |
| 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 | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 95 | Environment EnvironmentManager::BindExpr(const Environment& Env, Stmt* E,SVal V, |
Zhongxing Xu | 8cd5aae | 2008-10-30 05:33:54 +0000 | [diff] [blame] | 96 | bool isBlkExpr, bool Invalidate) { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 97 | assert (E); |
| 98 | |
| 99 | if (V.isUnknown()) { |
| 100 | if (Invalidate) |
| 101 | return isBlkExpr ? RemoveBlkExpr(Env, E) : RemoveSubExpr(Env, E); |
| 102 | else |
| 103 | return Env; |
| 104 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 106 | return isBlkExpr ? AddBlkExpr(Env, E, V) : AddSubExpr(Env, E, V); |
| 107 | } |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame^] | 109 | namespace { |
| 110 | class VISIBILITY_HIDDEN MarkLiveCallback : public SymbolVisitor { |
| 111 | SymbolReaper &SymReaper; |
| 112 | public: |
| 113 | MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {} |
| 114 | bool VisitSymbol(SymbolRef sym) { SymReaper.markLive(sym); return true; } |
| 115 | }; |
| 116 | } // end anonymous namespace |
| 117 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 118 | Environment |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 119 | EnvironmentManager::RemoveDeadBindings(Environment Env, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 120 | SymbolReaper& SymReaper, |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame^] | 121 | GRStateManager& StateMgr, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 122 | llvm::SmallVectorImpl<const MemRegion*>& DRoots) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 124 | // Drop bindings for subexpressions. |
| 125 | Env = RemoveSubExprBindings(Env); |
| 126 | |
| 127 | // Iterate over the block-expr bindings. |
| 128 | for (Environment::beb_iterator I = Env.beb_begin(), E = Env.beb_end(); |
| 129 | I != E; ++I) { |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 130 | Stmt* BlkExpr = I.getKey(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 132 | if (SymReaper.isLive(Loc, BlkExpr)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 133 | SVal X = I.getData(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 135 | // 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] | 136 | if (isa<loc::MemRegionVal>(X)) |
| 137 | DRoots.push_back(cast<loc::MemRegionVal>(X).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame^] | 139 | // Mark all symbols in the block expr's value live. |
| 140 | MarkLiveCallback cb(SymReaper); |
| 141 | StateMgr.scanReachableSymbols(X, cb); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 142 | } else { |
| 143 | // The block expr is dead. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 144 | SVal X = I.getData(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 145 | |
Zhongxing Xu | a950fe2 | 2008-08-21 23:00:21 +0000 | [diff] [blame] | 146 | // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the |
| 147 | // beginning of itself, but we need its UndefinedVal to determine its |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 148 | // SVal. |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 149 | |
| 150 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
| 151 | continue; |
| 152 | |
| 153 | Env = RemoveBlkExpr(Env, BlkExpr); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return Env; |
| 158 | } |