Skip to content

[lldb][TypeSystemClang] Pass ClangASTMetadata around by value #102161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
LLDB_LOG_ERROR(log, result.takeError(), "Couldn't import decl: {0}");
if (log) {
lldb::user_id_t user_id = LLDB_INVALID_UID;
ClangASTMetadata *metadata = GetDeclMetadata(decl);
if (metadata)
if (std::optional<ClangASTMetadata> metadata = GetDeclMetadata(decl))
user_id = metadata->GetUserID();

if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Expand Down Expand Up @@ -950,7 +949,8 @@ bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
return true;
}

ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
std::optional<ClangASTMetadata>
ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
DeclOrigin decl_origin = GetDeclOrigin(decl);

if (decl_origin.Valid()) {
Expand Down Expand Up @@ -1105,7 +1105,7 @@ ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {

// If we have a forcefully completed type, try to find an actual definition
// for it in other modules.
const ClangASTMetadata *md = m_main.GetDeclMetadata(From);
std::optional<ClangASTMetadata> md = m_main.GetDeclMetadata(From);
auto *td = dyn_cast<TagDecl>(From);
if (td && md && md->IsForcefullyCompleted()) {
Log *log = GetLog(LLDBLog::Expressions);
Expand Down Expand Up @@ -1284,8 +1284,7 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
}

lldb::user_id_t user_id = LLDB_INVALID_UID;
ClangASTMetadata *metadata = m_main.GetDeclMetadata(from);
if (metadata)
if (std::optional<ClangASTMetadata> metadata = m_main.GetDeclMetadata(from))
user_id = metadata->GetUserID();

if (log) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ClangASTImporter {
/// is only a shallow clone that lacks any contents.
void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl);

ClangASTMetadata *GetDeclMetadata(const clang::Decl *decl);
std::optional<ClangASTMetadata> GetDeclMetadata(const clang::Decl *decl);

//
// Namespace maps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
// whatever runtime the debug info says the object pointer belongs to. Do
// that here.

ClangASTMetadata *metadata =
TypeSystemClang::DeclContextGetMetaData(decl_context, function_decl);
if (metadata && metadata->HasObjectPtr()) {
if (std::optional<ClangASTMetadata> metadata =
TypeSystemClang::DeclContextGetMetaData(decl_context,
function_decl);
metadata && metadata->HasObjectPtr()) {
lldb::LanguageType language = metadata->GetObjectPtrLanguage();
if (language == lldb::eLanguageTypeC_plus_plus) {
if (m_enforce_valid_object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ bool AppleObjCDeclVendor::FinishDecl(clang::ObjCInterfaceDecl *interface_decl) {
Log *log(
GetLog(LLDBLog::Expressions)); // FIXME - a more appropriate log channel?

ClangASTMetadata *metadata = m_ast_ctx->GetMetadata(interface_decl);
ObjCLanguageRuntime::ObjCISA objc_isa = 0;
if (metadata)
if (std::optional<ClangASTMetadata> metadata =
m_ast_ctx->GetMetadata(interface_decl))
objc_isa = metadata->GetISAPtr();

if (!objc_isa)
Expand Down Expand Up @@ -559,8 +559,8 @@ uint32_t AppleObjCDeclVendor::FindDecls(ConstString name, bool append,
ast_ctx.getObjCInterfaceType(result_iface_decl);

uint64_t isa_value = LLDB_INVALID_ADDRESS;
ClangASTMetadata *metadata = m_ast_ctx->GetMetadata(result_iface_decl);
if (metadata)
if (std::optional<ClangASTMetadata> metadata =
m_ast_ctx->GetMetadata(result_iface_decl))
isa_value = metadata->GetISAPtr();

LLDB_LOGF(log,
Expand Down
52 changes: 29 additions & 23 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,8 +1787,8 @@ bool TypeSystemClang::RecordHasFields(const RecordDecl *record_decl) {
// -flimit-debug-info instead of just seeing nothing if this is a base class
// (since we were hiding empty base classes), or nothing when you turn open
// an valiable whose type was incomplete.
ClangASTMetadata *meta_data = GetMetadata(record_decl);
if (meta_data && meta_data->IsForcefullyCompleted())
if (std::optional<ClangASTMetadata> meta_data = GetMetadata(record_decl);
meta_data && meta_data->IsForcefullyCompleted())
return true;

return false;
Expand Down Expand Up @@ -2465,27 +2465,31 @@ void TypeSystemClang::SetMetadataAsUserID(const clang::Type *type,
}

void TypeSystemClang::SetMetadata(const clang::Decl *object,
ClangASTMetadata &metadata) {
ClangASTMetadata metadata) {
m_decl_metadata[object] = metadata;
}

void TypeSystemClang::SetMetadata(const clang::Type *object,
ClangASTMetadata &metadata) {
ClangASTMetadata metadata) {
m_type_metadata[object] = metadata;
}

ClangASTMetadata *TypeSystemClang::GetMetadata(const clang::Decl *object) {
std::optional<ClangASTMetadata>
TypeSystemClang::GetMetadata(const clang::Decl *object) {
auto It = m_decl_metadata.find(object);
if (It != m_decl_metadata.end())
return &It->second;
return nullptr;
return It->second;

return std::nullopt;
}

ClangASTMetadata *TypeSystemClang::GetMetadata(const clang::Type *object) {
std::optional<ClangASTMetadata>
TypeSystemClang::GetMetadata(const clang::Type *object) {
auto It = m_type_metadata.find(object);
if (It != m_type_metadata.end())
return &It->second;
return nullptr;
return It->second;

return std::nullopt;
}

void TypeSystemClang::SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object,
Expand Down Expand Up @@ -2934,9 +2938,10 @@ bool TypeSystemClang::IsRuntimeGeneratedType(
clang::ObjCInterfaceDecl *result_iface_decl =
llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);

ClangASTMetadata *ast_metadata = GetMetadata(result_iface_decl);
std::optional<ClangASTMetadata> ast_metadata = GetMetadata(result_iface_decl);
if (!ast_metadata)
return false;

return (ast_metadata->GetISAPtr() != 0);
}

Expand Down Expand Up @@ -3622,8 +3627,8 @@ bool TypeSystemClang::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
if (is_complete)
success = cxx_record_decl->isDynamicClass();
else {
ClangASTMetadata *metadata = GetMetadata(cxx_record_decl);
if (metadata)
if (std::optional<ClangASTMetadata> metadata =
GetMetadata(cxx_record_decl))
success = metadata->GetIsDynamicCXXType();
else {
is_complete = GetType(pointee_qual_type).GetCompleteType();
Expand Down Expand Up @@ -5325,7 +5330,8 @@ GetDynamicArrayInfo(TypeSystemClang &ast, SymbolFile *sym_file,
clang::QualType qual_type,
const ExecutionContext *exe_ctx) {
if (qual_type->isIncompleteArrayType())
if (auto *metadata = ast.GetMetadata(qual_type.getTypePtr()))
if (std::optional<ClangASTMetadata> metadata =
ast.GetMetadata(qual_type.getTypePtr()))
return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
exe_ctx);
return std::nullopt;
Expand Down Expand Up @@ -8866,8 +8872,7 @@ void TypeSystemClang::DumpTypeDescription(lldb::opaque_compiler_type_t type,

CompilerType ct(weak_from_this(), type);
const clang::Type *clang_type = ClangUtil::GetQualType(ct).getTypePtr();
ClangASTMetadata *metadata = GetMetadata(clang_type);
if (metadata) {
if (std::optional<ClangASTMetadata> metadata = GetMetadata(clang_type)) {
metadata->Dump(&s);
}
}
Expand Down Expand Up @@ -9488,7 +9493,7 @@ bool TypeSystemClang::DeclContextIsClassMethod(void *opaque_decl_ctx) {
return true;
} else if (clang::FunctionDecl *fun_decl =
llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
if (ClangASTMetadata *metadata = GetMetadata(fun_decl))
if (std::optional<ClangASTMetadata> metadata = GetMetadata(fun_decl))
return metadata->HasObjectPtr();
}

Expand Down Expand Up @@ -9541,7 +9546,7 @@ TypeSystemClang::DeclContextGetLanguage(void *opaque_decl_ctx) {
} else if (llvm::isa<clang::CXXMethodDecl>(decl_ctx)) {
return eLanguageTypeC_plus_plus;
} else if (auto *fun_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
if (ClangASTMetadata *metadata = GetMetadata(fun_decl))
if (std::optional<ClangASTMetadata> metadata = GetMetadata(fun_decl))
return metadata->GetObjectPtrLanguage();
}

Expand Down Expand Up @@ -9591,7 +9596,7 @@ TypeSystemClang::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
return nullptr;
}

ClangASTMetadata *
std::optional<ClangASTMetadata>
TypeSystemClang::DeclContextGetMetaData(const CompilerDeclContext &dc,
const Decl *object) {
TypeSystemClang *ast = llvm::cast<TypeSystemClang>(dc.GetTypeSystem());
Expand Down Expand Up @@ -9825,8 +9830,7 @@ bool TypeSystemClang::IsForcefullyCompleted(lldb::opaque_compiler_type_t type) {
if (record_type) {
const clang::RecordDecl *record_decl = record_type->getDecl();
assert(record_decl);
ClangASTMetadata *metadata = GetMetadata(record_decl);
if (metadata)
if (std::optional<ClangASTMetadata> metadata = GetMetadata(record_decl))
return metadata->IsForcefullyCompleted();
}
}
Expand All @@ -9836,11 +9840,13 @@ bool TypeSystemClang::IsForcefullyCompleted(lldb::opaque_compiler_type_t type) {
bool TypeSystemClang::SetDeclIsForcefullyCompleted(const clang::TagDecl *td) {
if (td == nullptr)
return false;
ClangASTMetadata *metadata = GetMetadata(td);
if (metadata == nullptr)
std::optional<ClangASTMetadata> metadata = GetMetadata(td);
if (!metadata)
return false;
m_has_forcefully_completed_types = true;
metadata->SetIsForcefullyCompleted();
SetMetadata(td, *metadata);

return true;
}

Expand Down
13 changes: 7 additions & 6 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ class TypeSystemClang : public TypeSystem {
void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id);
void SetMetadataAsUserID(const clang::Type *type, lldb::user_id_t user_id);

void SetMetadata(const clang::Decl *object, ClangASTMetadata &meta_data);
void SetMetadata(const clang::Decl *object, ClangASTMetadata meta_data);

void SetMetadata(const clang::Type *object, ClangASTMetadata &meta_data);
ClangASTMetadata *GetMetadata(const clang::Decl *object);
ClangASTMetadata *GetMetadata(const clang::Type *object);
void SetMetadata(const clang::Type *object, ClangASTMetadata meta_data);
std::optional<ClangASTMetadata> GetMetadata(const clang::Decl *object);
std::optional<ClangASTMetadata> GetMetadata(const clang::Type *object);

void SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object,
clang::AccessSpecifier access);
Expand Down Expand Up @@ -616,8 +616,9 @@ class TypeSystemClang : public TypeSystem {
static clang::NamespaceDecl *
DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc);

static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc,
const clang::Decl *object);
static std::optional<ClangASTMetadata>
DeclContextGetMetaData(const CompilerDeclContext &dc,
const clang::Decl *object);

static clang::ASTContext *
DeclContextGetTypeSystemClang(const CompilerDeclContext &dc);
Expand Down
6 changes: 3 additions & 3 deletions lldb/unittests/Symbol/TestClangASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ TEST_F(TestClangASTImporter, MetadataPropagation) {
ASSERT_NE(nullptr, imported);

// Check that we got the same Metadata.
ASSERT_NE(nullptr, importer.GetDeclMetadata(imported));
ASSERT_NE(std::nullopt, importer.GetDeclMetadata(imported));
EXPECT_EQ(metadata, importer.GetDeclMetadata(imported)->GetUserID());
}

Expand Down Expand Up @@ -219,7 +219,7 @@ TEST_F(TestClangASTImporter, MetadataPropagationIndirectImport) {
ASSERT_NE(nullptr, imported);

// Check that we got the same Metadata.
ASSERT_NE(nullptr, importer.GetDeclMetadata(imported));
ASSERT_NE(std::nullopt, importer.GetDeclMetadata(imported));
EXPECT_EQ(metadata, importer.GetDeclMetadata(imported)->GetUserID());
}

Expand All @@ -244,7 +244,7 @@ TEST_F(TestClangASTImporter, MetadataPropagationAfterCopying) {
source.ast->SetMetadataAsUserID(source.record_decl, metadata);

// Check that we got the same Metadata.
ASSERT_NE(nullptr, importer.GetDeclMetadata(imported));
ASSERT_NE(std::nullopt, importer.GetDeclMetadata(imported));
EXPECT_EQ(metadata, importer.GetDeclMetadata(imported)->GetUserID());
}

Expand Down
Loading