Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 1 | //== Environment.cpp - Map from Expr* to Locations/Values -------*- C++ -*--==// |
| 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 | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 21 | RVal Environment::GetRVal(Expr* E, BasicValueFactory& BasicVals) const { |
| 22 | |
| 23 | for (;;) { |
| 24 | |
| 25 | switch (E->getStmtClass()) { |
| 26 | |
| 27 | case Stmt::AddrLabelExprClass: |
| 28 | return LVal::MakeVal(cast<AddrLabelExpr>(E)); |
| 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); |
| 38 | return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); |
| 39 | } |
| 40 | |
| 41 | case Stmt::IntegerLiteralClass: { |
| 42 | return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E)); |
| 43 | } |
| 44 | |
| 45 | case Stmt::StringLiteralClass: |
| 46 | return LVal::MakeVal(cast<StringLiteral>(E)); |
| 47 | |
| 48 | // Casts where the source and target type are the same |
| 49 | // are no-ops. We blast through these to get the descendant |
| 50 | // subexpression that has a value. |
| 51 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 52 | case Stmt::ImplicitCastExprClass: |
| 53 | case Stmt::ExplicitCastExprClass: { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 54 | CastExpr* C = cast<CastExpr>(E); |
| 55 | QualType CT = C->getType(); |
| 56 | QualType ST = C->getSubExpr()->getType(); |
| 57 | |
| 58 | if (CT->isVoidType()) |
| 59 | return UnknownVal(); |
| 60 | |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | // Handle all other Expr* using a lookup. |
| 65 | |
| 66 | default: |
| 67 | break; |
| 68 | }; |
| 69 | |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | return LookupExpr(E); |
| 74 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 76 | RVal Environment::GetBlkExprRVal(Expr* E, BasicValueFactory& BasicVals) const { |
| 77 | |
| 78 | E = E->IgnoreParens(); |
| 79 | |
| 80 | switch (E->getStmtClass()) { |
| 81 | case Stmt::CharacterLiteralClass: { |
| 82 | CharacterLiteral* C = cast<CharacterLiteral>(E); |
| 83 | return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); |
| 84 | } |
| 85 | |
| 86 | case Stmt::IntegerLiteralClass: { |
| 87 | return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E)); |
| 88 | } |
| 89 | |
| 90 | default: |
| 91 | return LookupBlkExpr(E); |
| 92 | } |
| 93 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 95 | Environment EnvironmentManager::SetRVal(const Environment& Env, Expr* E, RVal V, |
| 96 | bool isBlkExpr, bool Invalidate) { |
| 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 | |
| 109 | Environment |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame^] | 110 | EnvironmentManager::RemoveDeadBindings(Environment Env, Stmt* Loc, |
| 111 | const LiveVariables& Liveness, |
| 112 | llvm::SmallVectorImpl<const MemRegion*>& DRoots, |
| 113 | StoreManager::LiveSymbolsTy& LSymbols) { |
| 114 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 115 | // Drop bindings for subexpressions. |
| 116 | Env = RemoveSubExprBindings(Env); |
| 117 | |
| 118 | // Iterate over the block-expr bindings. |
| 119 | for (Environment::beb_iterator I = Env.beb_begin(), E = Env.beb_end(); |
| 120 | I != E; ++I) { |
| 121 | Expr* BlkExpr = I.getKey(); |
| 122 | |
| 123 | if (Liveness.isLive(Loc, BlkExpr)) { |
| 124 | RVal X = I.getData(); |
| 125 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame^] | 126 | // If the block expr's value is a memory region, then mark that region. |
| 127 | if (isa<lval::MemRegionVal>(X)) |
| 128 | DRoots.push_back(cast<lval::MemRegionVal>(X).getRegion()); |
| 129 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 130 | |
| 131 | // Mark all symbols in the block expr's value. |
| 132 | for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 133 | SI != SE; ++SI) { |
| 134 | LSymbols.insert(*SI); |
| 135 | } |
| 136 | } else { |
| 137 | // The block expr is dead. |
| 138 | RVal X = I.getData(); |
| 139 | |
Zhongxing Xu | a950fe2 | 2008-08-21 23:00:21 +0000 | [diff] [blame] | 140 | // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the |
| 141 | // beginning of itself, but we need its UndefinedVal to determine its |
| 142 | // RVal. |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 143 | |
| 144 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
| 145 | continue; |
| 146 | |
| 147 | Env = RemoveBlkExpr(Env, BlkExpr); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return Env; |
| 152 | } |