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 | |
| 15 | #include "clang/Analysis/PathSensitive/SymbolManager.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame^] | 19 | SymbolRef SymbolManager::getSymbol(VarDecl* D) { |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 20 | |
Ted Kremenek | 72c59d0 | 2008-08-13 03:28:04 +0000 | [diff] [blame] | 21 | assert (isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) || |
| 22 | D->hasGlobalStorage()); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 24 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 26 | ParmVarDecl* PD = dyn_cast<ParmVarDecl>(D); |
| 27 | |
| 28 | if (PD) |
| 29 | SymbolDataParmVar::Profile(profile, PD); |
| 30 | else |
| 31 | SymbolDataGlobalVar::Profile(profile, D); |
| 32 | |
| 33 | void* InsertPos; |
| 34 | |
| 35 | SymbolData* SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 36 | |
| 37 | if (SD) |
| 38 | return SD->getSymbol(); |
| 39 | |
| 40 | if (PD) { |
| 41 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataParmVar>(); |
| 42 | new (SD) SymbolDataParmVar(SymbolCounter, PD); |
| 43 | } |
| 44 | else { |
| 45 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataGlobalVar>(); |
| 46 | new (SD) SymbolDataGlobalVar(SymbolCounter, D); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 49 | DataSet.InsertNode(SD, InsertPos); |
| 50 | |
| 51 | DataMap[SymbolCounter] = SD; |
| 52 | return SymbolCounter++; |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 53 | } |
Zhongxing Xu | eabf776 | 2008-11-19 11:03:17 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame^] | 55 | SymbolRef SymbolManager::getElementSymbol(const MemRegion* R, |
Zhongxing Xu | eabf776 | 2008-11-19 11:03:17 +0000 | [diff] [blame] | 56 | const llvm::APSInt* Idx){ |
| 57 | llvm::FoldingSetNodeID ID; |
| 58 | SymbolDataElement::Profile(ID, R, Idx); |
| 59 | void* InsertPos; |
| 60 | SymbolData* SD = DataSet.FindNodeOrInsertPos(ID, InsertPos); |
| 61 | |
| 62 | if (SD) |
| 63 | return SD->getSymbol(); |
| 64 | |
| 65 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataElement>(); |
| 66 | new (SD) SymbolDataElement(SymbolCounter, R, Idx); |
| 67 | |
| 68 | DataSet.InsertNode(SD, InsertPos); |
| 69 | DataMap[SymbolCounter] = SD; |
| 70 | return SymbolCounter++; |
| 71 | } |
| 72 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame^] | 73 | SymbolRef SymbolManager::getFieldSymbol(const MemRegion* R, const FieldDecl* D) { |
Zhongxing Xu | eabf776 | 2008-11-19 11:03:17 +0000 | [diff] [blame] | 74 | llvm::FoldingSetNodeID ID; |
| 75 | SymbolDataField::Profile(ID, R, D); |
| 76 | void* InsertPos; |
| 77 | SymbolData* SD = DataSet.FindNodeOrInsertPos(ID, InsertPos); |
| 78 | |
| 79 | if (SD) |
| 80 | return SD->getSymbol(); |
| 81 | |
| 82 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataField>(); |
| 83 | new (SD) SymbolDataField(SymbolCounter, R, D); |
| 84 | |
| 85 | DataSet.InsertNode(SD, InsertPos); |
| 86 | DataMap[SymbolCounter] = SD; |
| 87 | return SymbolCounter++; |
| 88 | } |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame^] | 90 | SymbolRef SymbolManager::getConjuredSymbol(Stmt* E, QualType T, unsigned Count) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 91 | |
| 92 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | 60a6e0c | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 93 | SymbolConjured::Profile(profile, E, T, Count); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 94 | void* InsertPos; |
| 95 | |
| 96 | SymbolData* SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 97 | |
| 98 | if (SD) |
| 99 | return SD->getSymbol(); |
| 100 | |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 101 | SD = (SymbolData*) BPAlloc.Allocate<SymbolConjured>(); |
Ted Kremenek | 60a6e0c | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 102 | new (SD) SymbolConjured(SymbolCounter, E, T, Count); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 103 | |
| 104 | DataSet.InsertNode(SD, InsertPos); |
| 105 | DataMap[SymbolCounter] = SD; |
| 106 | |
| 107 | return SymbolCounter++; |
| 108 | } |
| 109 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame^] | 110 | const SymbolData& SymbolManager::getSymbolData(SymbolRef Sym) const { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 111 | DataMapTy::const_iterator I = DataMap.find(Sym); |
| 112 | assert (I != DataMap.end()); |
| 113 | return *I->second; |
| 114 | } |
| 115 | |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | a888c98 | 2008-02-19 20:51:40 +0000 | [diff] [blame] | 117 | QualType SymbolData::getType(const SymbolManager& SymMgr) const { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 118 | switch (getKind()) { |
| 119 | default: |
| 120 | assert (false && "getType() not implemented for this symbol."); |
| 121 | |
| 122 | case ParmKind: |
| 123 | return cast<SymbolDataParmVar>(this)->getDecl()->getType(); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 124 | |
| 125 | case GlobalKind: |
| 126 | return cast<SymbolDataGlobalVar>(this)->getDecl()->getType(); |
Ted Kremenek | f3d4162 | 2008-12-05 01:31:31 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 128 | case ConjuredKind: |
Ted Kremenek | 60a6e0c | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 129 | return cast<SymbolConjured>(this)->getType(); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 133 | SymbolManager::~SymbolManager() {} |