139 lines · cpp
1//===-- ClangPersistentVariables.cpp --------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "ClangPersistentVariables.h"10#include "ClangASTImporter.h"11#include "ClangModulesDeclVendor.h"12 13#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"14#include "lldb/Core/Value.h"15#include "lldb/Target/Target.h"16#include "lldb/Utility/DataExtractor.h"17#include "lldb/Utility/Log.h"18#include "lldb/Utility/StreamString.h"19 20#include "clang/AST/Decl.h"21 22#include "llvm/ADT/StringMap.h"23#include <optional>24#include <memory>25 26using namespace lldb;27using namespace lldb_private;28 29char ClangPersistentVariables::ID;30 31ClangPersistentVariables::ClangPersistentVariables(32 std::shared_ptr<Target> target_sp)33 : m_target_sp(target_sp) {}34 35ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(36 const lldb::ValueObjectSP &valobj_sp) {37 return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));38}39 40ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(41 ExecutionContextScope *exe_scope, ConstString name,42 const CompilerType &compiler_type, lldb::ByteOrder byte_order,43 uint32_t addr_byte_size) {44 return AddNewlyConstructedVariable(new ClangExpressionVariable(45 exe_scope, name, compiler_type, byte_order, addr_byte_size));46}47 48void ClangPersistentVariables::RemovePersistentVariable(49 lldb::ExpressionVariableSP variable) {50 RemoveVariable(variable);51 52 // Check if the removed variable was the last one that was created. If yes,53 // reuse the variable id for the next variable.54 55 // Nothing to do if we have not assigned a variable id so far.56 if (m_next_persistent_variable_id == 0)57 return;58 59 llvm::StringRef name = variable->GetName().GetStringRef();60 // Remove the prefix from the variable that only the indes is left.61 if (!name.consume_front(GetPersistentVariablePrefix(false)))62 return;63 64 // Check if the variable contained a variable id.65 uint32_t variable_id;66 if (name.getAsInteger(10, variable_id))67 return;68 // If it's the most recent variable id that was assigned, make sure that this69 // variable id will be used for the next persistent variable.70 if (variable_id == m_next_persistent_variable_id - 1)71 m_next_persistent_variable_id--;72}73 74std::optional<CompilerType>75ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(76 ConstString type_name) {77 PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());78 79 if (p.m_decl == nullptr)80 return std::nullopt;81 82 auto ctx = std::static_pointer_cast<TypeSystemClang>(p.m_context.lock());83 if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {84 opaque_compiler_type_t t =85 static_cast<opaque_compiler_type_t>(const_cast<clang::Type *>(86 ctx->getASTContext().getTypeDeclType(tdecl).getTypePtr()));87 return CompilerType(p.m_context, t);88 }89 return std::nullopt;90}91 92void ClangPersistentVariables::RegisterPersistentDecl(93 ConstString name, clang::NamedDecl *decl,94 std::shared_ptr<TypeSystemClang> ctx) {95 PersistentDecl p = {decl, ctx};96 m_persistent_decls.insert(std::make_pair(name.GetCString(), p));97 98 if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {99 for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {100 p = {enumerator_decl, ctx};101 m_persistent_decls.insert(std::make_pair(102 ConstString(enumerator_decl->getNameAsString()).GetCString(), p));103 }104 }105}106 107clang::NamedDecl *108ClangPersistentVariables::GetPersistentDecl(ConstString name) {109 return m_persistent_decls.lookup(name.GetCString()).m_decl;110}111 112std::shared_ptr<ClangASTImporter>113ClangPersistentVariables::GetClangASTImporter() {114 if (!m_ast_importer_sp) {115 m_ast_importer_sp = std::make_shared<ClangASTImporter>();116 }117 return m_ast_importer_sp;118}119 120std::shared_ptr<ClangModulesDeclVendor>121ClangPersistentVariables::GetClangModulesDeclVendor() {122 if (!m_modules_decl_vendor_sp) {123 m_modules_decl_vendor_sp.reset(124 ClangModulesDeclVendor::Create(*m_target_sp));125 }126 return m_modules_decl_vendor_sp;127}128 129ConstString130ClangPersistentVariables::GetNextPersistentVariableName(bool is_error) {131 llvm::SmallString<64> name;132 {133 llvm::raw_svector_ostream os(name);134 os << GetPersistentVariablePrefix(is_error)135 << m_next_persistent_variable_id++;136 }137 return ConstString(name);138}139