From 2e331b938b38057e333fab0ba841130ea8467794 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sat, 16 Jan 2010 14:00:32 +0000 Subject: [PATCH] Give CXCursor_ObjCSuperClassRef a sane encoding, which is only known to CXCursor.cpp. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93634 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Index/c-index-api-loadTU-test.m | 4 ++-- tools/CIndex/CIndex.cpp | 20 ++++++++++---------- tools/CIndex/CXCursor.cpp | 24 ++++++++++++++++++++++-- tools/CIndex/CXCursor.h | 12 ++++++++++++ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/test/Index/c-index-api-loadTU-test.m b/test/Index/c-index-api-loadTU-test.m index 7ad12385d2..a851b0ba0f 100644 --- a/test/Index/c-index-api-loadTU-test.m +++ b/test/Index/c-index-api-loadTU-test.m @@ -57,7 +57,7 @@ int main (int argc, const char * argv[]) { // CHECK: c-index-api-loadTU-test.m:8:1: ObjCInstanceMethodDecl=foo:8:1 [Context=Foo] [Extent=8:1:8:6] // CHECK: c-index-api-loadTU-test.m:9:1: ObjCClassMethodDecl=fooC:9:1 [Context=Foo] [Extent=9:1:9:7] // CHECK: c-index-api-loadTU-test.m:13:12: ObjCInterfaceDecl=Bar:13:1 [Context=c-index-api-loadTU-test.m] [Extent=13:1:17:4] -// CHECK: c-index-api-loadTU-test.m:13:18: ObjCSuperClassRef=Foo:13:1 [Context=Bar] [Extent=13:1:17:4] +// CHECK: c-index-api-loadTU-test.m:13:18: ObjCSuperClassRef=Foo:4:1 [Context=Bar] [Extent=4:1:11:4] // CHECK: c-index-api-loadTU-test.m:19:1: ObjCCategoryDecl=FooCat:19:1 [Context=c-index-api-loadTU-test.m] [Extent=19:1:22:4] // CHECK: c-index-api-loadTU-test.m:4:1: ObjCClassRef=Foo:19:1 [Context=FooCat] [Extent=19:1:22:4] // CHECK: c-index-api-loadTU-test.m:20:1: ObjCInstanceMethodDecl=catMethodWithFloat::20:1 [Context=FooCat] [Extent=20:1:20:40] @@ -68,7 +68,7 @@ int main (int argc, const char * argv[]) { // CHECK: c-index-api-loadTU-test.m:24:1: ObjCProtocolRef=Proto:24:1 [Context=SubP] [Extent=24:1:26:4] // CHECK: c-index-api-loadTU-test.m:29:1: ObjCInstanceMethodDecl=spMethod:29:1 [Context=SubP] [Extent=29:1:29:11] // CHECK: c-index-api-loadTU-test.m:32:12: ObjCInterfaceDecl=Baz:32:1 [Context=c-index-api-loadTU-test.m] [Extent=32:1:39:4] -// CHECK: c-index-api-loadTU-test.m:32:18: ObjCSuperClassRef=Bar:32:1 [Context=Baz] [Extent=32:1:39:4] +// CHECK: c-index-api-loadTU-test.m:32:18: ObjCSuperClassRef=Bar:13:1 [Context=Baz] [Extent=13:1:17:4] // CHECK: c-index-api-loadTU-test.m:28:1: ObjCProtocolRef=SubP:28:1 [Context=Baz] [Extent=28:1:30:4] // CHECK: c-index-api-loadTU-test.m:34:9: ObjCIvarDecl=_anIVar:34:9 [Context=Baz] [Extent=34:9:34:15] // CHECK: c-index-api-loadTU-test.m:37:1: ObjCInstanceMethodDecl=bazMethod:37:1 [Context=Baz] [Extent=37:1:37:20] diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 0ecb54a86f..e58a08a0d6 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -209,6 +209,7 @@ class CDeclVisitor : public DeclVisitor { CXCursor C = { CK, { ND, 0, 0 } }; Callback(CDecl, C, CData); } + public: CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, unsigned MaxPCHLevel) : @@ -276,7 +277,10 @@ void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { // Issue callbacks for super class. if (D->getSuperClass()) - Call(CXCursor_ObjCSuperClassRef, D); + Callback(CDecl, + MakeCursorObjCSuperClassRef(D->getSuperClass(), + D->getSuperClassLoc()), + CData); for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I) @@ -340,11 +344,9 @@ static SourceLocation getLocationFromCursor(CXCursor C, assert(OID && "clang_getCursorLine(): Missing category decl"); return OID->getClassInterface()->getLocation(); } - case CXCursor_ObjCSuperClassRef: { - ObjCInterfaceDecl *OID = dyn_cast(ND); - assert(OID && "clang_getCursorLine(): Missing interface decl"); - return OID->getSuperClassLoc(); - } + case CXCursor_ObjCSuperClassRef: + return getCursorObjCSuperClassRef(C).second; + case CXCursor_ObjCProtocolRef: { ObjCProtocolDecl *OID = dyn_cast(ND); assert(OID && "clang_getCursorLine(): Missing protocol decl"); @@ -761,10 +763,8 @@ CXString clang_getCursorSpelling(CXCursor C) { if (clang_isReference(C.kind)) { switch (C.kind) { case CXCursor_ObjCSuperClassRef: { - ObjCInterfaceDecl *OID = dyn_cast(ND); - assert(OID && "clang_getCursorLine(): Missing interface decl"); - return CIndexer::createCXString(OID->getSuperClass()->getIdentifier() - ->getNameStart()); + ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; + return CIndexer::createCXString(Super->getIdentifier()->getNameStart()); } case CXCursor_ObjCClassRef: { if (ObjCInterfaceDecl *OID = dyn_cast(ND)) diff --git a/tools/CIndex/CXCursor.cpp b/tools/CIndex/CXCursor.cpp index a8f3eb1d3d..d6c38673e0 100644 --- a/tools/CIndex/CXCursor.cpp +++ b/tools/CIndex/CXCursor.cpp @@ -7,7 +7,9 @@ // //===----------------------------------------------------------------------===// // -// This file defines routines for manipulating CXCursors. +// This file defines routines for manipulating CXCursors. It should be the +// only file that has internal knowledge of the encoding of the data in +// CXCursor. // //===----------------------------------------------------------------------===// @@ -73,6 +75,21 @@ CXCursor cxcursor::MakeCXCursor(Decl *D) { return MakeCXCursor(GetCursorKind(D), D); } +CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, + SourceLocation Loc) { + void *RawLoc = reinterpret_cast(Loc.getRawEncoding()); + CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, 0 } }; + return C; +} + +std::pair +cxcursor::getCursorObjCSuperClassRef(CXCursor C) { + assert(C.kind == CXCursor_ObjCSuperClassRef); + return std::make_pair(static_cast(C.data[0]), + SourceLocation::getFromRawEncoding( + reinterpret_cast(C.data[1]))); +} + Decl *cxcursor::getCursorDecl(CXCursor Cursor) { return (Decl *)Cursor.data[0]; } @@ -82,6 +99,9 @@ Expr *cxcursor::getCursorExpr(CXCursor Cursor) { } Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { + if (Cursor.kind == CXCursor_ObjCSuperClassRef) + return 0; + return (Stmt *)Cursor.data[1]; } @@ -99,4 +119,4 @@ NamedDecl *cxcursor::getCursorInterfaceParent(CXCursor Cursor) { bool cxcursor::operator==(CXCursor X, CXCursor Y) { return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && X.data[2] == Y.data[2]; -} \ No newline at end of file +} diff --git a/tools/CIndex/CXCursor.h b/tools/CIndex/CXCursor.h index da51f13cdc..c2f8de3356 100644 --- a/tools/CIndex/CXCursor.h +++ b/tools/CIndex/CXCursor.h @@ -15,12 +15,15 @@ #define LLVM_CLANG_CXCursor_H #include "clang-c/Index.h" +#include "clang/Basic/SourceLocation.h" +#include namespace clang { class Decl; class Expr; class NamedDecl; +class ObjCInterfaceDecl; class Stmt; namespace cxcursor { @@ -29,6 +32,15 @@ CXCursor MakeCXCursor(CXCursorKind K, clang::Decl *D); CXCursor MakeCXCursor(CXCursorKind K, clang::Decl *D, clang::Stmt *S); CXCursor MakeCXCursor(clang::Decl *D); +/// \brief Create an Objective-C superclass reference at the given location. +CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, + SourceLocation Loc); + +/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references +/// and optionally the location where the reference occurred. +std::pair +getCursorObjCSuperClassRef(CXCursor C); + Decl *getCursorDecl(CXCursor Cursor); Expr *getCursorExpr(CXCursor Cursor); Stmt *getCursorStmt(CXCursor Cursor); -- 2.40.0