blob: 7040a7288a118193a3ed00efca5a4bf083049363 [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
Ted Kremenek0d435192009-11-17 18:13:31 +000041/******************************************************************************/
42/* Pretty-printing. */
43/******************************************************************************/
44
Steve Naroffaf08ddc2009-09-03 15:49:00 +000045static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000046 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +000047 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000048 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000049 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000050 CXString string;
51 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000052 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000053 clang_getCString(string));
54 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000055 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000056 if (DeclReferenced)
57 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
58 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000059 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000060}
Steve Naroff89922f82009-08-31 00:59:03 +000061
Ted Kremenek9298cfc2009-11-17 05:31:58 +000062static const char* GetCursorSource(CXCursor Cursor) {
63 const char *source = clang_getCursorSource(Cursor);
64 if (!source)
65 return "<invalid loc>";
66 return basename(source);
67}
68
Ted Kremenek0d435192009-11-17 18:13:31 +000069/******************************************************************************/
70/* Logic for testing clang_loadTranslationUnit(). */
71/******************************************************************************/
72
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000073static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +000074 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000075 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000076 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000077 clang_getCursorLine(Cursor),
78 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000079 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000080 string = clang_getDeclSpelling(Dcl);
81 printf(" [Context=%s]\n", clang_getCString(string));
82 clang_disposeString(string);
Daniel Dunbarbce6f622009-09-03 05:59:50 +000083 }
Steve Naroffc857ea42009-09-02 13:28:54 +000084}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000085
Steve Naroffc857ea42009-09-02 13:28:54 +000086static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000087 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +000088 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000089 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000090 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000091 clang_getCursorLine(Cursor),
92 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000093 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000094 string = clang_getTranslationUnitSpelling(Unit);
95 printf(" [Context=%s]\n",
96 basename(clang_getCString(string)));
97 clang_disposeString(string);
Steve Naroffff9e18c2009-09-24 20:03:06 +000098
99 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000100 }
Steve Naroff89922f82009-08-31 00:59:03 +0000101}
Steve Naroff50398192009-08-28 15:28:48 +0000102
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000103static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
104 CXClientData Filter) {
105 const char *startBuf, *endBuf;
106 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
107 CXCursor Ref;
108
109 if (Cursor.kind != CXCursor_FunctionDefn)
110 return;
111
112 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
113 &startLine, &startColumn,
114 &endLine, &endColumn);
115 /* Probe the entire body, looking for both decls and refs. */
116 curLine = startLine;
117 curColumn = startColumn;
118
119 while (startBuf < endBuf) {
120 if (*startBuf == '\n') {
121 startBuf++;
122 curLine++;
123 curColumn = 1;
124 } else if (*startBuf != '\t')
125 curColumn++;
126
127 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
128 curLine, curColumn);
129 if (Ref.kind == CXCursor_NoDeclFound) {
130 /* Nothing found here; that's fine. */
131 } else if (Ref.kind != CXCursor_FunctionDecl) {
132 CXString string;
133 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
134 curLine, curColumn);
135 PrintCursor(Ref);
136 string = clang_getDeclSpelling(Ref.decl);
137 printf(" [Context:%s]\n", clang_getCString(string));
138 clang_disposeString(string);
139 }
140 startBuf++;
141 }
142}
143
144static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
145 const char *filter) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000146 enum CXCursorKind K = CXCursor_NotImplemented;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000147 CXTranslationUnitIterator Visitor = TranslationUnitVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000148 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000149
Ted Kremenek0d435192009-11-17 18:13:31 +0000150 /* Perform some simple filtering. */
151 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
152 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
153 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
154 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
155 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
156 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000157 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000158 else {
159 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
160 return 1;
161 }
162
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000163 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000164 clang_disposeTranslationUnit(TU);
165 return 0;
166}
167
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000168int perform_test_load_tu(const char *file, const char *filter) {
169 CXIndex Idx;
170 CXTranslationUnit TU;
171 Idx = clang_createIndex(/* excludeDeclsFromPCH */
172 !strcmp(filter, "local") ? 1 : 0,
173 /* displayDiagnostics */ 1);
174
175 if (!CreateTranslationUnit(Idx, file, &TU))
176 return 1;
177
178 return perform_test_load(Idx, TU, filter);
179}
180
Daniel Dunbarada487d2009-12-01 02:03:10 +0000181int perform_test_load_source(int argc, const char **argv, const char *filter) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000182 const char *UseExternalASTs =
183 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000184 CXIndex Idx;
185 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000186 Idx = clang_createIndex(/* excludeDeclsFromPCH */
187 !strcmp(filter, "local") ? 1 : 0,
188 /* displayDiagnostics */ 1);
189
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000190 if (UseExternalASTs && strlen(UseExternalASTs))
191 clang_setUseExternalASTGeneration(Idx, 1);
192
Daniel Dunbarada487d2009-12-01 02:03:10 +0000193 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
194 if (!TU) {
195 fprintf(stderr, "Unable to load translation unit!\n");
196 return 1;
197 }
198
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000199 return perform_test_load(Idx, TU, filter);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000200}
201
Ted Kremenek0d435192009-11-17 18:13:31 +0000202/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000203/* Logic for testing clang_getCursor(). */
204/******************************************************************************/
205
206static void print_cursor_file_scan(CXCursor cursor,
207 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000208 unsigned end_line, unsigned end_col,
209 const char *prefix) {
210 printf("// CHECK");
211 if (prefix)
212 printf("-%s", prefix);
213 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
214 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000215 PrintCursor(cursor);
216 printf("\n");
217}
218
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000219static int perform_file_scan(const char *ast_file, const char *source_file,
220 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000221 CXIndex Idx;
222 CXTranslationUnit TU;
223 FILE *fp;
224 unsigned line;
225 CXCursor prevCursor;
226 unsigned printed;
227 unsigned start_line, start_col, last_line, last_col;
228 size_t i;
229
230 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
231 /* displayDiagnostics */ 1))) {
232 fprintf(stderr, "Could not create Index\n");
233 return 1;
234 }
235
236 if (!CreateTranslationUnit(Idx, ast_file, &TU))
237 return 1;
238
239 if ((fp = fopen(source_file, "r")) == NULL) {
240 fprintf(stderr, "Could not open '%s'\n", source_file);
241 return 1;
242 }
243
244 line = 0;
245 prevCursor = clang_getNullCursor();
246 printed = 0;
247 start_line = last_line = 1;
248 start_col = last_col = 1;
249
250 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000251 size_t len = 0;
252 int c;
253
254 while ((c = fgetc(fp)) != EOF) {
255 len++;
256 if (c == '\n')
257 break;
258 }
259
Ted Kremenek1c6da172009-11-17 19:37:36 +0000260 ++line;
261
262 for (i = 0; i < len ; ++i) {
263 CXCursor cursor;
264 cursor = clang_getCursor(TU, source_file, line, i+1);
265
266 if (!clang_equalCursors(cursor, prevCursor) &&
267 prevCursor.kind != CXCursor_InvalidFile) {
268 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000269 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000270 printed = 1;
271 start_line = line;
272 start_col = (unsigned) i+1;
273 }
274 else {
275 printed = 0;
276 }
277
278 prevCursor = cursor;
279 last_line = line;
280 last_col = (unsigned) i+1;
281 }
282 }
283
284 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
285 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000286 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000287 }
288
289 fclose(fp);
290 return 0;
291}
292
293/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000294/* Logic for testing clang_codeComplete(). */
295/******************************************************************************/
296
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000297/* Parse file:line:column from the input string. Returns 0 on success, non-zero
298 on failure. If successful, the pointer *filename will contain newly-allocated
299 memory (that will be owned by the caller) to store the file name. */
300int parse_file_line_column(const char *input, char **filename, unsigned *line,
301 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000302 /* Find the second colon. */
303 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000304 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000305 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000306 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
307 return 1;
308 }
309
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000310 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000311 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000312 if (*endptr != 0) {
313 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000314 return 1;
315 }
316
317 /* Find the first colon. */
318 first_colon = second_colon - 1;
319 while (first_colon != input && *first_colon != ':')
320 --first_colon;
321 if (first_colon == input) {
322 fprintf(stderr, "could not parse line in '%s'\n", input);
323 return 1;
324 }
325
326 /* Parse the line number. */
327 *line = strtol(first_colon + 1, &endptr, 10);
328 if (*endptr != ':') {
329 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000330 return 1;
331 }
332
Douglas Gregor88d23952009-11-09 18:19:57 +0000333 /* Copy the file name. */
334 *filename = (char*)malloc(first_colon - input + 1);
335 memcpy(*filename, input, first_colon - input);
336 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000337 return 0;
338}
339
340const char *
341clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
342 switch (Kind) {
343 case CXCompletionChunk_Optional: return "Optional";
344 case CXCompletionChunk_TypedText: return "TypedText";
345 case CXCompletionChunk_Text: return "Text";
346 case CXCompletionChunk_Placeholder: return "Placeholder";
347 case CXCompletionChunk_Informative: return "Informative";
348 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
349 case CXCompletionChunk_LeftParen: return "LeftParen";
350 case CXCompletionChunk_RightParen: return "RightParen";
351 case CXCompletionChunk_LeftBracket: return "LeftBracket";
352 case CXCompletionChunk_RightBracket: return "RightBracket";
353 case CXCompletionChunk_LeftBrace: return "LeftBrace";
354 case CXCompletionChunk_RightBrace: return "RightBrace";
355 case CXCompletionChunk_LeftAngle: return "LeftAngle";
356 case CXCompletionChunk_RightAngle: return "RightAngle";
357 case CXCompletionChunk_Comma: return "Comma";
358 }
359
360 return "Unknown";
361}
362
Douglas Gregor3ac73852009-11-09 16:04:45 +0000363void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000364 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000365
366 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000367 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000368 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000369 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000370 = clang_getCompletionChunkKind(completion_string, I);
371
372 if (Kind == CXCompletionChunk_Optional) {
373 fprintf(file, "{Optional ");
374 print_completion_string(
375 clang_getCompletionChunkCompletionString(completion_string, I),
376 file);
377 fprintf(file, "}");
378 continue;
379 }
380
Douglas Gregord5a20892009-11-09 17:05:28 +0000381 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000382 fprintf(file, "{%s %s}",
383 clang_getCompletionChunkKindSpelling(Kind),
384 text? text : "");
385 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000386}
387
388void print_completion_result(CXCompletionResult *completion_result,
389 CXClientData client_data) {
390 FILE *file = (FILE *)client_data;
391 fprintf(file, "%s:",
392 clang_getCursorKindSpelling(completion_result->CursorKind));
393 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000394 fprintf(file, "\n");
395}
396
Douglas Gregor735df882009-12-02 09:21:34 +0000397void free_remapped_files(struct CXUnsavedFile *unsaved_files,
398 int num_unsaved_files) {
399 int i;
400 for (i = 0; i != num_unsaved_files; ++i) {
401 free((char *)unsaved_files[i].Filename);
402 free((char *)unsaved_files[i].Contents);
403 }
404}
405
406int parse_remapped_files(int argc, const char **argv, int start_arg,
407 struct CXUnsavedFile **unsaved_files,
408 int *num_unsaved_files) {
409 int i;
410 int arg;
411 int prefix_len = strlen("-remap-file=");
412 *unsaved_files = 0;
413 *num_unsaved_files = 0;
414
415 /* Count the number of remapped files. */
416 for (arg = start_arg; arg < argc; ++arg) {
417 if (strncmp(argv[arg], "-remap-file=", prefix_len))
418 break;
419
420 ++*num_unsaved_files;
421 }
422
423 if (*num_unsaved_files == 0)
424 return 0;
425
426 *unsaved_files
427 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
428 *num_unsaved_files);
429 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
430 struct CXUnsavedFile *unsaved = *unsaved_files + i;
431 const char *arg_string = argv[arg] + prefix_len;
432 int filename_len;
433 char *filename;
434 char *contents;
435 FILE *to_file;
436 const char *semi = strchr(arg_string, ';');
437 if (!semi) {
438 fprintf(stderr,
439 "error: -remap-file=from;to argument is missing semicolon\n");
440 free_remapped_files(*unsaved_files, i);
441 *unsaved_files = 0;
442 *num_unsaved_files = 0;
443 return -1;
444 }
445
446 /* Open the file that we're remapping to. */
447 to_file = fopen(semi + 1, "r");
448 if (!to_file) {
449 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
450 semi + 1);
451 free_remapped_files(*unsaved_files, i);
452 *unsaved_files = 0;
453 *num_unsaved_files = 0;
454 return -1;
455 }
456
457 /* Determine the length of the file we're remapping to. */
458 fseek(to_file, 0, SEEK_END);
459 unsaved->Length = ftell(to_file);
460 fseek(to_file, 0, SEEK_SET);
461
462 /* Read the contents of the file we're remapping to. */
463 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000464 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
465 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
466 (feof(to_file) ? "EOF" : "error"), semi + 1);
467 fclose(to_file);
468 free_remapped_files(*unsaved_files, i);
469 *unsaved_files = 0;
470 *num_unsaved_files = 0;
471 return -1;
472 }
Douglas Gregor735df882009-12-02 09:21:34 +0000473 contents[unsaved->Length] = 0;
474 unsaved->Contents = contents;
475
476 /* Close the file. */
477 fclose(to_file);
478
479 /* Copy the file name that we're remapping from. */
480 filename_len = semi - arg_string;
481 filename = (char *)malloc(filename_len + 1);
482 memcpy(filename, arg_string, filename_len);
483 filename[filename_len] = 0;
484 unsaved->Filename = filename;
485 }
486
487 return 0;
488}
489
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000490int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000491 const char *input = argv[1];
492 char *filename = 0;
493 unsigned line;
494 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000495 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000496 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000497 struct CXUnsavedFile *unsaved_files = 0;
498 int num_unsaved_files = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000499
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000500 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000501 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
502 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000503
Douglas Gregor735df882009-12-02 09:21:34 +0000504 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
505 return -1;
506
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000507 CIdx = clang_createIndex(0, 0);
Douglas Gregor735df882009-12-02 09:21:34 +0000508 clang_codeComplete(CIdx, argv[argc - 1], argc - num_unsaved_files - 3,
509 argv + num_unsaved_files + 2,
510 num_unsaved_files, unsaved_files,
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000511 filename, line, column, &print_completion_result, stdout);
512 clang_disposeIndex(CIdx);
513 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000514
Douglas Gregor735df882009-12-02 09:21:34 +0000515 free_remapped_files(unsaved_files, num_unsaved_files);
516
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000517 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000518}
519
Ted Kremenek0d435192009-11-17 18:13:31 +0000520/******************************************************************************/
521/* Command line processing. */
522/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000523
524static void print_usage(void) {
525 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000526 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000527 " c-index-test -test-file-scan <AST file> <source file> "
528 "[FileCheck prefix]\n"
Ted Kremenek0d435192009-11-17 18:13:31 +0000529 " c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
Daniel Dunbarada487d2009-12-01 02:03:10 +0000530 " c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
531 " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000532 " all - load all symbols, including those from PCH\n"
533 " local - load all symbols except those in PCH\n"
534 " category - only load ObjC categories (non-PCH)\n"
535 " interface - only load ObjC interfaces (non-PCH)\n"
536 " protocol - only load ObjC protocols (non-PCH)\n"
537 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000538 " typedef - only load typdefs (non-PCH)\n"
539 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000540}
541
542int main(int argc, const char **argv) {
543 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
544 return perform_code_completion(argc, argv);
545 if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
546 return perform_test_load_tu(argv[2], argv[3]);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000547 if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
548 return perform_test_load_source(argc - 3, argv + 3, argv[2]);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000549 if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
550 return perform_file_scan(argv[2], argv[3],
551 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000552
553 print_usage();
554 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000555}