Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 1 | /* c-index-test.c */ |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 2 | |
| 3 | #include "clang-c/Index.h" |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 4 | #include <stdio.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 5 | #include <string.h> |
| 6 | |
| 7 | static void PrintCursor(CXCursor Cursor) { |
| 8 | printf("%s => %s\n", clang_getCursorKindSpelling(Cursor.kind), |
| 9 | clang_getCursorSpelling(Cursor)); |
| 10 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 11 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 12 | static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) |
| 13 | { |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 14 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 15 | PrintCursor(Cursor); |
| 16 | printf(" Context: %s\n", clang_getDeclSpelling(Dcl)); |
| 17 | printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor), |
| 18 | clang_getCursorLine(Cursor), |
| 19 | clang_getCursorColumn(Cursor)); |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 20 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 21 | } |
| 22 | static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
| 23 | CXClientData Filter) |
| 24 | { |
| 25 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 26 | PrintCursor(Cursor); |
| 27 | printf(" Context: %s\n", clang_getTranslationUnitSpelling(Unit)); |
| 28 | printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor), |
| 29 | clang_getCursorLine(Cursor), |
| 30 | clang_getCursorColumn(Cursor)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 31 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 32 | clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 33 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 34 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * First sign of life:-) |
| 38 | */ |
| 39 | int main(int argc, char **argv) { |
| 40 | CXIndex Idx = clang_createIndex(); |
| 41 | CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 42 | |
| 43 | if (argc == 2) { |
| 44 | /* malloc - returns a cursor of type CXCursor_FunctionDecl */ |
| 45 | CXCursor C = clang_getCursor(TU, "/usr/include/stdlib.h", 169, 7); |
| 46 | PrintCursor(C); |
| 47 | /* methodSignature - returns a cursor of type ObjCInstanceMethodDecl */ |
| 48 | C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1); |
| 49 | PrintCursor(C); |
| 50 | } else if (argc == 3) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 51 | enum CXCursorKind K = CXCursor_Invalid; |
| 52 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 53 | if (!strcmp(argv[2], "all")) { |
| 54 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); |
| 55 | return 1; |
| 56 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 57 | if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl; |
| 58 | else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 59 | else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 60 | else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl; |
| 61 | else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl; |
| 62 | |
| 63 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K); |
| 64 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 65 | return 1; |
| 66 | } |