1 //===-- VariableList.cpp ----------------------------------------*- 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 10 #include "lldb/Symbol/VariableList.h" 11 12 #include "lldb/Core/RegularExpression.h" 13 #include "lldb/Symbol/Block.h" 14 #include "lldb/Symbol/CompileUnit.h" 15 #include "lldb/Symbol/Function.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 //---------------------------------------------------------------------- 21 // VariableList constructor 22 //---------------------------------------------------------------------- 23 VariableList::VariableList() : m_variables() {} 24 25 //---------------------------------------------------------------------- 26 // Destructor 27 //---------------------------------------------------------------------- 28 VariableList::~VariableList() {} 29 30 void VariableList::AddVariable(const VariableSP &var_sp) { 31 m_variables.push_back(var_sp); 32 } 33 34 bool VariableList::AddVariableIfUnique(const lldb::VariableSP &var_sp) { 35 if (FindVariableIndex(var_sp) == UINT32_MAX) { 36 m_variables.push_back(var_sp); 37 return true; 38 } 39 return false; 40 } 41 42 void VariableList::AddVariables(VariableList *variable_list) { 43 if (variable_list) { 44 std::copy(variable_list->m_variables.begin(), // source begin 45 variable_list->m_variables.end(), // source end 46 back_inserter(m_variables)); // destination 47 } 48 } 49 50 void VariableList::Clear() { m_variables.clear(); } 51 52 VariableSP VariableList::GetVariableAtIndex(size_t idx) const { 53 VariableSP var_sp; 54 if (idx < m_variables.size()) 55 var_sp = m_variables[idx]; 56 return var_sp; 57 } 58 59 VariableSP VariableList::RemoveVariableAtIndex(size_t idx) { 60 VariableSP var_sp; 61 if (idx < m_variables.size()) { 62 var_sp = m_variables[idx]; 63 m_variables.erase(m_variables.begin() + idx); 64 } 65 return var_sp; 66 } 67 68 uint32_t VariableList::FindVariableIndex(const VariableSP &var_sp) { 69 iterator pos, end = m_variables.end(); 70 for (pos = m_variables.begin(); pos != end; ++pos) { 71 if (pos->get() == var_sp.get()) 72 return std::distance(m_variables.begin(), pos); 73 } 74 return UINT32_MAX; 75 } 76 77 VariableSP VariableList::FindVariable(const ConstString &name, 78 bool include_static_members) { 79 VariableSP var_sp; 80 iterator pos, end = m_variables.end(); 81 for (pos = m_variables.begin(); pos != end; ++pos) { 82 if ((*pos)->NameMatches(name)) { 83 if (include_static_members || !(*pos)->IsStaticMember()) { 84 var_sp = (*pos); 85 break; 86 } 87 } 88 } 89 return var_sp; 90 } 91 92 VariableSP VariableList::FindVariable(const ConstString &name, 93 lldb::ValueType value_type, 94 bool include_static_members) { 95 VariableSP var_sp; 96 iterator pos, end = m_variables.end(); 97 for (pos = m_variables.begin(); pos != end; ++pos) { 98 if ((*pos)->NameMatches(name) && (*pos)->GetScope() == value_type) { 99 if (include_static_members || !(*pos)->IsStaticMember()) { 100 var_sp = (*pos); 101 break; 102 } 103 } 104 } 105 return var_sp; 106 } 107 108 size_t VariableList::AppendVariablesIfUnique(VariableList &var_list) { 109 const size_t initial_size = var_list.GetSize(); 110 iterator pos, end = m_variables.end(); 111 for (pos = m_variables.begin(); pos != end; ++pos) 112 var_list.AddVariableIfUnique(*pos); 113 return var_list.GetSize() - initial_size; 114 } 115 116 size_t VariableList::AppendVariablesIfUnique(const RegularExpression ®ex, 117 VariableList &var_list, 118 size_t &total_matches) { 119 const size_t initial_size = var_list.GetSize(); 120 iterator pos, end = m_variables.end(); 121 for (pos = m_variables.begin(); pos != end; ++pos) { 122 if ((*pos)->NameMatches(regex)) { 123 // Note the total matches found 124 total_matches++; 125 // Only add this variable if it isn't already in the "var_list" 126 var_list.AddVariableIfUnique(*pos); 127 } 128 } 129 // Return the number of new unique variables added to "var_list" 130 return var_list.GetSize() - initial_size; 131 } 132 133 size_t VariableList::AppendVariablesWithScope(lldb::ValueType type, 134 VariableList &var_list, 135 bool if_unique) { 136 const size_t initial_size = var_list.GetSize(); 137 iterator pos, end = m_variables.end(); 138 for (pos = m_variables.begin(); pos != end; ++pos) { 139 if ((*pos)->GetScope() == type) { 140 if (if_unique) 141 var_list.AddVariableIfUnique(*pos); 142 else 143 var_list.AddVariable(*pos); 144 } 145 } 146 // Return the number of new unique variables added to "var_list" 147 return var_list.GetSize() - initial_size; 148 } 149 150 uint32_t VariableList::FindIndexForVariable(Variable *variable) { 151 VariableSP var_sp; 152 iterator pos; 153 const iterator begin = m_variables.begin(); 154 const iterator end = m_variables.end(); 155 for (pos = m_variables.begin(); pos != end; ++pos) { 156 if ((*pos).get() == variable) 157 return std::distance(begin, pos); 158 } 159 return UINT32_MAX; 160 } 161 162 size_t VariableList::MemorySize() const { 163 size_t mem_size = sizeof(VariableList); 164 const_iterator pos, end = m_variables.end(); 165 for (pos = m_variables.begin(); pos != end; ++pos) 166 mem_size += (*pos)->MemorySize(); 167 return mem_size; 168 } 169 170 size_t VariableList::GetSize() const { return m_variables.size(); } 171 172 void VariableList::Dump(Stream *s, bool show_context) const { 173 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this); 174 // s.Indent(); 175 // s << "VariableList\n"; 176 177 const_iterator pos, end = m_variables.end(); 178 for (pos = m_variables.begin(); pos != end; ++pos) { 179 (*pos)->Dump(s, show_context); 180 } 181 } 182