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; |
| 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | case BinaryOperator::GE: os << ">=" ; break; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 42 | 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 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 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 77 | void SymbolRegionValue::dumpToStream(llvm::raw_ostream& os) const { |
| 78 | os << "reg_$" << getSymbolID() << "<" << R << ">"; |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | const SymbolRegionValue* |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 82 | SymbolManager::getRegionValueSymbol(const TypedRegion* R) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 83 | llvm::FoldingSetNodeID profile; |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 84 | SymbolRegionValue::Profile(profile, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | void* InsertPos; |
| 86 | SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 87 | if (!SD) { |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 88 | SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>(); |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 89 | new (SD) SymbolRegionValue(SymbolCounter, R); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 90 | DataSet.InsertNode(SD, InsertPos); |
| 91 | ++SymbolCounter; |
| 92 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 94 | return cast<SymbolRegionValue>(SD); |
Zhongxing Xu | eabf776 | 2008-11-19 11:03:17 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 97 | const SymbolConjured* |
| 98 | SymbolManager::getConjuredSymbol(const Stmt* E, QualType T, unsigned Count, |
| 99 | const void* SymbolTag) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 101 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | a880b66 | 2009-03-04 22:53:46 +0000 | [diff] [blame] | 102 | SymbolConjured::Profile(profile, E, T, Count, SymbolTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | void* InsertPos; |
| 104 | SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 105 | if (!SD) { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 106 | SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | new (SD) SymbolConjured(SymbolCounter, E, T, Count, SymbolTag); |
| 108 | DataSet.InsertNode(SD, InsertPos); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 109 | ++SymbolCounter; |
| 110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 112 | return cast<SymbolConjured>(SD); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 115 | const SymbolDerived* |
| 116 | SymbolManager::getDerivedSymbol(SymbolRef parentSymbol, |
| 117 | const TypedRegion *R) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 119 | llvm::FoldingSetNodeID profile; |
| 120 | SymbolDerived::Profile(profile, parentSymbol, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | void* InsertPos; |
| 122 | SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 123 | if (!SD) { |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 124 | SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>(); |
| 125 | new (SD) SymbolDerived(SymbolCounter, parentSymbol, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | DataSet.InsertNode(SD, InsertPos); |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 127 | ++SymbolCounter; |
| 128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 130 | return cast<SymbolDerived>(SD); |
| 131 | } |
| 132 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 133 | const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | BinaryOperator::Opcode op, |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 135 | const llvm::APSInt& v, |
| 136 | QualType t) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 137 | llvm::FoldingSetNodeID ID; |
| 138 | SymIntExpr::Profile(ID, lhs, op, v, t); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 139 | void *InsertPos; |
| 140 | SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 142 | if (!data) { |
| 143 | data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>(); |
| 144 | new (data) SymIntExpr(lhs, op, v, t); |
| 145 | DataSet.InsertNode(data, InsertPos); |
| 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 148 | return cast<SymIntExpr>(data); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 151 | const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs, |
| 152 | BinaryOperator::Opcode op, |
| 153 | const SymExpr *rhs, |
| 154 | QualType t) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 155 | llvm::FoldingSetNodeID ID; |
| 156 | SymSymExpr::Profile(ID, lhs, op, rhs, t); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 157 | void *InsertPos; |
| 158 | SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 160 | if (!data) { |
| 161 | data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>(); |
| 162 | new (data) SymSymExpr(lhs, op, rhs, t); |
| 163 | DataSet.InsertNode(data, InsertPos); |
| 164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 166 | return cast<SymSymExpr>(data); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 169 | QualType SymbolConjured::getType(ASTContext&) const { |
| 170 | return T; |
| 171 | } |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | fb91c70 | 2009-07-15 02:27:32 +0000 | [diff] [blame] | 173 | |
| 174 | QualType SymbolDerived::getType(ASTContext& Ctx) const { |
| 175 | return R->getValueType(Ctx); |
| 176 | } |
| 177 | |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 178 | QualType SymbolRegionValue::getType(ASTContext& C) const { |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 179 | return R->getValueType(C); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 182 | SymbolManager::~SymbolManager() {} |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 184 | bool SymbolManager::canSymbolicate(QualType T) { |
Ted Kremenek | 4e6e0d7 | 2009-07-29 18:12:48 +0000 | [diff] [blame] | 185 | return Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType()); |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 188 | void SymbolReaper::markLive(SymbolRef sym) { |
Ted Kremenek | a97d54c | 2009-09-02 06:03:18 +0000 | [diff] [blame] | 189 | TheLiving.insert(sym); |
| 190 | TheDead.erase(sym); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bool SymbolReaper::maybeDead(SymbolRef sym) { |
| 194 | if (isLive(sym)) |
| 195 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | a97d54c | 2009-09-02 06:03:18 +0000 | [diff] [blame] | 197 | TheDead.insert(sym); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 201 | bool SymbolReaper::isLive(SymbolRef sym) { |
Ted Kremenek | a97d54c | 2009-09-02 06:03:18 +0000 | [diff] [blame] | 202 | if (TheLiving.count(sym)) |
Ted Kremenek | dcb6a26 | 2009-01-22 18:51:33 +0000 | [diff] [blame] | 203 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 205 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | dcb6a26 | 2009-01-22 18:51:33 +0000 | [diff] [blame] | 213 | // Interogate the symbol. It may derive from an input value to |
| 214 | // the analyzed function/method. |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 215 | return isa<SymbolRegionValue>(sym); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 216 | } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 217 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 218 | bool SymbolReaper::isLive(const Stmt* Loc, const Stmt* ExprVal) const { |
| 219 | return LCtx->getLiveVariables()->isLive(Loc, ExprVal); |
| 220 | } |
| 221 | |
Ted Kremenek | edeb5b6 | 2009-12-04 20:32:20 +0000 | [diff] [blame] | 222 | bool SymbolReaper::isLive(const Stmt *Loc, const VarRegion *VR) const { |
Ted Kremenek | 5348f94 | 2009-12-14 22:15:06 +0000 | [diff] [blame] | 223 | const StackFrameContext *SFC = VR->getStackFrame(); |
Zhongxing Xu | df3a61b | 2010-02-17 08:50:05 +0000 | [diff] [blame] | 224 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 225 | if (SFC == LCtx->getCurrentStackFrame()) |
| 226 | return LCtx->getLiveVariables()->isLive(Loc, VR->getDecl()); |
Zhongxing Xu | df3a61b | 2010-02-17 08:50:05 +0000 | [diff] [blame] | 227 | else |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 228 | return SFC->isParentOf(LCtx->getCurrentStackFrame()); |
Ted Kremenek | edeb5b6 | 2009-12-04 20:32:20 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 231 | SymbolVisitor::~SymbolVisitor() {} |