blob: 7bf2929161c9f5a815252bc85d2769e1a8725f58 [file] [log] [blame]
Ted Kremenekd4931632008-11-12 19:21:30 +00001//== Environment.cpp - Map from Stmt* to Locations/Values -------*- C++ -*--==//
Ted Kremenek8133a262008-07-08 21:46:56 +00002//
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//===----------------------------------------------------------------------===//
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000013
14#include "clang/Analysis/AnalysisContext.h"
15#include "clang/Analysis/CFG.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremenek8133a262008-07-08 21:46:56 +000017
18using namespace clang;
19
Ted Kremenek46584632010-12-05 23:36:15 +000020SVal Environment::lookupExpr(const Stmt* E) const {
21 const SVal* X = ExprBindings.lookup(E);
22 if (X) {
23 SVal V = *X;
24 return V;
25 }
26 return UnknownVal();
27}
28
Ted Kremenekc8413fd2010-12-02 07:49:45 +000029SVal Environment::getSVal(const Stmt *E, SValBuilder& svalBuilder) const {
Ted Kremenekd72ee902008-07-10 17:19:18 +000030 for (;;) {
Ted Kremenekd72ee902008-07-10 17:19:18 +000031 switch (E->getStmtClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +000032 case Stmt::AddrLabelExprClass:
Ted Kremenekc8413fd2010-12-02 07:49:45 +000033 return svalBuilder.makeLoc(cast<AddrLabelExpr>(E));
Mike Stump1eb44332009-09-09 15:08:12 +000034 case Stmt::ParenExprClass:
Ted Kremenekc8413fd2010-12-02 07:49:45 +000035 // ParenExprs are no-ops.
Ted Kremenekd72ee902008-07-10 17:19:18 +000036 E = cast<ParenExpr>(E)->getSubExpr();
37 continue;
Ted Kremenekd72ee902008-07-10 17:19:18 +000038 case Stmt::CharacterLiteralClass: {
Ted Kremenek23ec48c2009-06-18 23:58:37 +000039 const CharacterLiteral* C = cast<CharacterLiteral>(E);
Ted Kremenekc8413fd2010-12-02 07:49:45 +000040 return svalBuilder.makeIntVal(C->getValue(), C->getType());
Ted Kremenekd72ee902008-07-10 17:19:18 +000041 }
Zhongxing Xu477323d2010-04-14 06:29:29 +000042 case Stmt::CXXBoolLiteralExprClass: {
43 const SVal *X = ExprBindings.lookup(E);
44 if (X)
45 return *X;
46 else
Ted Kremenekc8413fd2010-12-02 07:49:45 +000047 return svalBuilder.makeIntVal(cast<CXXBoolLiteralExpr>(E));
Zhongxing Xu477323d2010-04-14 06:29:29 +000048 }
Ted Kremenekd72ee902008-07-10 17:19:18 +000049 case Stmt::IntegerLiteralClass: {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +000050 // In C++, this expression may have been bound to a temporary object.
51 SVal const *X = ExprBindings.lookup(E);
52 if (X)
53 return *X;
54 else
Ted Kremenekc8413fd2010-12-02 07:49:45 +000055 return svalBuilder.makeIntVal(cast<IntegerLiteral>(E));
Ted Kremenekd72ee902008-07-10 17:19:18 +000056 }
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +000057 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +000058 case Stmt::CStyleCastExprClass: {
Ted Kremenekc8413fd2010-12-02 07:49:45 +000059 // We blast through no-op casts to get the descendant
60 // subexpression that has a value.
Ted Kremenek23ec48c2009-06-18 23:58:37 +000061 const CastExpr* C = cast<CastExpr>(E);
Ted Kremenekd72ee902008-07-10 17:19:18 +000062 QualType CT = C->getType();
Ted Kremenekd72ee902008-07-10 17:19:18 +000063 if (CT->isVoidType())
64 return UnknownVal();
Ted Kremenek892697d2010-12-16 07:46:53 +000065 if (C->getCastKind() == CK_NoOp) {
Zhongxing Xua8d835a2010-12-22 07:40:30 +000066 E = C->getSubExpr();
Zhongxing Xud7064342010-11-24 13:08:51 +000067 continue;
68 }
Ted Kremenekd72ee902008-07-10 17:19:18 +000069 break;
70 }
John McCall4765fa02010-12-06 08:20:24 +000071 case Stmt::ExprWithCleanupsClass:
72 E = cast<ExprWithCleanups>(E)->getSubExpr();
Zhongxing Xud7064342010-11-24 13:08:51 +000073 continue;
Zhongxing Xud7064342010-11-24 13:08:51 +000074 case Stmt::CXXBindTemporaryExprClass:
75 E = cast<CXXBindTemporaryExpr>(E)->getSubExpr();
76 continue;
Zhongxing Xu0e38d5d2010-11-25 03:18:57 +000077 case Stmt::CXXFunctionalCastExprClass:
78 E = cast<CXXFunctionalCastExpr>(E)->getSubExpr();
Ted Kremenekc8413fd2010-12-02 07:49:45 +000079 continue;
Zhongxing Xud7064342010-11-24 13:08:51 +000080 // Handle all other Stmt* using a lookup.
Ted Kremenekd72ee902008-07-10 17:19:18 +000081 default:
82 break;
83 };
Ted Kremenekd72ee902008-07-10 17:19:18 +000084 break;
85 }
Ted Kremenek46584632010-12-05 23:36:15 +000086 return lookupExpr(E);
Ted Kremenekd72ee902008-07-10 17:19:18 +000087}
Ted Kremenek8133a262008-07-08 21:46:56 +000088
Ted Kremenek6d4c0222010-09-03 01:07:02 +000089Environment EnvironmentManager::bindExpr(Environment Env, const Stmt *S,
Mike Stump1eb44332009-09-09 15:08:12 +000090 SVal V, bool Invalidate) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000091 assert(S);
Mike Stump1eb44332009-09-09 15:08:12 +000092
93 if (V.isUnknown()) {
Ted Kremenekd72ee902008-07-10 17:19:18 +000094 if (Invalidate)
Ted Kremenek3baf6722010-11-24 00:54:37 +000095 return Environment(F.remove(Env.ExprBindings, S));
Ted Kremenekd72ee902008-07-10 17:19:18 +000096 else
97 return Env;
98 }
Ted Kremenek8133a262008-07-08 21:46:56 +000099
Ted Kremenek3baf6722010-11-24 00:54:37 +0000100 return Environment(F.add(Env.ExprBindings, S, V));
Ted Kremenekd72ee902008-07-10 17:19:18 +0000101}
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000102
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000103static inline const Stmt *MakeLocation(const Stmt *S) {
104 return (const Stmt*) (((uintptr_t) S) | 0x1);
105}
106
107Environment EnvironmentManager::bindExprAndLocation(Environment Env,
108 const Stmt *S,
109 SVal location, SVal V) {
Ted Kremenek3baf6722010-11-24 00:54:37 +0000110 return Environment(F.add(F.add(Env.ExprBindings, MakeLocation(S), location),
Zhanyong Wancf848872010-11-20 07:52:48 +0000111 S, V));
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000112}
113
Ted Kremenek5216ad72009-02-14 03:16:10 +0000114namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000115class MarkLiveCallback : public SymbolVisitor {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000116 SymbolReaper &SymReaper;
117public:
Mike Stump1eb44332009-09-09 15:08:12 +0000118 MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000119 bool VisitSymbol(SymbolRef sym) { SymReaper.markLive(sym); return true; }
120};
121} // end anonymous namespace
122
Zhongxing Xu7b73b922010-04-05 13:16:29 +0000123static bool isBlockExprInCallers(const Stmt *E, const LocationContext *LC) {
124 const LocationContext *ParentLC = LC->getParent();
125 while (ParentLC) {
126 CFG &C = *ParentLC->getCFG();
127 if (C.isBlkExpr(E))
128 return true;
129 ParentLC = ParentLC->getParent();
130 }
131
132 return false;
133}
134
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000135// In addition to mapping from Stmt * - > SVals in the Environment, we also
136// maintain a mapping from Stmt * -> SVals (locations) that were used during
137// a load and store.
138static inline bool IsLocation(const Stmt *S) {
139 return (bool) (((uintptr_t) S) & 0x1);
140}
Zhongxing Xu7b73b922010-04-05 13:16:29 +0000141
Zhongxing Xu9d8d0fc2009-03-12 07:54:17 +0000142// RemoveDeadBindings:
143// - Remove subexpression bindings.
144// - Remove dead block expression bindings.
145// - Keep live block expression bindings:
Mike Stump1eb44332009-09-09 15:08:12 +0000146// - Mark their reachable symbols live in SymbolReaper,
Zhongxing Xu9d8d0fc2009-03-12 07:54:17 +0000147// see ScanReachableSymbols.
148// - Mark the region in DRoots if the binding is a loc::MemRegionVal.
Mike Stump1eb44332009-09-09 15:08:12 +0000149Environment
Jordy Rose7dadf792010-07-01 20:09:55 +0000150EnvironmentManager::RemoveDeadBindings(Environment Env,
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000151 SymbolReaper &SymReaper,
152 const GRState *ST,
153 llvm::SmallVectorImpl<const MemRegion*> &DRoots) {
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000155 CFG &C = *SymReaper.getLocationContext()->getCFG();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000157 // We construct a new Environment object entirely, as this is cheaper than
158 // individually removing all the subexpression bindings (which will greatly
159 // outnumber block-level expression bindings).
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000160 Environment NewEnv = getInitialEnvironment();
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000161
162 llvm::SmallVector<std::pair<const Stmt*, SVal>, 10> deferredLocations;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000164 // Iterate over the block-expr bindings.
Mike Stump1eb44332009-09-09 15:08:12 +0000165 for (Environment::iterator I = Env.begin(), E = Env.end();
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000166 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000168 const Stmt *BlkExpr = I.getKey();
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000169
170 // For recorded locations (used when evaluating loads and stores), we
171 // consider them live only when their associated normal expression is
172 // also live.
173 // NOTE: This assumes that loads/stores that evaluated to UnknownVal
174 // still have an entry in the map.
175 if (IsLocation(BlkExpr)) {
176 deferredLocations.push_back(std::make_pair(BlkExpr, I.getData()));
177 continue;
178 }
179
Zhongxing Xu7b73b922010-04-05 13:16:29 +0000180 const SVal &X = I.getData();
181
182 // Block-level expressions in callers are assumed always live.
183 if (isBlockExprInCallers(BlkExpr, SymReaper.getLocationContext())) {
Ted Kremenek3baf6722010-11-24 00:54:37 +0000184 NewEnv.ExprBindings = F.add(NewEnv.ExprBindings, BlkExpr, X);
Zhongxing Xu7b73b922010-04-05 13:16:29 +0000185
186 if (isa<loc::MemRegionVal>(X)) {
187 const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion();
188 DRoots.push_back(R);
189 }
190
191 // Mark all symbols in the block expr's value live.
192 MarkLiveCallback cb(SymReaper);
193 ST->scanReachableSymbols(X, cb);
194 continue;
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000197 // Not a block-level expression?
198 if (!C.isBlkExpr(BlkExpr))
199 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Jordy Rose7dadf792010-07-01 20:09:55 +0000201 if (SymReaper.isLive(BlkExpr)) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000202 // Copy the binding to the new map.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000203 NewEnv.ExprBindings = F.add(NewEnv.ExprBindings, BlkExpr, X);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek9e240492008-10-04 05:50:14 +0000205 // If the block expr's value is a memory region, then mark that region.
Zhongxing Xuce2f9bd2009-06-30 13:00:53 +0000206 if (isa<loc::MemRegionVal>(X)) {
207 const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion();
208 DRoots.push_back(R);
Zhongxing Xuce2f9bd2009-06-30 13:00:53 +0000209 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000210
Ted Kremenek5216ad72009-02-14 03:16:10 +0000211 // Mark all symbols in the block expr's value live.
212 MarkLiveCallback cb(SymReaper);
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000213 ST->scanReachableSymbols(X, cb);
214 continue;
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000215 }
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000216
217 // Otherwise the expression is dead with a couple exceptions.
218 // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the
219 // beginning of itself, but we need its UndefinedVal to determine its
220 // SVal.
221 if (X.isUndef() && cast<UndefinedVal>(X).getData())
Ted Kremenek3baf6722010-11-24 00:54:37 +0000222 NewEnv.ExprBindings = F.add(NewEnv.ExprBindings, BlkExpr, X);
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000223 }
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000224
225 // Go through he deferred locations and add them to the new environment if
226 // the correspond Stmt* is in the map as well.
227 for (llvm::SmallVectorImpl<std::pair<const Stmt*, SVal> >::iterator
228 I = deferredLocations.begin(), E = deferredLocations.end(); I != E; ++I) {
229 const Stmt *S = (Stmt*) (((uintptr_t) I->first) & (uintptr_t) ~0x1);
230 if (NewEnv.ExprBindings.lookup(S))
Ted Kremenek3baf6722010-11-24 00:54:37 +0000231 NewEnv.ExprBindings = F.add(NewEnv.ExprBindings, I->first, I->second);
Ted Kremenek6d4c0222010-09-03 01:07:02 +0000232 }
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000233
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000234 return NewEnv;
Ted Kremenekdf9cdf82008-08-20 17:08:29 +0000235}