Skip to content

Commit 36fb199

Browse files
committed
[lldb][NFC] Remove GetASTContext call in ClangPersistentVariables
We try to build a CompilerType from the persistent decls so we need a ClangASTContext. With this patch the ClangPersistentVariables store the associated ClangASTContext of the persistent decls (which is always the scratch ClangASTContext) and no longer call GetASTContext to map back from clang::ASTContext to ClangASTContext.
1 parent 0acfc49 commit 36fb199

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,17 @@ void ASTResultSynthesizer::RecordPersistentDecl(NamedDecl *D) {
448448
}
449449

450450
void ASTResultSynthesizer::CommitPersistentDecls() {
451+
PersistentExpressionState *state =
452+
m_target.GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC);
453+
auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
454+
ClangASTContext *scratch_ctx = ClangASTContext::GetScratch(m_target);
455+
451456
for (clang::NamedDecl *decl : m_decls) {
452457
StringRef name = decl->getName();
453458
ConstString name_cs(name.str().c_str());
454459

455460
Decl *D_scratch = m_target.GetClangASTImporter()->DeportDecl(
456-
&ClangASTContext::GetScratch(m_target)->getASTContext(), decl);
461+
&scratch_ctx->getASTContext(), decl);
457462

458463
if (!D_scratch) {
459464
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
@@ -471,10 +476,8 @@ void ASTResultSynthesizer::CommitPersistentDecls() {
471476
}
472477

473478
if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch))
474-
llvm::cast<ClangPersistentVariables>(
475-
m_target.GetPersistentExpressionStateForLanguage(
476-
lldb::eLanguageTypeC))
477-
->RegisterPersistentDecl(name_cs, NamedDecl_scratch);
479+
persistent_vars->RegisterPersistentDecl(name_cs, NamedDecl_scratch,
480+
scratch_ctx);
478481
}
479482
}
480483

lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,36 @@ llvm::Optional<CompilerType>
6868
ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
6969
ConstString type_name) {
7070
CompilerType compiler_type;
71-
if (clang::TypeDecl *tdecl = llvm::dyn_cast_or_null<clang::TypeDecl>(
72-
GetPersistentDecl(type_name))) {
73-
compiler_type.SetCompilerType(
74-
ClangASTContext::GetASTContext(&tdecl->getASTContext()),
75-
reinterpret_cast<lldb::opaque_compiler_type_t>(
76-
const_cast<clang::Type *>(tdecl->getTypeForDecl())));
77-
return compiler_type;
71+
72+
PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());
73+
74+
if (p.m_decl == nullptr)
75+
return llvm::None;
76+
77+
if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
78+
opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
79+
const_cast<clang::Type *>(tdecl->getTypeForDecl()));
80+
return CompilerType(p.m_context, t);
7881
}
7982
return llvm::None;
8083
}
8184

8285
void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
83-
clang::NamedDecl *decl) {
84-
m_persistent_decls.insert(
85-
std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
86+
clang::NamedDecl *decl,
87+
ClangASTContext *ctx) {
88+
PersistentDecl p = {decl, ctx};
89+
m_persistent_decls.insert(std::make_pair(name.GetCString(), p));
8690

8791
if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
8892
for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
89-
m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
90-
ConstString(enumerator_decl->getNameAsString()).GetCString(),
91-
enumerator_decl));
93+
p = {enumerator_decl, ctx};
94+
m_persistent_decls.insert(std::make_pair(
95+
ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
9296
}
9397
}
9498
}
9599

96100
clang::NamedDecl *
97101
ClangPersistentVariables::GetPersistentDecl(ConstString name) {
98-
PersistentDeclMap::const_iterator i =
99-
m_persistent_decls.find(name.GetCString());
100-
101-
if (i == m_persistent_decls.end())
102-
return nullptr;
103-
else
104-
return i->second;
102+
return m_persistent_decls.lookup(name.GetCString()).m_decl;
105103
}

lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class ClangPersistentVariables : public PersistentExpressionState {
6262
llvm::Optional<CompilerType>
6363
GetCompilerTypeFromPersistentDecl(ConstString type_name) override;
6464

65-
void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl);
65+
void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl,
66+
ClangASTContext *ctx);
6667

6768
clang::NamedDecl *GetPersistentDecl(ConstString name);
6869

@@ -80,7 +81,14 @@ class ClangPersistentVariables : public PersistentExpressionState {
8081
// The counter used by GetNextPersistentVariableName
8182
uint32_t m_next_persistent_variable_id = 0;
8283

83-
typedef llvm::DenseMap<const char *, clang::NamedDecl *> PersistentDeclMap;
84+
struct PersistentDecl {
85+
/// The persistent decl.
86+
clang::NamedDecl *m_decl = nullptr;
87+
/// The ClangASTContext for the ASTContext of m_decl.
88+
ClangASTContext *m_context = nullptr;
89+
};
90+
91+
typedef llvm::DenseMap<const char *, PersistentDecl> PersistentDeclMap;
8492
PersistentDeclMap
8593
m_persistent_decls; ///< Persistent entities declared by the user.
8694

0 commit comments

Comments
 (0)