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-exception 6 // 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 24 using namespace lldb; 25 using namespace lldb_private; 26 27 ClangPersistentVariables::ClangPersistentVariables( 28 std::shared_ptr<Target> target_sp) 29 : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang), 30 m_target_sp(target_sp) {} 31 32 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable( 33 const lldb::ValueObjectSP &valobj_sp) { 34 return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp)); 35 } 36 37 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable( 38 ExecutionContextScope *exe_scope, ConstString name, 39 const CompilerType &compiler_type, lldb::ByteOrder byte_order, 40 uint32_t addr_byte_size) { 41 return AddNewlyConstructedVariable(new ClangExpressionVariable( 42 exe_scope, name, compiler_type, byte_order, addr_byte_size)); 43 } 44 45 void ClangPersistentVariables::RemovePersistentVariable( 46 lldb::ExpressionVariableSP variable) { 47 RemoveVariable(variable); 48 49 // Check if the removed variable was the last one that was created. If yes, 50 // reuse the variable id for the next variable. 51 52 // Nothing to do if we have not assigned a variable id so far. 53 if (m_next_persistent_variable_id == 0) 54 return; 55 56 llvm::StringRef name = variable->GetName().GetStringRef(); 57 // Remove the prefix from the variable that only the indes is left. 58 if (!name.consume_front(GetPersistentVariablePrefix(false))) 59 return; 60 61 // Check if the variable contained a variable id. 62 uint32_t variable_id; 63 if (name.getAsInteger(10, variable_id)) 64 return; 65 // If it's the most recent variable id that was assigned, make sure that this 66 // variable id will be used for the next persistent variable. 67 if (variable_id == m_next_persistent_variable_id - 1) 68 m_next_persistent_variable_id--; 69 } 70 71 llvm::Optional<CompilerType> 72 ClangPersistentVariables::GetCompilerTypeFromPersistentDecl( 73 ConstString type_name) { 74 PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString()); 75 76 if (p.m_decl == nullptr) 77 return llvm::None; 78 79 if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) { 80 opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>( 81 const_cast<clang::Type *>(tdecl->getTypeForDecl())); 82 return CompilerType(p.m_context, t); 83 } 84 return llvm::None; 85 } 86 87 void ClangPersistentVariables::RegisterPersistentDecl(ConstString name, 88 clang::NamedDecl *decl, 89 TypeSystemClang *ctx) { 90 PersistentDecl p = {decl, ctx}; 91 m_persistent_decls.insert(std::make_pair(name.GetCString(), p)); 92 93 if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) { 94 for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) { 95 p = {enumerator_decl, ctx}; 96 m_persistent_decls.insert(std::make_pair( 97 ConstString(enumerator_decl->getNameAsString()).GetCString(), p)); 98 } 99 } 100 } 101 102 clang::NamedDecl * 103 ClangPersistentVariables::GetPersistentDecl(ConstString name) { 104 return m_persistent_decls.lookup(name.GetCString()).m_decl; 105 } 106 107 std::shared_ptr<ClangASTImporter> 108 ClangPersistentVariables::GetClangASTImporter() { 109 if (!m_ast_importer_sp) { 110 m_ast_importer_sp = std::make_shared<ClangASTImporter>(); 111 } 112 return m_ast_importer_sp; 113 } 114 115 std::shared_ptr<ClangModulesDeclVendor> 116 ClangPersistentVariables::GetClangModulesDeclVendor() { 117 if (!m_modules_decl_vendor_sp) { 118 m_modules_decl_vendor_sp.reset( 119 ClangModulesDeclVendor::Create(*m_target_sp.get())); 120 } 121 return m_modules_decl_vendor_sp; 122 } 123 124 ConstString 125 ClangPersistentVariables::GetNextPersistentVariableName(bool is_error) { 126 llvm::SmallString<64> name; 127 { 128 llvm::raw_svector_ostream os(name); 129 os << GetPersistentVariablePrefix(is_error) 130 << m_next_persistent_variable_id++; 131 } 132 return ConstString(name); 133 } 134