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"
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#include <optional>
24#include <memory>
25
26using namespace lldb;
27using namespace lldb_private;
28
30
32 std::shared_ptr<Target> target_sp)
33 : m_target_sp(target_sp) {}
34
36 const lldb::ValueObjectSP &valobj_sp) {
37 return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
38}
39
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
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.
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 this
69 // variable id will be used for the next persistent variable.
70 if (variable_id == m_next_persistent_variable_id - 1)
72}
73
74std::optional<CompilerType>
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 if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
84 const_cast<clang::Type *>(tdecl->getTypeForDecl()));
85 return CompilerType(p.m_context, t);
86 }
87 return std::nullopt;
88}
89
91 ConstString name, clang::NamedDecl *decl,
92 std::shared_ptr<TypeSystemClang> ctx) {
93 PersistentDecl p = {decl, ctx};
94 m_persistent_decls.insert(std::make_pair(name.GetCString(), p));
95
96 if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
97 for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
98 p = {enumerator_decl, ctx};
99 m_persistent_decls.insert(std::make_pair(
100 ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
101 }
102 }
103}
104
105clang::NamedDecl *
107 return m_persistent_decls.lookup(name.GetCString()).m_decl;
108}
109
110std::shared_ptr<ClangASTImporter>
112 if (!m_ast_importer_sp) {
113 m_ast_importer_sp = std::make_shared<ClangASTImporter>();
114 }
115 return m_ast_importer_sp;
116}
117
118std::shared_ptr<ClangModulesDeclVendor>
123 }
125}
126
129 llvm::SmallString<64> name;
130 {
131 llvm::raw_svector_ostream os(name);
132 os << GetPersistentVariablePrefix(is_error)
134 }
135 return ConstString(name);
136}
"lldb/Expression/ClangExpressionVariable.h" Encapsulates one variable for the expression parser.
static ClangModulesDeclVendor * Create(Target &target)
void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl, std::shared_ptr< TypeSystemClang > ctx)
ConstString GetNextPersistentVariableName(bool is_error=false) override
ClangPersistentVariables(std::shared_ptr< Target > target_sp)
std::shared_ptr< ClangModulesDeclVendor > GetClangModulesDeclVendor()
lldb::ExpressionVariableSP CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) override
clang::NamedDecl * GetPersistentDecl(ConstString name)
void RemovePersistentVariable(lldb::ExpressionVariableSP variable) override
std::shared_ptr< ClangModulesDeclVendor > m_modules_decl_vendor_sp
PersistentDeclMap m_persistent_decls
Persistent entities declared by the user.
std::shared_ptr< ClangASTImporter > GetClangASTImporter()
std::optional< CompilerType > GetCompilerTypeFromPersistentDecl(ConstString type_name) override
llvm::StringRef GetPersistentVariablePrefix(bool is_error=false) const override
std::shared_ptr< ClangASTImporter > m_ast_importer_sp
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:214
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
void * opaque_compiler_type_t
Definition: lldb-types.h:87
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
Definition: lldb-forward.h:343
ByteOrder
Byte ordering definitions.
lldb::TypeSystemWP m_context
The TypeSystemClang for the ASTContext of m_decl.