diff --git a/lib/Index/IndexingContext.cpp b/lib/Index/IndexingContext.cpp index 3d1d9902dd61b14eee23bcebb838da43f875f00c..424c1034ae4b21d783fb7ac823fdbabf905b44ea 100644 --- a/lib/Index/IndexingContext.cpp +++ b/lib/Index/IndexingContext.cpp @@ -171,7 +171,7 @@ static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) { return nullptr; } -static bool isDeclADefinition(const Decl *D, ASTContext &Ctx) { +static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, ASTContext &Ctx) { if (auto VD = dyn_cast<VarDecl>(D)) return VD->isThisDeclarationADefinition(Ctx); @@ -182,7 +182,7 @@ static bool isDeclADefinition(const Decl *D, ASTContext &Ctx) { return TD->isThisDeclarationADefinition(); if (auto MD = dyn_cast<ObjCMethodDecl>(D)) - return MD->isThisDeclarationADefinition(); + return MD->isThisDeclarationADefinition() || isa<ObjCImplDecl>(ContainerDC); if (isa<TypedefNameDecl>(D) || isa<EnumConstantDecl>(D) || @@ -284,7 +284,7 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc, if (IsRef) Roles |= (unsigned)SymbolRole::Reference; - else if (isDeclADefinition(D, *Ctx)) + else if (isDeclADefinition(D, ContainerDC, *Ctx)) Roles |= (unsigned)SymbolRole::Definition; else Roles |= (unsigned)SymbolRole::Declaration; diff --git a/test/Index/Core/index-source.m b/test/Index/Core/index-source.m index 0869c0ac7e1ad65db5a0f030b8a8f7bb23d1ee72..2e9c01c02d481f2cddc511bdee8115f44f1a7edf 100644 --- a/test/Index/Core/index-source.m +++ b/test/Index/Core/index-source.m @@ -71,3 +71,37 @@ typedef int jmp_buf[(18)]; // CHECK: [[@LINE+2]]:12 | function/C | setjmp | c:@F@setjmp | _setjmp | Decl | rel: 0 // CHECK: [[@LINE+1]]:19 | type-alias/C | jmp_buf | c:index-source.m@T@jmp_buf | <no-cgname> | Ref | rel: 0 extern int setjmp(jmp_buf); + +@class I1; +@interface I1 +// CHECK: [[@LINE+1]]:1 | instance-method/ObjC | meth | c:objc(cs)I1(im)meth | -[I1 meth] | Decl,Dyn,RelChild | rel: 1 +-(void)meth; +@end + +@interface I2 +@property (readwrite) id prop; +@end + +// CHECK: [[@LINE+2]]:17 | field/ObjC | _prop | c:objc(cs)I2@_prop | <no-cgname> | Def,Impl,RelChild | rel: 1 +// CHECK-NEXT: RelChild | I2 | c:objc(cs)I2 +@implementation I2 +// CHECK: [[@LINE+5]]:13 | instance-property/ObjC | prop | c:objc(cs)I2(py)prop | <no-cgname> | Ref | rel: 0 +// CHECK: [[@LINE+4]]:13 | instance-method/ObjC | prop | c:objc(cs)I2(im)prop | -[I2 prop] | Def,RelChild | rel: 1 +// CHECK-NEXT: RelChild | I2 | c:objc(cs)I2 +// CHECK: [[@LINE+2]]:13 | instance-method/ObjC | setProp: | c:objc(cs)I2(im)setProp: | -[I2 setProp:] | Def,RelChild | rel: 1 +// CHECK-NEXT: RelChild | I2 | c:objc(cs)I2 +@synthesize prop = _prop; +@end + +@interface I3 +@property (readwrite) id prop; +-(id)prop; +-(void)setProp:(id)p; +@end + +@implementation I3 +// CHECK: [[@LINE+3]]:13 | instance-property/ObjC | prop | c:objc(cs)I3(py)prop | <no-cgname> | Ref | rel: 0 +// CHECK: [[@LINE+2]]:13 | instance-method/ObjC | prop | c:objc(cs)I3(im)prop | -[I3 prop] | Def,RelChild | rel: 1 +// CHECK: [[@LINE+1]]:13 | instance-method/ObjC | setProp: | c:objc(cs)I3(im)setProp: | -[I3 setProp:] | Def,RelChild | rel: 1 +@synthesize prop = _prop; +@end diff --git a/test/Index/index-decls.m b/test/Index/index-decls.m index a405abc78c884dd36347f58aeb3f6289872cb32d..3564117523e594ddf7feaea87533ead1b85f7a25 100644 --- a/test/Index/index-decls.m +++ b/test/Index/index-decls.m @@ -48,6 +48,12 @@ int test1() { } @end +// rdar://25372906 +@class I5; +@interface I5 +-(void)meth; +@end + // RUN: c-index-test -index-file %s -target x86_64-apple-macosx10.7 > %t // RUN: FileCheck %s -input-file=%t // CHECK: [indexDeclaration]: kind: objc-class | name: I | {{.*}} | loc: 1:12 @@ -71,5 +77,8 @@ int test1() { // CHECK: [indexEntityReference]: kind: function | name: extfn | {{.*}} | loc: 33:10 // CHECK: [indexDeclaration]: kind: objc-class | name: I4 | {{.*}} | loc: 36:12 +// CHECK: [indexEntityReference]: kind: objc-property | name: prop | {{.*}} | cursor: ObjCSynthesizeDecl=prop:37:34 (Definition) | loc: 43:13 | <parent>:: kind: objc-class | name: I4 | {{.*}} | container: [I4:42:17] | refkind: direct // CHECK-NOT: [indexDeclaration]: kind: objc-instance-method {{.*}} loc: 37: // CHECK-NOT: [indexDeclaration]: kind: objc-instance-method {{.*}} loc: 43: + +// CHECK: [indexDeclaration]: kind: objc-instance-method | name: meth | {{.*}} loc: 54:1 | {{.*}} | isRedecl: 0 | isDef: 0 | diff --git a/tools/libclang/CXIndexDataConsumer.cpp b/tools/libclang/CXIndexDataConsumer.cpp index bc19d53aeac63138541e71d976842851f10b806d..3b556d441c7da3066615c20b46205507b5461f2f 100644 --- a/tools/libclang/CXIndexDataConsumer.cpp +++ b/tools/libclang/CXIndexDataConsumer.cpp @@ -92,7 +92,7 @@ public: } bool VisitObjCMethodDecl(const ObjCMethodDecl *D) { - if (D->getDeclContext() != LexicalDC) + if (isa<ObjCImplDecl>(LexicalDC) && !D->isThisDeclarationADefinition()) DataConsumer.handleSynthesizedObjCMethod(D, DeclLoc, LexicalDC); else DataConsumer.handleObjCMethod(D); @@ -191,22 +191,28 @@ bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D, cast<Decl>(ASTNode.ContainerDC), getCXTU()); } else { - const NamedDecl *CursorD = dyn_cast_or_null<NamedDecl>(ASTNode.OrigD); - if (!CursorD) - CursorD = ND; - Cursor = getRefCursor(CursorD, Loc); + if (ASTNode.OrigD) { + if (auto *OrigND = dyn_cast<NamedDecl>(ASTNode.OrigD)) + Cursor = getRefCursor(OrigND, Loc); + else + Cursor = MakeCXCursor(ASTNode.OrigD, CXTU); + } else { + Cursor = getRefCursor(ND, Loc); + } } handleReference(ND, Loc, Cursor, dyn_cast_or_null<NamedDecl>(ASTNode.Parent), ASTNode.ContainerDC, ASTNode.OrigE, Kind); } else { - const DeclContext *DC = nullptr; - for (const auto &SymRel : Relations) { - if (SymRel.Roles & (unsigned)SymbolRole::RelationChildOf) - DC = dyn_cast<DeclContext>(SymRel.RelatedSymbol); + const DeclContext *LexicalDC = ASTNode.ContainerDC; + if (!LexicalDC) { + for (const auto &SymRel : Relations) { + if (SymRel.Roles & (unsigned)SymbolRole::RelationChildOf) + LexicalDC = dyn_cast<DeclContext>(SymRel.RelatedSymbol); + } } - IndexingDeclVisitor(*this, Loc, DC).Visit(ASTNode.OrigD); + IndexingDeclVisitor(*this, Loc, LexicalDC).Visit(ASTNode.OrigD); } return !shouldAbort(); @@ -816,7 +822,7 @@ bool CXIndexDataConsumer::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, const DeclContext *LexicalDC) { DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, /*isContainer=*/false); - return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC, LexicalDC); + return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC, D->getDeclContext()); } bool CXIndexDataConsumer::handleObjCProperty(const ObjCPropertyDecl *D) {