blob: 289dee96298719aa02ef76b5c377b64ac2bbd72f [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor0c8296d2009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00006#include <string.h>
7
Ted Kremenek0d435192009-11-17 18:13:31 +00008/******************************************************************************/
9/* Utility functions. */
10/******************************************************************************/
11
John Thompson2e06fc82009-10-27 13:42:56 +000012#ifdef _MSC_VER
13char *basename(const char* path)
14{
15 char* base1 = (char*)strrchr(path, '/');
16 char* base2 = (char*)strrchr(path, '\\');
17 if (base1 && base2)
18 return((base1 > base2) ? base1 + 1 : base2 + 1);
19 else if (base1)
20 return(base1 + 1);
21 else if (base2)
22 return(base2 + 1);
23
24 return((char*)path);
25}
26#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000027extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000028#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000029
Ted Kremenek1c6da172009-11-17 19:37:36 +000030static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
31 CXTranslationUnit *TU) {
32
33 *TU = clang_createTranslationUnit(Idx, file);
34 if (!TU) {
35 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
36 return 0;
37 }
38 return 1;
39}
40
41
Ted Kremenek0d435192009-11-17 18:13:31 +000042/******************************************************************************/
43/* Pretty-printing. */
44/******************************************************************************/
45
Steve Naroffaf08ddc2009-09-03 15:49:00 +000046static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000047 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +000048 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000049 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000050 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000051 CXString string;
52 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000053 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000054 clang_getCString(string));
55 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000056 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000057 if (DeclReferenced)
58 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
59 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000060 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000061}
Steve Naroff89922f82009-08-31 00:59:03 +000062
Ted Kremenek9298cfc2009-11-17 05:31:58 +000063static const char* GetCursorSource(CXCursor Cursor) {
64 const char *source = clang_getCursorSource(Cursor);
65 if (!source)
66 return "<invalid loc>";
67 return basename(source);
68}
69
Ted Kremenek0d435192009-11-17 18:13:31 +000070/******************************************************************************/
71/* Logic for testing clang_loadTranslationUnit(). */
72/******************************************************************************/
73
Eric Christopherf393c3b2009-10-05 21:33:42 +000074static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000075{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000076 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000077 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000078 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000079 clang_getCursorLine(Cursor),
80 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000081 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000082 string = clang_getDeclSpelling(Dcl);
83 printf(" [Context=%s]\n", clang_getCString(string));
84 clang_disposeString(string);
Daniel Dunbarbce6f622009-09-03 05:59:50 +000085 }
Steve Naroffc857ea42009-09-02 13:28:54 +000086}
87static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000088 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000089{
90 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000091 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000092 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000093 clang_getCursorLine(Cursor),
94 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000095 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000096 string = clang_getTranslationUnitSpelling(Unit);
97 printf(" [Context=%s]\n",
98 basename(clang_getCString(string)));
99 clang_disposeString(string);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000100
101 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +0000102
Steve Naroff4ade6d62009-09-23 17:52:52 +0000103 if (Cursor.kind == CXCursor_FunctionDefn) {
104 const char *startBuf, *endBuf;
105 unsigned startLine, startColumn, endLine, endColumn;
106 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
107 &startLine, &startColumn,
108 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +0000109 {
110 /* Probe the entire body, looking for both decls and refs. */
111 unsigned curLine = startLine, curColumn = startColumn;
112 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000113
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000114 while (startBuf < endBuf) {
Steve Narofff7469a32009-09-23 20:00:53 +0000115 if (*startBuf == '\n') {
116 startBuf++;
117 curLine++;
118 curColumn = 1;
119 } else if (*startBuf != '\t')
120 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000121
Steve Narofff96b5242009-10-28 20:44:47 +0000122 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
123 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +0000124 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +0000125 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +0000126 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000127 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000128 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
129 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +0000130 PrintCursor(Ref);
Steve Naroffef0cef62009-11-09 17:45:52 +0000131 string = clang_getDeclSpelling(Ref.decl);
132 printf(" [Context:%s]\n", clang_getCString(string));
133 clang_disposeString(string);
Steve Narofff7469a32009-09-23 20:00:53 +0000134 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000135 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +0000136 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000137 }
138 }
Steve Naroff2d4d6292009-08-31 14:26:51 +0000139 }
Steve Naroff89922f82009-08-31 00:59:03 +0000140}
Steve Naroff50398192009-08-28 15:28:48 +0000141
Ted Kremenek0d435192009-11-17 18:13:31 +0000142int perform_test_load_tu(const char *file, const char *filter) {
143 CXIndex Idx;
144 CXTranslationUnit TU;
145 enum CXCursorKind K = CXCursor_NotImplemented;
146 enum CXCursorKind *ck = &K;
147 Idx = clang_createIndex(/* excludeDeclsFromPCH */
148 !strcmp(filter, "local") ? 1 : 0,
149 /* displayDiagnostics */ 1);
150
Ted Kremenek1c6da172009-11-17 19:37:36 +0000151 if (!CreateTranslationUnit(Idx, file, &TU))
Ted Kremenek0d435192009-11-17 18:13:31 +0000152 return 1;
Ted Kremenek0d435192009-11-17 18:13:31 +0000153
154 /* Perform some simple filtering. */
155 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
156 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
157 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
158 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
159 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
160 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
161 else {
162 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
163 return 1;
164 }
165
166 clang_loadTranslationUnit(TU, TranslationUnitVisitor, ck);
167 clang_disposeTranslationUnit(TU);
168 return 0;
169}
170
171/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000172/* Logic for testing clang_getCursor(). */
173/******************************************************************************/
174
175static void print_cursor_file_scan(CXCursor cursor,
176 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000177 unsigned end_line, unsigned end_col,
178 const char *prefix) {
179 printf("// CHECK");
180 if (prefix)
181 printf("-%s", prefix);
182 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
183 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000184 PrintCursor(cursor);
185 printf("\n");
186}
187
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000188static int perform_file_scan(const char *ast_file, const char *source_file,
189 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000190 CXIndex Idx;
191 CXTranslationUnit TU;
192 FILE *fp;
193 unsigned line;
194 CXCursor prevCursor;
195 unsigned printed;
196 unsigned start_line, start_col, last_line, last_col;
197 size_t i;
198
199 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
200 /* displayDiagnostics */ 1))) {
201 fprintf(stderr, "Could not create Index\n");
202 return 1;
203 }
204
205 if (!CreateTranslationUnit(Idx, ast_file, &TU))
206 return 1;
207
208 if ((fp = fopen(source_file, "r")) == NULL) {
209 fprintf(stderr, "Could not open '%s'\n", source_file);
210 return 1;
211 }
212
213 line = 0;
214 prevCursor = clang_getNullCursor();
215 printed = 0;
216 start_line = last_line = 1;
217 start_col = last_col = 1;
218
219 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000220 size_t len = 0;
221 int c;
222
223 while ((c = fgetc(fp)) != EOF) {
224 len++;
225 if (c == '\n')
226 break;
227 }
228
Ted Kremenek1c6da172009-11-17 19:37:36 +0000229 ++line;
230
231 for (i = 0; i < len ; ++i) {
232 CXCursor cursor;
233 cursor = clang_getCursor(TU, source_file, line, i+1);
234
235 if (!clang_equalCursors(cursor, prevCursor) &&
236 prevCursor.kind != CXCursor_InvalidFile) {
237 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000238 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000239 printed = 1;
240 start_line = line;
241 start_col = (unsigned) i+1;
242 }
243 else {
244 printed = 0;
245 }
246
247 prevCursor = cursor;
248 last_line = line;
249 last_col = (unsigned) i+1;
250 }
251 }
252
253 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
254 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000255 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000256 }
257
258 fclose(fp);
259 return 0;
260}
261
262/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000263/* Logic for testing clang_codeComplete(). */
264/******************************************************************************/
265
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000266/* Parse file:line:column from the input string. Returns 0 on success, non-zero
267 on failure. If successful, the pointer *filename will contain newly-allocated
268 memory (that will be owned by the caller) to store the file name. */
269int parse_file_line_column(const char *input, char **filename, unsigned *line,
270 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000271 /* Find the second colon. */
272 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000273 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000274 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000275 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
276 return 1;
277 }
278
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000279 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000280 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000281 if (*endptr != 0) {
282 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000283 return 1;
284 }
285
286 /* Find the first colon. */
287 first_colon = second_colon - 1;
288 while (first_colon != input && *first_colon != ':')
289 --first_colon;
290 if (first_colon == input) {
291 fprintf(stderr, "could not parse line in '%s'\n", input);
292 return 1;
293 }
294
295 /* Parse the line number. */
296 *line = strtol(first_colon + 1, &endptr, 10);
297 if (*endptr != ':') {
298 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000299 return 1;
300 }
301
Douglas Gregor88d23952009-11-09 18:19:57 +0000302 /* Copy the file name. */
303 *filename = (char*)malloc(first_colon - input + 1);
304 memcpy(*filename, input, first_colon - input);
305 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000306 return 0;
307}
308
309const char *
310clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
311 switch (Kind) {
312 case CXCompletionChunk_Optional: return "Optional";
313 case CXCompletionChunk_TypedText: return "TypedText";
314 case CXCompletionChunk_Text: return "Text";
315 case CXCompletionChunk_Placeholder: return "Placeholder";
316 case CXCompletionChunk_Informative: return "Informative";
317 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
318 case CXCompletionChunk_LeftParen: return "LeftParen";
319 case CXCompletionChunk_RightParen: return "RightParen";
320 case CXCompletionChunk_LeftBracket: return "LeftBracket";
321 case CXCompletionChunk_RightBracket: return "RightBracket";
322 case CXCompletionChunk_LeftBrace: return "LeftBrace";
323 case CXCompletionChunk_RightBrace: return "RightBrace";
324 case CXCompletionChunk_LeftAngle: return "LeftAngle";
325 case CXCompletionChunk_RightAngle: return "RightAngle";
326 case CXCompletionChunk_Comma: return "Comma";
327 }
328
329 return "Unknown";
330}
331
Douglas Gregor3ac73852009-11-09 16:04:45 +0000332void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000333 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000334
335 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000336 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000337 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000338 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000339 = clang_getCompletionChunkKind(completion_string, I);
340
341 if (Kind == CXCompletionChunk_Optional) {
342 fprintf(file, "{Optional ");
343 print_completion_string(
344 clang_getCompletionChunkCompletionString(completion_string, I),
345 file);
346 fprintf(file, "}");
347 continue;
348 }
349
Douglas Gregord5a20892009-11-09 17:05:28 +0000350 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000351 fprintf(file, "{%s %s}",
352 clang_getCompletionChunkKindSpelling(Kind),
353 text? text : "");
354 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000355}
356
357void print_completion_result(CXCompletionResult *completion_result,
358 CXClientData client_data) {
359 FILE *file = (FILE *)client_data;
360 fprintf(file, "%s:",
361 clang_getCursorKindSpelling(completion_result->CursorKind));
362 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000363 fprintf(file, "\n");
364}
365
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000366int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000367 const char *input = argv[1];
368 char *filename = 0;
369 unsigned line;
370 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000371 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000372 int errorCode;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000373
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000374 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000375 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
376 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000377
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000378 CIdx = clang_createIndex(0, 0);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000379 clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
380 filename, line, column, &print_completion_result, stdout);
381 clang_disposeIndex(CIdx);
382 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000383
384 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000385}
386
Ted Kremenek0d435192009-11-17 18:13:31 +0000387/******************************************************************************/
388/* Command line processing. */
389/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000390
391static void print_usage(void) {
392 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000393 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000394 " c-index-test -test-file-scan <AST file> <source file> "
395 "[FileCheck prefix]\n"
Ted Kremenek0d435192009-11-17 18:13:31 +0000396 " c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
397 " <symbol filter> options for -test-load-tu:\n%s",
398 " all - load all symbols, including those from PCH\n"
399 " local - load all symbols except those in PCH\n"
400 " category - only load ObjC categories (non-PCH)\n"
401 " interface - only load ObjC interfaces (non-PCH)\n"
402 " protocol - only load ObjC protocols (non-PCH)\n"
403 " function - only load functions (non-PCH)\n"
404 " typedef - only load typdefs (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000405}
406
407int main(int argc, const char **argv) {
408 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
409 return perform_code_completion(argc, argv);
410 if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
411 return perform_test_load_tu(argv[2], argv[3]);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000412 if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
413 return perform_file_scan(argv[2], argv[3],
414 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000415
416 print_usage();
417 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000418}