LLDB  mainline
ClangPersistentVariables.cpp
Go to the documentation of this file.
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 
10 #include "ClangASTImporter.h"
11 #include "ClangModulesDeclVendor.h"
12 
14 #include "lldb/Core/Value.h"
15 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/Log.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)
30  m_target_sp(target_sp) {}
31 
33  const lldb::ValueObjectSP &valobj_sp) {
35 }
36 
38  ExecutionContextScope *exe_scope, ConstString name,
39  const CompilerType &compiler_type, lldb::ByteOrder byte_order,
40  uint32_t addr_byte_size) {
42  exe_scope, name, compiler_type, byte_order, addr_byte_size));
43 }
44 
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.
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)
69 }
70 
71 llvm::Optional<CompilerType>
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)) {
81  const_cast<clang::Type *>(tdecl->getTypeForDecl()));
82  return CompilerType(p.m_context, t);
83  }
84  return llvm::None;
85 }
86 
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 *
104  return m_persistent_decls.lookup(name.GetCString()).m_decl;
105 }
106 
107 std::shared_ptr<ClangASTImporter>
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>
120  }
122 }
123 
126  llvm::SmallString<64> name;
127  {
128  llvm::raw_svector_ostream os(name);
129  os << GetPersistentVariablePrefix(is_error)
131  }
132  return ConstString(name);
133 }
lldb_private::ExpressionVariableList::RemoveVariable
void RemoveVariable(lldb::ExpressionVariableSP var_sp)
Definition: ExpressionVariable.h:185
lldb_private::ClangPersistentVariables::GetPersistentDecl
clang::NamedDecl * GetPersistentDecl(ConstString name)
Definition: ClangPersistentVariables.cpp:103
ClangModulesDeclVendor.h
lldb_private::ClangPersistentVariables::PersistentDecl
Definition: ClangPersistentVariables.h:96
lldb_private::PersistentExpressionState::LLVMCastKind
LLVMCastKind
Definition: ExpressionVariable.h:206
lldb_private::ClangExpressionVariable
Definition: ClangExpressionVariable.h:60
lldb_private::ExpressionVariableList::AddNewlyConstructedVariable
lldb::ExpressionVariableSP AddNewlyConstructedVariable(ExpressionVariable *var)
Definition: ExpressionVariable.h:138
lldb_private::ClangModulesDeclVendor::Create
static ClangModulesDeclVendor * Create(Target &target)
Definition: ClangModulesDeclVendor.cpp:590
lldb_private::ClangPersistentVariables::PersistentDecl::m_decl
clang::NamedDecl * m_decl
The persistent decl.
Definition: ClangPersistentVariables.h:98
lldb_private::ClangPersistentVariables::GetClangASTImporter
std::shared_ptr< ClangASTImporter > GetClangASTImporter()
Definition: ClangPersistentVariables.cpp:108
Target.h
Log.h
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
TypeSystemClang.h
lldb_private::ClangPersistentVariables::GetCompilerTypeFromPersistentDecl
llvm::Optional< CompilerType > GetCompilerTypeFromPersistentDecl(ConstString type_name) override
Definition: ClangPersistentVariables.cpp:72
StreamString.h
ClangPersistentVariables.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ClangPersistentVariables::GetClangModulesDeclVendor
std::shared_ptr< ClangModulesDeclVendor > GetClangModulesDeclVendor()
Definition: ClangPersistentVariables.cpp:116
uint32_t
lldb_private::ClangPersistentVariables::m_target_sp
std::shared_ptr< Target > m_target_sp
Definition: ClangPersistentVariables.h:113
lldb_private::ClangPersistentVariables::PersistentDecl::m_context
TypeSystemClang * m_context
The TypeSystemClang for the ASTContext of m_decl.
Definition: ClangPersistentVariables.h:100
lldb_private::ClangPersistentVariables::GetNextPersistentVariableName
ConstString GetNextPersistentVariableName(bool is_error=false) override
Return a new persistent variable name with the specified prefix.
Definition: ClangPersistentVariables.cpp:125
lldb_private::ClangPersistentVariables::RegisterPersistentDecl
void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl, TypeSystemClang *ctx)
Definition: ClangPersistentVariables.cpp:87
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::ClangPersistentVariables::m_next_persistent_variable_id
uint32_t m_next_persistent_variable_id
Definition: ClangPersistentVariables.h:94
lldb_private::PersistentExpressionState
Definition: ExpressionVariable.h:203
lldb_private::ClangPersistentVariables::CreatePersistentVariable
lldb::ExpressionVariableSP CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) override
Definition: ClangPersistentVariables.cpp:32
lldb_private::ClangPersistentVariables::m_modules_decl_vendor_sp
std::shared_ptr< ClangModulesDeclVendor > m_modules_decl_vendor_sp
Definition: ClangPersistentVariables.h:112
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::TypeSystemClang
A TypeSystem implementation based on Clang.
Definition: TypeSystemClang.h:106
ClangASTImporter.h
lldb_private::ClangPersistentVariables::m_persistent_decls
PersistentDeclMap m_persistent_decls
Persistent entities declared by the user.
Definition: ClangPersistentVariables.h:105
lldb_private::ClangPersistentVariables::m_ast_importer_sp
std::shared_ptr< ClangASTImporter > m_ast_importer_sp
Definition: ClangPersistentVariables.h:111
lldb_private::ClangPersistentVariables::GetPersistentVariablePrefix
llvm::StringRef GetPersistentVariablePrefix(bool is_error=false) const override
Definition: ClangPersistentVariables.h:86
lldb
Definition: SBAddress.h:15
Value.h
lldb_private::ClangPersistentVariables::RemovePersistentVariable
void RemovePersistentVariable(lldb::ExpressionVariableSP variable) override
Definition: ClangPersistentVariables.cpp:45
lldb::opaque_compiler_type_t
void * opaque_compiler_type_t
Definition: lldb-types.h:90
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138