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