Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
39648 views
//===-- ClangPersistentVariables.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 "ClangPersistentVariables.h"9#include "ClangASTImporter.h"10#include "ClangModulesDeclVendor.h"1112#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"13#include "lldb/Core/Value.h"14#include "lldb/Target/Target.h"15#include "lldb/Utility/DataExtractor.h"16#include "lldb/Utility/Log.h"17#include "lldb/Utility/StreamString.h"1819#include "clang/AST/Decl.h"2021#include "llvm/ADT/StringMap.h"22#include <optional>23#include <memory>2425using namespace lldb;26using namespace lldb_private;2728char ClangPersistentVariables::ID;2930ClangPersistentVariables::ClangPersistentVariables(31std::shared_ptr<Target> target_sp)32: m_target_sp(target_sp) {}3334ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(35const lldb::ValueObjectSP &valobj_sp) {36return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));37}3839ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(40ExecutionContextScope *exe_scope, ConstString name,41const CompilerType &compiler_type, lldb::ByteOrder byte_order,42uint32_t addr_byte_size) {43return AddNewlyConstructedVariable(new ClangExpressionVariable(44exe_scope, name, compiler_type, byte_order, addr_byte_size));45}4647void ClangPersistentVariables::RemovePersistentVariable(48lldb::ExpressionVariableSP variable) {49RemoveVariable(variable);5051// Check if the removed variable was the last one that was created. If yes,52// reuse the variable id for the next variable.5354// Nothing to do if we have not assigned a variable id so far.55if (m_next_persistent_variable_id == 0)56return;5758llvm::StringRef name = variable->GetName().GetStringRef();59// Remove the prefix from the variable that only the indes is left.60if (!name.consume_front(GetPersistentVariablePrefix(false)))61return;6263// Check if the variable contained a variable id.64uint32_t variable_id;65if (name.getAsInteger(10, variable_id))66return;67// If it's the most recent variable id that was assigned, make sure that this68// variable id will be used for the next persistent variable.69if (variable_id == m_next_persistent_variable_id - 1)70m_next_persistent_variable_id--;71}7273std::optional<CompilerType>74ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(75ConstString type_name) {76PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());7778if (p.m_decl == nullptr)79return std::nullopt;8081if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {82opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(83const_cast<clang::Type *>(tdecl->getTypeForDecl()));84return CompilerType(p.m_context, t);85}86return std::nullopt;87}8889void ClangPersistentVariables::RegisterPersistentDecl(90ConstString name, clang::NamedDecl *decl,91std::shared_ptr<TypeSystemClang> ctx) {92PersistentDecl p = {decl, ctx};93m_persistent_decls.insert(std::make_pair(name.GetCString(), p));9495if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {96for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {97p = {enumerator_decl, ctx};98m_persistent_decls.insert(std::make_pair(99ConstString(enumerator_decl->getNameAsString()).GetCString(), p));100}101}102}103104clang::NamedDecl *105ClangPersistentVariables::GetPersistentDecl(ConstString name) {106return m_persistent_decls.lookup(name.GetCString()).m_decl;107}108109std::shared_ptr<ClangASTImporter>110ClangPersistentVariables::GetClangASTImporter() {111if (!m_ast_importer_sp) {112m_ast_importer_sp = std::make_shared<ClangASTImporter>();113}114return m_ast_importer_sp;115}116117std::shared_ptr<ClangModulesDeclVendor>118ClangPersistentVariables::GetClangModulesDeclVendor() {119if (!m_modules_decl_vendor_sp) {120m_modules_decl_vendor_sp.reset(121ClangModulesDeclVendor::Create(*m_target_sp));122}123return m_modules_decl_vendor_sp;124}125126ConstString127ClangPersistentVariables::GetNextPersistentVariableName(bool is_error) {128llvm::SmallString<64> name;129{130llvm::raw_svector_ostream os(name);131os << GetPersistentVariablePrefix(is_error)132<< m_next_persistent_variable_id++;133}134return ConstString(name);135}136137138