Skip to content

Commit

Permalink
Add Objective-C property accessors loaded from Clang module DWARF to …
Browse files Browse the repository at this point in the history
…lookup

This patch fixes a bug when synthesizing an ObjC property from
-gmodules debug info. Because the method declaration that is injected
via the non-modular property implementation is not added to the
ObjCInterfaceDecl's lookup pointer, a second copy of the accessor
would be generated when processing the ObjCPropertyDecl. This can be
avoided by finding the existing method decl in
ClangExternalASTSourceCallbacks::FindExternalVisibleDeclsByName() and
adding it to the LookupPtr.

Differential Revision: https://reviews.llvm.org/D78333
  • Loading branch information
adrian-prantl committed Apr 24, 2020
1 parent fdbf493 commit ef423a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
Expand Up @@ -10,6 +10,7 @@
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"

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

using namespace lldb_private;

Expand Down Expand Up @@ -46,6 +47,19 @@ void ClangExternalASTSourceCallbacks::FindExternalLexicalDecls(
}
}

bool ClangExternalASTSourceCallbacks::FindExternalVisibleDeclsByName(
const clang::DeclContext *DC, clang::DeclarationName Name) {
llvm::SmallVector<clang::NamedDecl *, 4> decls;
// Objective-C methods are not added into the LookupPtr when they originate
// from an external source. SetExternalVisibleDeclsForName() adds them.
if (auto *oid = llvm::dyn_cast<clang::ObjCInterfaceDecl>(DC)) {
for (auto *omd : oid->methods())
if (omd->getDeclName() == Name)
decls.push_back(omd);
}
return !SetExternalVisibleDeclsForName(DC, Name, decls).empty();
}

OptionalClangModuleID
ClangExternalASTSourceCallbacks::RegisterModule(clang::Module *module) {
m_modules.push_back(module);
Expand Down
Expand Up @@ -30,6 +30,9 @@ class ClangExternalASTSourceCallbacks : public clang::ExternalASTSource {
llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
llvm::SmallVectorImpl<clang::Decl *> &Result) override;

bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name) override;

void CompleteType(clang::TagDecl *tag_decl) override;

void CompleteType(clang::ObjCInterfaceDecl *objc_decl) override;
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Expand Up @@ -344,6 +344,13 @@ static void SetMemberOwningModule(clang::Decl *member,
member->setFromASTFile();
member->setOwningModuleID(id.GetValue());
member->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
if (auto *nd = llvm::dyn_cast<clang::NamedDecl>(member))
if (auto *dc = llvm::dyn_cast<clang::DeclContext>(parent)) {
dc->setHasExternalVisibleStorage(true);
// This triggers ExternalASTSource::FindExternalVisibleDeclsByName() to be
// called when searching for members.
dc->setHasExternalLexicalStorage(true);
}
}

char TypeSystemClang::ID;
Expand Down
13 changes: 10 additions & 3 deletions lldb/test/Shell/SymbolFile/DWARF/module-ownership.mm
Expand Up @@ -33,14 +33,21 @@
// CHECK-DAG: EnumDecl {{.*}} imported in A {{.*}} Enum_e
// FIXME: -EnumConstantDecl {{.*}} imported in A a

@implementation SomeClass {
int private_ivar;
}
@synthesize number = private_ivar;
@end

SomeClass *obj1;
// RUN: lldb-test symbols -dump-clang-ast -find type --language=ObjC++ \
// RUN: -compiler-context 'Module:A,Struct:SomeClass' %t.o \
// RUN: | FileCheck %s --check-prefix=CHECK-OBJC
// CHECK-OBJC: ObjCInterfaceDecl {{.*}} imported in A SomeClass
// CHECK-OBJC: |-ObjCPropertyDecl {{.*}} imported in A number 'int' readonly
// CHECK-OBJC: | `-getter ObjCMethod {{.*}} 'number'
// CHECK-OBJC: `-ObjCMethodDecl {{.*}} imported in A implicit - number 'int'
// CHECK-OBJC-NEXT: |-ObjCIvarDecl
// CHECK-OBJC-NEXT: |-ObjCMethodDecl 0x[[NUMBER:[0-9a-f]+]]{{.*}} imported in A
// CHECK-OBJC-NEXT: `-ObjCPropertyDecl {{.*}} imported in A number 'int' readonly
// CHECK-OBJC-NEXT: `-getter ObjCMethod 0x[[NUMBER]] 'number'

// Template specializations are not yet supported, so they lack the ownership info:
Template<int> t2;
Expand Down

0 comments on commit ef423a3

Please sign in to comment.