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 | |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 7 | extern char *basename(const char *); |
| 8 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 9 | static void PrintCursor(CXCursor Cursor) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 10 | if (clang_isInvalid(Cursor.kind)) |
| 11 | printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 12 | else { |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 13 | printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), |
| 14 | clang_getCursorSpelling(Cursor)); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame^] | 15 | CXDecl DeclReferenced = clang_getCursorDecl(Cursor); |
| 16 | if (DeclReferenced) |
| 17 | printf(":%d:%d", clang_getDeclLine(DeclReferenced), |
| 18 | clang_getDeclColumn(DeclReferenced)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 19 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 20 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 21 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 22 | static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) |
| 23 | { |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 24 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 25 | printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)), |
| 26 | clang_getCursorLine(Cursor), |
| 27 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 28 | PrintCursor(Cursor); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 29 | printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl)); |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 30 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 31 | } |
| 32 | static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
| 33 | CXClientData Filter) |
| 34 | { |
| 35 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 36 | printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)), |
| 37 | clang_getCursorLine(Cursor), |
| 38 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 39 | PrintCursor(Cursor); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 40 | printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit))); |
| 41 | |
| 42 | clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 43 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 44 | if (Cursor.kind == CXCursor_FunctionDefn) { |
| 45 | const char *startBuf, *endBuf; |
| 46 | unsigned startLine, startColumn, endLine, endColumn; |
| 47 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 48 | &startLine, &startColumn, |
| 49 | &endLine, &endColumn); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 50 | { |
| 51 | /* Probe the entire body, looking for both decls and refs. */ |
| 52 | unsigned curLine = startLine, curColumn = startColumn; |
| 53 | CXCursor Ref; |
| 54 | |
| 55 | while (startBuf <= endBuf) { |
| 56 | if (*startBuf == '\n') { |
| 57 | startBuf++; |
| 58 | curLine++; |
| 59 | curColumn = 1; |
| 60 | } else if (*startBuf != '\t') |
| 61 | curColumn++; |
| 62 | |
| 63 | Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor), |
| 64 | curLine, curColumn); |
| 65 | if (Ref.kind != CXCursor_FunctionDecl) { |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 66 | printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)), |
| 67 | curLine, curColumn); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 68 | PrintCursor(Ref); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 69 | printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl)); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 70 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 71 | startBuf++; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 72 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 75 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 76 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * First sign of life:-) |
| 80 | */ |
| 81 | int main(int argc, char **argv) { |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 82 | if (argc != 3) { |
| 83 | printf("Incorrect usage of c-index-test (requires 3 arguments)\n"); |
| 84 | return 0; |
| 85 | } |
| 86 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 87 | CXIndex Idx = clang_createIndex(); |
| 88 | CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 89 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 90 | |
| 91 | if (!strcmp(argv[2], "all")) { |
| 92 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); |
| 93 | return 1; |
| 94 | } |
| 95 | /* Perform some simple filtering. */ |
| 96 | if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl; |
| 97 | else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 98 | else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 99 | else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl; |
| 100 | else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl; |
| 101 | |
| 102 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 103 | return 1; |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 104 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 105 | } |