r265042 - [index] Fix regression where ObjC method declarations may mistakenly get indexed as definition.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 31 13:18:22 PDT 2016


Author: akirtzidis
Date: Thu Mar 31 15:18:22 2016
New Revision: 265042

URL: http://llvm.org/viewvc/llvm-project?rev=265042&view=rev
Log:
[index] Fix regression where ObjC method declarations may mistakenly get indexed as definition.

rdar://25372906

Modified:
    cfe/trunk/lib/Index/IndexingContext.cpp
    cfe/trunk/test/Index/Core/index-source.m
    cfe/trunk/test/Index/index-decls.m
    cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp

Modified: cfe/trunk/lib/Index/IndexingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingContext.cpp?rev=265042&r1=265041&r2=265042&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexingContext.cpp (original)
+++ cfe/trunk/lib/Index/IndexingContext.cpp Thu Mar 31 15:18:22 2016
@@ -171,7 +171,7 @@ static const Decl *adjustTemplateImplici
   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
     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::handleDeclOccurren
 
   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;

Modified: cfe/trunk/test/Index/Core/index-source.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/index-source.m?rev=265042&r1=265041&r2=265042&view=diff
==============================================================================
--- cfe/trunk/test/Index/Core/index-source.m (original)
+++ cfe/trunk/test/Index/Core/index-source.m Thu Mar 31 15:18:22 2016
@@ -71,3 +71,37 @@ typedef int jmp_buf[(18)];
 // CHECK: [[@LINE+2]]:12 | function/C | setjmp | c:@F at setjmp | _setjmp | Decl | rel: 0
 // CHECK: [[@LINE+1]]:19 | type-alias/C | jmp_buf | c:index-source.m at T@jmp_buf | <no-cgname> | Ref | rel: 0
 extern int setjmp(jmp_buf);
+
+ at class I1;
+ at 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;
+ at end
+
+ at interface I2
+ at property (readwrite) id prop;
+ at end
+
+// CHECK: [[@LINE+2]]:17 | field/ObjC | _prop | c:objc(cs)I2 at _prop | <no-cgname> | Def,Impl,RelChild | rel: 1
+// CHECK-NEXT: RelChild | I2 | c:objc(cs)I2
+ at 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
+ at synthesize prop = _prop;
+ at end
+
+ at interface I3
+ at property (readwrite) id prop;
+-(id)prop;
+-(void)setProp:(id)p;
+ at end
+
+ at 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
+ at synthesize prop = _prop;
+ at end

Modified: cfe/trunk/test/Index/index-decls.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/index-decls.m?rev=265042&r1=265041&r2=265042&view=diff
==============================================================================
--- cfe/trunk/test/Index/index-decls.m (original)
+++ cfe/trunk/test/Index/index-decls.m Thu Mar 31 15:18:22 2016
@@ -48,6 +48,12 @@ int test1() {
 }
 @end
 
+// rdar://25372906
+ at class I5;
+ at interface I5
+-(void)meth;
+ at 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 |

Modified: cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp?rev=265042&r1=265041&r2=265042&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp (original)
+++ cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp Thu Mar 31 15:18:22 2016
@@ -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::handleDeclOccu
                                       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::handleSynthesi
                                                  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) {




More information about the cfe-commits mailing list