blob: f3a803c57d32a92841ee76a4d144bbccaca5812c [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;
31 case BinaryOperator::Mul: os << '*' ; break;
32 case BinaryOperator::Div: os << '/' ; break;
33 case BinaryOperator::Rem: os << '%' ; break;
34 case BinaryOperator::Add: os << '+' ; break;
35 case BinaryOperator::Sub: os << '-' ; break;
36 case BinaryOperator::Shl: os << "<<" ; break;
37 case BinaryOperator::Shr: os << ">>" ; break;
38 case BinaryOperator::LT: os << "<" ; break;
39 case BinaryOperator::GT: os << '>' ; break;
40 case BinaryOperator::LE: os << "<=" ; break;
Mike Stump1eb44332009-09-09 15:08:12 +000041 case BinaryOperator::GE: os << ">=" ; break;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000042 case BinaryOperator::EQ: os << "==" ; break;
43 case BinaryOperator::NE: os << "!=" ; break;
44 case BinaryOperator::And: os << '&' ; break;
45 case BinaryOperator::Xor: os << '^' ; break;
46 case BinaryOperator::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
Ted Kremenek8800ad42009-07-13 23:31:04 +000077void SymbolRegionValue::dumpToStream(llvm::raw_ostream& os) const {
78 os << "reg_$" << getSymbolID() << "<" << R << ">";
Ted Kremenek94c96982009-03-03 22:06:47 +000079}
80
Mike Stump1eb44332009-09-09 15:08:12 +000081const SymbolRegionValue*
Zhongxing Xu14d23282010-03-01 06:56:52 +000082SymbolManager::getRegionValueSymbol(const TypedRegion* R) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000083 llvm::FoldingSetNodeID profile;
Zhongxing Xu14d23282010-03-01 06:56:52 +000084 SymbolRegionValue::Profile(profile, R);
Mike Stump1eb44332009-09-09 15:08:12 +000085 void* InsertPos;
86 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
87 if (!SD) {
Zhongxing Xud9b6ad62009-05-09 04:08:27 +000088 SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>();
Zhongxing Xu14d23282010-03-01 06:56:52 +000089 new (SD) SymbolRegionValue(SymbolCounter, R);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000090 DataSet.InsertNode(SD, InsertPos);
91 ++SymbolCounter;
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Zhongxing Xud9b6ad62009-05-09 04:08:27 +000094 return cast<SymbolRegionValue>(SD);
Zhongxing Xueabf7762008-11-19 11:03:17 +000095}
96
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000097const SymbolConjured*
98SymbolManager::getConjuredSymbol(const Stmt* E, QualType T, unsigned Count,
99 const void* SymbolTag) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000101 llvm::FoldingSetNodeID profile;
Ted Kremeneka880b662009-03-04 22:53:46 +0000102 SymbolConjured::Profile(profile, E, T, Count, SymbolTag);
Mike Stump1eb44332009-09-09 15:08:12 +0000103 void* InsertPos;
104 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
105 if (!SD) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000106 SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>();
Mike Stump1eb44332009-09-09 15:08:12 +0000107 new (SD) SymbolConjured(SymbolCounter, E, T, Count, SymbolTag);
108 DataSet.InsertNode(SD, InsertPos);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000109 ++SymbolCounter;
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000112 return cast<SymbolConjured>(SD);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000113}
114
Ted Kremenekfb91c702009-07-15 02:27:32 +0000115const SymbolDerived*
116SymbolManager::getDerivedSymbol(SymbolRef parentSymbol,
117 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenekfb91c702009-07-15 02:27:32 +0000119 llvm::FoldingSetNodeID profile;
120 SymbolDerived::Profile(profile, parentSymbol, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000121 void* InsertPos;
122 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
123 if (!SD) {
Ted Kremenekfb91c702009-07-15 02:27:32 +0000124 SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>();
125 new (SD) SymbolDerived(SymbolCounter, parentSymbol, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000126 DataSet.InsertNode(SD, InsertPos);
Ted Kremenekfb91c702009-07-15 02:27:32 +0000127 ++SymbolCounter;
128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenekfb91c702009-07-15 02:27:32 +0000130 return cast<SymbolDerived>(SD);
131}
132
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000133const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000134 BinaryOperator::Opcode op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000135 const llvm::APSInt& v,
136 QualType t) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000137 llvm::FoldingSetNodeID ID;
138 SymIntExpr::Profile(ID, lhs, op, v, t);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000139 void *InsertPos;
140 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000141
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000142 if (!data) {
143 data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>();
144 new (data) SymIntExpr(lhs, op, v, t);
145 DataSet.InsertNode(data, InsertPos);
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000148 return cast<SymIntExpr>(data);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000149}
150
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000151const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs,
152 BinaryOperator::Opcode op,
153 const SymExpr *rhs,
154 QualType t) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000155 llvm::FoldingSetNodeID ID;
156 SymSymExpr::Profile(ID, lhs, op, rhs, t);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000157 void *InsertPos;
158 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000159
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000160 if (!data) {
161 data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>();
162 new (data) SymSymExpr(lhs, op, rhs, t);
163 DataSet.InsertNode(data, InsertPos);
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000166 return cast<SymSymExpr>(data);
Zhongxing Xua129eb92009-03-25 05:58:37 +0000167}
168
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000169QualType SymbolConjured::getType(ASTContext&) const {
170 return T;
171}
Ted Kremenekd763eb92008-02-26 02:15:56 +0000172
Ted Kremenekfb91c702009-07-15 02:27:32 +0000173
174QualType SymbolDerived::getType(ASTContext& Ctx) const {
175 return R->getValueType(Ctx);
176}
177
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000178QualType SymbolRegionValue::getType(ASTContext& C) const {
Zhongxing Xu14d23282010-03-01 06:56:52 +0000179 return R->getValueType(C);
Ted Kremenekd70d0b02008-02-16 01:12:31 +0000180}
181
Ted Kremenekd70d0b02008-02-16 01:12:31 +0000182SymbolManager::~SymbolManager() {}
Ted Kremenek241677a2009-01-21 22:26:05 +0000183
Ted Kremenek693de5d2009-03-23 15:42:58 +0000184bool SymbolManager::canSymbolicate(QualType T) {
Ted Kremenek4e6e0d72009-07-29 18:12:48 +0000185 return Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType());
Ted Kremenek693de5d2009-03-23 15:42:58 +0000186}
187
Ted Kremenek241677a2009-01-21 22:26:05 +0000188void SymbolReaper::markLive(SymbolRef sym) {
Ted Kremeneka97d54c2009-09-02 06:03:18 +0000189 TheLiving.insert(sym);
190 TheDead.erase(sym);
Ted Kremenek241677a2009-01-21 22:26:05 +0000191}
192
193bool SymbolReaper::maybeDead(SymbolRef sym) {
194 if (isLive(sym))
195 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremeneka97d54c2009-09-02 06:03:18 +0000197 TheDead.insert(sym);
Ted Kremenek241677a2009-01-21 22:26:05 +0000198 return true;
199}
200
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000201bool SymbolReaper::isLive(SymbolRef sym) {
Ted Kremeneka97d54c2009-09-02 06:03:18 +0000202 if (TheLiving.count(sym))
Ted Kremenekdcb6a262009-01-22 18:51:33 +0000203 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000205 if (const SymbolDerived *derived = dyn_cast<SymbolDerived>(sym)) {
206 if (isLive(derived->getParentSymbol())) {
207 markLive(sym);
208 return true;
209 }
210 return false;
211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenekdcb6a262009-01-22 18:51:33 +0000213 // Interogate the symbol. It may derive from an input value to
214 // the analyzed function/method.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000215 return isa<SymbolRegionValue>(sym);
Ted Kremenek241677a2009-01-21 22:26:05 +0000216}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000217
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000218bool SymbolReaper::isLive(const Stmt* Loc, const Stmt* ExprVal) const {
219 return LCtx->getLiveVariables()->isLive(Loc, ExprVal);
220}
221
Ted Kremenekedeb5b62009-12-04 20:32:20 +0000222bool SymbolReaper::isLive(const Stmt *Loc, const VarRegion *VR) const {
Ted Kremenek5348f942009-12-14 22:15:06 +0000223 const StackFrameContext *SFC = VR->getStackFrame();
Zhongxing Xudf3a61b2010-02-17 08:50:05 +0000224
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000225 if (SFC == LCtx->getCurrentStackFrame())
226 return LCtx->getLiveVariables()->isLive(Loc, VR->getDecl());
Zhongxing Xudf3a61b2010-02-17 08:50:05 +0000227 else
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000228 return SFC->isParentOf(LCtx->getCurrentStackFrame());
Ted Kremenekedeb5b62009-12-04 20:32:20 +0000229}
230
Ted Kremenek5216ad72009-02-14 03:16:10 +0000231SymbolVisitor::~SymbolVisitor() {}