blob: 0e532ffa0e795a3d459343eba8d23ee044c783ef [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 Kremenekfe6fd3d2010-01-05 23:18:49 +0000163 const char *filter, const char *prefix) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000164 enum CXCursorKind K = CXCursor_NotImplemented;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000165 CXTranslationUnitIterator Visitor = TranslationUnitVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000166 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,
190 const char *prefix) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000191 CXIndex Idx;
192 CXTranslationUnit TU;
193 Idx = clang_createIndex(/* excludeDeclsFromPCH */
194 !strcmp(filter, "local") ? 1 : 0,
195 /* displayDiagnostics */ 1);
196
197 if (!CreateTranslationUnit(Idx, file, &TU))
198 return 1;
199
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000200 return perform_test_load(Idx, TU, filter, prefix);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000201}
202
Daniel Dunbarada487d2009-12-01 02:03:10 +0000203int perform_test_load_source(int argc, const char **argv, const char *filter) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000204 const char *UseExternalASTs =
205 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000206 CXIndex Idx;
207 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000208 Idx = clang_createIndex(/* excludeDeclsFromPCH */
209 !strcmp(filter, "local") ? 1 : 0,
210 /* displayDiagnostics */ 1);
211
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000212 if (UseExternalASTs && strlen(UseExternalASTs))
213 clang_setUseExternalASTGeneration(Idx, 1);
214
Daniel Dunbarada487d2009-12-01 02:03:10 +0000215 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
216 if (!TU) {
217 fprintf(stderr, "Unable to load translation unit!\n");
218 return 1;
219 }
220
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000221 return perform_test_load(Idx, TU, filter, NULL);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000222}
223
Ted Kremenek0d435192009-11-17 18:13:31 +0000224/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000225/* Logic for testing clang_getCursor(). */
226/******************************************************************************/
227
228static void print_cursor_file_scan(CXCursor cursor,
229 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000230 unsigned end_line, unsigned end_col,
231 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000232 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000233 if (prefix)
234 printf("-%s", prefix);
235 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
236 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000237 PrintCursor(cursor);
238 printf("\n");
239}
240
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000241static int perform_file_scan(const char *ast_file, const char *source_file,
242 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000243 CXIndex Idx;
244 CXTranslationUnit TU;
245 FILE *fp;
246 unsigned line;
247 CXCursor prevCursor;
248 unsigned printed;
249 unsigned start_line, start_col, last_line, last_col;
250 size_t i;
251
252 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
253 /* displayDiagnostics */ 1))) {
254 fprintf(stderr, "Could not create Index\n");
255 return 1;
256 }
257
258 if (!CreateTranslationUnit(Idx, ast_file, &TU))
259 return 1;
260
261 if ((fp = fopen(source_file, "r")) == NULL) {
262 fprintf(stderr, "Could not open '%s'\n", source_file);
263 return 1;
264 }
265
266 line = 0;
267 prevCursor = clang_getNullCursor();
268 printed = 0;
269 start_line = last_line = 1;
270 start_col = last_col = 1;
271
272 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000273 size_t len = 0;
274 int c;
275
276 while ((c = fgetc(fp)) != EOF) {
277 len++;
278 if (c == '\n')
279 break;
280 }
281
Ted Kremenek1c6da172009-11-17 19:37:36 +0000282 ++line;
283
284 for (i = 0; i < len ; ++i) {
285 CXCursor cursor;
286 cursor = clang_getCursor(TU, source_file, line, i+1);
287
288 if (!clang_equalCursors(cursor, prevCursor) &&
289 prevCursor.kind != CXCursor_InvalidFile) {
290 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000291 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000292 printed = 1;
293 start_line = line;
294 start_col = (unsigned) i+1;
295 }
296 else {
297 printed = 0;
298 }
299
300 prevCursor = cursor;
301 last_line = line;
302 last_col = (unsigned) i+1;
303 }
304 }
305
306 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
307 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000308 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000309 }
310
311 fclose(fp);
312 return 0;
313}
314
315/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000316/* Logic for testing clang_codeComplete(). */
317/******************************************************************************/
318
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000319/* Parse file:line:column from the input string. Returns 0 on success, non-zero
320 on failure. If successful, the pointer *filename will contain newly-allocated
321 memory (that will be owned by the caller) to store the file name. */
322int parse_file_line_column(const char *input, char **filename, unsigned *line,
323 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000324 /* Find the second colon. */
325 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000326 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000327 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000328 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
329 return 1;
330 }
331
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000332 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000333 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000334 if (*endptr != 0) {
335 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000336 return 1;
337 }
338
339 /* Find the first colon. */
340 first_colon = second_colon - 1;
341 while (first_colon != input && *first_colon != ':')
342 --first_colon;
343 if (first_colon == input) {
344 fprintf(stderr, "could not parse line in '%s'\n", input);
345 return 1;
346 }
347
348 /* Parse the line number. */
349 *line = strtol(first_colon + 1, &endptr, 10);
350 if (*endptr != ':') {
351 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000352 return 1;
353 }
354
Douglas Gregor88d23952009-11-09 18:19:57 +0000355 /* Copy the file name. */
356 *filename = (char*)malloc(first_colon - input + 1);
357 memcpy(*filename, input, first_colon - input);
358 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000359 return 0;
360}
361
362const char *
363clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
364 switch (Kind) {
365 case CXCompletionChunk_Optional: return "Optional";
366 case CXCompletionChunk_TypedText: return "TypedText";
367 case CXCompletionChunk_Text: return "Text";
368 case CXCompletionChunk_Placeholder: return "Placeholder";
369 case CXCompletionChunk_Informative: return "Informative";
370 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
371 case CXCompletionChunk_LeftParen: return "LeftParen";
372 case CXCompletionChunk_RightParen: return "RightParen";
373 case CXCompletionChunk_LeftBracket: return "LeftBracket";
374 case CXCompletionChunk_RightBracket: return "RightBracket";
375 case CXCompletionChunk_LeftBrace: return "LeftBrace";
376 case CXCompletionChunk_RightBrace: return "RightBrace";
377 case CXCompletionChunk_LeftAngle: return "LeftAngle";
378 case CXCompletionChunk_RightAngle: return "RightAngle";
379 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000380 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000381 case CXCompletionChunk_Colon: return "Colon";
382 case CXCompletionChunk_SemiColon: return "SemiColon";
383 case CXCompletionChunk_Equal: return "Equal";
384 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
385 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000386 }
387
388 return "Unknown";
389}
390
Douglas Gregor3ac73852009-11-09 16:04:45 +0000391void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000392 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000393
394 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000395 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000396 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000397 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000398 = clang_getCompletionChunkKind(completion_string, I);
399
400 if (Kind == CXCompletionChunk_Optional) {
401 fprintf(file, "{Optional ");
402 print_completion_string(
403 clang_getCompletionChunkCompletionString(completion_string, I),
404 file);
405 fprintf(file, "}");
406 continue;
407 }
408
Douglas Gregord5a20892009-11-09 17:05:28 +0000409 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000410 fprintf(file, "{%s %s}",
411 clang_getCompletionChunkKindSpelling(Kind),
412 text? text : "");
413 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000414}
415
416void print_completion_result(CXCompletionResult *completion_result,
417 CXClientData client_data) {
418 FILE *file = (FILE *)client_data;
419 fprintf(file, "%s:",
420 clang_getCursorKindSpelling(completion_result->CursorKind));
421 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000422 fprintf(file, "\n");
423}
424
Douglas Gregor735df882009-12-02 09:21:34 +0000425void free_remapped_files(struct CXUnsavedFile *unsaved_files,
426 int num_unsaved_files) {
427 int i;
428 for (i = 0; i != num_unsaved_files; ++i) {
429 free((char *)unsaved_files[i].Filename);
430 free((char *)unsaved_files[i].Contents);
431 }
432}
433
434int parse_remapped_files(int argc, const char **argv, int start_arg,
435 struct CXUnsavedFile **unsaved_files,
436 int *num_unsaved_files) {
437 int i;
438 int arg;
439 int prefix_len = strlen("-remap-file=");
440 *unsaved_files = 0;
441 *num_unsaved_files = 0;
442
443 /* Count the number of remapped files. */
444 for (arg = start_arg; arg < argc; ++arg) {
445 if (strncmp(argv[arg], "-remap-file=", prefix_len))
446 break;
447
448 ++*num_unsaved_files;
449 }
450
451 if (*num_unsaved_files == 0)
452 return 0;
453
454 *unsaved_files
455 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
456 *num_unsaved_files);
457 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
458 struct CXUnsavedFile *unsaved = *unsaved_files + i;
459 const char *arg_string = argv[arg] + prefix_len;
460 int filename_len;
461 char *filename;
462 char *contents;
463 FILE *to_file;
464 const char *semi = strchr(arg_string, ';');
465 if (!semi) {
466 fprintf(stderr,
467 "error: -remap-file=from;to argument is missing semicolon\n");
468 free_remapped_files(*unsaved_files, i);
469 *unsaved_files = 0;
470 *num_unsaved_files = 0;
471 return -1;
472 }
473
474 /* Open the file that we're remapping to. */
475 to_file = fopen(semi + 1, "r");
476 if (!to_file) {
477 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
478 semi + 1);
479 free_remapped_files(*unsaved_files, i);
480 *unsaved_files = 0;
481 *num_unsaved_files = 0;
482 return -1;
483 }
484
485 /* Determine the length of the file we're remapping to. */
486 fseek(to_file, 0, SEEK_END);
487 unsaved->Length = ftell(to_file);
488 fseek(to_file, 0, SEEK_SET);
489
490 /* Read the contents of the file we're remapping to. */
491 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000492 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
493 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
494 (feof(to_file) ? "EOF" : "error"), semi + 1);
495 fclose(to_file);
496 free_remapped_files(*unsaved_files, i);
497 *unsaved_files = 0;
498 *num_unsaved_files = 0;
499 return -1;
500 }
Douglas Gregor735df882009-12-02 09:21:34 +0000501 contents[unsaved->Length] = 0;
502 unsaved->Contents = contents;
503
504 /* Close the file. */
505 fclose(to_file);
506
507 /* Copy the file name that we're remapping from. */
508 filename_len = semi - arg_string;
509 filename = (char *)malloc(filename_len + 1);
510 memcpy(filename, arg_string, filename_len);
511 filename[filename_len] = 0;
512 unsaved->Filename = filename;
513 }
514
515 return 0;
516}
517
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000518int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000519 const char *input = argv[1];
520 char *filename = 0;
521 unsigned line;
522 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000523 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000524 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000525 struct CXUnsavedFile *unsaved_files = 0;
526 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000527 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000528
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000529 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000530 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
531 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000532
Douglas Gregor735df882009-12-02 09:21:34 +0000533 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
534 return -1;
535
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000536 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000537 results = clang_codeComplete(CIdx,
538 argv[argc - 1], argc - num_unsaved_files - 3,
539 argv + num_unsaved_files + 2,
540 num_unsaved_files, unsaved_files,
541 filename, line, column);
542 if (results) {
543 unsigned i, n = results->NumResults;
544 for (i = 0; i != n; ++i)
545 print_completion_result(results->Results + i, stdout);
546 clang_disposeCodeCompleteResults(results);
547 }
548
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000549 clang_disposeIndex(CIdx);
550 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000551
Douglas Gregor735df882009-12-02 09:21:34 +0000552 free_remapped_files(unsaved_files, num_unsaved_files);
553
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000554 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000555}
556
Ted Kremenek0d435192009-11-17 18:13:31 +0000557/******************************************************************************/
558/* Command line processing. */
559/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000560
561static void print_usage(void) {
562 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000563 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000564 " c-index-test -test-file-scan <AST file> <source file> "
565 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000566 " c-index-test -test-load-tu <AST file> <symbol filter> "
567 "[FileCheck prefix]\n"
Daniel Dunbarada487d2009-12-01 02:03:10 +0000568 " c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
569 " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000570 " all - load all symbols, including those from PCH\n"
571 " local - load all symbols except those in PCH\n"
572 " category - only load ObjC categories (non-PCH)\n"
573 " interface - only load ObjC interfaces (non-PCH)\n"
574 " protocol - only load ObjC protocols (non-PCH)\n"
575 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000576 " typedef - only load typdefs (non-PCH)\n"
577 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000578}
579
580int main(int argc, const char **argv) {
581 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
582 return perform_code_completion(argc, argv);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000583 if (argc >= 4 && strcmp(argv[1], "-test-load-tu") == 0)
584 return perform_test_load_tu(argv[2], argv[3],
585 argc >= 5 ? argv[4] : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000586 if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
587 return perform_test_load_source(argc - 3, argv + 3, argv[2]);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000588 if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
589 return perform_file_scan(argv[2], argv[3],
590 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000591
592 print_usage();
593 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000594}