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