From c62ab8d064f6c6a8553ffe4571c9da7e9ecbc337 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 21 Jun 2010 20:48:56 +0000 Subject: [PATCH] Add CXType support for querying the return type of Objective-C methods. This is done by adding a clang_getCursorResultType() function (which complements clang_getResultType()). llvm-svn: 106473 --- clang/include/clang-c/Index.h | 8 +++++++- clang/test/Index/print-typekind.m | 3 +++ clang/tools/c-index-test/c-index-test.c | 2 +- clang/tools/libclang/CXTypes.cpp | 12 ++++++++++++ clang/tools/libclang/libclang.darwin.exports | 1 + clang/tools/libclang/libclang.exports | 1 + 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h index 9a56f332a0f5..b377b6d5fe2c 100644 --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -1141,10 +1141,16 @@ CINDEX_LINKAGE CXCursor clang_getTypeDeclaration(CXType T); CINDEX_LINKAGE CXString clang_getTypeKindSpelling(enum CXTypeKind K); /** - * \brief Retrieve the result type associated with a function or method type. + * \brief Retrieve the result type associated with a function type. */ CINDEX_LINKAGE CXType clang_getResultType(CXType T); +/** + * \brief Retrieve the result type associated with a given cursor. This only + * returns a valid type of the cursor refers to a function or method. + */ +CINDEX_LINKAGE CXType clang_getCursorResultType(CXCursor C); + /** * @} */ diff --git a/clang/test/Index/print-typekind.m b/clang/test/Index/print-typekind.m index 62cbb70c8ee5..68827fb7ca6e 100644 --- a/clang/test/Index/print-typekind.m +++ b/clang/test/Index/print-typekind.m @@ -1,7 +1,10 @@ @interface Foo @property (readonly) id x; +-(int) mymethod; @end // RUN: c-index-test -test-print-typekind %s | FileCheck %s // CHECK: ObjCPropertyDecl=x:2:25 typekind=Typedef [canonical=ObjCObjectPointer] +// CHECK: ObjCInstanceMethodDecl=mymethod:3:1 typekind=Invalid [result=Int] + diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 27c51a0c596a..05e2d9e16b1a 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -470,7 +470,7 @@ static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, } // Print the return type if it exists. { - CXType RT = clang_getResultType(T); + CXType RT = clang_getCursorResultType(cursor); if (RT.kind != CXType_Invalid) { CXString RS = clang_getTypeKindSpelling(RT.kind); printf(" [result=%s]", clang_getCString(RS)); diff --git a/clang/tools/libclang/CXTypes.cpp b/clang/tools/libclang/CXTypes.cpp index 64e49fba2b54..d5c9f452100b 100644 --- a/clang/tools/libclang/CXTypes.cpp +++ b/clang/tools/libclang/CXTypes.cpp @@ -271,4 +271,16 @@ CXType clang_getResultType(CXType X) { return MakeCXType(QualType(), GetASTU(X)); } +CXType clang_getCursorResultType(CXCursor C) { + if (clang_isDeclaration(C.kind)) { + Decl *D = cxcursor::getCursorDecl(C); + if (const ObjCMethodDecl *MD = dyn_cast(D)) + return MakeCXType(MD->getResultType(), cxcursor::getCursorASTUnit(C)); + + return clang_getResultType(clang_getCursorType(C)); + } + + return MakeCXType(QualType(), cxcursor::getCursorASTUnit(C)); +} + } // end: extern "C" diff --git a/clang/tools/libclang/libclang.darwin.exports b/clang/tools/libclang/libclang.darwin.exports index 6c7eddfec62d..f21fec63f48a 100644 --- a/clang/tools/libclang/libclang.darwin.exports +++ b/clang/tools/libclang/libclang.darwin.exports @@ -41,6 +41,7 @@ _clang_getCursorLinkage _clang_getCursorLocation _clang_getCursorReferenced _clang_getCursorSpelling +_clang_getCursorResultType _clang_getCursorType _clang_getCursorUSR _clang_getDefinitionSpellingAndExtent diff --git a/clang/tools/libclang/libclang.exports b/clang/tools/libclang/libclang.exports index 138b9767b19f..dcb40d413ca9 100644 --- a/clang/tools/libclang/libclang.exports +++ b/clang/tools/libclang/libclang.exports @@ -41,6 +41,7 @@ clang_getCursorLinkage clang_getCursorLocation clang_getCursorReferenced clang_getCursorSpelling +clang_getCursorResultType clang_getCursorType clang_getCursorUSR clang_getDefinitionSpellingAndExtent