diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-04-11 19:32:19 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-04-11 19:32:19 +0000 |
commit | d98ef9ae48ab4090d4d5d703ce65cfac62807fda (patch) | |
tree | 10f93d77fa3dec4dc2bef3c008d440205485da6b /tools | |
parent | eba8cd5967e47592285590360bde73063c9c226f (diff) |
[libclang] Introduce a couple of functions to make it convenient
to get at the parameters (and their types) of a function or objc method cursor.
int clang_Cursor_getNumArguments(CXCursor C);
CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i);
rdar://11201527
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154523 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/c-index-test/c-index-test.c | 16 | ||||
-rw-r--r-- | tools/libclang/CXCursor.cpp | 29 | ||||
-rw-r--r-- | tools/libclang/CXType.cpp | 6 | ||||
-rw-r--r-- | tools/libclang/libclang.exports | 2 |
4 files changed, 50 insertions, 3 deletions
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index f82d2d2820..3dfc732d2b 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -663,6 +663,22 @@ static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, clang_disposeString(RS); } } + /* Print the argument types if they exist. */ + { + int numArgs = clang_Cursor_getNumArguments(cursor); + if (numArgs != -1 && numArgs != 0) { + printf(" [args="); + for (int i = 0; i < numArgs; ++i) { + CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i)); + if (T.kind != CXType_Invalid) { + CXString S = clang_getTypeKindSpelling(T.kind); + printf(" %s", clang_getCString(S)); + clang_disposeString(S); + } + } + printf("]"); + } + } /* Print if this is a non-POD type. */ printf(" [isPOD=%d]", clang_isPODType(T)); diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp index 8371a4f273..d84cf29098 100644 --- a/tools/libclang/CXCursor.cpp +++ b/tools/libclang/CXCursor.cpp @@ -1024,6 +1024,35 @@ CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) { return getCursorTU(cursor); } +int clang_Cursor_getNumArguments(CXCursor C) { + if (clang_isDeclaration(C.kind)) { + Decl *D = cxcursor::getCursorDecl(C); + if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) + return MD->param_size(); + if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) + return FD->param_size(); + } + + return -1; +} + +CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) { + if (clang_isDeclaration(C.kind)) { + Decl *D = cxcursor::getCursorDecl(C); + if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) { + if (i < MD->param_size()) + return cxcursor::MakeCXCursor(MD->param_begin()[i], + cxcursor::getCursorTU(C)); + } else if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { + if (i < FD->param_size()) + return cxcursor::MakeCXCursor(FD->param_begin()[i], + cxcursor::getCursorTU(C)); + } + } + + return clang_getNullCursor(); +} + } // end: extern "C" //===----------------------------------------------------------------------===// diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp index 0d4a5195ab..850fac129e 100644 --- a/tools/libclang/CXType.cpp +++ b/tools/libclang/CXType.cpp @@ -455,10 +455,10 @@ CXCallingConv clang_getFunctionTypeCallingConv(CXType X) { return CXCallingConv_Invalid; } -unsigned clang_getNumArgTypes(CXType X) { +int clang_getNumArgTypes(CXType X) { QualType T = GetQualType(X); if (T.isNull()) - return UINT_MAX; + return -1; if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) { return FD->getNumArgs(); @@ -468,7 +468,7 @@ unsigned clang_getNumArgTypes(CXType X) { return 0; } - return UINT_MAX; + return -1; } CXType clang_getArgType(CXType X, unsigned i) { diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index 95cfb025cb..07bdece8eb 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -4,6 +4,8 @@ clang_CXIndex_getGlobalOptions clang_CXIndex_setGlobalOptions clang_CXXMethod_isStatic clang_CXXMethod_isVirtual +clang_Cursor_getArgument +clang_Cursor_getNumArguments clang_Cursor_getSpellingNameRange clang_Cursor_getTranslationUnit clang_Cursor_getObjCSelectorIndex |