Ted Kremenek | 94e915e | 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 | 2224fcb | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines SymbolManager, a class that manages symbolic values |
Ted Kremenek | 94e915e | 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 | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 19 | SymbolID SymbolManager::getSymbol(VarDecl* D) { |
| 20 | |
| 21 | assert (isa<ParmVarDecl>(D) || D->hasGlobalStorage()); |
| 22 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 23 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 24 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 25 | ParmVarDecl* PD = dyn_cast<ParmVarDecl>(D); |
| 26 | |
| 27 | if (PD) |
| 28 | SymbolDataParmVar::Profile(profile, PD); |
| 29 | else |
| 30 | SymbolDataGlobalVar::Profile(profile, D); |
| 31 | |
| 32 | void* InsertPos; |
| 33 | |
| 34 | SymbolData* SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 35 | |
| 36 | if (SD) |
| 37 | return SD->getSymbol(); |
| 38 | |
| 39 | if (PD) { |
| 40 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataParmVar>(); |
| 41 | new (SD) SymbolDataParmVar(SymbolCounter, PD); |
| 42 | } |
| 43 | else { |
| 44 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataGlobalVar>(); |
| 45 | new (SD) SymbolDataGlobalVar(SymbolCounter, D); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 48 | DataSet.InsertNode(SD, InsertPos); |
| 49 | |
| 50 | DataMap[SymbolCounter] = SD; |
| 51 | return SymbolCounter++; |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 54 | SymbolID SymbolManager::getContentsOfSymbol(SymbolID sym) { |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 56 | llvm::FoldingSetNodeID profile; |
| 57 | SymbolDataContentsOf::Profile(profile, sym); |
| 58 | void* InsertPos; |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 60 | SymbolData* SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 61 | |
| 62 | if (SD) |
| 63 | return SD->getSymbol(); |
| 64 | |
| 65 | SD = (SymbolData*) BPAlloc.Allocate<SymbolDataContentsOf>(); |
| 66 | new (SD) SymbolDataContentsOf(SymbolCounter, sym); |
| 67 | |
| 68 | |
| 69 | DataSet.InsertNode(SD, InsertPos); |
| 70 | DataMap[SymbolCounter] = SD; |
| 71 | |
| 72 | return SymbolCounter++; |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 73 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 75 | SymbolID SymbolManager::getConjuredSymbol(Expr* E, unsigned Count) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 76 | |
| 77 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 78 | SymbolConjured::Profile(profile, E, Count); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 79 | void* InsertPos; |
| 80 | |
| 81 | SymbolData* SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); |
| 82 | |
| 83 | if (SD) |
| 84 | return SD->getSymbol(); |
| 85 | |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 86 | SD = (SymbolData*) BPAlloc.Allocate<SymbolConjured>(); |
| 87 | new (SD) SymbolConjured(SymbolCounter, E, Count); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 88 | |
| 89 | DataSet.InsertNode(SD, InsertPos); |
| 90 | DataMap[SymbolCounter] = SD; |
| 91 | |
| 92 | return SymbolCounter++; |
| 93 | } |
| 94 | |
| 95 | const SymbolData& SymbolManager::getSymbolData(SymbolID Sym) const { |
| 96 | DataMapTy::const_iterator I = DataMap.find(Sym); |
| 97 | assert (I != DataMap.end()); |
| 98 | return *I->second; |
| 99 | } |
| 100 | |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 962af8b | 2008-02-19 20:51:40 +0000 | [diff] [blame] | 102 | QualType SymbolData::getType(const SymbolManager& SymMgr) const { |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 103 | switch (getKind()) { |
| 104 | default: |
| 105 | assert (false && "getType() not implemented for this symbol."); |
| 106 | |
| 107 | case ParmKind: |
| 108 | return cast<SymbolDataParmVar>(this)->getDecl()->getType(); |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 109 | |
| 110 | case GlobalKind: |
| 111 | return cast<SymbolDataGlobalVar>(this)->getDecl()->getType(); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 962af8b | 2008-02-19 20:51:40 +0000 | [diff] [blame] | 113 | case ContentsOfKind: { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 114 | SymbolID x = cast<SymbolDataContentsOf>(this)->getContainerSymbol(); |
Ted Kremenek | 962af8b | 2008-02-19 20:51:40 +0000 | [diff] [blame] | 115 | QualType T = SymMgr.getSymbolData(x).getType(SymMgr); |
| 116 | return T->getAsPointerType()->getPointeeType(); |
| 117 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 119 | case ConjuredKind: |
| 120 | return cast<SymbolConjured>(this)->getExpr()->getType(); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 124 | SymbolManager::~SymbolManager() {} |