Skip to content

Commit

Permalink
[lldb][NFC] Remove GetASTContext call in ClangPersistentVariables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Teemperor authored and bondhugula committed Jan 7, 2020
1 parent 4a9236c commit 4137884
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,17 @@ void ASTResultSynthesizer::RecordPersistentDecl(NamedDecl *D) {
}

void ASTResultSynthesizer::CommitPersistentDecls() {
PersistentExpressionState *state =
m_target.GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC);
auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
ClangASTContext *scratch_ctx = ClangASTContext::GetScratch(m_target);

for (clang::NamedDecl *decl : m_decls) {
StringRef name = decl->getName();
ConstString name_cs(name.str().c_str());

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

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

if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch))
llvm::cast<ClangPersistentVariables>(
m_target.GetPersistentExpressionStateForLanguage(
lldb::eLanguageTypeC))
->RegisterPersistentDecl(name_cs, NamedDecl_scratch);
persistent_vars->RegisterPersistentDecl(name_cs, NamedDecl_scratch,
scratch_ctx);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,36 @@ llvm::Optional<CompilerType>
ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
ConstString type_name) {
CompilerType compiler_type;
if (clang::TypeDecl *tdecl = llvm::dyn_cast_or_null<clang::TypeDecl>(
GetPersistentDecl(type_name))) {
compiler_type.SetCompilerType(
ClangASTContext::GetASTContext(&tdecl->getASTContext()),
reinterpret_cast<lldb::opaque_compiler_type_t>(
const_cast<clang::Type *>(tdecl->getTypeForDecl())));
return compiler_type;

PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());

if (p.m_decl == nullptr)
return llvm::None;

if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
const_cast<clang::Type *>(tdecl->getTypeForDecl()));
return CompilerType(p.m_context, t);
}
return llvm::None;
}

void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
clang::NamedDecl *decl) {
m_persistent_decls.insert(
std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
clang::NamedDecl *decl,
ClangASTContext *ctx) {
PersistentDecl p = {decl, ctx};
m_persistent_decls.insert(std::make_pair(name.GetCString(), p));

if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
ConstString(enumerator_decl->getNameAsString()).GetCString(),
enumerator_decl));
p = {enumerator_decl, ctx};
m_persistent_decls.insert(std::make_pair(
ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
}
}
}

clang::NamedDecl *
ClangPersistentVariables::GetPersistentDecl(ConstString name) {
PersistentDeclMap::const_iterator i =
m_persistent_decls.find(name.GetCString());

if (i == m_persistent_decls.end())
return nullptr;
else
return i->second;
return m_persistent_decls.lookup(name.GetCString()).m_decl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class ClangPersistentVariables : public PersistentExpressionState {
llvm::Optional<CompilerType>
GetCompilerTypeFromPersistentDecl(ConstString type_name) override;

void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl);
void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl,
ClangASTContext *ctx);

clang::NamedDecl *GetPersistentDecl(ConstString name);

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

typedef llvm::DenseMap<const char *, clang::NamedDecl *> PersistentDeclMap;
struct PersistentDecl {
/// The persistent decl.
clang::NamedDecl *m_decl = nullptr;
/// The ClangASTContext for the ASTContext of m_decl.
ClangASTContext *m_context = nullptr;
};

typedef llvm::DenseMap<const char *, PersistentDecl> PersistentDeclMap;
PersistentDeclMap
m_persistent_decls; ///< Persistent entities declared by the user.

Expand Down

0 comments on commit 4137884

Please sign in to comment.