blob: fedf7a5300a11c33b841a2fb0454528e093af1b4 [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 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
85 GetCursorSource(Cursor),
86 clang_getCursorLine(Cursor),
87 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000088 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000089
Steve Naroffef0cef62009-11-09 17:45:52 +000090 string = clang_getDeclSpelling(Dcl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000091 printf(" [Context=%s]", clang_getCString(string));
Steve Naroffef0cef62009-11-09 17:45:52 +000092 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000093
94 PrintDeclExtent(clang_getCursorDecl(Cursor));
95
96 printf("\n");
Daniel Dunbarbce6f622009-09-03 05:59:50 +000097 }
Steve Naroffc857ea42009-09-02 13:28:54 +000098}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000099
Steve Naroffc857ea42009-09-02 13:28:54 +0000100static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000101 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000102 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000103 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000104 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
105 GetCursorSource(Cursor), clang_getCursorLine(Cursor),
106 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000107 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +0000108 string = clang_getTranslationUnitSpelling(Unit);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000109 printf(" [Context=%s]",
Steve Naroffef0cef62009-11-09 17:45:52 +0000110 basename(clang_getCString(string)));
111 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000112
113 PrintDeclExtent(Cursor.decl);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000114
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000115 printf("\n");
116
Steve Naroffff9e18c2009-09-24 20:03:06 +0000117 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000118 }
Steve Naroff89922f82009-08-31 00:59:03 +0000119}
Steve Naroff50398192009-08-28 15:28:48 +0000120
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000121static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
122 CXClientData Filter) {
123 const char *startBuf, *endBuf;
124 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
125 CXCursor Ref;
126
127 if (Cursor.kind != CXCursor_FunctionDefn)
128 return;
129
130 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
131 &startLine, &startColumn,
132 &endLine, &endColumn);
133 /* Probe the entire body, looking for both decls and refs. */
134 curLine = startLine;
135 curColumn = startColumn;
136
137 while (startBuf < endBuf) {
138 if (*startBuf == '\n') {
139 startBuf++;
140 curLine++;
141 curColumn = 1;
142 } else if (*startBuf != '\t')
143 curColumn++;
144
145 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
146 curLine, curColumn);
147 if (Ref.kind == CXCursor_NoDeclFound) {
148 /* Nothing found here; that's fine. */
149 } else if (Ref.kind != CXCursor_FunctionDecl) {
150 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000151 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000152 curLine, curColumn);
153 PrintCursor(Ref);
154 string = clang_getDeclSpelling(Ref.decl);
155 printf(" [Context:%s]\n", clang_getCString(string));
156 clang_disposeString(string);
157 }
158 startBuf++;
159 }
160}
161
162static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000163 const char *filter, const char *prefix,
164 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000165 enum CXCursorKind K = CXCursor_NotImplemented;
166 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000167
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000168 if (prefix)
169 FileCheckPrefix = prefix;
170
Ted Kremenek0d435192009-11-17 18:13:31 +0000171 /* Perform some simple filtering. */
172 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
173 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
174 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
175 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
176 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
177 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000178 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000179 else {
180 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
181 return 1;
182 }
183
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000184 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000185 clang_disposeTranslationUnit(TU);
186 return 0;
187}
188
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000189int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000190 const char *prefix,
191 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000192 CXIndex Idx;
193 CXTranslationUnit TU;
194 Idx = clang_createIndex(/* excludeDeclsFromPCH */
195 !strcmp(filter, "local") ? 1 : 0,
196 /* displayDiagnostics */ 1);
197
198 if (!CreateTranslationUnit(Idx, file, &TU))
199 return 1;
200
Ted Kremenek98271562010-01-12 18:53:15 +0000201 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000202}
203
Ted Kremenek98271562010-01-12 18:53:15 +0000204int perform_test_load_source(int argc, const char **argv, const char *filter,
205 CXTranslationUnitIterator Visitor) {
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 Kremenek98271562010-01-12 18:53:15 +0000223 return perform_test_load(Idx, TU, filter, NULL, Visitor);
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) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000234 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000235 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 Gregor01dfea02010-01-10 23:08:15 +0000383 case CXCompletionChunk_Colon: return "Colon";
384 case CXCompletionChunk_SemiColon: return "SemiColon";
385 case CXCompletionChunk_Equal: return "Equal";
386 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
387 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000388 }
389
390 return "Unknown";
391}
392
Douglas Gregor3ac73852009-11-09 16:04:45 +0000393void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000394 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000395
396 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000397 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000398 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000399 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000400 = clang_getCompletionChunkKind(completion_string, I);
401
402 if (Kind == CXCompletionChunk_Optional) {
403 fprintf(file, "{Optional ");
404 print_completion_string(
405 clang_getCompletionChunkCompletionString(completion_string, I),
406 file);
407 fprintf(file, "}");
408 continue;
409 }
410
Douglas Gregord5a20892009-11-09 17:05:28 +0000411 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000412 fprintf(file, "{%s %s}",
413 clang_getCompletionChunkKindSpelling(Kind),
414 text? text : "");
415 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000416}
417
418void print_completion_result(CXCompletionResult *completion_result,
419 CXClientData client_data) {
420 FILE *file = (FILE *)client_data;
421 fprintf(file, "%s:",
422 clang_getCursorKindSpelling(completion_result->CursorKind));
423 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000424 fprintf(file, "\n");
425}
426
Douglas Gregor735df882009-12-02 09:21:34 +0000427void free_remapped_files(struct CXUnsavedFile *unsaved_files,
428 int num_unsaved_files) {
429 int i;
430 for (i = 0; i != num_unsaved_files; ++i) {
431 free((char *)unsaved_files[i].Filename);
432 free((char *)unsaved_files[i].Contents);
433 }
434}
435
436int parse_remapped_files(int argc, const char **argv, int start_arg,
437 struct CXUnsavedFile **unsaved_files,
438 int *num_unsaved_files) {
439 int i;
440 int arg;
441 int prefix_len = strlen("-remap-file=");
442 *unsaved_files = 0;
443 *num_unsaved_files = 0;
444
445 /* Count the number of remapped files. */
446 for (arg = start_arg; arg < argc; ++arg) {
447 if (strncmp(argv[arg], "-remap-file=", prefix_len))
448 break;
449
450 ++*num_unsaved_files;
451 }
452
453 if (*num_unsaved_files == 0)
454 return 0;
455
456 *unsaved_files
457 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
458 *num_unsaved_files);
459 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
460 struct CXUnsavedFile *unsaved = *unsaved_files + i;
461 const char *arg_string = argv[arg] + prefix_len;
462 int filename_len;
463 char *filename;
464 char *contents;
465 FILE *to_file;
466 const char *semi = strchr(arg_string, ';');
467 if (!semi) {
468 fprintf(stderr,
469 "error: -remap-file=from;to argument is missing semicolon\n");
470 free_remapped_files(*unsaved_files, i);
471 *unsaved_files = 0;
472 *num_unsaved_files = 0;
473 return -1;
474 }
475
476 /* Open the file that we're remapping to. */
477 to_file = fopen(semi + 1, "r");
478 if (!to_file) {
479 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
480 semi + 1);
481 free_remapped_files(*unsaved_files, i);
482 *unsaved_files = 0;
483 *num_unsaved_files = 0;
484 return -1;
485 }
486
487 /* Determine the length of the file we're remapping to. */
488 fseek(to_file, 0, SEEK_END);
489 unsaved->Length = ftell(to_file);
490 fseek(to_file, 0, SEEK_SET);
491
492 /* Read the contents of the file we're remapping to. */
493 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000494 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
495 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
496 (feof(to_file) ? "EOF" : "error"), semi + 1);
497 fclose(to_file);
498 free_remapped_files(*unsaved_files, i);
499 *unsaved_files = 0;
500 *num_unsaved_files = 0;
501 return -1;
502 }
Douglas Gregor735df882009-12-02 09:21:34 +0000503 contents[unsaved->Length] = 0;
504 unsaved->Contents = contents;
505
506 /* Close the file. */
507 fclose(to_file);
508
509 /* Copy the file name that we're remapping from. */
510 filename_len = semi - arg_string;
511 filename = (char *)malloc(filename_len + 1);
512 memcpy(filename, arg_string, filename_len);
513 filename[filename_len] = 0;
514 unsaved->Filename = filename;
515 }
516
517 return 0;
518}
519
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000520int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000521 const char *input = argv[1];
522 char *filename = 0;
523 unsigned line;
524 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000525 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000526 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000527 struct CXUnsavedFile *unsaved_files = 0;
528 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000529 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000530
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000531 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000532 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
533 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000534
Douglas Gregor735df882009-12-02 09:21:34 +0000535 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
536 return -1;
537
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000538 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000539 results = clang_codeComplete(CIdx,
540 argv[argc - 1], argc - num_unsaved_files - 3,
541 argv + num_unsaved_files + 2,
542 num_unsaved_files, unsaved_files,
543 filename, line, column);
544 if (results) {
545 unsigned i, n = results->NumResults;
546 for (i = 0; i != n; ++i)
547 print_completion_result(results->Results + i, stdout);
548 clang_disposeCodeCompleteResults(results);
549 }
550
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000551 clang_disposeIndex(CIdx);
552 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000553
Douglas Gregor735df882009-12-02 09:21:34 +0000554 free_remapped_files(unsaved_files, num_unsaved_files);
555
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000556 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000557}
558
Ted Kremenek0d435192009-11-17 18:13:31 +0000559/******************************************************************************/
560/* Command line processing. */
561/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000562
563static void print_usage(void) {
564 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000565 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000566 " c-index-test -test-file-scan <AST file> <source file> "
567 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000568 " c-index-test -test-load-tu <AST file> <symbol filter> "
569 "[FileCheck prefix]\n"
Daniel Dunbarada487d2009-12-01 02:03:10 +0000570 " c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
571 " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000572 " all - load all symbols, including those from PCH\n"
573 " local - load all symbols except those in PCH\n"
574 " category - only load ObjC categories (non-PCH)\n"
575 " interface - only load ObjC interfaces (non-PCH)\n"
576 " protocol - only load ObjC protocols (non-PCH)\n"
577 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000578 " typedef - only load typdefs (non-PCH)\n"
579 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000580}
581
582int main(int argc, const char **argv) {
583 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
584 return perform_code_completion(argc, argv);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000585 if (argc >= 4 && strcmp(argv[1], "-test-load-tu") == 0)
586 return perform_test_load_tu(argv[2], argv[3],
Ted Kremenek98271562010-01-12 18:53:15 +0000587 argc >= 5 ? argv[4] : 0,
588 TranslationUnitVisitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000589 if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
Ted Kremenek98271562010-01-12 18:53:15 +0000590 return perform_test_load_source(argc - 3, argv + 3, argv[2],
591 TranslationUnitVisitor);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000592 if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
593 return perform_file_scan(argv[2], argv[3],
594 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000595
596 print_usage();
597 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000598}