| 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" | 
 | 15 | #include "llvm/ADT/ImmutableMap.h" | 
 | 16 |  | 
 | 17 | using namespace clang; | 
 | 18 |  | 
| Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 19 | RVal Environment::GetRVal(Expr* E, BasicValueFactory& BasicVals) const { | 
 | 20 |    | 
 | 21 |   for (;;) { | 
 | 22 |      | 
 | 23 |     switch (E->getStmtClass()) { | 
 | 24 |          | 
 | 25 |       case Stmt::AddrLabelExprClass:         | 
 | 26 |         return LVal::MakeVal(cast<AddrLabelExpr>(E)); | 
 | 27 |          | 
 | 28 |         // ParenExprs are no-ops. | 
 | 29 |          | 
 | 30 |       case Stmt::ParenExprClass:         | 
 | 31 |         E = cast<ParenExpr>(E)->getSubExpr(); | 
 | 32 |         continue; | 
 | 33 |          | 
 | 34 |       case Stmt::CharacterLiteralClass: { | 
 | 35 |         CharacterLiteral* C = cast<CharacterLiteral>(E); | 
 | 36 |         return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); | 
 | 37 |       } | 
 | 38 |          | 
 | 39 |       case Stmt::IntegerLiteralClass: { | 
 | 40 |         return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E)); | 
 | 41 |       } | 
 | 42 |          | 
 | 43 |       case Stmt::StringLiteralClass: | 
 | 44 |         return LVal::MakeVal(cast<StringLiteral>(E)); | 
 | 45 |          | 
 | 46 |         // Casts where the source and target type are the same | 
 | 47 |         // are no-ops.  We blast through these to get the descendant | 
 | 48 |         // subexpression that has a value. | 
 | 49 |          | 
| Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 50 |       case Stmt::ImplicitCastExprClass: | 
 | 51 |       case Stmt::ExplicitCastExprClass: { | 
| Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 52 |         CastExpr* C = cast<CastExpr>(E); | 
 | 53 |         QualType CT = C->getType(); | 
 | 54 |         QualType ST = C->getSubExpr()->getType(); | 
 | 55 |          | 
 | 56 |         if (CT->isVoidType()) | 
 | 57 |           return UnknownVal(); | 
 | 58 |          | 
 | 59 |         break; | 
 | 60 |       } | 
 | 61 |          | 
 | 62 |         // Handle all other Expr* using a lookup. | 
 | 63 |          | 
 | 64 |       default: | 
 | 65 |         break; | 
 | 66 |     }; | 
 | 67 |      | 
 | 68 |     break; | 
 | 69 |   } | 
 | 70 |    | 
 | 71 |   return LookupExpr(E); | 
 | 72 | } | 
| Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 73 |  | 
| Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 74 | RVal Environment::GetBlkExprRVal(Expr* E, BasicValueFactory& BasicVals) const { | 
 | 75 |    | 
 | 76 |   E = E->IgnoreParens(); | 
 | 77 |    | 
 | 78 |   switch (E->getStmtClass()) { | 
 | 79 |     case Stmt::CharacterLiteralClass: { | 
 | 80 |       CharacterLiteral* C = cast<CharacterLiteral>(E); | 
 | 81 |       return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); | 
 | 82 |     } | 
 | 83 |        | 
 | 84 |     case Stmt::IntegerLiteralClass: { | 
 | 85 |       return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E)); | 
 | 86 |     } | 
 | 87 |        | 
 | 88 |     default: | 
 | 89 |       return LookupBlkExpr(E); | 
 | 90 |   } | 
 | 91 | } | 
| Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 92 |  | 
| Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 93 | Environment EnvironmentManager::SetRVal(const Environment& Env, Expr* E, RVal V, | 
 | 94 |                                         bool isBlkExpr, bool Invalidate) {   | 
 | 95 |   assert (E); | 
 | 96 |    | 
 | 97 |   if (V.isUnknown()) {     | 
 | 98 |     if (Invalidate) | 
 | 99 |       return isBlkExpr ? RemoveBlkExpr(Env, E) : RemoveSubExpr(Env, E); | 
 | 100 |     else | 
 | 101 |       return Env; | 
 | 102 |   } | 
| Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 103 |  | 
| Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 104 |   return isBlkExpr ? AddBlkExpr(Env, E, V) : AddSubExpr(Env, E, V); | 
 | 105 | } |