Path: blob/main/contrib/llvm-project/lldb/source/Symbol/VariableList.cpp
39587 views
//===-- VariableList.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Symbol/VariableList.h"910#include "lldb/Symbol/Block.h"11#include "lldb/Symbol/CompileUnit.h"12#include "lldb/Symbol/Function.h"13#include "lldb/Utility/RegularExpression.h"1415using namespace lldb;16using namespace lldb_private;1718// VariableList constructor19VariableList::VariableList() : m_variables() {}2021// Destructor22VariableList::~VariableList() = default;2324void VariableList::AddVariable(const VariableSP &var_sp) {25m_variables.push_back(var_sp);26}2728bool VariableList::AddVariableIfUnique(const lldb::VariableSP &var_sp) {29if (FindVariableIndex(var_sp) == UINT32_MAX) {30m_variables.push_back(var_sp);31return true;32}33return false;34}3536void VariableList::AddVariables(VariableList *variable_list) {37if (variable_list) {38std::copy(variable_list->m_variables.begin(), // source begin39variable_list->m_variables.end(), // source end40back_inserter(m_variables)); // destination41}42}4344void VariableList::Clear() { m_variables.clear(); }4546VariableSP VariableList::GetVariableAtIndex(size_t idx) const {47VariableSP var_sp;48if (idx < m_variables.size())49var_sp = m_variables[idx];50return var_sp;51}5253VariableSP VariableList::RemoveVariableAtIndex(size_t idx) {54VariableSP var_sp;55if (idx < m_variables.size()) {56var_sp = m_variables[idx];57m_variables.erase(m_variables.begin() + idx);58}59return var_sp;60}6162uint32_t VariableList::FindVariableIndex(const VariableSP &var_sp) {63iterator pos, end = m_variables.end();64for (pos = m_variables.begin(); pos != end; ++pos) {65if (pos->get() == var_sp.get())66return std::distance(m_variables.begin(), pos);67}68return UINT32_MAX;69}7071VariableSP VariableList::FindVariable(ConstString name,72bool include_static_members) {73VariableSP var_sp;74iterator pos, end = m_variables.end();75for (pos = m_variables.begin(); pos != end; ++pos) {76if ((*pos)->NameMatches(name)) {77if (include_static_members || !(*pos)->IsStaticMember()) {78var_sp = (*pos);79break;80}81}82}83return var_sp;84}8586VariableSP VariableList::FindVariable(ConstString name,87lldb::ValueType value_type,88bool include_static_members) {89VariableSP var_sp;90iterator pos, end = m_variables.end();91for (pos = m_variables.begin(); pos != end; ++pos) {92if ((*pos)->NameMatches(name) && (*pos)->GetScope() == value_type) {93if (include_static_members || !(*pos)->IsStaticMember()) {94var_sp = (*pos);95break;96}97}98}99return var_sp;100}101102size_t VariableList::AppendVariablesIfUnique(VariableList &var_list) {103const size_t initial_size = var_list.GetSize();104iterator pos, end = m_variables.end();105for (pos = m_variables.begin(); pos != end; ++pos)106var_list.AddVariableIfUnique(*pos);107return var_list.GetSize() - initial_size;108}109110size_t VariableList::AppendVariablesIfUnique(const RegularExpression ®ex,111VariableList &var_list,112size_t &total_matches) {113const size_t initial_size = var_list.GetSize();114iterator pos, end = m_variables.end();115for (pos = m_variables.begin(); pos != end; ++pos) {116if ((*pos)->NameMatches(regex)) {117// Note the total matches found118total_matches++;119// Only add this variable if it isn't already in the "var_list"120var_list.AddVariableIfUnique(*pos);121}122}123// Return the number of new unique variables added to "var_list"124return var_list.GetSize() - initial_size;125}126127size_t VariableList::AppendVariablesWithScope(lldb::ValueType type,128VariableList &var_list,129bool if_unique) {130const size_t initial_size = var_list.GetSize();131iterator pos, end = m_variables.end();132for (pos = m_variables.begin(); pos != end; ++pos) {133if ((*pos)->GetScope() == type) {134if (if_unique)135var_list.AddVariableIfUnique(*pos);136else137var_list.AddVariable(*pos);138}139}140// Return the number of new unique variables added to "var_list"141return var_list.GetSize() - initial_size;142}143144uint32_t VariableList::FindIndexForVariable(Variable *variable) {145VariableSP var_sp;146iterator pos;147const iterator begin = m_variables.begin();148const iterator end = m_variables.end();149for (pos = m_variables.begin(); pos != end; ++pos) {150if ((*pos).get() == variable)151return std::distance(begin, pos);152}153return UINT32_MAX;154}155156size_t VariableList::MemorySize() const {157size_t mem_size = sizeof(VariableList);158const_iterator pos, end = m_variables.end();159for (pos = m_variables.begin(); pos != end; ++pos)160mem_size += (*pos)->MemorySize();161return mem_size;162}163164size_t VariableList::GetSize() const { return m_variables.size(); }165166void VariableList::Dump(Stream *s, bool show_context) const {167// s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);168// s.Indent();169// s << "VariableList\n";170171const_iterator pos, end = m_variables.end();172for (pos = m_variables.begin(); pos != end; ++pos) {173(*pos)->Dump(s, show_context);174}175}176177178