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 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/PathSensitive/Environment.h" |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Streams.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(); |
| 53 | QualType ST = C->getSubExpr()->getType(); |
| 54 | |
| 55 | if (CT->isVoidType()) |
| 56 | return UnknownVal(); |
| 57 | |
| 58 | break; |
| 59 | } |
| 60 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame^] | 61 | // Handle all other Stmt* using a lookup. |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 62 | |
| 63 | default: |
| 64 | break; |
| 65 | }; |
| 66 | |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | return LookupExpr(E); |
| 71 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame^] | 73 | SVal Environment::GetBlkExprSVal(Stmt* E, BasicValueFactory& BasicVals) const { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame^] | 75 | while (1) { |
| 76 | switch (E->getStmtClass()) { |
| 77 | case Stmt::ParenExprClass: |
| 78 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 79 | continue; |
| 80 | |
| 81 | case Stmt::CharacterLiteralClass: { |
| 82 | CharacterLiteral* C = cast<CharacterLiteral>(E); |
| 83 | return NonLoc::MakeVal(BasicVals, C->getValue(), C->getType()); |
| 84 | } |
| 85 | |
| 86 | case Stmt::IntegerLiteralClass: { |
| 87 | return NonLoc::MakeVal(BasicVals, cast<IntegerLiteral>(E)); |
| 88 | } |
| 89 | |
| 90 | default: |
| 91 | return LookupBlkExpr(E); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame^] | 96 | Environment EnvironmentManager::BindExpr(const Environment& Env, Stmt* E,SVal V, |
Zhongxing Xu | 8cd5aae | 2008-10-30 05:33:54 +0000 | [diff] [blame] | 97 | bool isBlkExpr, 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 | |
| 110 | Environment |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 111 | EnvironmentManager::RemoveDeadBindings(Environment Env, Stmt* Loc, |
| 112 | const LiveVariables& Liveness, |
| 113 | llvm::SmallVectorImpl<const MemRegion*>& DRoots, |
| 114 | StoreManager::LiveSymbolsTy& LSymbols) { |
| 115 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 116 | // Drop bindings for subexpressions. |
| 117 | Env = RemoveSubExprBindings(Env); |
| 118 | |
| 119 | // Iterate over the block-expr bindings. |
| 120 | for (Environment::beb_iterator I = Env.beb_begin(), E = Env.beb_end(); |
| 121 | I != E; ++I) { |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame^] | 122 | Stmt* BlkExpr = I.getKey(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 123 | |
| 124 | if (Liveness.isLive(Loc, BlkExpr)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 125 | SVal X = I.getData(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 127 | // 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] | 128 | if (isa<loc::MemRegionVal>(X)) |
| 129 | DRoots.push_back(cast<loc::MemRegionVal>(X).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 131 | |
| 132 | // Mark all symbols in the block expr's value. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 133 | for (SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 134 | SI != SE; ++SI) { |
| 135 | LSymbols.insert(*SI); |
| 136 | } |
| 137 | } else { |
| 138 | // The block expr is dead. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 139 | SVal X = I.getData(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 140 | |
Zhongxing Xu | a950fe2 | 2008-08-21 23:00:21 +0000 | [diff] [blame] | 141 | // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the |
| 142 | // beginning of itself, but we need its UndefinedVal to determine its |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 143 | // SVal. |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 144 | |
| 145 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
| 146 | continue; |
| 147 | |
| 148 | Env = RemoveBlkExpr(Env, BlkExpr); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return Env; |
| 153 | } |