Skip to content

Commit

Permalink
[lldb][NFC] Remove ClangExternalASTSourceCommon
Browse files Browse the repository at this point in the history
ClangExternalASTSourceCommon's purpose is to store a map from
Decl*/Type* to ClangASTMetadata. Usually this data is accessed
via the ClangASTContext interface which then grabs the
current ExternalASTSource of its ASTContext, tries to cast it
to ClangExternalASTSourceCommon and then accesses the metadata
map. If the casting fails the setter does nothing and the getter
returns a nullptr as if there was no known metadata for a type/decl.

This system breaks as soon as any non-LLDB ExternalASTSource is added via
a multiplexer to our existing ExternalASTSource (in which case we suddenly
loose all out metadata as the casting always fails with an ExternalASTSource
that is not inheriting from ClangExternalASTSourceCommon).

This patch moves the metadata map to the ClangASTContext. This gets
rid of all the fragile casting, the requirement that every ExternalASTSource in
LLDB has to inherit from ClangExternalASTSourceCommon and simplifies
the metadata implementation to a simple map lookup. As ClangExternalASTSourceCommon
had no other purpose than storing metadata, this patch deletes this class
and replaces all uses with clang::ExternalASTSource.

No other code changes in this commit beside the AppleObjCDeclVendor which
was the only code that did not use the ClangASTContext interface but directly
accessed the ClangExternalASTSourceCommon.
  • Loading branch information
Teemperor committed Dec 24, 2019
1 parent 8131c04 commit 4657a39
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 142 deletions.
9 changes: 9 additions & 0 deletions lldb/include/lldb/Symbol/ClangASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,15 @@ class ClangASTContext : public TypeSystem {
std::unique_ptr<clang::MangleContext> m_mangle_ctx_up;
uint32_t m_pointer_byte_size = 0;
bool m_ast_owned = false;

typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap;
/// Maps Decls to their associated ClangASTMetadata.
DeclMetadataMap m_decl_metadata;

typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap;
/// Maps Types to their associated ClangASTMetadata.
TypeMetadataMap m_type_metadata;

/// The sema associated that is currently used to build this ASTContext.
/// May be null if we are already done parsing this ASTContext or the
/// ASTContext wasn't created by parsing source code.
Expand Down
5 changes: 3 additions & 2 deletions lldb/include/lldb/Symbol/ClangExternalASTSourceCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
#ifndef liblldb_ClangExternalASTSourceCallbacks_h_
#define liblldb_ClangExternalASTSourceCallbacks_h_

#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "clang/AST/ExternalASTSource.h"

namespace lldb_private {

class ClangASTContext;

class ClangExternalASTSourceCallbacks : public ClangExternalASTSourceCommon {
class ClangExternalASTSourceCallbacks : public clang::ExternalASTSource {
public:
ClangExternalASTSourceCallbacks(ClangASTContext &ast) : m_ast(ast) {}

Expand Down
82 changes: 0 additions & 82 deletions lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h

This file was deleted.

6 changes: 3 additions & 3 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <set>

#include "lldb/Symbol/ClangASTImporter.h"
#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Target/Target.h"
#include "clang/AST/ExternalASTSource.h"
#include "clang/Basic/IdentifierTable.h"

#include "llvm/ADT/SmallSet.h"
Expand All @@ -29,7 +29,7 @@ namespace lldb_private {
/// knows the name it is looking for, but nothing else. The ExternalSemaSource
/// class provides Decls (VarDecl, FunDecl, TypeDecl) to Clang for these
/// names, consulting the ClangExpressionDeclMap to do the actual lookups.
class ClangASTSource : public ClangExternalASTSourceCommon,
class ClangASTSource : public clang::ExternalASTSource,
public ClangASTImporter::MapCompleter {
public:
/// Constructor
Expand Down Expand Up @@ -211,7 +211,7 @@ class ClangASTSource : public ClangExternalASTSourceCommon,
///
/// Clang AST contexts like to own their AST sources, so this is a state-
/// free proxy object.
class ClangASTSourceProxy : public ClangExternalASTSourceCommon {
class ClangASTSourceProxy : public clang::ExternalASTSource {
public:
ClangASTSourceProxy(ClangASTSource &original) : m_original(original) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "lldb/Host/HostInfo.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/ClangASTMetadata.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@

#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
#include "lldb/Core/Module.h"
#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
#include "lldb/Symbol/ClangASTMetadata.h"
#include "lldb/Symbol/ClangUtil.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/Log.h"

#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"

#include "clang/AST/ExternalASTSource.h"

using namespace lldb_private;

class lldb_private::AppleObjCExternalASTSource
: public ClangExternalASTSourceCommon {
: public clang::ExternalASTSource {
public:
AppleObjCExternalASTSource(AppleObjCDeclVendor &decl_vendor)
: m_decl_vendor(decl_vendor) {}
Expand Down Expand Up @@ -182,7 +182,7 @@ AppleObjCDeclVendor::GetDeclForISA(ObjCLanguageRuntime::ObjCISA isa) {

ClangASTMetadata meta_data;
meta_data.SetISAPtr(isa);
m_external_source->SetMetadata(new_iface_decl, meta_data);
m_ast_ctx.SetMetadata(new_iface_decl, meta_data);

new_iface_decl->setHasExternalVisibleStorage();
new_iface_decl->setHasExternalLexicalStorage();
Expand Down Expand Up @@ -413,7 +413,7 @@ bool AppleObjCDeclVendor::FinishDecl(clang::ObjCInterfaceDecl *interface_decl) {
Log *log(GetLogIfAllCategoriesSet(
LIBLLDB_LOG_EXPRESSIONS)); // FIXME - a more appropriate log channel?

ClangASTMetadata *metadata = m_external_source->GetMetadata(interface_decl);
ClangASTMetadata *metadata = m_ast_ctx.GetMetadata(interface_decl);
ObjCLanguageRuntime::ObjCISA objc_isa = 0;
if (metadata)
objc_isa = metadata->GetISAPtr();
Expand Down Expand Up @@ -577,8 +577,7 @@ AppleObjCDeclVendor::FindDecls(ConstString name, bool append,
ast_ctx.getObjCInterfaceType(result_iface_decl);

uint64_t isa_value = LLDB_INVALID_ADDRESS;
ClangASTMetadata *metadata =
m_external_source->GetMetadata(result_iface_decl);
ClangASTMetadata *metadata = m_ast_ctx.GetMetadata(result_iface_decl);
if (metadata)
isa_value = metadata->GetISAPtr();

Expand Down
1 change: 0 additions & 1 deletion lldb/source/Symbol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ add_lldb_library(lldbSymbol
ClangASTImporter.cpp
ClangASTMetadata.cpp
ClangExternalASTSourceCallbacks.cpp
ClangExternalASTSourceCommon.cpp
ClangUtil.cpp
CompactUnwindInfo.cpp
CompileUnit.cpp
Expand Down
24 changes: 11 additions & 13 deletions lldb/source/Symbol/ClangASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
#include "lldb/Core/ThreadSafeDenseMap.h"
#include "lldb/Core/UniqueCStringMap.h"
#include "lldb/Symbol/ClangASTImporter.h"
#include "lldb/Symbol/ClangASTMetadata.h"
#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
#include "lldb/Symbol/ClangUtil.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolFile.h"
Expand Down Expand Up @@ -2340,31 +2340,29 @@ void ClangASTContext::SetMetadataAsUserID(const clang::Type *type,

void ClangASTContext::SetMetadata(const clang::Decl *object,
ClangASTMetadata &metadata) {
if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
getASTContext().getExternalSource()))
A->SetMetadata(object, metadata);
m_decl_metadata[object] = metadata;
}

void ClangASTContext::SetMetadata(const clang::Type *object,
ClangASTMetadata &metadata) {
if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
getASTContext().getExternalSource()))
A->SetMetadata(object, metadata);
m_type_metadata[object] = metadata;
}

ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
const clang::Decl *object) {
if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
ast->getExternalSource()))
return A->GetMetadata(object);
ClangASTContext *self = GetASTContext(ast);
auto It = self->m_decl_metadata.find(object);
if (It != self->m_decl_metadata.end())
return &It->second;
return nullptr;
}

ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
const clang::Type *object) {
if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
ast->getExternalSource()))
return A->GetMetadata(object);
ClangASTContext *self = GetASTContext(ast);
auto It = self->m_type_metadata.find(object);
if (It != self->m_type_metadata.end())
return &It->second;
return nullptr;
}

Expand Down
34 changes: 0 additions & 34 deletions lldb/source/Symbol/ClangExternalASTSourceCommon.cpp

This file was deleted.

0 comments on commit 4657a39

Please sign in to comment.