Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 1 | //== 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 Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines SymbolManager, a class that manages symbolic values |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 11 | // created for use by GRExprEngine and related classes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/SymbolManager.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Checker/PathSensitive/MemRegion.h" |
Ted Kremenek | 562731e | 2008-12-05 02:45:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 22 | void SymExpr::dump() const { |
| 23 | dumpToStream(llvm::errs()); |
| 24 | } |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 25 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 26 | static void print(llvm::raw_ostream& os, BinaryOperator::Opcode Op) { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 27 | switch (Op) { |
| 28 | default: |
| 29 | assert(false && "operator printing not implemented"); |
| 30 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 31 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | } |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 50 | void SymIntExpr::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 51 | os << '('; |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 52 | getLHS()->dumpToStream(os); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 53 | os << ") "; |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 54 | print(os, getOpcode()); |
| 55 | os << ' ' << getRHS().getZExtValue(); |
| 56 | if (getRHS().isUnsigned()) os << 'U'; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 59 | void SymSymExpr::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 60 | os << '('; |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 61 | getLHS()->dumpToStream(os); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 62 | os << ") "; |
| 63 | os << '('; |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 64 | getRHS()->dumpToStream(os); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | os << ')'; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 68 | void SymbolConjured::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 69 | os << "conj_$" << getSymbolID() << '{' << T.getAsString() << '}'; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 72 | void SymbolDerived::dumpToStream(llvm::raw_ostream& os) const { |
| 73 | os << "derived_$" << getSymbolID() << '{' |
| 74 | << getParentSymbol() << ',' << getRegion() << '}'; |
| 75 | } |
| 76 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 77 | void SymbolExtent::dumpToStream(llvm::raw_ostream& os) const { |
| 78 | os << "extent_$" << getSymbolID() << '{' << getRegion() << '}'; |
| 79 | } |
| 80 | |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 81 | void SymbolMetadata::dumpToStream(llvm::raw_ostream& os) const { |
| 82 | os << "meta_$" << getSymbolID() << '{' |
| 83 | << getRegion() << ',' << T.getAsString() << '}'; |
| 84 | } |
| 85 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 86 | void SymbolRegionValue::dumpToStream(llvm::raw_ostream& os) const { |
| 87 | os << "reg_$" << getSymbolID() << "<" << R << ">"; |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | const SymbolRegionValue* |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 91 | SymbolManager::getRegionValueSymbol(const TypedRegion* R) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 92 | llvm::FoldingSetNodeID profile; |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 93 | SymbolRegionValue::Profile(profile, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | void* InsertPos; |
| 95 | SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 96 | if (!SD) { |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 97 | SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>(); |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 98 | new (SD) SymbolRegionValue(SymbolCounter, R); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 99 | DataSet.InsertNode(SD, InsertPos); |
| 100 | ++SymbolCounter; |
| 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 103 | return cast<SymbolRegionValue>(SD); |
Zhongxing Xu | eabf776 | 2008-11-19 11:03:17 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 106 | const SymbolConjured* |
| 107 | SymbolManager::getConjuredSymbol(const Stmt* E, QualType T, unsigned Count, |
| 108 | const void* SymbolTag) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 110 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | a880b66 | 2009-03-04 22:53:46 +0000 | [diff] [blame] | 111 | SymbolConjured::Profile(profile, E, T, Count, SymbolTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | void* InsertPos; |
| 113 | SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 114 | if (!SD) { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 115 | SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | new (SD) SymbolConjured(SymbolCounter, E, T, Count, SymbolTag); |
| 117 | DataSet.InsertNode(SD, InsertPos); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 118 | ++SymbolCounter; |
| 119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 121 | return cast<SymbolConjured>(SD); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 124 | const SymbolDerived* |
| 125 | SymbolManager::getDerivedSymbol(SymbolRef parentSymbol, |
| 126 | const TypedRegion *R) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 128 | llvm::FoldingSetNodeID profile; |
| 129 | SymbolDerived::Profile(profile, parentSymbol, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | void* InsertPos; |
| 131 | SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 132 | if (!SD) { |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 133 | SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>(); |
| 134 | new (SD) SymbolDerived(SymbolCounter, parentSymbol, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | DataSet.InsertNode(SD, InsertPos); |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 136 | ++SymbolCounter; |
| 137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 139 | return cast<SymbolDerived>(SD); |
| 140 | } |
| 141 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 142 | const SymbolExtent* |
| 143 | SymbolManager::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 Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 158 | const SymbolMetadata* |
| 159 | SymbolManager::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 Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 176 | const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | BinaryOperator::Opcode op, |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 178 | const llvm::APSInt& v, |
| 179 | QualType t) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 180 | llvm::FoldingSetNodeID ID; |
| 181 | SymIntExpr::Profile(ID, lhs, op, v, t); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 182 | void *InsertPos; |
| 183 | SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 185 | if (!data) { |
| 186 | data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>(); |
| 187 | new (data) SymIntExpr(lhs, op, v, t); |
| 188 | DataSet.InsertNode(data, InsertPos); |
| 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 191 | return cast<SymIntExpr>(data); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 194 | const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs, |
| 195 | BinaryOperator::Opcode op, |
| 196 | const SymExpr *rhs, |
| 197 | QualType t) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 198 | llvm::FoldingSetNodeID ID; |
| 199 | SymSymExpr::Profile(ID, lhs, op, rhs, t); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 200 | void *InsertPos; |
| 201 | SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 203 | if (!data) { |
| 204 | data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>(); |
| 205 | new (data) SymSymExpr(lhs, op, rhs, t); |
| 206 | DataSet.InsertNode(data, InsertPos); |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 209 | return cast<SymSymExpr>(data); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 212 | QualType SymbolConjured::getType(ASTContext&) const { |
| 213 | return T; |
| 214 | } |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 216 | QualType SymbolDerived::getType(ASTContext& Ctx) const { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 217 | return R->getValueType(); |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 220 | QualType SymbolExtent::getType(ASTContext& Ctx) const { |
| 221 | return Ctx.getSizeType(); |
| 222 | } |
| 223 | |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 224 | QualType SymbolMetadata::getType(ASTContext&) const { |
| 225 | return T; |
| 226 | } |
| 227 | |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 228 | QualType SymbolRegionValue::getType(ASTContext& C) const { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 229 | return R->getValueType(); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 232 | SymbolManager::~SymbolManager() {} |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 234 | bool SymbolManager::canSymbolicate(QualType T) { |
Ted Kremenek | 17c571c | 2010-07-29 00:28:43 +0000 | [diff] [blame] | 235 | 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 Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 247 | void SymbolReaper::markLive(SymbolRef sym) { |
Ted Kremenek | a97d54c | 2009-09-02 06:03:18 +0000 | [diff] [blame] | 248 | TheLiving.insert(sym); |
| 249 | TheDead.erase(sym); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 252 | void SymbolReaper::markInUse(SymbolRef sym) { |
| 253 | if (isa<SymbolMetadata>(sym)) |
| 254 | MetadataInUse.insert(sym); |
| 255 | } |
| 256 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 257 | bool SymbolReaper::maybeDead(SymbolRef sym) { |
| 258 | if (isLive(sym)) |
| 259 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | a97d54c | 2009-09-02 06:03:18 +0000 | [diff] [blame] | 261 | TheDead.insert(sym); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 265 | static 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 Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 290 | bool SymbolReaper::isLive(SymbolRef sym) { |
Ted Kremenek | a97d54c | 2009-09-02 06:03:18 +0000 | [diff] [blame] | 291 | if (TheLiving.count(sym)) |
Ted Kremenek | dcb6a26 | 2009-01-22 18:51:33 +0000 | [diff] [blame] | 292 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 294 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 302 | if (const SymbolExtent *extent = dyn_cast<SymbolExtent>(sym)) { |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 303 | 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 Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Ted Kremenek | dcb6a26 | 2009-01-22 18:51:33 +0000 | [diff] [blame] | 321 | // Interogate the symbol. It may derive from an input value to |
| 322 | // the analyzed function/method. |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 323 | return isa<SymbolRegionValue>(sym); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 325 | |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 326 | bool SymbolReaper::isLive(const Stmt* ExprVal) const { |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 327 | return LCtx->getAnalysisContext()->getRelaxedLiveVariables()-> |
| 328 | isLive(Loc, ExprVal); |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 331 | bool SymbolReaper::isLive(const VarRegion *VR) const { |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 332 | const StackFrameContext *VarContext = VR->getStackFrame(); |
| 333 | const StackFrameContext *CurrentContext = LCtx->getCurrentStackFrame(); |
Zhongxing Xu | df3a61b | 2010-02-17 08:50:05 +0000 | [diff] [blame] | 334 | |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 335 | if (VarContext == CurrentContext) |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 336 | return LCtx->getAnalysisContext()->getRelaxedLiveVariables()-> |
| 337 | isLive(Loc, VR->getDecl()); |
Jordy Rose | bd533aa | 2010-08-14 20:18:45 +0000 | [diff] [blame] | 338 | |
| 339 | return VarContext->isParentOf(CurrentContext); |
Ted Kremenek | edeb5b6 | 2009-12-04 20:32:20 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 342 | SymbolVisitor::~SymbolVisitor() {} |