blob: e7253e08d2fd2a029462daafe0406d21ea311234 [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
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000073static const char *FileCheckPrefix = "CHECK";
74
75static void PrintDeclExtent(CXDecl Dcl) {
76 CXSourceExtent extent = clang_getDeclExtent(Dcl);
77 printf(" [Extent=%d:%d:%d:%d]", extent.begin.line, extent.begin.column,
78 extent.end.line, extent.end.column);
79}
80
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000081static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +000082 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000083 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000084 CXDecl subDecl;
85 CXSourceExtent extent;
86 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
87 GetCursorSource(Cursor),
88 clang_getCursorLine(Cursor),
89 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000090 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000091
Steve Naroffef0cef62009-11-09 17:45:52 +000092 string = clang_getDeclSpelling(Dcl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000093 printf(" [Context=%s]", clang_getCString(string));
Steve Naroffef0cef62009-11-09 17:45:52 +000094 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000095
96 PrintDeclExtent(clang_getCursorDecl(Cursor));
97
98 printf("\n");
Daniel Dunbarbce6f622009-09-03 05:59:50 +000099 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000100}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000101
Steve Naroffc857ea42009-09-02 13:28:54 +0000102static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000103 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000104 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000105 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000106 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
107 GetCursorSource(Cursor), clang_getCursorLine(Cursor),
108 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000109 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +0000110 string = clang_getTranslationUnitSpelling(Unit);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000111 printf(" [Context=%s]",
Steve Naroffef0cef62009-11-09 17:45:52 +0000112 basename(clang_getCString(string)));
113 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000114
115 PrintDeclExtent(Cursor.decl);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000116
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000117 printf("\n");
118
Steve Naroffff9e18c2009-09-24 20:03:06 +0000119 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000120 }
Steve Naroff89922f82009-08-31 00:59:03 +0000121}
Steve Naroff50398192009-08-28 15:28:48 +0000122
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000123static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
124 CXClientData Filter) {
125 const char *startBuf, *endBuf;
126 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
127 CXCursor Ref;
128
129 if (Cursor.kind != CXCursor_FunctionDefn)
130 return;
131
132 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
133 &startLine, &startColumn,
134 &endLine, &endColumn);
135 /* Probe the entire body, looking for both decls and refs. */
136 curLine = startLine;
137 curColumn = startColumn;
138
139 while (startBuf < endBuf) {
140 if (*startBuf == '\n') {
141 startBuf++;
142 curLine++;
143 curColumn = 1;
144 } else if (*startBuf != '\t')
145 curColumn++;
146
147 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
148 curLine, curColumn);
149 if (Ref.kind == CXCursor_NoDeclFound) {
150 /* Nothing found here; that's fine. */
151 } else if (Ref.kind != CXCursor_FunctionDecl) {
152 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000153 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000154 curLine, curColumn);
155 PrintCursor(Ref);
156 string = clang_getDeclSpelling(Ref.decl);
157 printf(" [Context:%s]\n", clang_getCString(string));
158 clang_disposeString(string);
159 }
160 startBuf++;
161 }
162}
163
164static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000165 const char *filter, const char *prefix) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000166 enum CXCursorKind K = CXCursor_NotImplemented;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000167 CXTranslationUnitIterator Visitor = TranslationUnitVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000168 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000169
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000170 if (prefix)
171 FileCheckPrefix = prefix;
172
Ted Kremenek0d435192009-11-17 18:13:31 +0000173 /* Perform some simple filtering. */
174 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
175 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
176 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
177 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
178 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
179 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000180 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000181 else {
182 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
183 return 1;
184 }
185
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000186 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000187 clang_disposeTranslationUnit(TU);
188 return 0;
189}
190
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000191int perform_test_load_tu(const char *file, const char *filter,
192 const char *prefix) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000193 CXIndex Idx;
194 CXTranslationUnit TU;
195 Idx = clang_createIndex(/* excludeDeclsFromPCH */
196 !strcmp(filter, "local") ? 1 : 0,
197 /* displayDiagnostics */ 1);
198
199 if (!CreateTranslationUnit(Idx, file, &TU))
200 return 1;
201
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000202 return perform_test_load(Idx, TU, filter, prefix);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000203}
204
Daniel Dunbarada487d2009-12-01 02:03:10 +0000205int perform_test_load_source(int argc, const char **argv, const char *filter) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000206 const char *UseExternalASTs =
207 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000208 CXIndex Idx;
209 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000210 Idx = clang_createIndex(/* excludeDeclsFromPCH */
211 !strcmp(filter, "local") ? 1 : 0,
212 /* displayDiagnostics */ 1);
213
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000214 if (UseExternalASTs && strlen(UseExternalASTs))
215 clang_setUseExternalASTGeneration(Idx, 1);
216
Daniel Dunbarada487d2009-12-01 02:03:10 +0000217 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
218 if (!TU) {
219 fprintf(stderr, "Unable to load translation unit!\n");
220 return 1;
221 }
222
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000223 return perform_test_load(Idx, TU, filter, NULL);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000224}
225
Ted Kremenek0d435192009-11-17 18:13:31 +0000226/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000227/* Logic for testing clang_getCursor(). */
228/******************************************************************************/
229
230static void print_cursor_file_scan(CXCursor cursor,
231 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000232 unsigned end_line, unsigned end_col,
233 const char *prefix) {
234 printf("// CHECK");
235 if (prefix)
236 printf("-%s", prefix);
237 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
238 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000239 PrintCursor(cursor);
240 printf("\n");
241}
242
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000243static int perform_file_scan(const char *ast_file, const char *source_file,
244 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000245 CXIndex Idx;
246 CXTranslationUnit TU;
247 FILE *fp;
248 unsigned line;
249 CXCursor prevCursor;
250 unsigned printed;
251 unsigned start_line, start_col, last_line, last_col;
252 size_t i;
253
254 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
255 /* displayDiagnostics */ 1))) {
256 fprintf(stderr, "Could not create Index\n");
257 return 1;
258 }
259
260 if (!CreateTranslationUnit(Idx, ast_file, &TU))
261 return 1;
262
263 if ((fp = fopen(source_file, "r")) == NULL) {
264 fprintf(stderr, "Could not open '%s'\n", source_file);
265 return 1;
266 }
267
268 line = 0;
269 prevCursor = clang_getNullCursor();
270 printed = 0;
271 start_line = last_line = 1;
272 start_col = last_col = 1;
273
274 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000275 size_t len = 0;
276 int c;
277
278 while ((c = fgetc(fp)) != EOF) {
279 len++;
280 if (c == '\n')
281 break;
282 }
283
Ted Kremenek1c6da172009-11-17 19:37:36 +0000284 ++line;
285
286 for (i = 0; i < len ; ++i) {
287 CXCursor cursor;
288 cursor = clang_getCursor(TU, source_file, line, i+1);
289
290 if (!clang_equalCursors(cursor, prevCursor) &&
291 prevCursor.kind != CXCursor_InvalidFile) {
292 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000293 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000294 printed = 1;
295 start_line = line;
296 start_col = (unsigned) i+1;
297 }
298 else {
299 printed = 0;
300 }
301
302 prevCursor = cursor;
303 last_line = line;
304 last_col = (unsigned) i+1;
305 }
306 }
307
308 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
309 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000310 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000311 }
312
313 fclose(fp);
314 return 0;
315}
316
317/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000318/* Logic for testing clang_codeComplete(). */
319/******************************************************************************/
320
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000321/* Parse file:line:column from the input string. Returns 0 on success, non-zero
322 on failure. If successful, the pointer *filename will contain newly-allocated
323 memory (that will be owned by the caller) to store the file name. */
324int parse_file_line_column(const char *input, char **filename, unsigned *line,
325 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000326 /* Find the second colon. */
327 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000328 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000329 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000330 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
331 return 1;
332 }
333
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000334 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000335 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000336 if (*endptr != 0) {
337 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000338 return 1;
339 }
340
341 /* Find the first colon. */
342 first_colon = second_colon - 1;
343 while (first_colon != input && *first_colon != ':')
344 --first_colon;
345 if (first_colon == input) {
346 fprintf(stderr, "could not parse line in '%s'\n", input);
347 return 1;
348 }
349
350 /* Parse the line number. */
351 *line = strtol(first_colon + 1, &endptr, 10);
352 if (*endptr != ':') {
353 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000354 return 1;
355 }
356
Douglas Gregor88d23952009-11-09 18:19:57 +0000357 /* Copy the file name. */
358 *filename = (char*)malloc(first_colon - input + 1);
359 memcpy(*filename, input, first_colon - input);
360 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000361 return 0;
362}
363
364const char *
365clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
366 switch (Kind) {
367 case CXCompletionChunk_Optional: return "Optional";
368 case CXCompletionChunk_TypedText: return "TypedText";
369 case CXCompletionChunk_Text: return "Text";
370 case CXCompletionChunk_Placeholder: return "Placeholder";
371 case CXCompletionChunk_Informative: return "Informative";
372 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
373 case CXCompletionChunk_LeftParen: return "LeftParen";
374 case CXCompletionChunk_RightParen: return "RightParen";
375 case CXCompletionChunk_LeftBracket: return "LeftBracket";
376 case CXCompletionChunk_RightBracket: return "RightBracket";
377 case CXCompletionChunk_LeftBrace: return "LeftBrace";
378 case CXCompletionChunk_RightBrace: return "RightBrace";
379 case CXCompletionChunk_LeftAngle: return "LeftAngle";
380 case CXCompletionChunk_RightAngle: return "RightAngle";
381 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000382 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000383 }
384
385 return "Unknown";
386}
387
Douglas Gregor3ac73852009-11-09 16:04:45 +0000388void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000389 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000390
391 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000392 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000393 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000394 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000395 = clang_getCompletionChunkKind(completion_string, I);
396
397 if (Kind == CXCompletionChunk_Optional) {
398 fprintf(file, "{Optional ");
399 print_completion_string(
400 clang_getCompletionChunkCompletionString(completion_string, I),
401 file);
402 fprintf(file, "}");
403 continue;
404 }
405
Douglas Gregord5a20892009-11-09 17:05:28 +0000406 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000407 fprintf(file, "{%s %s}",
408 clang_getCompletionChunkKindSpelling(Kind),
409 text? text : "");
410 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000411}
412
413void print_completion_result(CXCompletionResult *completion_result,
414 CXClientData client_data) {
415 FILE *file = (FILE *)client_data;
416 fprintf(file, "%s:",
417 clang_getCursorKindSpelling(completion_result->CursorKind));
418 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000419 fprintf(file, "\n");
420}
421
Douglas Gregor735df882009-12-02 09:21:34 +0000422void free_remapped_files(struct CXUnsavedFile *unsaved_files,
423 int num_unsaved_files) {
424 int i;
425 for (i = 0; i != num_unsaved_files; ++i) {
426 free((char *)unsaved_files[i].Filename);
427 free((char *)unsaved_files[i].Contents);
428 }
429}
430
431int parse_remapped_files(int argc, const char **argv, int start_arg,
432 struct CXUnsavedFile **unsaved_files,
433 int *num_unsaved_files) {
434 int i;
435 int arg;
436 int prefix_len = strlen("-remap-file=");
437 *unsaved_files = 0;
438 *num_unsaved_files = 0;
439
440 /* Count the number of remapped files. */
441 for (arg = start_arg; arg < argc; ++arg) {
442 if (strncmp(argv[arg], "-remap-file=", prefix_len))
443 break;
444
445 ++*num_unsaved_files;
446 }
447
448 if (*num_unsaved_files == 0)
449 return 0;
450
451 *unsaved_files
452 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
453 *num_unsaved_files);
454 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
455 struct CXUnsavedFile *unsaved = *unsaved_files + i;
456 const char *arg_string = argv[arg] + prefix_len;
457 int filename_len;
458 char *filename;
459 char *contents;
460 FILE *to_file;
461 const char *semi = strchr(arg_string, ';');
462 if (!semi) {
463 fprintf(stderr,
464 "error: -remap-file=from;to argument is missing semicolon\n");
465 free_remapped_files(*unsaved_files, i);
466 *unsaved_files = 0;
467 *num_unsaved_files = 0;
468 return -1;
469 }
470
471 /* Open the file that we're remapping to. */
472 to_file = fopen(semi + 1, "r");
473 if (!to_file) {
474 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
475 semi + 1);
476 free_remapped_files(*unsaved_files, i);
477 *unsaved_files = 0;
478 *num_unsaved_files = 0;
479 return -1;
480 }
481
482 /* Determine the length of the file we're remapping to. */
483 fseek(to_file, 0, SEEK_END);
484 unsaved->Length = ftell(to_file);
485 fseek(to_file, 0, SEEK_SET);
486
487 /* Read the contents of the file we're remapping to. */
488 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000489 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
490 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
491 (feof(to_file) ? "EOF" : "error"), semi + 1);
492 fclose(to_file);
493 free_remapped_files(*unsaved_files, i);
494 *unsaved_files = 0;
495 *num_unsaved_files = 0;
496 return -1;
497 }
Douglas Gregor735df882009-12-02 09:21:34 +0000498 contents[unsaved->Length] = 0;
499 unsaved->Contents = contents;
500
501 /* Close the file. */
502 fclose(to_file);
503
504 /* Copy the file name that we're remapping from. */
505 filename_len = semi - arg_string;
506 filename = (char *)malloc(filename_len + 1);
507 memcpy(filename, arg_string, filename_len);
508 filename[filename_len] = 0;
509 unsaved->Filename = filename;
510 }
511
512 return 0;
513}
514
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000515int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000516 const char *input = argv[1];
517 char *filename = 0;
518 unsigned line;
519 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000520 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000521 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000522 struct CXUnsavedFile *unsaved_files = 0;
523 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000524 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000525
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000526 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000527 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
528 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000529
Douglas Gregor735df882009-12-02 09:21:34 +0000530 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
531 return -1;
532
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000533 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000534 results = clang_codeComplete(CIdx,
535 argv[argc - 1], argc - num_unsaved_files - 3,
536 argv + num_unsaved_files + 2,
537 num_unsaved_files, unsaved_files,
538 filename, line, column);
539 if (results) {
540 unsigned i, n = results->NumResults;
541 for (i = 0; i != n; ++i)
542 print_completion_result(results->Results + i, stdout);
543 clang_disposeCodeCompleteResults(results);
544 }
545
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000546 clang_disposeIndex(CIdx);
547 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000548
Douglas Gregor735df882009-12-02 09:21:34 +0000549 free_remapped_files(unsaved_files, num_unsaved_files);
550
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000551 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000552}
553
Ted Kremenek0d435192009-11-17 18:13:31 +0000554/******************************************************************************/
555/* Command line processing. */
556/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000557
558static void print_usage(void) {
559 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000560 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000561 " c-index-test -test-file-scan <AST file> <source file> "
562 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000563 " c-index-test -test-load-tu <AST file> <symbol filter> "
564 "[FileCheck prefix]\n"
Daniel Dunbarada487d2009-12-01 02:03:10 +0000565 " c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
566 " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000567 " all - load all symbols, including those from PCH\n"
568 " local - load all symbols except those in PCH\n"
569 " category - only load ObjC categories (non-PCH)\n"
570 " interface - only load ObjC interfaces (non-PCH)\n"
571 " protocol - only load ObjC protocols (non-PCH)\n"
572 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000573 " typedef - only load typdefs (non-PCH)\n"
574 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000575}
576
577int main(int argc, const char **argv) {
578 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
579 return perform_code_completion(argc, argv);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000580 if (argc >= 4 && strcmp(argv[1], "-test-load-tu") == 0)
581 return perform_test_load_tu(argv[2], argv[3],
582 argc >= 5 ? argv[4] : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000583 if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
584 return perform_test_load_source(argc - 3, argv + 3, argv[2]);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000585 if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
586 return perform_file_scan(argv[2], argv[3],
587 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000588
589 print_usage();
590 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000591}