blob: a2d4fe980814c182c11a0cf4a0b1b34f0962c1ec [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>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00007#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008
Ted Kremenek0d435192009-11-17 18:13:31 +00009/******************************************************************************/
10/* Utility functions. */
11/******************************************************************************/
12
John Thompson2e06fc82009-10-27 13:42:56 +000013#ifdef _MSC_VER
14char *basename(const char* path)
15{
16 char* base1 = (char*)strrchr(path, '/');
17 char* base2 = (char*)strrchr(path, '\\');
18 if (base1 && base2)
19 return((base1 > base2) ? base1 + 1 : base2 + 1);
20 else if (base1)
21 return(base1 + 1);
22 else if (base2)
23 return(base2 + 1);
24
25 return((char*)path);
26}
27#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000028extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000029#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000030
Ted Kremenek1c6da172009-11-17 19:37:36 +000031static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
32 CXTranslationUnit *TU) {
33
34 *TU = clang_createTranslationUnit(Idx, file);
35 if (!TU) {
36 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
37 return 0;
38 }
39 return 1;
40}
41
Ted Kremenek0d435192009-11-17 18:13:31 +000042/******************************************************************************/
43/* Pretty-printing. */
44/******************************************************************************/
45
Steve Naroffaf08ddc2009-09-03 15:49:00 +000046static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000047 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +000048 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000049 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000050 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000051 CXString string;
52 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000053 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000054 clang_getCString(string));
55 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000056 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000057 if (DeclReferenced)
58 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
59 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000060 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000061}
Steve Naroff89922f82009-08-31 00:59:03 +000062
Ted Kremenek9298cfc2009-11-17 05:31:58 +000063static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor98258af2010-01-18 22:46:11 +000064 const char *source = clang_getFileName(clang_getCursorLocation(Cursor).file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +000065 if (!source)
66 return "<invalid loc>";
67 return basename(source);
68}
69
Ted Kremenek0d435192009-11-17 18:13:31 +000070/******************************************************************************/
71/* Logic for testing clang_loadTranslationUnit(). */
72/******************************************************************************/
73
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000074static const char *FileCheckPrefix = "CHECK";
75
76static void PrintDeclExtent(CXDecl Dcl) {
Douglas Gregor3c7313d2010-01-18 22:13:09 +000077 CXSourceRange extent;
Ted Kremenek70ee5422010-01-16 01:44:12 +000078 if (!Dcl)
79 return;
80 extent = clang_getDeclExtent(Dcl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000081 printf(" [Extent=%d:%d:%d:%d]", extent.begin.line, extent.begin.column,
82 extent.end.line, extent.end.column);
83}
84
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000085static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +000086 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +000087 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
88 const char *source = clang_getFileName(Loc.file);
89 if (!source)
90 source = "<invalid loc>";
91 printf("// %s: %s:%d:%d: ", FileCheckPrefix, source, Loc.line, Loc.column);
Ted Kremenekcf84aa42010-01-18 20:23:29 +000092 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000093 PrintDeclExtent(clang_getCursorDecl(Cursor));
94
95 printf("\n");
Daniel Dunbarbce6f622009-09-03 05:59:50 +000096 }
Steve Naroffc857ea42009-09-02 13:28:54 +000097}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000098
Steve Naroffc857ea42009-09-02 13:28:54 +000099static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000100 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000101 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000102 CXDecl D;
Douglas Gregor98258af2010-01-18 22:46:11 +0000103 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000104 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor98258af2010-01-18 22:46:11 +0000105 GetCursorSource(Cursor), Loc.line, Loc.column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000106 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000107
Ted Kremenek70ee5422010-01-16 01:44:12 +0000108 D = clang_getCursorDecl(Cursor);
109 if (!D) {
110 printf("\n");
111 return;
112 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000113
Ted Kremenek70ee5422010-01-16 01:44:12 +0000114 PrintDeclExtent(D);
115 printf("\n");
116 clang_loadDeclaration(D, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000117 }
Steve Naroff89922f82009-08-31 00:59:03 +0000118}
Steve Naroff50398192009-08-28 15:28:48 +0000119
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000120static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
121 CXClientData Filter) {
122 const char *startBuf, *endBuf;
123 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
124 CXCursor Ref;
125
126 if (Cursor.kind != CXCursor_FunctionDefn)
127 return;
128
129 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
130 &startLine, &startColumn,
131 &endLine, &endColumn);
132 /* Probe the entire body, looking for both decls and refs. */
133 curLine = startLine;
134 curColumn = startColumn;
135
136 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000137 CXSourceLocation Loc;
138 const char *source = 0;
139
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000140 if (*startBuf == '\n') {
141 startBuf++;
142 curLine++;
143 curColumn = 1;
144 } else if (*startBuf != '\t')
145 curColumn++;
146
Douglas Gregor98258af2010-01-18 22:46:11 +0000147 Loc = clang_getCursorLocation(Cursor);
148 source = clang_getFileName(Loc.file);
149 if (source) {
150 Ref = clang_getCursor(Unit, source, curLine, curColumn);
151 if (Ref.kind == CXCursor_NoDeclFound) {
152 /* Nothing found here; that's fine. */
153 } else if (Ref.kind != CXCursor_FunctionDecl) {
154 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
155 curLine, curColumn);
156 PrintCursor(Ref);
157 printf("\n");
158 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000159 }
160 startBuf++;
161 }
162}
163
Ted Kremenek7d405622010-01-12 23:34:26 +0000164/******************************************************************************/
165/* USR testing. */
166/******************************************************************************/
167
168static void USRDeclVisitor(CXDecl D, CXCursor C, CXClientData Filter) {
169 if (!Filter || (C.kind == *(enum CXCursorKind *)Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000170 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000171 if (!USR.Spelling) {
172 clang_disposeString(USR);
173 return;
174 }
175 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000176 PrintDeclExtent(clang_getCursorDecl(C));
Ted Kremenek7d405622010-01-12 23:34:26 +0000177 printf("\n");
178 clang_disposeString(USR);
179 }
180}
181
182static void USRVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Ted Kremenek70ee5422010-01-16 01:44:12 +0000183 CXClientData Filter) {
184 CXDecl D = clang_getCursorDecl(Cursor);
185 if (D) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000186 /* USRDeclVisitor(Unit, Cursor.decl, Cursor, Filter);*/
Ted Kremenek70ee5422010-01-16 01:44:12 +0000187 clang_loadDeclaration(D, USRDeclVisitor, 0);
Ted Kremenek7d405622010-01-12 23:34:26 +0000188 }
189}
190
191/******************************************************************************/
192/* Loading ASTs/source. */
193/******************************************************************************/
194
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000195static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000196 const char *filter, const char *prefix,
197 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000198 enum CXCursorKind K = CXCursor_NotImplemented;
199 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000200
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000201 if (prefix)
202 FileCheckPrefix = prefix;
203
Ted Kremenek0d435192009-11-17 18:13:31 +0000204 /* Perform some simple filtering. */
205 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
206 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
207 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
208 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
209 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
210 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000211 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000212 else {
213 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
214 return 1;
215 }
216
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000217 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000218 clang_disposeTranslationUnit(TU);
219 return 0;
220}
221
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000222int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000223 const char *prefix,
224 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000225 CXIndex Idx;
226 CXTranslationUnit TU;
227 Idx = clang_createIndex(/* excludeDeclsFromPCH */
228 !strcmp(filter, "local") ? 1 : 0,
229 /* displayDiagnostics */ 1);
230
231 if (!CreateTranslationUnit(Idx, file, &TU))
232 return 1;
233
Ted Kremenek98271562010-01-12 18:53:15 +0000234 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000235}
236
Ted Kremenek98271562010-01-12 18:53:15 +0000237int perform_test_load_source(int argc, const char **argv, const char *filter,
238 CXTranslationUnitIterator Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000239 const char *UseExternalASTs =
240 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000241 CXIndex Idx;
242 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000243 Idx = clang_createIndex(/* excludeDeclsFromPCH */
244 !strcmp(filter, "local") ? 1 : 0,
245 /* displayDiagnostics */ 1);
246
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000247 if (UseExternalASTs && strlen(UseExternalASTs))
248 clang_setUseExternalASTGeneration(Idx, 1);
249
Daniel Dunbarada487d2009-12-01 02:03:10 +0000250 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
251 if (!TU) {
252 fprintf(stderr, "Unable to load translation unit!\n");
253 return 1;
254 }
255
Ted Kremenek98271562010-01-12 18:53:15 +0000256 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000257}
258
Ted Kremenek0d435192009-11-17 18:13:31 +0000259/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000260/* Logic for testing clang_getCursor(). */
261/******************************************************************************/
262
263static void print_cursor_file_scan(CXCursor cursor,
264 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000265 unsigned end_line, unsigned end_col,
266 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000267 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000268 if (prefix)
269 printf("-%s", prefix);
270 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
271 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000272 PrintCursor(cursor);
273 printf("\n");
274}
275
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000276static int perform_file_scan(const char *ast_file, const char *source_file,
277 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000278 CXIndex Idx;
279 CXTranslationUnit TU;
280 FILE *fp;
281 unsigned line;
282 CXCursor prevCursor;
283 unsigned printed;
284 unsigned start_line, start_col, last_line, last_col;
285 size_t i;
286
287 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
288 /* displayDiagnostics */ 1))) {
289 fprintf(stderr, "Could not create Index\n");
290 return 1;
291 }
292
293 if (!CreateTranslationUnit(Idx, ast_file, &TU))
294 return 1;
295
296 if ((fp = fopen(source_file, "r")) == NULL) {
297 fprintf(stderr, "Could not open '%s'\n", source_file);
298 return 1;
299 }
300
301 line = 0;
302 prevCursor = clang_getNullCursor();
303 printed = 0;
304 start_line = last_line = 1;
305 start_col = last_col = 1;
306
307 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000308 size_t len = 0;
309 int c;
310
311 while ((c = fgetc(fp)) != EOF) {
312 len++;
313 if (c == '\n')
314 break;
315 }
316
Ted Kremenek1c6da172009-11-17 19:37:36 +0000317 ++line;
318
319 for (i = 0; i < len ; ++i) {
320 CXCursor cursor;
321 cursor = clang_getCursor(TU, source_file, line, i+1);
322
323 if (!clang_equalCursors(cursor, prevCursor) &&
324 prevCursor.kind != CXCursor_InvalidFile) {
325 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000326 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000327 printed = 1;
328 start_line = line;
329 start_col = (unsigned) i+1;
330 }
331 else {
332 printed = 0;
333 }
334
335 prevCursor = cursor;
336 last_line = line;
337 last_col = (unsigned) i+1;
338 }
339 }
340
341 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
342 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000343 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000344 }
345
346 fclose(fp);
347 return 0;
348}
349
350/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000351/* Logic for testing clang_codeComplete(). */
352/******************************************************************************/
353
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000354/* Parse file:line:column from the input string. Returns 0 on success, non-zero
355 on failure. If successful, the pointer *filename will contain newly-allocated
356 memory (that will be owned by the caller) to store the file name. */
357int parse_file_line_column(const char *input, char **filename, unsigned *line,
358 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000359 /* Find the second colon. */
360 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000361 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000362 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000363 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
364 return 1;
365 }
366
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000367 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000368 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000369 if (*endptr != 0) {
370 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000371 return 1;
372 }
373
374 /* Find the first colon. */
375 first_colon = second_colon - 1;
376 while (first_colon != input && *first_colon != ':')
377 --first_colon;
378 if (first_colon == input) {
379 fprintf(stderr, "could not parse line in '%s'\n", input);
380 return 1;
381 }
382
383 /* Parse the line number. */
384 *line = strtol(first_colon + 1, &endptr, 10);
385 if (*endptr != ':') {
386 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000387 return 1;
388 }
389
Douglas Gregor88d23952009-11-09 18:19:57 +0000390 /* Copy the file name. */
391 *filename = (char*)malloc(first_colon - input + 1);
392 memcpy(*filename, input, first_colon - input);
393 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000394 return 0;
395}
396
397const char *
398clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
399 switch (Kind) {
400 case CXCompletionChunk_Optional: return "Optional";
401 case CXCompletionChunk_TypedText: return "TypedText";
402 case CXCompletionChunk_Text: return "Text";
403 case CXCompletionChunk_Placeholder: return "Placeholder";
404 case CXCompletionChunk_Informative: return "Informative";
405 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
406 case CXCompletionChunk_LeftParen: return "LeftParen";
407 case CXCompletionChunk_RightParen: return "RightParen";
408 case CXCompletionChunk_LeftBracket: return "LeftBracket";
409 case CXCompletionChunk_RightBracket: return "RightBracket";
410 case CXCompletionChunk_LeftBrace: return "LeftBrace";
411 case CXCompletionChunk_RightBrace: return "RightBrace";
412 case CXCompletionChunk_LeftAngle: return "LeftAngle";
413 case CXCompletionChunk_RightAngle: return "RightAngle";
414 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000415 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000416 case CXCompletionChunk_Colon: return "Colon";
417 case CXCompletionChunk_SemiColon: return "SemiColon";
418 case CXCompletionChunk_Equal: return "Equal";
419 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
420 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000421 }
422
423 return "Unknown";
424}
425
Douglas Gregor3ac73852009-11-09 16:04:45 +0000426void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000427 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000428
429 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000430 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000431 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000432 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000433 = clang_getCompletionChunkKind(completion_string, I);
434
435 if (Kind == CXCompletionChunk_Optional) {
436 fprintf(file, "{Optional ");
437 print_completion_string(
438 clang_getCompletionChunkCompletionString(completion_string, I),
439 file);
440 fprintf(file, "}");
441 continue;
442 }
443
Douglas Gregord5a20892009-11-09 17:05:28 +0000444 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000445 fprintf(file, "{%s %s}",
446 clang_getCompletionChunkKindSpelling(Kind),
447 text? text : "");
448 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000449}
450
451void print_completion_result(CXCompletionResult *completion_result,
452 CXClientData client_data) {
453 FILE *file = (FILE *)client_data;
454 fprintf(file, "%s:",
455 clang_getCursorKindSpelling(completion_result->CursorKind));
456 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000457 fprintf(file, "\n");
458}
459
Douglas Gregor735df882009-12-02 09:21:34 +0000460void free_remapped_files(struct CXUnsavedFile *unsaved_files,
461 int num_unsaved_files) {
462 int i;
463 for (i = 0; i != num_unsaved_files; ++i) {
464 free((char *)unsaved_files[i].Filename);
465 free((char *)unsaved_files[i].Contents);
466 }
467}
468
469int parse_remapped_files(int argc, const char **argv, int start_arg,
470 struct CXUnsavedFile **unsaved_files,
471 int *num_unsaved_files) {
472 int i;
473 int arg;
474 int prefix_len = strlen("-remap-file=");
475 *unsaved_files = 0;
476 *num_unsaved_files = 0;
477
478 /* Count the number of remapped files. */
479 for (arg = start_arg; arg < argc; ++arg) {
480 if (strncmp(argv[arg], "-remap-file=", prefix_len))
481 break;
482
483 ++*num_unsaved_files;
484 }
485
486 if (*num_unsaved_files == 0)
487 return 0;
488
489 *unsaved_files
490 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
491 *num_unsaved_files);
492 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
493 struct CXUnsavedFile *unsaved = *unsaved_files + i;
494 const char *arg_string = argv[arg] + prefix_len;
495 int filename_len;
496 char *filename;
497 char *contents;
498 FILE *to_file;
499 const char *semi = strchr(arg_string, ';');
500 if (!semi) {
501 fprintf(stderr,
502 "error: -remap-file=from;to argument is missing semicolon\n");
503 free_remapped_files(*unsaved_files, i);
504 *unsaved_files = 0;
505 *num_unsaved_files = 0;
506 return -1;
507 }
508
509 /* Open the file that we're remapping to. */
510 to_file = fopen(semi + 1, "r");
511 if (!to_file) {
512 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
513 semi + 1);
514 free_remapped_files(*unsaved_files, i);
515 *unsaved_files = 0;
516 *num_unsaved_files = 0;
517 return -1;
518 }
519
520 /* Determine the length of the file we're remapping to. */
521 fseek(to_file, 0, SEEK_END);
522 unsaved->Length = ftell(to_file);
523 fseek(to_file, 0, SEEK_SET);
524
525 /* Read the contents of the file we're remapping to. */
526 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000527 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
528 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
529 (feof(to_file) ? "EOF" : "error"), semi + 1);
530 fclose(to_file);
531 free_remapped_files(*unsaved_files, i);
532 *unsaved_files = 0;
533 *num_unsaved_files = 0;
534 return -1;
535 }
Douglas Gregor735df882009-12-02 09:21:34 +0000536 contents[unsaved->Length] = 0;
537 unsaved->Contents = contents;
538
539 /* Close the file. */
540 fclose(to_file);
541
542 /* Copy the file name that we're remapping from. */
543 filename_len = semi - arg_string;
544 filename = (char *)malloc(filename_len + 1);
545 memcpy(filename, arg_string, filename_len);
546 filename[filename_len] = 0;
547 unsaved->Filename = filename;
548 }
549
550 return 0;
551}
552
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000553int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000554 const char *input = argv[1];
555 char *filename = 0;
556 unsigned line;
557 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000558 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000559 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000560 struct CXUnsavedFile *unsaved_files = 0;
561 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000562 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000563
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000564 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000565 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
566 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000567
Douglas Gregor735df882009-12-02 09:21:34 +0000568 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
569 return -1;
570
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000571 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000572 results = clang_codeComplete(CIdx,
573 argv[argc - 1], argc - num_unsaved_files - 3,
574 argv + num_unsaved_files + 2,
575 num_unsaved_files, unsaved_files,
576 filename, line, column);
577 if (results) {
578 unsigned i, n = results->NumResults;
579 for (i = 0; i != n; ++i)
580 print_completion_result(results->Results + i, stdout);
581 clang_disposeCodeCompleteResults(results);
582 }
583
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000584 clang_disposeIndex(CIdx);
585 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000586
Douglas Gregor735df882009-12-02 09:21:34 +0000587 free_remapped_files(unsaved_files, num_unsaved_files);
588
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000589 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000590}
591
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000592typedef struct {
593 char *filename;
594 unsigned line;
595 unsigned column;
596} CursorSourceLocation;
597
598int inspect_cursor_at(int argc, const char **argv) {
599 CXIndex CIdx;
600 int errorCode;
601 struct CXUnsavedFile *unsaved_files = 0;
602 int num_unsaved_files = 0;
603 CXTranslationUnit TU;
604 CXCursor Cursor;
605 CursorSourceLocation *Locations = 0;
606 unsigned NumLocations = 0, Loc;
607
608 /* Count the number of locations. */
609 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
610 ++NumLocations;
611
612 /* Parse the locations. */
613 assert(NumLocations > 0 && "Unable to count locations?");
614 Locations = (CursorSourceLocation *)malloc(
615 NumLocations * sizeof(CursorSourceLocation));
616 for (Loc = 0; Loc < NumLocations; ++Loc) {
617 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
618 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
619 &Locations[Loc].line,
620 &Locations[Loc].column)))
621 return errorCode;
622 }
623
624 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
625 &num_unsaved_files))
626 return -1;
627
628 if (num_unsaved_files > 0) {
629 fprintf(stderr, "cannot remap files when looking for a cursor\n");
630 return -1;
631 }
632
633 CIdx = clang_createIndex(0, 1);
634 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
635 argc - num_unsaved_files - 2 - NumLocations,
636 argv + num_unsaved_files + 1 + NumLocations);
637 if (!TU) {
638 fprintf(stderr, "unable to parse input\n");
639 return -1;
640 }
641
642 for (Loc = 0; Loc < NumLocations; ++Loc) {
643 Cursor = clang_getCursor(TU, Locations[Loc].filename,
644 Locations[Loc].line, Locations[Loc].column);
645 PrintCursor(Cursor);
646 printf("\n");
647 free(Locations[Loc].filename);
648 }
649
650 clang_disposeTranslationUnit(TU);
651 clang_disposeIndex(CIdx);
652 free(Locations);
653 free_remapped_files(unsaved_files, num_unsaved_files);
654 return 0;
655}
656
Ted Kremenek0d435192009-11-17 18:13:31 +0000657/******************************************************************************/
658/* Command line processing. */
659/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000660
Ted Kremenek7d405622010-01-12 23:34:26 +0000661static CXTranslationUnitIterator GetVisitor(const char *s) {
662 if (s[0] == '\0')
663 return TranslationUnitVisitor;
664 if (strcmp(s, "-usrs") == 0)
665 return USRVisitor;
666 return NULL;
667}
668
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000669static void print_usage(void) {
670 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000671 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000672 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000673 " c-index-test -test-file-scan <AST file> <source file> "
674 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000675 " c-index-test -test-load-tu <AST file> <symbol filter> "
676 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000677 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
678 "[FileCheck prefix]\n"
679 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000680 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
681 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000682 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000683 " all - load all symbols, including those from PCH\n"
684 " local - load all symbols except those in PCH\n"
685 " category - only load ObjC categories (non-PCH)\n"
686 " interface - only load ObjC interfaces (non-PCH)\n"
687 " protocol - only load ObjC protocols (non-PCH)\n"
688 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000689 " typedef - only load typdefs (non-PCH)\n"
690 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000691}
692
693int main(int argc, const char **argv) {
694 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
695 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000696 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
697 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000698 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
699 CXTranslationUnitIterator I = GetVisitor(argv[1] + 13);
700 if (I)
701 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
702 }
703 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
704 CXTranslationUnitIterator I = GetVisitor(argv[1] + 17);
705 if (I)
706 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
707 }
708 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000709 return perform_file_scan(argv[2], argv[3],
710 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000711
712 print_usage();
713 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000714}