From 52d6bbe3aa1e70d40c7cc892a12f41b6017f5c6c Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Sat, 5 Feb 2011 01:10:26 +0000 Subject: [PATCH] Don't crash when generating USRs for ObjC methods in protocols. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124920 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Index/usrs.m | 10 ++++++++-- tools/libclang/CIndexUSRs.cpp | 21 +++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/test/Index/usrs.m b/test/Index/usrs.m index d5da2dcf5e..edfb81437d 100644 --- a/test/Index/usrs.m +++ b/test/Index/usrs.m @@ -76,6 +76,10 @@ int test_multi_declaration(void) { return 0; } +@protocol P1 +- (void)method; +@end + // CHECK: usrs.m c:usrs.m@85@F@my_helper Extent=[3:19 - 3:60] // CHECK: usrs.m c:usrs.m@95@F@my_helper@x Extent=[3:29 - 3:34] // CHECK: usrs.m c:usrs.m@102@F@my_helper@y Extent=[3:36 - 3:41] @@ -131,6 +135,8 @@ int test_multi_declaration(void) { // CHECK: usrs.m c:usrs.m@980@F@test_multi_declaration@foo Extent=[74:3 - 74:14] // CHECK: usrs.m c:usrs.m@980@F@test_multi_declaration@bar Extent=[74:16 - 74:23] // CHECK: usrs.m c:usrs.m@980@F@test_multi_declaration@baz Extent=[74:25 - 74:32] +// CHECK: usrs.m c:objc(pl)P1 Extent=[79:1 - 81:5] +// CHECK: usrs.m c:objc(pl)P1(im)method Extent=[80:1 - 80:16] // RUN: c-index-test -test-load-source all %s | FileCheck -check-prefix=CHECK-source %s // CHECK-source: usrs.m:3:19: FunctionDecl=my_helper:3:19 (Definition) Extent=[3:19 - 3:60] @@ -259,6 +265,6 @@ int test_multi_declaration(void) { // CHECK-source: usrs.m:75:19: DeclRefExpr=baz:74:25 Extent=[75:19 - 75:22] // CHECK-source: usrs.m:76:3: UnexposedStmt= Extent=[76:3 - 76:11] // CHECK-source: usrs.m:76:10: UnexposedExpr= Extent=[76:10 - 76:11] - - +// CHECK-source: usrs.m:79:1: ObjCProtocolDecl=P1:79:1 (Definition) Extent=[79:1 - 81:5] +// CHECK-source: usrs.m:80:1: ObjCInstanceMethodDecl=method:80:1 Extent=[80:1 - 80:16] diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp index 36b91cf978..6843f924c7 100644 --- a/tools/libclang/CIndexUSRs.cpp +++ b/tools/libclang/CIndexUSRs.cpp @@ -283,15 +283,20 @@ void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { } void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { - // The USR for a method declared in a class extension or category is based on - // the ObjCInterfaceDecl, not the ObjCCategoryDecl. - ObjCInterfaceDecl *ID = D->getClassInterface(); - if (!ID) { - IgnoreResults = true; - return; + DeclContext *container = D->getDeclContext(); + if (ObjCProtocolDecl *pd = dyn_cast(container)) { + Visit(pd); + } + else { + // The USR for a method declared in a class extension or category is based on + // the ObjCInterfaceDecl, not the ObjCCategoryDecl. + ObjCInterfaceDecl *ID = D->getClassInterface(); + if (!ID) { + IgnoreResults = true; + return; + } + Visit(ID); } - Visit(ID); - // Ideally we would use 'GenObjCMethod', but this is such a hot path // for Objective-C code that we don't want to use // DeclarationName::getAsString(). -- 2.50.1