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" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 4 | #include <stdlib.h> |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 8 | #ifdef _MSC_VER |
| 9 | char *basename(const char* path) |
| 10 | { |
| 11 | char* base1 = (char*)strrchr(path, '/'); |
| 12 | char* base2 = (char*)strrchr(path, '\\'); |
| 13 | if (base1 && base2) |
| 14 | return((base1 > base2) ? base1 + 1 : base2 + 1); |
| 15 | else if (base1) |
| 16 | return(base1 + 1); |
| 17 | else if (base2) |
| 18 | return(base2 + 1); |
| 19 | |
| 20 | return((char*)path); |
| 21 | } |
| 22 | #else |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 23 | extern char *basename(const char *); |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 24 | #endif |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 25 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 26 | static void PrintCursor(CXCursor Cursor) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 27 | if (clang_isInvalid(Cursor.kind)) |
| 28 | printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 29 | else { |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 30 | CXDecl DeclReferenced; |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 31 | CXString string; |
| 32 | string = clang_getCursorSpelling(Cursor); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 33 | printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 34 | clang_getCString(string)); |
| 35 | clang_disposeString(string); |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 36 | DeclReferenced = clang_getCursorDecl(Cursor); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 37 | if (DeclReferenced) |
| 38 | printf(":%d:%d", clang_getDeclLine(DeclReferenced), |
| 39 | clang_getDeclColumn(DeclReferenced)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 40 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 41 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 42 | |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 43 | static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 44 | { |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 45 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 46 | CXString string; |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 47 | printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)), |
| 48 | clang_getCursorLine(Cursor), |
| 49 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 50 | PrintCursor(Cursor); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 51 | string = clang_getDeclSpelling(Dcl); |
| 52 | printf(" [Context=%s]\n", clang_getCString(string)); |
| 53 | clang_disposeString(string); |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 54 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 55 | } |
| 56 | static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 57 | CXClientData Filter) |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 58 | { |
| 59 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 60 | CXString string; |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 61 | printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)), |
| 62 | clang_getCursorLine(Cursor), |
| 63 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 64 | PrintCursor(Cursor); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 65 | string = clang_getTranslationUnitSpelling(Unit); |
| 66 | printf(" [Context=%s]\n", |
| 67 | basename(clang_getCString(string))); |
| 68 | clang_disposeString(string); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 69 | |
| 70 | clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 71 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 72 | if (Cursor.kind == CXCursor_FunctionDefn) { |
| 73 | const char *startBuf, *endBuf; |
| 74 | unsigned startLine, startColumn, endLine, endColumn; |
| 75 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 76 | &startLine, &startColumn, |
| 77 | &endLine, &endColumn); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 78 | { |
| 79 | /* Probe the entire body, looking for both decls and refs. */ |
| 80 | unsigned curLine = startLine, curColumn = startColumn; |
| 81 | CXCursor Ref; |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 82 | |
Steve Naroff | 6a6de8b | 2009-10-21 13:56:23 +0000 | [diff] [blame] | 83 | while (startBuf < endBuf) { |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 84 | if (*startBuf == '\n') { |
| 85 | startBuf++; |
| 86 | curLine++; |
| 87 | curColumn = 1; |
| 88 | } else if (*startBuf != '\t') |
| 89 | curColumn++; |
Ted Kremenek | fbcb2b7 | 2009-10-22 17:22:53 +0000 | [diff] [blame] | 90 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 91 | Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor), |
| 92 | curLine, curColumn); |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 93 | if (Ref.kind == CXCursor_NoDeclFound) { |
Ted Kremenek | 8ce88a4 | 2009-10-17 06:37:16 +0000 | [diff] [blame] | 94 | /* Nothing found here; that's fine. */ |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 95 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 96 | CXString string; |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 97 | printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)), |
| 98 | curLine, curColumn); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 99 | PrintCursor(Ref); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame^] | 100 | string = clang_getDeclSpelling(Ref.decl); |
| 101 | printf(" [Context:%s]\n", clang_getCString(string)); |
| 102 | clang_disposeString(string); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 103 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 104 | startBuf++; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 105 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 108 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 109 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 111 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 112 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 113 | memory (that will be owned by the caller) to store the file name. */ |
| 114 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
| 115 | unsigned *column) { |
| 116 | const char *colon = strchr(input, ':'); |
| 117 | char *endptr = 0; |
| 118 | if (!colon) { |
| 119 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /* Copy the file name. */ |
| 124 | *filename = (char*)malloc(colon - input); |
| 125 | strncpy(*filename, input, colon - input); |
| 126 | (*filename)[colon - input] = 0; |
| 127 | input = colon + 1; |
| 128 | |
| 129 | /* Parse the line number. */ |
| 130 | *line = strtol(input, &endptr, 10); |
| 131 | if (*endptr != ':') { |
| 132 | fprintf(stderr, "could not parse line:column in '%s'\n", input); |
| 133 | free(filename); |
| 134 | *filename = 0; |
| 135 | return 1; |
| 136 | } |
| 137 | input = endptr + 1; |
| 138 | |
| 139 | /* Parse the column number. */ |
| 140 | *column = strtol(input, &endptr, 10); |
| 141 | if (*endptr != 0) { |
| 142 | fprintf(stderr, "could not parse column in '%s'\n", input); |
| 143 | free(filename); |
| 144 | *filename = 0; |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | const char * |
| 152 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 153 | switch (Kind) { |
| 154 | case CXCompletionChunk_Optional: return "Optional"; |
| 155 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 156 | case CXCompletionChunk_Text: return "Text"; |
| 157 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 158 | case CXCompletionChunk_Informative: return "Informative"; |
| 159 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 160 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 161 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 162 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 163 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 164 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 165 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 166 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 167 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 168 | case CXCompletionChunk_Comma: return "Comma"; |
| 169 | } |
| 170 | |
| 171 | return "Unknown"; |
| 172 | } |
| 173 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 174 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 175 | int I, N; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 176 | |
| 177 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 178 | for (I = 0; I != N; ++I) { |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 179 | const char *text = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 180 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 181 | = clang_getCompletionChunkKind(completion_string, I); |
| 182 | |
| 183 | if (Kind == CXCompletionChunk_Optional) { |
| 184 | fprintf(file, "{Optional "); |
| 185 | print_completion_string( |
| 186 | clang_getCompletionChunkCompletionString(completion_string, I), |
| 187 | file); |
| 188 | fprintf(file, "}"); |
| 189 | continue; |
| 190 | } |
| 191 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 192 | text = clang_getCompletionChunkText(completion_string, I); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 193 | fprintf(file, "{%s %s}", |
| 194 | clang_getCompletionChunkKindSpelling(Kind), |
| 195 | text? text : ""); |
| 196 | } |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void print_completion_result(CXCompletionResult *completion_result, |
| 200 | CXClientData client_data) { |
| 201 | FILE *file = (FILE *)client_data; |
| 202 | fprintf(file, "%s:", |
| 203 | clang_getCursorKindSpelling(completion_result->CursorKind)); |
| 204 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 205 | fprintf(file, "\n"); |
| 206 | } |
| 207 | |
| 208 | void perform_code_completion(int argc, const char **argv) { |
| 209 | const char *input = argv[1]; |
| 210 | char *filename = 0; |
| 211 | unsigned line; |
| 212 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 213 | CXIndex CIdx; |
| 214 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 215 | input += strlen("-code-completion-at="); |
| 216 | if (parse_file_line_column(input, &filename, &line, &column)) |
| 217 | return; |
| 218 | |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 219 | CIdx = clang_createIndex(0, 0); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 220 | clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2, |
| 221 | filename, line, column, &print_completion_result, stdout); |
| 222 | clang_disposeIndex(CIdx); |
| 223 | free(filename); |
| 224 | } |
| 225 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 226 | /* |
| 227 | * First sign of life:-) |
| 228 | */ |
| 229 | int main(int argc, char **argv) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 230 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) { |
| 231 | perform_code_completion(argc, (const char **)argv); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 236 | if (argc != 3) { |
| 237 | printf("Incorrect usage of c-index-test (requires 3 arguments)\n"); |
| 238 | return 0; |
| 239 | } |
| 240 | { |
Ted Kremenek | 85f5468 | 2009-10-17 06:42:15 +0000 | [diff] [blame] | 241 | CXIndex Idx; |
| 242 | CXTranslationUnit TU; |
| 243 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 244 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 245 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0, |
| 246 | /* displayDiagnostics */ 1); |
Ted Kremenek | 85f5468 | 2009-10-17 06:42:15 +0000 | [diff] [blame] | 247 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 248 | TU = clang_createTranslationUnit(Idx, argv[1]); |
Ted Kremenek | 85f5468 | 2009-10-17 06:42:15 +0000 | [diff] [blame] | 249 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 250 | if (!TU) { |
| 251 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 252 | return 1; |
| 253 | } |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 255 | if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) { |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 256 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 257 | clang_disposeTranslationUnit(TU); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 258 | return 1; |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 259 | } |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 260 | /* Perform some simple filtering. */ |
| 261 | if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl; |
| 262 | else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 263 | else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 264 | else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl; |
| 265 | else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl; |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 266 | |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 267 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 268 | clang_disposeTranslationUnit(TU); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 269 | return 1; |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 270 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 271 | } |