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) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 8 | if (clang_isInvalid(Cursor.kind)) |
| 9 | printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind)); |
| 10 | else |
| 11 | printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind), |
| 12 | clang_getCursorSpelling(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 13 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 14 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 15 | static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) |
| 16 | { |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 17 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 18 | PrintCursor(Cursor); |
Steve Naroff | 8b26cbd | 2009-09-11 00:12:01 +0000 | [diff] [blame] | 19 | printf("(Context: %s", clang_getDeclSpelling(Dcl)); |
| 20 | printf(" Source: %s (%d:%d))\n", clang_getCursorSource(Cursor), |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 21 | clang_getCursorLine(Cursor), |
| 22 | clang_getCursorColumn(Cursor)); |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 23 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 24 | } |
| 25 | static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
| 26 | CXClientData Filter) |
| 27 | { |
| 28 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 29 | PrintCursor(Cursor); |
Steve Naroff | 8b26cbd | 2009-09-11 00:12:01 +0000 | [diff] [blame] | 30 | printf("(Context: %s", clang_getTranslationUnitSpelling(Unit)); |
| 31 | printf(" Source: %s (%d:%d))\n", clang_getCursorSource(Cursor), |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 32 | clang_getCursorLine(Cursor), |
| 33 | clang_getCursorColumn(Cursor)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 34 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame^] | 35 | if (Cursor.kind == CXCursor_FunctionDefn) { |
| 36 | const char *startBuf, *endBuf; |
| 37 | unsigned startLine, startColumn, endLine, endColumn; |
| 38 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 39 | &startLine, &startColumn, |
| 40 | &endLine, &endColumn); |
| 41 | /* Probe the entire body, looking for "refs". */ |
| 42 | unsigned curLine = startLine, curColumn = startColumn; |
| 43 | while (startBuf <= endBuf) { |
| 44 | if (*startBuf == '\n') { |
| 45 | startBuf++; |
| 46 | curLine++; |
| 47 | curColumn = 1; |
| 48 | } |
| 49 | CXCursor Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor), |
| 50 | curLine, curColumn); |
| 51 | if (Ref.kind != CXCursor_FunctionDecl) { |
| 52 | PrintCursor(Ref); |
| 53 | printf("(Context: %s", clang_getDeclSpelling(Ref.decl)); |
| 54 | printf(" Source: %s (%d:%d))\n", clang_getCursorSource(Ref), |
| 55 | curLine, curColumn); |
| 56 | } |
| 57 | startBuf++; |
| 58 | curColumn++; |
| 59 | } |
| 60 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 61 | clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 62 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 63 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * First sign of life:-) |
| 67 | */ |
| 68 | int main(int argc, char **argv) { |
| 69 | CXIndex Idx = clang_createIndex(); |
| 70 | CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 71 | |
| 72 | if (argc == 2) { |
| 73 | /* malloc - returns a cursor of type CXCursor_FunctionDecl */ |
| 74 | CXCursor C = clang_getCursor(TU, "/usr/include/stdlib.h", 169, 7); |
| 75 | PrintCursor(C); |
| 76 | /* methodSignature - returns a cursor of type ObjCInstanceMethodDecl */ |
| 77 | C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1); |
| 78 | PrintCursor(C); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 79 | C = clang_getCursor(TU, "Large.m", 5, 18); |
| 80 | PrintCursor(C); |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 81 | } else if (argc == 3) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 82 | enum CXCursorKind K = CXCursor_NotImplemented; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 83 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 84 | if (!strcmp(argv[2], "all")) { |
| 85 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); |
| 86 | return 1; |
| 87 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 88 | if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl; |
| 89 | else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 90 | else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 91 | else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl; |
| 92 | else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl; |
| 93 | |
| 94 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K); |
| 95 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 96 | return 1; |
| 97 | } |