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