blob: 7f6fbd3051354530a0a64bfc903504a34c6eec3a [file] [log] [blame]
Ted Kremenek8133a262008-07-08 21:46:56 +00001//== 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
17using namespace clang;
18
Ted Kremenekd72ee902008-07-10 17:19:18 +000019RVal 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 Kyrtzidis0835a3c2008-08-18 23:01:59 +000050 case Stmt::ImplicitCastExprClass:
51 case Stmt::ExplicitCastExprClass: {
Ted Kremenekd72ee902008-07-10 17:19:18 +000052 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 Kremenek8133a262008-07-08 21:46:56 +000073
Ted Kremenekd72ee902008-07-10 17:19:18 +000074RVal 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 Kremenek8133a262008-07-08 21:46:56 +000092
Ted Kremenekd72ee902008-07-10 17:19:18 +000093Environment 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 Kremenek8133a262008-07-08 21:46:56 +0000103
Ted Kremenekd72ee902008-07-10 17:19:18 +0000104 return isBlkExpr ? AddBlkExpr(Env, E, V) : AddSubExpr(Env, E, V);
105}