blob: 3b1bb6d98d95cc38a3f8dad54494573bc3ada9cf [file] [log] [blame]
Ted Kremenekd70d0b02008-02-16 01:12:31 +00001//== SymbolManager.h - Management of Symbolic 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//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines SymbolManager, a class that manages symbolic values
Ted Kremenekd70d0b02008-02-16 01:12:31 +000011// created for use by GRExprEngine and related classes.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek1309f9a2010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/SymbolManager.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
Ted Kremenek562731e2008-12-05 02:45:20 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenekd70d0b02008-02-16 01:12:31 +000019
20using namespace clang;
21
Ted Kremenek8800ad42009-07-13 23:31:04 +000022void SymExpr::dump() const {
23 dumpToStream(llvm::errs());
24}
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000025
Mike Stump1eb44332009-09-09 15:08:12 +000026static void print(llvm::raw_ostream& os, BinaryOperator::Opcode Op) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000027 switch (Op) {
28 default:
29 assert(false && "operator printing not implemented");
30 break;
John McCall2de56d12010-08-25 11:45:40 +000031 case BO_Mul: os << '*' ; break;
32 case BO_Div: os << '/' ; break;
33 case BO_Rem: os << '%' ; break;
34 case BO_Add: os << '+' ; break;
35 case BO_Sub: os << '-' ; break;
36 case BO_Shl: os << "<<" ; break;
37 case BO_Shr: os << ">>" ; break;
38 case BO_LT: os << "<" ; break;
39 case BO_GT: os << '>' ; break;
40 case BO_LE: os << "<=" ; break;
41 case BO_GE: os << ">=" ; break;
42 case BO_EQ: os << "==" ; break;
43 case BO_NE: os << "!=" ; break;
44 case BO_And: os << '&' ; break;
45 case BO_Xor: os << '^' ; break;
46 case BO_Or: os << '|' ; break;
Mike Stump1eb44332009-09-09 15:08:12 +000047 }
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000048}
49
Ted Kremenek8800ad42009-07-13 23:31:04 +000050void SymIntExpr::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000051 os << '(';
Ted Kremenek8800ad42009-07-13 23:31:04 +000052 getLHS()->dumpToStream(os);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000053 os << ") ";
Ted Kremenek8800ad42009-07-13 23:31:04 +000054 print(os, getOpcode());
55 os << ' ' << getRHS().getZExtValue();
56 if (getRHS().isUnsigned()) os << 'U';
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000057}
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenek8800ad42009-07-13 23:31:04 +000059void SymSymExpr::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000060 os << '(';
Ted Kremenek8800ad42009-07-13 23:31:04 +000061 getLHS()->dumpToStream(os);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000062 os << ") ";
63 os << '(';
Ted Kremenek8800ad42009-07-13 23:31:04 +000064 getRHS()->dumpToStream(os);
Mike Stump1eb44332009-09-09 15:08:12 +000065 os << ')';
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000066}
67
Ted Kremenek8800ad42009-07-13 23:31:04 +000068void SymbolConjured::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000069 os << "conj_$" << getSymbolID() << '{' << T.getAsString() << '}';
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000070}
71
Ted Kremenekfb91c702009-07-15 02:27:32 +000072void SymbolDerived::dumpToStream(llvm::raw_ostream& os) const {
73 os << "derived_$" << getSymbolID() << '{'
74 << getParentSymbol() << ',' << getRegion() << '}';
75}
76
Jordy Rose32f26562010-07-04 00:00:41 +000077void SymbolExtent::dumpToStream(llvm::raw_ostream& os) const {
78 os << "extent_$" << getSymbolID() << '{' << getRegion() << '}';
79}
80
Jordy Rosebd533aa2010-08-14 20:18:45 +000081void SymbolMetadata::dumpToStream(llvm::raw_ostream& os) const {
82 os << "meta_$" << getSymbolID() << '{'
83 << getRegion() << ',' << T.getAsString() << '}';
84}
85
Ted Kremenek8800ad42009-07-13 23:31:04 +000086void SymbolRegionValue::dumpToStream(llvm::raw_ostream& os) const {
87 os << "reg_$" << getSymbolID() << "<" << R << ">";
Ted Kremenek94c96982009-03-03 22:06:47 +000088}
89
Mike Stump1eb44332009-09-09 15:08:12 +000090const SymbolRegionValue*
Zhongxing Xu14d23282010-03-01 06:56:52 +000091SymbolManager::getRegionValueSymbol(const TypedRegion* R) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000092 llvm::FoldingSetNodeID profile;
Zhongxing Xu14d23282010-03-01 06:56:52 +000093 SymbolRegionValue::Profile(profile, R);
Mike Stump1eb44332009-09-09 15:08:12 +000094 void* InsertPos;
95 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
96 if (!SD) {
Zhongxing Xud9b6ad62009-05-09 04:08:27 +000097 SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>();
Zhongxing Xu14d23282010-03-01 06:56:52 +000098 new (SD) SymbolRegionValue(SymbolCounter, R);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000099 DataSet.InsertNode(SD, InsertPos);
100 ++SymbolCounter;
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000103 return cast<SymbolRegionValue>(SD);
Zhongxing Xueabf7762008-11-19 11:03:17 +0000104}
105
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000106const SymbolConjured*
107SymbolManager::getConjuredSymbol(const Stmt* E, QualType T, unsigned Count,
108 const void* SymbolTag) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000110 llvm::FoldingSetNodeID profile;
Ted Kremeneka880b662009-03-04 22:53:46 +0000111 SymbolConjured::Profile(profile, E, T, Count, SymbolTag);
Mike Stump1eb44332009-09-09 15:08:12 +0000112 void* InsertPos;
113 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
114 if (!SD) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000115 SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>();
Mike Stump1eb44332009-09-09 15:08:12 +0000116 new (SD) SymbolConjured(SymbolCounter, E, T, Count, SymbolTag);
117 DataSet.InsertNode(SD, InsertPos);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000118 ++SymbolCounter;
119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000121 return cast<SymbolConjured>(SD);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000122}
123
Ted Kremenekfb91c702009-07-15 02:27:32 +0000124const SymbolDerived*
125SymbolManager::getDerivedSymbol(SymbolRef parentSymbol,
126 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenekfb91c702009-07-15 02:27:32 +0000128 llvm::FoldingSetNodeID profile;
129 SymbolDerived::Profile(profile, parentSymbol, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000130 void* InsertPos;
131 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
132 if (!SD) {
Ted Kremenekfb91c702009-07-15 02:27:32 +0000133 SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>();
134 new (SD) SymbolDerived(SymbolCounter, parentSymbol, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000135 DataSet.InsertNode(SD, InsertPos);
Ted Kremenekfb91c702009-07-15 02:27:32 +0000136 ++SymbolCounter;
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenekfb91c702009-07-15 02:27:32 +0000139 return cast<SymbolDerived>(SD);
140}
141
Jordy Rose32f26562010-07-04 00:00:41 +0000142const SymbolExtent*
143SymbolManager::getExtentSymbol(const SubRegion *R) {
144 llvm::FoldingSetNodeID profile;
145 SymbolExtent::Profile(profile, R);
146 void* InsertPos;
147 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
148 if (!SD) {
149 SD = (SymExpr*) BPAlloc.Allocate<SymbolExtent>();
150 new (SD) SymbolExtent(SymbolCounter, R);
151 DataSet.InsertNode(SD, InsertPos);
152 ++SymbolCounter;
153 }
154
155 return cast<SymbolExtent>(SD);
156}
157
Jordy Rosebd533aa2010-08-14 20:18:45 +0000158const SymbolMetadata*
159SymbolManager::getMetadataSymbol(const MemRegion* R, const Stmt* S, QualType T,
160 unsigned Count, const void* SymbolTag) {
161
162 llvm::FoldingSetNodeID profile;
163 SymbolMetadata::Profile(profile, R, S, T, Count, SymbolTag);
164 void* InsertPos;
165 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
166 if (!SD) {
167 SD = (SymExpr*) BPAlloc.Allocate<SymbolMetadata>();
168 new (SD) SymbolMetadata(SymbolCounter, R, S, T, Count, SymbolTag);
169 DataSet.InsertNode(SD, InsertPos);
170 ++SymbolCounter;
171 }
172
173 return cast<SymbolMetadata>(SD);
174}
175
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000176const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000177 BinaryOperator::Opcode op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000178 const llvm::APSInt& v,
179 QualType t) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000180 llvm::FoldingSetNodeID ID;
181 SymIntExpr::Profile(ID, lhs, op, v, t);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000182 void *InsertPos;
183 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000184
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000185 if (!data) {
186 data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>();
187 new (data) SymIntExpr(lhs, op, v, t);
188 DataSet.InsertNode(data, InsertPos);
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000191 return cast<SymIntExpr>(data);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000192}
193
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000194const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs,
195 BinaryOperator::Opcode op,
196 const SymExpr *rhs,
197 QualType t) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000198 llvm::FoldingSetNodeID ID;
199 SymSymExpr::Profile(ID, lhs, op, rhs, t);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000200 void *InsertPos;
201 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000202
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000203 if (!data) {
204 data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>();
205 new (data) SymSymExpr(lhs, op, rhs, t);
206 DataSet.InsertNode(data, InsertPos);
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000209 return cast<SymSymExpr>(data);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000210}
211
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000212QualType SymbolConjured::getType(ASTContext&) const {
213 return T;
214}
Ted Kremenekd763eb92008-02-26 02:15:56 +0000215
Ted Kremenekfb91c702009-07-15 02:27:32 +0000216QualType SymbolDerived::getType(ASTContext& Ctx) const {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000217 return R->getValueType();
Ted Kremenekfb91c702009-07-15 02:27:32 +0000218}
219
Jordy Rose32f26562010-07-04 00:00:41 +0000220QualType SymbolExtent::getType(ASTContext& Ctx) const {
221 return Ctx.getSizeType();
222}
223
Jordy Rosebd533aa2010-08-14 20:18:45 +0000224QualType SymbolMetadata::getType(ASTContext&) const {
225 return T;
226}
227
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000228QualType SymbolRegionValue::getType(ASTContext& C) const {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000229 return R->getValueType();
Ted Kremenekd70d0b02008-02-16 01:12:31 +0000230}
231
Ted Kremenekd70d0b02008-02-16 01:12:31 +0000232SymbolManager::~SymbolManager() {}
Ted Kremenek241677a2009-01-21 22:26:05 +0000233
Ted Kremenek693de5d2009-03-23 15:42:58 +0000234bool SymbolManager::canSymbolicate(QualType T) {
Ted Kremenek17c571c2010-07-29 00:28:43 +0000235 if (Loc::IsLocType(T))
236 return true;
237
238 if (T->isIntegerType())
239 return T->isScalarType();
240
241 if (T->isRecordType())
242 return true;
243
244 return false;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000245}
246
Ted Kremenek241677a2009-01-21 22:26:05 +0000247void SymbolReaper::markLive(SymbolRef sym) {
Ted Kremeneka97d54c2009-09-02 06:03:18 +0000248 TheLiving.insert(sym);
249 TheDead.erase(sym);
Ted Kremenek241677a2009-01-21 22:26:05 +0000250}
251
Jordy Rosebd533aa2010-08-14 20:18:45 +0000252void SymbolReaper::markInUse(SymbolRef sym) {
253 if (isa<SymbolMetadata>(sym))
254 MetadataInUse.insert(sym);
255}
256
Ted Kremenek241677a2009-01-21 22:26:05 +0000257bool SymbolReaper::maybeDead(SymbolRef sym) {
258 if (isLive(sym))
259 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremeneka97d54c2009-09-02 06:03:18 +0000261 TheDead.insert(sym);
Ted Kremenek241677a2009-01-21 22:26:05 +0000262 return true;
263}
264
Jordy Rosebd533aa2010-08-14 20:18:45 +0000265static bool IsLiveRegion(SymbolReaper &Reaper, const MemRegion *MR) {
266 MR = MR->getBaseRegion();
267
268 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
269 return Reaper.isLive(SR->getSymbol());
270
271 if (const VarRegion *VR = dyn_cast<VarRegion>(MR))
272 return Reaper.isLive(VR);
273
274 // FIXME: This is a gross over-approximation. What we really need is a way to
275 // tell if anything still refers to this region. Unlike SymbolicRegions,
276 // AllocaRegions don't have associated symbols, though, so we don't actually
277 // have a way to track their liveness.
278 if (isa<AllocaRegion>(MR))
279 return true;
280
281 if (isa<CXXThisRegion>(MR))
282 return true;
283
284 if (isa<MemSpaceRegion>(MR))
285 return true;
286
287 return false;
288}
289
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000290bool SymbolReaper::isLive(SymbolRef sym) {
Ted Kremeneka97d54c2009-09-02 06:03:18 +0000291 if (TheLiving.count(sym))
Ted Kremenekdcb6a262009-01-22 18:51:33 +0000292 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000294 if (const SymbolDerived *derived = dyn_cast<SymbolDerived>(sym)) {
295 if (isLive(derived->getParentSymbol())) {
296 markLive(sym);
297 return true;
298 }
299 return false;
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Jordy Rose32f26562010-07-04 00:00:41 +0000302 if (const SymbolExtent *extent = dyn_cast<SymbolExtent>(sym)) {
Jordy Rosebd533aa2010-08-14 20:18:45 +0000303 if (IsLiveRegion(*this, extent->getRegion())) {
304 markLive(sym);
305 return true;
306 }
307 return false;
308 }
309
310 if (const SymbolMetadata *metadata = dyn_cast<SymbolMetadata>(sym)) {
311 if (MetadataInUse.count(sym)) {
312 if (IsLiveRegion(*this, metadata->getRegion())) {
313 markLive(sym);
314 MetadataInUse.erase(sym);
315 return true;
316 }
317 }
Jordy Rose32f26562010-07-04 00:00:41 +0000318 return false;
319 }
320
Ted Kremenekdcb6a262009-01-22 18:51:33 +0000321 // Interogate the symbol. It may derive from an input value to
322 // the analyzed function/method.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000323 return isa<SymbolRegionValue>(sym);
Ted Kremenek241677a2009-01-21 22:26:05 +0000324}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000325
Jordy Rose7dadf792010-07-01 20:09:55 +0000326bool SymbolReaper::isLive(const Stmt* ExprVal) const {
Tom Care6d0e6ce2010-08-27 22:46:32 +0000327 return LCtx->getAnalysisContext()->getRelaxedLiveVariables()->
328 isLive(Loc, ExprVal);
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000329}
330
Jordy Rose7dadf792010-07-01 20:09:55 +0000331bool SymbolReaper::isLive(const VarRegion *VR) const {
Jordy Rosebd533aa2010-08-14 20:18:45 +0000332 const StackFrameContext *VarContext = VR->getStackFrame();
333 const StackFrameContext *CurrentContext = LCtx->getCurrentStackFrame();
Zhongxing Xudf3a61b2010-02-17 08:50:05 +0000334
Jordy Rosebd533aa2010-08-14 20:18:45 +0000335 if (VarContext == CurrentContext)
Tom Care6d0e6ce2010-08-27 22:46:32 +0000336 return LCtx->getAnalysisContext()->getRelaxedLiveVariables()->
337 isLive(Loc, VR->getDecl());
Jordy Rosebd533aa2010-08-14 20:18:45 +0000338
339 return VarContext->isParentOf(CurrentContext);
Ted Kremenekedeb5b62009-12-04 20:32:20 +0000340}
341
Ted Kremenek5216ad72009-02-14 03:16:10 +0000342SymbolVisitor::~SymbolVisitor() {}