[index] Fix regression where ObjC method declarations may mistakenly get indexed...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 31 Mar 2016 20:18:22 +0000 (20:18 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 31 Mar 2016 20:18:22 +0000 (20:18 +0000)
rdar://25372906

llvm-svn: 265042

clang/lib/Index/IndexingContext.cpp
clang/test/Index/Core/index-source.m
clang/test/Index/index-decls.m
clang/tools/libclang/CXIndexDataConsumer.cpp

index 3d1d990..424c103 100644 (file)
@@ -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;
index 0869c0a..2e9c01c 100644 (file)
@@ -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
index a405abc..3564117 100644 (file)
@@ -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 |
index bc19d53..3b556d4 100644 (file)
@@ -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) {