blob: 33013f3b66b2fd3a0c9847b109915bd7ca2834a7 [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";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000358 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000359 }
360
361 return "Unknown";
362}
363
Douglas Gregor3ac73852009-11-09 16:04:45 +0000364void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000365 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000366
367 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000368 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000369 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000370 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000371 = clang_getCompletionChunkKind(completion_string, I);
372
373 if (Kind == CXCompletionChunk_Optional) {
374 fprintf(file, "{Optional ");
375 print_completion_string(
376 clang_getCompletionChunkCompletionString(completion_string, I),
377 file);
378 fprintf(file, "}");
379 continue;
380 }
381
Douglas Gregord5a20892009-11-09 17:05:28 +0000382 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000383 fprintf(file, "{%s %s}",
384 clang_getCompletionChunkKindSpelling(Kind),
385 text? text : "");
386 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000387}
388
389void print_completion_result(CXCompletionResult *completion_result,
390 CXClientData client_data) {
391 FILE *file = (FILE *)client_data;
392 fprintf(file, "%s:",
393 clang_getCursorKindSpelling(completion_result->CursorKind));
394 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000395 fprintf(file, "\n");
396}
397
Douglas Gregor735df882009-12-02 09:21:34 +0000398void free_remapped_files(struct CXUnsavedFile *unsaved_files,
399 int num_unsaved_files) {
400 int i;
401 for (i = 0; i != num_unsaved_files; ++i) {
402 free((char *)unsaved_files[i].Filename);
403 free((char *)unsaved_files[i].Contents);
404 }
405}
406
407int parse_remapped_files(int argc, const char **argv, int start_arg,
408 struct CXUnsavedFile **unsaved_files,
409 int *num_unsaved_files) {
410 int i;
411 int arg;
412 int prefix_len = strlen("-remap-file=");
413 *unsaved_files = 0;
414 *num_unsaved_files = 0;
415
416 /* Count the number of remapped files. */
417 for (arg = start_arg; arg < argc; ++arg) {
418 if (strncmp(argv[arg], "-remap-file=", prefix_len))
419 break;
420
421 ++*num_unsaved_files;
422 }
423
424 if (*num_unsaved_files == 0)
425 return 0;
426
427 *unsaved_files
428 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
429 *num_unsaved_files);
430 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
431 struct CXUnsavedFile *unsaved = *unsaved_files + i;
432 const char *arg_string = argv[arg] + prefix_len;
433 int filename_len;
434 char *filename;
435 char *contents;
436 FILE *to_file;
437 const char *semi = strchr(arg_string, ';');
438 if (!semi) {
439 fprintf(stderr,
440 "error: -remap-file=from;to argument is missing semicolon\n");
441 free_remapped_files(*unsaved_files, i);
442 *unsaved_files = 0;
443 *num_unsaved_files = 0;
444 return -1;
445 }
446
447 /* Open the file that we're remapping to. */
448 to_file = fopen(semi + 1, "r");
449 if (!to_file) {
450 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
451 semi + 1);
452 free_remapped_files(*unsaved_files, i);
453 *unsaved_files = 0;
454 *num_unsaved_files = 0;
455 return -1;
456 }
457
458 /* Determine the length of the file we're remapping to. */
459 fseek(to_file, 0, SEEK_END);
460 unsaved->Length = ftell(to_file);
461 fseek(to_file, 0, SEEK_SET);
462
463 /* Read the contents of the file we're remapping to. */
464 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000465 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
466 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
467 (feof(to_file) ? "EOF" : "error"), semi + 1);
468 fclose(to_file);
469 free_remapped_files(*unsaved_files, i);
470 *unsaved_files = 0;
471 *num_unsaved_files = 0;
472 return -1;
473 }
Douglas Gregor735df882009-12-02 09:21:34 +0000474 contents[unsaved->Length] = 0;
475 unsaved->Contents = contents;
476
477 /* Close the file. */
478 fclose(to_file);
479
480 /* Copy the file name that we're remapping from. */
481 filename_len = semi - arg_string;
482 filename = (char *)malloc(filename_len + 1);
483 memcpy(filename, arg_string, filename_len);
484 filename[filename_len] = 0;
485 unsaved->Filename = filename;
486 }
487
488 return 0;
489}
490
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000491int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000492 const char *input = argv[1];
493 char *filename = 0;
494 unsigned line;
495 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000496 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000497 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000498 struct CXUnsavedFile *unsaved_files = 0;
499 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000500 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000501
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000502 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000503 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
504 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000505
Douglas Gregor735df882009-12-02 09:21:34 +0000506 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
507 return -1;
508
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000509 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000510 results = clang_codeComplete(CIdx,
511 argv[argc - 1], argc - num_unsaved_files - 3,
512 argv + num_unsaved_files + 2,
513 num_unsaved_files, unsaved_files,
514 filename, line, column);
515 if (results) {
516 unsigned i, n = results->NumResults;
517 for (i = 0; i != n; ++i)
518 print_completion_result(results->Results + i, stdout);
519 clang_disposeCodeCompleteResults(results);
520 }
521
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000522 clang_disposeIndex(CIdx);
523 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000524
Douglas Gregor735df882009-12-02 09:21:34 +0000525 free_remapped_files(unsaved_files, num_unsaved_files);
526
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000527 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000528}
529
Ted Kremenek0d435192009-11-17 18:13:31 +0000530/******************************************************************************/
531/* Command line processing. */
532/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000533
534static void print_usage(void) {
535 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000536 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000537 " c-index-test -test-file-scan <AST file> <source file> "
538 "[FileCheck prefix]\n"
Ted Kremenek0d435192009-11-17 18:13:31 +0000539 " c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
Daniel Dunbarada487d2009-12-01 02:03:10 +0000540 " c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
541 " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000542 " all - load all symbols, including those from PCH\n"
543 " local - load all symbols except those in PCH\n"
544 " category - only load ObjC categories (non-PCH)\n"
545 " interface - only load ObjC interfaces (non-PCH)\n"
546 " protocol - only load ObjC protocols (non-PCH)\n"
547 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000548 " typedef - only load typdefs (non-PCH)\n"
549 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000550}
551
552int main(int argc, const char **argv) {
553 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
554 return perform_code_completion(argc, argv);
555 if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
556 return perform_test_load_tu(argv[2], argv[3]);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000557 if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
558 return perform_test_load_source(argc - 3, argv + 3, argv[2]);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000559 if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
560 return perform_file_scan(argv[2], argv[3],
561 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000562
563 print_usage();
564 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000565}