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 std::shared_ptr<Target> target_sp)
32 m_target_sp(target_sp) {}
33
35 const lldb::ValueObjectSP &valobj_sp) {
37}
38
40 ExecutionContextScope *exe_scope, ConstString name,
41 const CompilerType &compiler_type, lldb::ByteOrder byte_order,
42 uint32_t addr_byte_size) {
44 exe_scope, name, compiler_type, byte_order, addr_byte_size));
45}
46
48 lldb::ExpressionVariableSP variable) {
49 RemoveVariable(variable);
50
51 // Check if the removed variable was the last one that was created. If yes,
52 // reuse the variable id for the next variable.
53
54 // Nothing to do if we have not assigned a variable id so far.
56 return;
57
58 llvm::StringRef name = variable->GetName().GetStringRef();
59 // Remove the prefix from the variable that only the indes is left.
60 if (!name.consume_front(GetPersistentVariablePrefix(false)))
61 return;
62
63 // Check if the variable contained a variable id.
64 uint32_t variable_id;
65 if (name.getAsInteger(10, variable_id))
66 return;
67 // If it's the most recent variable id that was assigned, make sure that this
68 // variable id will be used for the next persistent variable.
69 if (variable_id == m_next_persistent_variable_id - 1)
71}
72
73std::optional<CompilerType>
75 ConstString type_name) {
76 PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());
77
78 if (p.m_decl == nullptr)
79 return std::nullopt;
80
81 if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
83 const_cast<clang::Type *>(tdecl->getTypeForDecl()));
84 return CompilerType(p.m_context, t);
85 }
86 return std::nullopt;
87}
88
90 ConstString name, clang::NamedDecl *decl,
91 std::shared_ptr<TypeSystemClang> ctx) {
92 PersistentDecl p = {decl, ctx};
93 m_persistent_decls.insert(std::make_pair(name.GetCString(), p));
94
95 if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
96 for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
97 p = {enumerator_decl, ctx};
98 m_persistent_decls.insert(std::make_pair(
99 ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
100 }
101 }
102}
103
104clang::NamedDecl *
106 return m_persistent_decls.lookup(name.GetCString()).m_decl;
107}
108
109std::shared_ptr<ClangASTImporter>
111 if (!m_ast_importer_sp) {
112 m_ast_importer_sp = std::make_shared<ClangASTImporter>();
113 }
114 return m_ast_importer_sp;
115}
116
117std::shared_ptr<ClangModulesDeclVendor>
122 }
124}
125
128 llvm::SmallString<64> name;
129 {
130 llvm::raw_svector_ostream os(name);
131 os << GetPersistentVariablePrefix(is_error)
133 }
134 return ConstString(name);
135}
"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
Return a new persistent variable name with the specified prefix.
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:39
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
lldb::ExpressionVariableSP AddNewlyConstructedVariable(ExpressionVariable *var)
void RemoveVariable(lldb::ExpressionVariableSP var_sp)
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
void * opaque_compiler_type_t
Definition: lldb-types.h:90
ByteOrder
Byte ordering definitions.
lldb::TypeSystemWP m_context
The TypeSystemClang for the ASTContext of m_decl.