diff --git a/lib/Index/IndexDecl.cpp b/lib/Index/IndexDecl.cpp index af438f368435d82fa3a41a7a5ece48419c4a3cc5..5f5c49a2e3345b17099ef3e412302f8738634071 100644 --- a/lib/Index/IndexDecl.cpp +++ b/lib/Index/IndexDecl.cpp @@ -227,8 +227,8 @@ public: TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D)); TRY_TO(IndexCtx.indexDeclContext(D)); } else { - return IndexCtx.handleReference(D, D->getLocation(), nullptr, nullptr, - SymbolRoleSet()); + return IndexCtx.handleReference(D, D->getLocation(), nullptr, + D->getDeclContext(), SymbolRoleSet()); } return true; } @@ -239,8 +239,8 @@ public: TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D)); TRY_TO(IndexCtx.indexDeclContext(D)); } else { - return IndexCtx.handleReference(D, D->getLocation(), nullptr, nullptr, - SymbolRoleSet()); + return IndexCtx.handleReference(D, D->getLocation(), nullptr, + D->getDeclContext(), SymbolRoleSet()); } return true; } diff --git a/test/Index/index-refs.m b/test/Index/index-refs.m index f25013b882f6e914dc6978dbd9e55a155e1d9d5a..457712bcbc7ec3a16b0440edaf223df0a6f9f317 100644 --- a/test/Index/index-refs.m +++ b/test/Index/index-refs.m @@ -21,7 +21,12 @@ void foo2() { [I clsMeth]; } +@protocol ForwardProt; + // RUN: c-index-test -index-file %s | FileCheck %s // CHECK: [indexEntityReference]: kind: objc-protocol | name: Prot | {{.*}} | loc: 12:27 // CHECK: [indexEntityReference]: kind: struct | name: FooS | {{.*}} | loc: 13:18 // CHECK: [indexEntityReference]: kind: objc-class | name: I | {{.*}} | loc: 21:4 + +// CHECK: [indexDeclaration]: kind: objc-protocol | name: ForwardProt | {{.*}} | loc: 24:11 +// CHECK-NEXT: <ObjCContainerInfo>: kind: forward-ref diff --git a/tools/libclang/CXIndexDataConsumer.cpp b/tools/libclang/CXIndexDataConsumer.cpp index 322725ec654d4b79322701835c6a3e44fa233844..4f89e43fd7b0b068bcdbe56d7f4e367b17e95e84 100644 --- a/tools/libclang/CXIndexDataConsumer.cpp +++ b/tools/libclang/CXIndexDataConsumer.cpp @@ -171,6 +171,14 @@ bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D, return true; } } + if (auto *ObjCPD = dyn_cast_or_null<ObjCProtocolDecl>(ASTNode.OrigD)) { + if (!ObjCPD->isThisDeclarationADefinition() && + ObjCPD->getLocation() == Loc) { + // The libclang API treats this as ObjCProtocolRef declaration. + IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCPD); + return true; + } + } CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct; if (Roles & (unsigned)SymbolRole::Implicit) {