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