blob: 01a0d439297b0f225131085638060d2dac1f1054 [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
Ted Kremenek7d405622010-01-12 23:34:26 +0000162/******************************************************************************/
163/* USR testing. */
164/******************************************************************************/
165
166static void USRDeclVisitor(CXDecl D, CXCursor C, CXClientData Filter) {
167 if (!Filter || (C.kind == *(enum CXCursorKind *)Filter)) {
168 CXString USR = clang_getDeclUSR(C.decl);
169 if (!USR.Spelling) {
170 clang_disposeString(USR);
171 return;
172 }
173 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
174 PrintDeclExtent(C.decl);
175 printf("\n");
176 clang_disposeString(USR);
177 }
178}
179
180static void USRVisitor(CXTranslationUnit Unit, CXCursor Cursor,
181 CXClientData Filter) {
182 if (Cursor.decl) {
183 /* USRDeclVisitor(Unit, Cursor.decl, Cursor, Filter);*/
184 clang_loadDeclaration(Cursor.decl, USRDeclVisitor, 0);
185 }
186}
187
188/******************************************************************************/
189/* Loading ASTs/source. */
190/******************************************************************************/
191
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000192static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000193 const char *filter, const char *prefix,
194 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000195 enum CXCursorKind K = CXCursor_NotImplemented;
196 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000197
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000198 if (prefix)
199 FileCheckPrefix = prefix;
200
Ted Kremenek0d435192009-11-17 18:13:31 +0000201 /* Perform some simple filtering. */
202 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
203 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
204 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
205 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
206 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
207 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000208 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000209 else {
210 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
211 return 1;
212 }
213
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000214 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000215 clang_disposeTranslationUnit(TU);
216 return 0;
217}
218
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000219int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000220 const char *prefix,
221 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000222 CXIndex Idx;
223 CXTranslationUnit TU;
224 Idx = clang_createIndex(/* excludeDeclsFromPCH */
225 !strcmp(filter, "local") ? 1 : 0,
226 /* displayDiagnostics */ 1);
227
228 if (!CreateTranslationUnit(Idx, file, &TU))
229 return 1;
230
Ted Kremenek98271562010-01-12 18:53:15 +0000231 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000232}
233
Ted Kremenek98271562010-01-12 18:53:15 +0000234int perform_test_load_source(int argc, const char **argv, const char *filter,
235 CXTranslationUnitIterator Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000236 const char *UseExternalASTs =
237 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000238 CXIndex Idx;
239 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000240 Idx = clang_createIndex(/* excludeDeclsFromPCH */
241 !strcmp(filter, "local") ? 1 : 0,
242 /* displayDiagnostics */ 1);
243
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000244 if (UseExternalASTs && strlen(UseExternalASTs))
245 clang_setUseExternalASTGeneration(Idx, 1);
246
Daniel Dunbarada487d2009-12-01 02:03:10 +0000247 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
248 if (!TU) {
249 fprintf(stderr, "Unable to load translation unit!\n");
250 return 1;
251 }
252
Ted Kremenek98271562010-01-12 18:53:15 +0000253 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000254}
255
Ted Kremenek0d435192009-11-17 18:13:31 +0000256/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000257/* Logic for testing clang_getCursor(). */
258/******************************************************************************/
259
260static void print_cursor_file_scan(CXCursor cursor,
261 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000262 unsigned end_line, unsigned end_col,
263 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000264 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000265 if (prefix)
266 printf("-%s", prefix);
267 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
268 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000269 PrintCursor(cursor);
270 printf("\n");
271}
272
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000273static int perform_file_scan(const char *ast_file, const char *source_file,
274 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000275 CXIndex Idx;
276 CXTranslationUnit TU;
277 FILE *fp;
278 unsigned line;
279 CXCursor prevCursor;
280 unsigned printed;
281 unsigned start_line, start_col, last_line, last_col;
282 size_t i;
283
284 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
285 /* displayDiagnostics */ 1))) {
286 fprintf(stderr, "Could not create Index\n");
287 return 1;
288 }
289
290 if (!CreateTranslationUnit(Idx, ast_file, &TU))
291 return 1;
292
293 if ((fp = fopen(source_file, "r")) == NULL) {
294 fprintf(stderr, "Could not open '%s'\n", source_file);
295 return 1;
296 }
297
298 line = 0;
299 prevCursor = clang_getNullCursor();
300 printed = 0;
301 start_line = last_line = 1;
302 start_col = last_col = 1;
303
304 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000305 size_t len = 0;
306 int c;
307
308 while ((c = fgetc(fp)) != EOF) {
309 len++;
310 if (c == '\n')
311 break;
312 }
313
Ted Kremenek1c6da172009-11-17 19:37:36 +0000314 ++line;
315
316 for (i = 0; i < len ; ++i) {
317 CXCursor cursor;
318 cursor = clang_getCursor(TU, source_file, line, i+1);
319
320 if (!clang_equalCursors(cursor, prevCursor) &&
321 prevCursor.kind != CXCursor_InvalidFile) {
322 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000323 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000324 printed = 1;
325 start_line = line;
326 start_col = (unsigned) i+1;
327 }
328 else {
329 printed = 0;
330 }
331
332 prevCursor = cursor;
333 last_line = line;
334 last_col = (unsigned) i+1;
335 }
336 }
337
338 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
339 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000340 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000341 }
342
343 fclose(fp);
344 return 0;
345}
346
347/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000348/* Logic for testing clang_codeComplete(). */
349/******************************************************************************/
350
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000351/* Parse file:line:column from the input string. Returns 0 on success, non-zero
352 on failure. If successful, the pointer *filename will contain newly-allocated
353 memory (that will be owned by the caller) to store the file name. */
354int parse_file_line_column(const char *input, char **filename, unsigned *line,
355 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000356 /* Find the second colon. */
357 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000358 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000359 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000360 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
361 return 1;
362 }
363
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000364 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000365 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000366 if (*endptr != 0) {
367 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000368 return 1;
369 }
370
371 /* Find the first colon. */
372 first_colon = second_colon - 1;
373 while (first_colon != input && *first_colon != ':')
374 --first_colon;
375 if (first_colon == input) {
376 fprintf(stderr, "could not parse line in '%s'\n", input);
377 return 1;
378 }
379
380 /* Parse the line number. */
381 *line = strtol(first_colon + 1, &endptr, 10);
382 if (*endptr != ':') {
383 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000384 return 1;
385 }
386
Douglas Gregor88d23952009-11-09 18:19:57 +0000387 /* Copy the file name. */
388 *filename = (char*)malloc(first_colon - input + 1);
389 memcpy(*filename, input, first_colon - input);
390 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000391 return 0;
392}
393
394const char *
395clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
396 switch (Kind) {
397 case CXCompletionChunk_Optional: return "Optional";
398 case CXCompletionChunk_TypedText: return "TypedText";
399 case CXCompletionChunk_Text: return "Text";
400 case CXCompletionChunk_Placeholder: return "Placeholder";
401 case CXCompletionChunk_Informative: return "Informative";
402 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
403 case CXCompletionChunk_LeftParen: return "LeftParen";
404 case CXCompletionChunk_RightParen: return "RightParen";
405 case CXCompletionChunk_LeftBracket: return "LeftBracket";
406 case CXCompletionChunk_RightBracket: return "RightBracket";
407 case CXCompletionChunk_LeftBrace: return "LeftBrace";
408 case CXCompletionChunk_RightBrace: return "RightBrace";
409 case CXCompletionChunk_LeftAngle: return "LeftAngle";
410 case CXCompletionChunk_RightAngle: return "RightAngle";
411 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000412 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000413 case CXCompletionChunk_Colon: return "Colon";
414 case CXCompletionChunk_SemiColon: return "SemiColon";
415 case CXCompletionChunk_Equal: return "Equal";
416 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
417 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000418 }
419
420 return "Unknown";
421}
422
Douglas Gregor3ac73852009-11-09 16:04:45 +0000423void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000424 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000425
426 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000427 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000428 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000429 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000430 = clang_getCompletionChunkKind(completion_string, I);
431
432 if (Kind == CXCompletionChunk_Optional) {
433 fprintf(file, "{Optional ");
434 print_completion_string(
435 clang_getCompletionChunkCompletionString(completion_string, I),
436 file);
437 fprintf(file, "}");
438 continue;
439 }
440
Douglas Gregord5a20892009-11-09 17:05:28 +0000441 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000442 fprintf(file, "{%s %s}",
443 clang_getCompletionChunkKindSpelling(Kind),
444 text? text : "");
445 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000446}
447
448void print_completion_result(CXCompletionResult *completion_result,
449 CXClientData client_data) {
450 FILE *file = (FILE *)client_data;
451 fprintf(file, "%s:",
452 clang_getCursorKindSpelling(completion_result->CursorKind));
453 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000454 fprintf(file, "\n");
455}
456
Douglas Gregor735df882009-12-02 09:21:34 +0000457void free_remapped_files(struct CXUnsavedFile *unsaved_files,
458 int num_unsaved_files) {
459 int i;
460 for (i = 0; i != num_unsaved_files; ++i) {
461 free((char *)unsaved_files[i].Filename);
462 free((char *)unsaved_files[i].Contents);
463 }
464}
465
466int parse_remapped_files(int argc, const char **argv, int start_arg,
467 struct CXUnsavedFile **unsaved_files,
468 int *num_unsaved_files) {
469 int i;
470 int arg;
471 int prefix_len = strlen("-remap-file=");
472 *unsaved_files = 0;
473 *num_unsaved_files = 0;
474
475 /* Count the number of remapped files. */
476 for (arg = start_arg; arg < argc; ++arg) {
477 if (strncmp(argv[arg], "-remap-file=", prefix_len))
478 break;
479
480 ++*num_unsaved_files;
481 }
482
483 if (*num_unsaved_files == 0)
484 return 0;
485
486 *unsaved_files
487 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
488 *num_unsaved_files);
489 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
490 struct CXUnsavedFile *unsaved = *unsaved_files + i;
491 const char *arg_string = argv[arg] + prefix_len;
492 int filename_len;
493 char *filename;
494 char *contents;
495 FILE *to_file;
496 const char *semi = strchr(arg_string, ';');
497 if (!semi) {
498 fprintf(stderr,
499 "error: -remap-file=from;to argument is missing semicolon\n");
500 free_remapped_files(*unsaved_files, i);
501 *unsaved_files = 0;
502 *num_unsaved_files = 0;
503 return -1;
504 }
505
506 /* Open the file that we're remapping to. */
507 to_file = fopen(semi + 1, "r");
508 if (!to_file) {
509 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
510 semi + 1);
511 free_remapped_files(*unsaved_files, i);
512 *unsaved_files = 0;
513 *num_unsaved_files = 0;
514 return -1;
515 }
516
517 /* Determine the length of the file we're remapping to. */
518 fseek(to_file, 0, SEEK_END);
519 unsaved->Length = ftell(to_file);
520 fseek(to_file, 0, SEEK_SET);
521
522 /* Read the contents of the file we're remapping to. */
523 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000524 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
525 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
526 (feof(to_file) ? "EOF" : "error"), semi + 1);
527 fclose(to_file);
528 free_remapped_files(*unsaved_files, i);
529 *unsaved_files = 0;
530 *num_unsaved_files = 0;
531 return -1;
532 }
Douglas Gregor735df882009-12-02 09:21:34 +0000533 contents[unsaved->Length] = 0;
534 unsaved->Contents = contents;
535
536 /* Close the file. */
537 fclose(to_file);
538
539 /* Copy the file name that we're remapping from. */
540 filename_len = semi - arg_string;
541 filename = (char *)malloc(filename_len + 1);
542 memcpy(filename, arg_string, filename_len);
543 filename[filename_len] = 0;
544 unsaved->Filename = filename;
545 }
546
547 return 0;
548}
549
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000550int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000551 const char *input = argv[1];
552 char *filename = 0;
553 unsigned line;
554 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000555 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000556 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000557 struct CXUnsavedFile *unsaved_files = 0;
558 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000559 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000560
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000561 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000562 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
563 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000564
Douglas Gregor735df882009-12-02 09:21:34 +0000565 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
566 return -1;
567
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000568 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000569 results = clang_codeComplete(CIdx,
570 argv[argc - 1], argc - num_unsaved_files - 3,
571 argv + num_unsaved_files + 2,
572 num_unsaved_files, unsaved_files,
573 filename, line, column);
574 if (results) {
575 unsigned i, n = results->NumResults;
576 for (i = 0; i != n; ++i)
577 print_completion_result(results->Results + i, stdout);
578 clang_disposeCodeCompleteResults(results);
579 }
580
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000581 clang_disposeIndex(CIdx);
582 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000583
Douglas Gregor735df882009-12-02 09:21:34 +0000584 free_remapped_files(unsaved_files, num_unsaved_files);
585
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000586 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000587}
588
Ted Kremenek0d435192009-11-17 18:13:31 +0000589/******************************************************************************/
590/* Command line processing. */
591/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000592
Ted Kremenek7d405622010-01-12 23:34:26 +0000593static CXTranslationUnitIterator GetVisitor(const char *s) {
594 if (s[0] == '\0')
595 return TranslationUnitVisitor;
596 if (strcmp(s, "-usrs") == 0)
597 return USRVisitor;
598 return NULL;
599}
600
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000601static void print_usage(void) {
602 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000603 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000604 " c-index-test -test-file-scan <AST file> <source file> "
605 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000606 " c-index-test -test-load-tu <AST file> <symbol filter> "
607 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000608 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
609 "[FileCheck prefix]\n"
610 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
611 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n"
612 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000613 " all - load all symbols, including those from PCH\n"
614 " local - load all symbols except those in PCH\n"
615 " category - only load ObjC categories (non-PCH)\n"
616 " interface - only load ObjC interfaces (non-PCH)\n"
617 " protocol - only load ObjC protocols (non-PCH)\n"
618 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000619 " typedef - only load typdefs (non-PCH)\n"
620 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000621}
622
623int main(int argc, const char **argv) {
624 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
625 return perform_code_completion(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000626 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
627 CXTranslationUnitIterator I = GetVisitor(argv[1] + 13);
628 if (I)
629 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
630 }
631 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
632 CXTranslationUnitIterator I = GetVisitor(argv[1] + 17);
633 if (I)
634 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
635 }
636 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000637 return perform_file_scan(argv[2], argv[3],
638 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000639
640 print_usage();
641 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000642}