blob: 4c9723da019ea2a536ad03779cdb4e1785b76458 [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 Gregor1e5e6682010-08-26 13:48:20 +00004#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00005#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00006#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00007#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00008#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009
Ted Kremenek0d435192009-11-17 18:13:31 +000010/******************************************************************************/
11/* Utility functions. */
12/******************************************************************************/
13
John Thompson2e06fc82009-10-27 13:42:56 +000014#ifdef _MSC_VER
15char *basename(const char* path)
16{
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
25
26 return((char*)path);
27}
28#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000029extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000030#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000031
Douglas Gregor45ba9a12010-07-25 17:39:21 +000032/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000033static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
35
36 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000037 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000038 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000040 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
41 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000042 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
43 options |= CXTranslationUnit_SkipFunctionBodies;
Douglas Gregor44c181a2010-07-23 00:33:23 +000044
45 return options;
46}
47
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000048static int checkForErrors(CXTranslationUnit TU);
49
Daniel Dunbar51b058c2010-02-14 08:32:24 +000050static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
51 unsigned end_line, unsigned end_column) {
52 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000053 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000054}
55
Ted Kremenek1c6da172009-11-17 19:37:36 +000056static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
57 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000058
Douglas Gregora88084b2010-02-18 18:08:43 +000059 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000060 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000061 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
62 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000063 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000064 return 1;
65}
66
Douglas Gregor4db64a42010-01-23 00:14:00 +000067void free_remapped_files(struct CXUnsavedFile *unsaved_files,
68 int num_unsaved_files) {
69 int i;
70 for (i = 0; i != num_unsaved_files; ++i) {
71 free((char *)unsaved_files[i].Filename);
72 free((char *)unsaved_files[i].Contents);
73 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000074 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000075}
76
77int parse_remapped_files(int argc, const char **argv, int start_arg,
78 struct CXUnsavedFile **unsaved_files,
79 int *num_unsaved_files) {
80 int i;
81 int arg;
82 int prefix_len = strlen("-remap-file=");
83 *unsaved_files = 0;
84 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000085
Douglas Gregor4db64a42010-01-23 00:14:00 +000086 /* Count the number of remapped files. */
87 for (arg = start_arg; arg < argc; ++arg) {
88 if (strncmp(argv[arg], "-remap-file=", prefix_len))
89 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 ++*num_unsaved_files;
92 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000093
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 if (*num_unsaved_files == 0)
95 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000096
Douglas Gregor4db64a42010-01-23 00:14:00 +000097 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000098 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
99 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
101 struct CXUnsavedFile *unsaved = *unsaved_files + i;
102 const char *arg_string = argv[arg] + prefix_len;
103 int filename_len;
104 char *filename;
105 char *contents;
106 FILE *to_file;
107 const char *semi = strchr(arg_string, ';');
108 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000109 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000110 "error: -remap-file=from;to argument is missing semicolon\n");
111 free_remapped_files(*unsaved_files, i);
112 *unsaved_files = 0;
113 *num_unsaved_files = 0;
114 return -1;
115 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000118 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 if (!to_file) {
120 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
121 semi + 1);
122 free_remapped_files(*unsaved_files, i);
123 *unsaved_files = 0;
124 *num_unsaved_files = 0;
125 return -1;
126 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000127
Douglas Gregor4db64a42010-01-23 00:14:00 +0000128 /* Determine the length of the file we're remapping to. */
129 fseek(to_file, 0, SEEK_END);
130 unsaved->Length = ftell(to_file);
131 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000132
Douglas Gregor4db64a42010-01-23 00:14:00 +0000133 /* Read the contents of the file we're remapping to. */
134 contents = (char *)malloc(unsaved->Length + 1);
135 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
136 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
137 (feof(to_file) ? "EOF" : "error"), semi + 1);
138 fclose(to_file);
139 free_remapped_files(*unsaved_files, i);
140 *unsaved_files = 0;
141 *num_unsaved_files = 0;
142 return -1;
143 }
144 contents[unsaved->Length] = 0;
145 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000146
Douglas Gregor4db64a42010-01-23 00:14:00 +0000147 /* Close the file. */
148 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 /* Copy the file name that we're remapping from. */
151 filename_len = semi - arg_string;
152 filename = (char *)malloc(filename_len + 1);
153 memcpy(filename, arg_string, filename_len);
154 filename[filename_len] = 0;
155 unsaved->Filename = filename;
156 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000157
Douglas Gregor4db64a42010-01-23 00:14:00 +0000158 return 0;
159}
160
Ted Kremenek0d435192009-11-17 18:13:31 +0000161/******************************************************************************/
162/* Pretty-printing. */
163/******************************************************************************/
164
Douglas Gregor430d7a12011-07-25 17:48:11 +0000165static void PrintRange(CXSourceRange R, const char *str) {
166 CXFile begin_file, end_file;
167 unsigned begin_line, begin_column, end_line, end_column;
168
169 clang_getSpellingLocation(clang_getRangeStart(R),
170 &begin_file, &begin_line, &begin_column, 0);
171 clang_getSpellingLocation(clang_getRangeEnd(R),
172 &end_file, &end_line, &end_column, 0);
173 if (!begin_file || !end_file)
174 return;
175
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000176 if (str)
177 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000178 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
179}
180
Douglas Gregor358559d2010-10-02 22:49:11 +0000181int want_display_name = 0;
182
Douglas Gregorcc889662012-05-08 00:14:45 +0000183static void printVersion(const char *Prefix, CXVersion Version) {
184 if (Version.Major < 0)
185 return;
186 printf("%s%d", Prefix, Version.Major);
187
188 if (Version.Minor < 0)
189 return;
190 printf(".%d", Version.Minor);
191
192 if (Version.Subminor < 0)
193 return;
194 printf(".%d", Version.Subminor);
195}
196
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000197static void PrintCursor(CXCursor Cursor) {
198 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000199 if (clang_isInvalid(Cursor.kind)) {
200 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
201 printf("Invalid Cursor => %s", clang_getCString(ks));
202 clang_disposeString(ks);
203 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000204 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000205 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000206 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000207 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000208 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000209 CXCursor *overridden;
210 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000211 unsigned RefNameRangeNr;
212 CXSourceRange CursorExtent;
213 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000214 int AlwaysUnavailable;
215 int AlwaysDeprecated;
216 CXString UnavailableMessage;
217 CXString DeprecatedMessage;
218 CXPlatformAvailability PlatformAvailability[2];
219 int NumPlatformAvailability;
220 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000221 CXString Comment;
222 const char *CommentCString;
223
Ted Kremeneke68fff62010-02-17 00:41:32 +0000224 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000225 string = want_display_name? clang_getCursorDisplayName(Cursor)
226 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000227 printf("%s=%s", clang_getCString(ks),
228 clang_getCString(string));
229 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000230 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000231
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000232 Referenced = clang_getCursorReferenced(Cursor);
233 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000234 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
235 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
236 printf("[");
237 for (I = 0; I != N; ++I) {
238 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000239 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000240 if (I)
241 printf(", ");
242
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000243 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000244 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000245 printf("%d:%d", line, column);
246 }
247 printf("]");
248 } else {
249 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000250 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000251 printf(":%d:%d", line, column);
252 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000253 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000254
255 if (clang_isCursorDefinition(Cursor))
256 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000257
258 switch (clang_getCursorAvailability(Cursor)) {
259 case CXAvailability_Available:
260 break;
261
262 case CXAvailability_Deprecated:
263 printf(" (deprecated)");
264 break;
265
266 case CXAvailability_NotAvailable:
267 printf(" (unavailable)");
268 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000269
270 case CXAvailability_NotAccessible:
271 printf(" (inaccessible)");
272 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000273 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000274
Douglas Gregorcc889662012-05-08 00:14:45 +0000275 NumPlatformAvailability
276 = clang_getCursorPlatformAvailability(Cursor,
277 &AlwaysDeprecated,
278 &DeprecatedMessage,
279 &AlwaysUnavailable,
280 &UnavailableMessage,
281 PlatformAvailability, 2);
282 if (AlwaysUnavailable) {
283 printf(" (always unavailable: \"%s\")",
284 clang_getCString(UnavailableMessage));
285 } else if (AlwaysDeprecated) {
286 printf(" (always deprecated: \"%s\")",
287 clang_getCString(DeprecatedMessage));
288 } else {
289 for (I = 0; I != NumPlatformAvailability; ++I) {
290 if (I >= 2)
291 break;
292
293 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
294 if (PlatformAvailability[I].Unavailable)
295 printf(", unavailable");
296 else {
297 printVersion(", introduced=", PlatformAvailability[I].Introduced);
298 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
299 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
300 }
301 if (clang_getCString(PlatformAvailability[I].Message)[0])
302 printf(", message=\"%s\"",
303 clang_getCString(PlatformAvailability[I].Message));
304 printf(")");
305 }
306 }
307 for (I = 0; I != NumPlatformAvailability; ++I) {
308 if (I >= 2)
309 break;
310 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
311 }
312
313 clang_disposeString(DeprecatedMessage);
314 clang_disposeString(UnavailableMessage);
315
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000316 if (clang_CXXMethod_isStatic(Cursor))
317 printf(" (static)");
318 if (clang_CXXMethod_isVirtual(Cursor))
319 printf(" (virtual)");
320
Ted Kremenek95f33552010-08-26 01:42:22 +0000321 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
322 CXType T =
323 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
324 CXString S = clang_getTypeKindSpelling(T.kind);
325 printf(" [IBOutletCollection=%s]", clang_getCString(S));
326 clang_disposeString(S);
327 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000328
329 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
330 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
331 unsigned isVirtual = clang_isVirtualBase(Cursor);
332 const char *accessStr = 0;
333
334 switch (access) {
335 case CX_CXXInvalidAccessSpecifier:
336 accessStr = "invalid"; break;
337 case CX_CXXPublic:
338 accessStr = "public"; break;
339 case CX_CXXProtected:
340 accessStr = "protected"; break;
341 case CX_CXXPrivate:
342 accessStr = "private"; break;
343 }
344
345 printf(" [access=%s isVirtual=%s]", accessStr,
346 isVirtual ? "true" : "false");
347 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000348
349 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
350 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
351 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
352 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000353 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000354 printf(" [Specialization of %s:%d:%d]",
355 clang_getCString(Name), line, column);
356 clang_disposeString(Name);
357 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000358
359 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
360 if (num_overridden) {
361 unsigned I;
362 printf(" [Overrides ");
363 for (I = 0; I != num_overridden; ++I) {
364 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000365 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000366 if (I)
367 printf(", ");
368 printf("@%d:%d", line, column);
369 }
370 printf("]");
371 clang_disposeOverriddenCursors(overridden);
372 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000373
374 if (Cursor.kind == CXCursor_InclusionDirective) {
375 CXFile File = clang_getIncludedFile(Cursor);
376 CXString Included = clang_getFileName(File);
377 printf(" (%s)", clang_getCString(Included));
378 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000379
380 if (clang_isFileMultipleIncludeGuarded(TU, File))
381 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000382 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000383
384 CursorExtent = clang_getCursorExtent(Cursor);
385 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
386 CXNameRange_WantQualifier
387 | CXNameRange_WantSinglePiece
388 | CXNameRange_WantTemplateArgs,
389 0);
390 if (!clang_equalRanges(CursorExtent, RefNameRange))
391 PrintRange(RefNameRange, "SingleRefName");
392
393 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
394 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
395 CXNameRange_WantQualifier
396 | CXNameRange_WantTemplateArgs,
397 RefNameRangeNr);
398 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
399 break;
400 if (!clang_equalRanges(CursorExtent, RefNameRange))
401 PrintRange(RefNameRange, "RefName");
402 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000403
404 Comment = clang_Cursor_getRawCommentText(Cursor);
405 CommentCString = clang_getCString(Comment);
406 if (CommentCString != NULL && CommentCString[0] != '\0') {
407 printf(" Comment=[");
408 for ( ; *CommentCString; ++CommentCString) {
409 if (*CommentCString != '\n')
410 putchar(*CommentCString);
411 else
412 printf("\\n");
413 }
414 printf("]");
415
416 PrintRange(clang_Cursor_getCommentRange(Cursor), "CommentRange");
417 }
418 clang_disposeString(Comment);
Steve Naroff699a07d2009-09-25 21:32:34 +0000419 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000420}
Steve Naroff89922f82009-08-31 00:59:03 +0000421
Ted Kremeneke68fff62010-02-17 00:41:32 +0000422static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000423 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000424 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000425 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000426 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000427 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000428 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000429 clang_disposeString(source);
430 return "<invalid loc>";
431 }
432 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000433 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000434 clang_disposeString(source);
435 return b;
436 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000437}
438
Ted Kremenek0d435192009-11-17 18:13:31 +0000439/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000440/* Callbacks. */
441/******************************************************************************/
442
443typedef void (*PostVisitTU)(CXTranslationUnit);
444
Douglas Gregora88084b2010-02-18 18:08:43 +0000445void PrintDiagnostic(CXDiagnostic Diagnostic) {
446 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000447 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000448 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000449 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000450 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
451 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000452 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000453
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000454 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000455 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000456
Douglas Gregor274f1902010-02-22 23:17:23 +0000457 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
458 fprintf(stderr, "%s\n", clang_getCString(Msg));
459 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000460
Douglas Gregora9b06d42010-11-09 06:24:54 +0000461 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
462 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000463 if (!file)
464 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000465
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000466 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000467 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000468 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000469 CXSourceRange range;
470 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
471 CXSourceLocation start = clang_getRangeStart(range);
472 CXSourceLocation end = clang_getRangeEnd(range);
473 unsigned start_line, start_column, end_line, end_column;
474 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000475 clang_getSpellingLocation(start, &start_file, &start_line,
476 &start_column, 0);
477 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000478 if (clang_equalLocations(start, end)) {
479 /* Insertion. */
480 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000481 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000482 clang_getCString(insertion_text), start_line, start_column);
483 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
484 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000485 if (start_file == file && end_file == file) {
486 fprintf(out, "FIX-IT: Remove ");
487 PrintExtent(out, start_line, start_column, end_line, end_column);
488 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000489 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000490 } else {
491 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000492 if (start_file == end_file) {
493 fprintf(out, "FIX-IT: Replace ");
494 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000495 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000496 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000497 break;
498 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000499 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000500 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000501}
502
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000503void PrintDiagnosticSet(CXDiagnosticSet Set) {
504 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
505 for ( ; i != n ; ++i) {
506 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
507 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000508 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000509 if (ChildDiags)
510 PrintDiagnosticSet(ChildDiags);
511 }
512}
513
514void PrintDiagnostics(CXTranslationUnit TU) {
515 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
516 PrintDiagnosticSet(TUSet);
517 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000518}
519
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000520void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000521 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000522 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000523 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000524 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000525 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000526 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000527 unsigned long amount = usage.entries[i].amount;
528 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000529 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000530 ((double) amount)/(1024*1024));
531 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000532 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000533 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000534 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000535}
536
Ted Kremenekce2ae882010-01-26 17:59:48 +0000537/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000538/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000539/******************************************************************************/
540
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000541static const char *FileCheckPrefix = "CHECK";
542
Douglas Gregora7bde202010-01-19 00:34:46 +0000543static void PrintCursorExtent(CXCursor C) {
544 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000545 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000546}
547
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000548/* Data used by all of the visitors. */
549typedef struct {
550 CXTranslationUnit TU;
551 enum CXCursorKind *Filter;
552} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000553
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000554
Ted Kremeneke68fff62010-02-17 00:41:32 +0000555enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000556 CXCursor Parent,
557 CXClientData ClientData) {
558 VisitorData *Data = (VisitorData *)ClientData;
559 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000560 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000561 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000562 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000563 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000564 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000565 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000566 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000567 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000568 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000569 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000570
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000571 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000572}
Steve Naroff50398192009-08-28 15:28:48 +0000573
Ted Kremeneke68fff62010-02-17 00:41:32 +0000574static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000575 CXCursor Parent,
576 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000577 const char *startBuf, *endBuf;
578 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
579 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000580 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000581
Douglas Gregorb6998662010-01-19 19:34:47 +0000582 if (Cursor.kind != CXCursor_FunctionDecl ||
583 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000584 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000585
586 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
587 &startLine, &startColumn,
588 &endLine, &endColumn);
589 /* Probe the entire body, looking for both decls and refs. */
590 curLine = startLine;
591 curColumn = startColumn;
592
593 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000594 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000595 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000596 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000597
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000598 if (*startBuf == '\n') {
599 startBuf++;
600 curLine++;
601 curColumn = 1;
602 } else if (*startBuf != '\t')
603 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000604
Douglas Gregor98258af2010-01-18 22:46:11 +0000605 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000606 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000607
Douglas Gregor1db19de2010-01-19 21:36:55 +0000608 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000609 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000610 CXSourceLocation RefLoc
611 = clang_getLocation(Data->TU, file, curLine, curColumn);
612 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000613 if (Ref.kind == CXCursor_NoDeclFound) {
614 /* Nothing found here; that's fine. */
615 } else if (Ref.kind != CXCursor_FunctionDecl) {
616 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
617 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000618 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000619 printf("\n");
620 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000621 }
Ted Kremenek74844072010-02-17 00:41:20 +0000622 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000623 startBuf++;
624 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000625
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000626 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000627}
628
Ted Kremenek7d405622010-01-12 23:34:26 +0000629/******************************************************************************/
630/* USR testing. */
631/******************************************************************************/
632
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000633enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
634 CXClientData ClientData) {
635 VisitorData *Data = (VisitorData *)ClientData;
636 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000637 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000638 const char *cstr = clang_getCString(USR);
639 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000640 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000641 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000642 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000643 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
644
Douglas Gregora7bde202010-01-19 00:34:46 +0000645 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000646 printf("\n");
647 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000648
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000649 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000650 }
651
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000652 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000653}
654
655/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000656/* Inclusion stack testing. */
657/******************************************************************************/
658
659void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
660 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000661
Ted Kremenek16b55a72010-01-26 19:31:51 +0000662 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000663 CXString fname;
664
665 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000666 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000667 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000668
Ted Kremenek16b55a72010-01-26 19:31:51 +0000669 for (i = 0; i < includeStackLen; ++i) {
670 CXFile includingFile;
671 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000672 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
673 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000674 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000676 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000677 }
678 printf("\n");
679}
680
681void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000682 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000683}
684
685/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000686/* Linkage testing. */
687/******************************************************************************/
688
689static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
690 CXClientData d) {
691 const char *linkage = 0;
692
693 if (clang_isInvalid(clang_getCursorKind(cursor)))
694 return CXChildVisit_Recurse;
695
696 switch (clang_getCursorLinkage(cursor)) {
697 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000698 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
699 case CXLinkage_Internal: linkage = "Internal"; break;
700 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
701 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000702 }
703
704 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000705 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000706 printf("linkage=%s\n", linkage);
707 }
708
709 return CXChildVisit_Recurse;
710}
711
712/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000713/* Typekind testing. */
714/******************************************************************************/
715
716static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
717 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000718 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
719 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000720 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000721 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000722 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000723 if (clang_isConstQualifiedType(T))
724 printf(" const");
725 if (clang_isVolatileQualifiedType(T))
726 printf(" volatile");
727 if (clang_isRestrictQualifiedType(T))
728 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000729 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000730 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000731 {
732 CXType CT = clang_getCanonicalType(T);
733 if (!clang_equalTypes(T, CT)) {
734 CXString CS = clang_getTypeKindSpelling(CT.kind);
735 printf(" [canonical=%s]", clang_getCString(CS));
736 clang_disposeString(CS);
737 }
738 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000739 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000740 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000741 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000742 if (RT.kind != CXType_Invalid) {
743 CXString RS = clang_getTypeKindSpelling(RT.kind);
744 printf(" [result=%s]", clang_getCString(RS));
745 clang_disposeString(RS);
746 }
747 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000748 /* Print the argument types if they exist. */
749 {
750 int numArgs = clang_Cursor_getNumArguments(cursor);
751 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000752 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000753 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000754 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000755 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
756 if (T.kind != CXType_Invalid) {
757 CXString S = clang_getTypeKindSpelling(T.kind);
758 printf(" %s", clang_getCString(S));
759 clang_disposeString(S);
760 }
761 }
762 printf("]");
763 }
764 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000765 /* Print if this is a non-POD type. */
766 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000767
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000768 printf("\n");
769 }
770 return CXChildVisit_Recurse;
771}
772
773
774/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000775/* Loading ASTs/source. */
776/******************************************************************************/
777
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000778static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000779 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000780 CXCursorVisitor Visitor,
781 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000782
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000783 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000784 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000785
786 if (Visitor) {
787 enum CXCursorKind K = CXCursor_NotImplemented;
788 enum CXCursorKind *ck = &K;
789 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000790
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000791 /* Perform some simple filtering. */
792 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000793 else if (!strcmp(filter, "all-display") ||
794 !strcmp(filter, "local-display")) {
795 ck = NULL;
796 want_display_name = 1;
797 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000798 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000799 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
800 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
801 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
802 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
803 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
804 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
805 else {
806 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
807 return 1;
808 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000809
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000810 Data.TU = TU;
811 Data.Filter = ck;
812 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000813 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000814
Ted Kremenekce2ae882010-01-26 17:59:48 +0000815 if (PV)
816 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000817
Douglas Gregora88084b2010-02-18 18:08:43 +0000818 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000819 if (checkForErrors(TU) != 0) {
820 clang_disposeTranslationUnit(TU);
821 return -1;
822 }
823
Ted Kremenek0d435192009-11-17 18:13:31 +0000824 clang_disposeTranslationUnit(TU);
825 return 0;
826}
827
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000828int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000829 const char *prefix, CXCursorVisitor Visitor,
830 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000831 CXIndex Idx;
832 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000833 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000834 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000835 !strcmp(filter, "local") ? 1 : 0,
836 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000837
Ted Kremenek020a0952010-02-11 07:41:25 +0000838 if (!CreateTranslationUnit(Idx, file, &TU)) {
839 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000840 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000841 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000842
Ted Kremenek020a0952010-02-11 07:41:25 +0000843 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
844 clang_disposeIndex(Idx);
845 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000846}
847
Ted Kremenekce2ae882010-01-26 17:59:48 +0000848int perform_test_load_source(int argc, const char **argv,
849 const char *filter, CXCursorVisitor Visitor,
850 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000851 CXIndex Idx;
852 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000853 struct CXUnsavedFile *unsaved_files = 0;
854 int num_unsaved_files = 0;
855 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000856
Daniel Dunbarada487d2009-12-01 02:03:10 +0000857 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000858 (!strcmp(filter, "local") ||
859 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000860 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000861
Ted Kremenek020a0952010-02-11 07:41:25 +0000862 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
863 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000864 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000865 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000866
Douglas Gregordca8ee82011-05-06 16:33:08 +0000867 TU = clang_parseTranslationUnit(Idx, 0,
868 argv + num_unsaved_files,
869 argc - num_unsaved_files,
870 unsaved_files, num_unsaved_files,
871 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000872 if (!TU) {
873 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000874 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000875 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000876 return 1;
877 }
878
Ted Kremenekce2ae882010-01-26 17:59:48 +0000879 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000880 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000881 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000882 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000883}
884
Douglas Gregorabc563f2010-07-19 21:46:24 +0000885int perform_test_reparse_source(int argc, const char **argv, int trials,
886 const char *filter, CXCursorVisitor Visitor,
887 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000888 CXIndex Idx;
889 CXTranslationUnit TU;
890 struct CXUnsavedFile *unsaved_files = 0;
891 int num_unsaved_files = 0;
892 int result;
893 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000894 int remap_after_trial = 0;
895 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000896
897 Idx = clang_createIndex(/* excludeDeclsFromPCH */
898 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000899 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000900
Douglas Gregorabc563f2010-07-19 21:46:24 +0000901 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
902 clang_disposeIndex(Idx);
903 return -1;
904 }
905
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000906 /* Load the initial translation unit -- we do this without honoring remapped
907 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000908 TU = clang_parseTranslationUnit(Idx, 0,
909 argv + num_unsaved_files,
910 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000911 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000912 if (!TU) {
913 fprintf(stderr, "Unable to load translation unit!\n");
914 free_remapped_files(unsaved_files, num_unsaved_files);
915 clang_disposeIndex(Idx);
916 return 1;
917 }
918
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000919 if (checkForErrors(TU) != 0)
920 return -1;
921
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000922 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
923 remap_after_trial =
924 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
925 }
926
Douglas Gregorabc563f2010-07-19 21:46:24 +0000927 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000928 if (clang_reparseTranslationUnit(TU,
929 trial >= remap_after_trial ? num_unsaved_files : 0,
930 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000931 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000932 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000933 clang_disposeTranslationUnit(TU);
934 free_remapped_files(unsaved_files, num_unsaved_files);
935 clang_disposeIndex(Idx);
936 return -1;
937 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000938
939 if (checkForErrors(TU) != 0)
940 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000941 }
942
943 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000944
Douglas Gregorabc563f2010-07-19 21:46:24 +0000945 free_remapped_files(unsaved_files, num_unsaved_files);
946 clang_disposeIndex(Idx);
947 return result;
948}
949
Ted Kremenek0d435192009-11-17 18:13:31 +0000950/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000951/* Logic for testing clang_getCursor(). */
952/******************************************************************************/
953
Douglas Gregordd3e5542011-05-04 00:14:37 +0000954static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000955 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000956 unsigned end_line, unsigned end_col,
957 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000958 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000959 if (prefix)
960 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000961 PrintExtent(stdout, start_line, start_col, end_line, end_col);
962 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000963 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000964 printf("\n");
965}
966
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000967static int perform_file_scan(const char *ast_file, const char *source_file,
968 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000969 CXIndex Idx;
970 CXTranslationUnit TU;
971 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000972 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000973 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000974 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000975 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000976
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000977 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
978 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000979 fprintf(stderr, "Could not create Index\n");
980 return 1;
981 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000982
Ted Kremenek1c6da172009-11-17 19:37:36 +0000983 if (!CreateTranslationUnit(Idx, ast_file, &TU))
984 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000985
Ted Kremenek1c6da172009-11-17 19:37:36 +0000986 if ((fp = fopen(source_file, "r")) == NULL) {
987 fprintf(stderr, "Could not open '%s'\n", source_file);
988 return 1;
989 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000990
Douglas Gregorb9790342010-01-22 21:44:22 +0000991 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000992 for (;;) {
993 CXCursor cursor;
994 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000995
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000996 if (c == '\n') {
997 ++line;
998 col = 1;
999 } else
1000 ++col;
1001
1002 /* Check the cursor at this position, and dump the previous one if we have
1003 * found something new.
1004 */
1005 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1006 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1007 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001008 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001009 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001010 start_line = line;
1011 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001012 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001013 if (c == EOF)
1014 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001015
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001016 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001017 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001018
Ted Kremenek1c6da172009-11-17 19:37:36 +00001019 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001020 clang_disposeTranslationUnit(TU);
1021 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001022 return 0;
1023}
1024
1025/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001026/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001027/******************************************************************************/
1028
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001029/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1030 on failure. If successful, the pointer *filename will contain newly-allocated
1031 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001032int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001033 unsigned *column, unsigned *second_line,
1034 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001035 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001036 const char *last_colon = strrchr(input, ':');
1037 unsigned values[4], i;
1038 unsigned num_values = (second_line && second_column)? 4 : 2;
1039
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001040 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001041 if (!last_colon || last_colon == input) {
1042 if (num_values == 4)
1043 fprintf(stderr, "could not parse filename:line:column:line:column in "
1044 "'%s'\n", input);
1045 else
1046 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001047 return 1;
1048 }
1049
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001050 for (i = 0; i != num_values; ++i) {
1051 const char *prev_colon;
1052
1053 /* Parse the next line or column. */
1054 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1055 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001056 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001057 (i % 2 ? "column" : "line"), input);
1058 return 1;
1059 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001060
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001061 if (i + 1 == num_values)
1062 break;
1063
1064 /* Find the previous colon. */
1065 prev_colon = last_colon - 1;
1066 while (prev_colon != input && *prev_colon != ':')
1067 --prev_colon;
1068 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001069 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001070 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001071 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001072 }
1073
1074 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001075 }
1076
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001077 *line = values[0];
1078 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001079
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001080 if (second_line && second_column) {
1081 *second_line = values[2];
1082 *second_column = values[3];
1083 }
1084
Douglas Gregor88d23952009-11-09 18:19:57 +00001085 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001086 *filename = (char*)malloc(last_colon - input + 1);
1087 memcpy(*filename, input, last_colon - input);
1088 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001089 return 0;
1090}
1091
1092const char *
1093clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1094 switch (Kind) {
1095 case CXCompletionChunk_Optional: return "Optional";
1096 case CXCompletionChunk_TypedText: return "TypedText";
1097 case CXCompletionChunk_Text: return "Text";
1098 case CXCompletionChunk_Placeholder: return "Placeholder";
1099 case CXCompletionChunk_Informative: return "Informative";
1100 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1101 case CXCompletionChunk_LeftParen: return "LeftParen";
1102 case CXCompletionChunk_RightParen: return "RightParen";
1103 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1104 case CXCompletionChunk_RightBracket: return "RightBracket";
1105 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1106 case CXCompletionChunk_RightBrace: return "RightBrace";
1107 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1108 case CXCompletionChunk_RightAngle: return "RightAngle";
1109 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001110 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001111 case CXCompletionChunk_Colon: return "Colon";
1112 case CXCompletionChunk_SemiColon: return "SemiColon";
1113 case CXCompletionChunk_Equal: return "Equal";
1114 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1115 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001116 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001117
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001118 return "Unknown";
1119}
1120
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001121static int checkForErrors(CXTranslationUnit TU) {
1122 unsigned Num, i;
1123 CXDiagnostic Diag;
1124 CXString DiagStr;
1125
1126 if (!getenv("CINDEXTEST_FAILONERROR"))
1127 return 0;
1128
1129 Num = clang_getNumDiagnostics(TU);
1130 for (i = 0; i != Num; ++i) {
1131 Diag = clang_getDiagnostic(TU, i);
1132 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1133 DiagStr = clang_formatDiagnostic(Diag,
1134 clang_defaultDiagnosticDisplayOptions());
1135 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1136 clang_disposeString(DiagStr);
1137 clang_disposeDiagnostic(Diag);
1138 return -1;
1139 }
1140 clang_disposeDiagnostic(Diag);
1141 }
1142
1143 return 0;
1144}
1145
Douglas Gregor3ac73852009-11-09 16:04:45 +00001146void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001147 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001148
Douglas Gregor3ac73852009-11-09 16:04:45 +00001149 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001150 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001151 CXString text;
1152 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001153 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001154 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001155
Douglas Gregor3ac73852009-11-09 16:04:45 +00001156 if (Kind == CXCompletionChunk_Optional) {
1157 fprintf(file, "{Optional ");
1158 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001159 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001160 file);
1161 fprintf(file, "}");
1162 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001163 }
1164
1165 if (Kind == CXCompletionChunk_VerticalSpace) {
1166 fprintf(file, "{VerticalSpace }");
1167 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001168 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001169
Douglas Gregord5a20892009-11-09 17:05:28 +00001170 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001171 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001172 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001173 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001174 cstr ? cstr : "");
1175 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001176 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001177
Douglas Gregor3ac73852009-11-09 16:04:45 +00001178}
1179
1180void print_completion_result(CXCompletionResult *completion_result,
1181 CXClientData client_data) {
1182 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001183 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001184 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001185 enum CXCursorKind ParentKind;
1186 CXString ParentName;
1187
Ted Kremeneke68fff62010-02-17 00:41:32 +00001188 fprintf(file, "%s:", clang_getCString(ks));
1189 clang_disposeString(ks);
1190
Douglas Gregor3ac73852009-11-09 16:04:45 +00001191 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001192 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001193 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001194 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1195 case CXAvailability_Available:
1196 break;
1197
1198 case CXAvailability_Deprecated:
1199 fprintf(file, " (deprecated)");
1200 break;
1201
1202 case CXAvailability_NotAvailable:
1203 fprintf(file, " (unavailable)");
1204 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001205
1206 case CXAvailability_NotAccessible:
1207 fprintf(file, " (inaccessible)");
1208 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001209 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001210
1211 annotationCount = clang_getCompletionNumAnnotations(
1212 completion_result->CompletionString);
1213 if (annotationCount) {
1214 unsigned i;
1215 fprintf(file, " (");
1216 for (i = 0; i < annotationCount; ++i) {
1217 if (i != 0)
1218 fprintf(file, ", ");
1219 fprintf(file, "\"%s\"",
1220 clang_getCString(clang_getCompletionAnnotation(
1221 completion_result->CompletionString, i)));
1222 }
1223 fprintf(file, ")");
1224 }
1225
Douglas Gregorba103062012-03-27 23:34:16 +00001226 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1227 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1228 &ParentKind);
1229 if (ParentKind != CXCursor_NotImplemented) {
1230 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1231 fprintf(file, " (parent: %s '%s')",
1232 clang_getCString(KindSpelling),
1233 clang_getCString(ParentName));
1234 clang_disposeString(KindSpelling);
1235 }
1236 clang_disposeString(ParentName);
1237 }
1238
Douglas Gregor58ddb602010-08-23 23:00:57 +00001239 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001240}
1241
Douglas Gregor3da626b2011-07-07 16:03:39 +00001242void print_completion_contexts(unsigned long long contexts, FILE *file) {
1243 fprintf(file, "Completion contexts:\n");
1244 if (contexts == CXCompletionContext_Unknown) {
1245 fprintf(file, "Unknown\n");
1246 }
1247 if (contexts & CXCompletionContext_AnyType) {
1248 fprintf(file, "Any type\n");
1249 }
1250 if (contexts & CXCompletionContext_AnyValue) {
1251 fprintf(file, "Any value\n");
1252 }
1253 if (contexts & CXCompletionContext_ObjCObjectValue) {
1254 fprintf(file, "Objective-C object value\n");
1255 }
1256 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1257 fprintf(file, "Objective-C selector value\n");
1258 }
1259 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1260 fprintf(file, "C++ class type value\n");
1261 }
1262 if (contexts & CXCompletionContext_DotMemberAccess) {
1263 fprintf(file, "Dot member access\n");
1264 }
1265 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1266 fprintf(file, "Arrow member access\n");
1267 }
1268 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1269 fprintf(file, "Objective-C property access\n");
1270 }
1271 if (contexts & CXCompletionContext_EnumTag) {
1272 fprintf(file, "Enum tag\n");
1273 }
1274 if (contexts & CXCompletionContext_UnionTag) {
1275 fprintf(file, "Union tag\n");
1276 }
1277 if (contexts & CXCompletionContext_StructTag) {
1278 fprintf(file, "Struct tag\n");
1279 }
1280 if (contexts & CXCompletionContext_ClassTag) {
1281 fprintf(file, "Class name\n");
1282 }
1283 if (contexts & CXCompletionContext_Namespace) {
1284 fprintf(file, "Namespace or namespace alias\n");
1285 }
1286 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1287 fprintf(file, "Nested name specifier\n");
1288 }
1289 if (contexts & CXCompletionContext_ObjCInterface) {
1290 fprintf(file, "Objective-C interface\n");
1291 }
1292 if (contexts & CXCompletionContext_ObjCProtocol) {
1293 fprintf(file, "Objective-C protocol\n");
1294 }
1295 if (contexts & CXCompletionContext_ObjCCategory) {
1296 fprintf(file, "Objective-C category\n");
1297 }
1298 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1299 fprintf(file, "Objective-C instance method\n");
1300 }
1301 if (contexts & CXCompletionContext_ObjCClassMessage) {
1302 fprintf(file, "Objective-C class method\n");
1303 }
1304 if (contexts & CXCompletionContext_ObjCSelectorName) {
1305 fprintf(file, "Objective-C selector name\n");
1306 }
1307 if (contexts & CXCompletionContext_MacroName) {
1308 fprintf(file, "Macro name\n");
1309 }
1310 if (contexts & CXCompletionContext_NaturalLanguage) {
1311 fprintf(file, "Natural language\n");
1312 }
1313}
1314
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001315int my_stricmp(const char *s1, const char *s2) {
1316 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001317 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001318 if (c1 < c2)
1319 return -1;
1320 else if (c1 > c2)
1321 return 1;
1322
1323 ++s1;
1324 ++s2;
1325 }
1326
1327 if (*s1)
1328 return 1;
1329 else if (*s2)
1330 return -1;
1331 return 0;
1332}
1333
Douglas Gregor1982c182010-07-12 18:38:41 +00001334int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001335 const char *input = argv[1];
1336 char *filename = 0;
1337 unsigned line;
1338 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001339 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001340 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001341 struct CXUnsavedFile *unsaved_files = 0;
1342 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001343 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001344 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001345 unsigned I, Repeats = 1;
1346 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1347
1348 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1349 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001350
Douglas Gregor1982c182010-07-12 18:38:41 +00001351 if (timing_only)
1352 input += strlen("-code-completion-timing=");
1353 else
1354 input += strlen("-code-completion-at=");
1355
Ted Kremeneke68fff62010-02-17 00:41:32 +00001356 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001357 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001358 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001359
Douglas Gregor735df882009-12-02 09:21:34 +00001360 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1361 return -1;
1362
Douglas Gregor32be4a52010-10-11 21:37:58 +00001363 CIdx = clang_createIndex(0, 0);
1364
1365 if (getenv("CINDEXTEST_EDITING"))
1366 Repeats = 5;
1367
1368 TU = clang_parseTranslationUnit(CIdx, 0,
1369 argv + num_unsaved_files + 2,
1370 argc - num_unsaved_files - 2,
1371 0, 0, getDefaultParsingOptions());
1372 if (!TU) {
1373 fprintf(stderr, "Unable to load translation unit!\n");
1374 return 1;
1375 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001376
1377 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1378 fprintf(stderr, "Unable to reparse translation init!\n");
1379 return 1;
1380 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001381
1382 for (I = 0; I != Repeats; ++I) {
1383 results = clang_codeCompleteAt(TU, filename, line, column,
1384 unsaved_files, num_unsaved_files,
1385 completionOptions);
1386 if (!results) {
1387 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001388 return 1;
1389 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001390 if (I != Repeats-1)
1391 clang_disposeCodeCompleteResults(results);
1392 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001393
Douglas Gregorec6762c2009-12-18 16:20:58 +00001394 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001395 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001396 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001397 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001398 CXString objCSelector;
1399 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001400 if (!timing_only) {
1401 /* Sort the code-completion results based on the typed text. */
1402 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1403
Douglas Gregor1982c182010-07-12 18:38:41 +00001404 for (i = 0; i != n; ++i)
1405 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001406 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001407 n = clang_codeCompleteGetNumDiagnostics(results);
1408 for (i = 0; i != n; ++i) {
1409 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1410 PrintDiagnostic(diag);
1411 clang_disposeDiagnostic(diag);
1412 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001413
1414 contexts = clang_codeCompleteGetContexts(results);
1415 print_completion_contexts(contexts, stdout);
1416
Douglas Gregor0a47d692011-07-26 15:24:30 +00001417 containerKind = clang_codeCompleteGetContainerKind(results,
1418 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001419
1420 if (containerKind != CXCursor_InvalidCode) {
1421 /* We have found a container */
1422 CXString containerUSR, containerKindSpelling;
1423 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1424 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1425 clang_disposeString(containerKindSpelling);
1426
1427 if (containerIsIncomplete) {
1428 printf("Container is incomplete\n");
1429 }
1430 else {
1431 printf("Container is complete\n");
1432 }
1433
1434 containerUSR = clang_codeCompleteGetContainerUSR(results);
1435 printf("Container USR: %s\n", clang_getCString(containerUSR));
1436 clang_disposeString(containerUSR);
1437 }
1438
Douglas Gregor0a47d692011-07-26 15:24:30 +00001439 objCSelector = clang_codeCompleteGetObjCSelector(results);
1440 selectorString = clang_getCString(objCSelector);
1441 if (selectorString && strlen(selectorString) > 0) {
1442 printf("Objective-C selector: %s\n", selectorString);
1443 }
1444 clang_disposeString(objCSelector);
1445
Douglas Gregorec6762c2009-12-18 16:20:58 +00001446 clang_disposeCodeCompleteResults(results);
1447 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001448 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001449 clang_disposeIndex(CIdx);
1450 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001451
Douglas Gregor735df882009-12-02 09:21:34 +00001452 free_remapped_files(unsaved_files, num_unsaved_files);
1453
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001454 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001455}
1456
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001457typedef struct {
1458 char *filename;
1459 unsigned line;
1460 unsigned column;
1461} CursorSourceLocation;
1462
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001463static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001464 CXIndex CIdx;
1465 int errorCode;
1466 struct CXUnsavedFile *unsaved_files = 0;
1467 int num_unsaved_files = 0;
1468 CXTranslationUnit TU;
1469 CXCursor Cursor;
1470 CursorSourceLocation *Locations = 0;
1471 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001472 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001473 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001474
Ted Kremeneke68fff62010-02-17 00:41:32 +00001475 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001476 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1477 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001478
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001479 /* Parse the locations. */
1480 assert(NumLocations > 0 && "Unable to count locations?");
1481 Locations = (CursorSourceLocation *)malloc(
1482 NumLocations * sizeof(CursorSourceLocation));
1483 for (Loc = 0; Loc < NumLocations; ++Loc) {
1484 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001485 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1486 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001487 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001488 return errorCode;
1489 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001490
1491 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001492 &num_unsaved_files))
1493 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001494
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001495 if (getenv("CINDEXTEST_EDITING"))
1496 Repeats = 5;
1497
1498 /* Parse the translation unit. When we're testing clang_getCursor() after
1499 reparsing, don't remap unsaved files until the second parse. */
1500 CIdx = clang_createIndex(1, 1);
1501 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1502 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001503 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001504 unsaved_files,
1505 Repeats > 1? 0 : num_unsaved_files,
1506 getDefaultParsingOptions());
1507
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001508 if (!TU) {
1509 fprintf(stderr, "unable to parse input\n");
1510 return -1;
1511 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001512
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001513 if (checkForErrors(TU) != 0)
1514 return -1;
1515
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001516 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001517 if (Repeats > 1 &&
1518 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1519 clang_defaultReparseOptions(TU))) {
1520 clang_disposeTranslationUnit(TU);
1521 return 1;
1522 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001523
1524 if (checkForErrors(TU) != 0)
1525 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001526
1527 for (Loc = 0; Loc < NumLocations; ++Loc) {
1528 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1529 if (!file)
1530 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001531
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001532 Cursor = clang_getCursor(TU,
1533 clang_getLocation(TU, file, Locations[Loc].line,
1534 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001535
1536 if (checkForErrors(TU) != 0)
1537 return -1;
1538
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001539 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001540 CXCompletionString completionString = clang_getCursorCompletionString(
1541 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001542 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1543 CXString Spelling;
1544 const char *cspell;
1545 unsigned line, column;
1546 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1547 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001548 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001549 PrintCursorExtent(Cursor);
1550 Spelling = clang_getCursorSpelling(Cursor);
1551 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001552 if (cspell && strlen(cspell) != 0) {
1553 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001554 printf(" Spelling=%s (", cspell);
1555 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001556 CXSourceRange range =
1557 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001558 if (clang_Range_isNull(range))
1559 break;
1560 PrintRange(range, 0);
1561 }
1562 printf(")");
1563 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001564 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001565 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1566 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001567 if (completionString != NULL) {
1568 printf("\nCompletion string: ");
1569 print_completion_string(completionString, stdout);
1570 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001571 printf("\n");
1572 free(Locations[Loc].filename);
1573 }
1574 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001575 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001576
Douglas Gregora88084b2010-02-18 18:08:43 +00001577 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001578 clang_disposeTranslationUnit(TU);
1579 clang_disposeIndex(CIdx);
1580 free(Locations);
1581 free_remapped_files(unsaved_files, num_unsaved_files);
1582 return 0;
1583}
1584
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001585static enum CXVisitorResult findFileRefsVisit(void *context,
1586 CXCursor cursor, CXSourceRange range) {
1587 if (clang_Range_isNull(range))
1588 return CXVisit_Continue;
1589
1590 PrintCursor(cursor);
1591 PrintRange(range, "");
1592 printf("\n");
1593 return CXVisit_Continue;
1594}
1595
1596static int find_file_refs_at(int argc, const char **argv) {
1597 CXIndex CIdx;
1598 int errorCode;
1599 struct CXUnsavedFile *unsaved_files = 0;
1600 int num_unsaved_files = 0;
1601 CXTranslationUnit TU;
1602 CXCursor Cursor;
1603 CursorSourceLocation *Locations = 0;
1604 unsigned NumLocations = 0, Loc;
1605 unsigned Repeats = 1;
1606 unsigned I;
1607
1608 /* Count the number of locations. */
1609 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1610 ++NumLocations;
1611
1612 /* Parse the locations. */
1613 assert(NumLocations > 0 && "Unable to count locations?");
1614 Locations = (CursorSourceLocation *)malloc(
1615 NumLocations * sizeof(CursorSourceLocation));
1616 for (Loc = 0; Loc < NumLocations; ++Loc) {
1617 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1618 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1619 &Locations[Loc].line,
1620 &Locations[Loc].column, 0, 0)))
1621 return errorCode;
1622 }
1623
1624 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1625 &num_unsaved_files))
1626 return -1;
1627
1628 if (getenv("CINDEXTEST_EDITING"))
1629 Repeats = 5;
1630
1631 /* Parse the translation unit. When we're testing clang_getCursor() after
1632 reparsing, don't remap unsaved files until the second parse. */
1633 CIdx = clang_createIndex(1, 1);
1634 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1635 argv + num_unsaved_files + 1 + NumLocations,
1636 argc - num_unsaved_files - 2 - NumLocations,
1637 unsaved_files,
1638 Repeats > 1? 0 : num_unsaved_files,
1639 getDefaultParsingOptions());
1640
1641 if (!TU) {
1642 fprintf(stderr, "unable to parse input\n");
1643 return -1;
1644 }
1645
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001646 if (checkForErrors(TU) != 0)
1647 return -1;
1648
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001649 for (I = 0; I != Repeats; ++I) {
1650 if (Repeats > 1 &&
1651 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1652 clang_defaultReparseOptions(TU))) {
1653 clang_disposeTranslationUnit(TU);
1654 return 1;
1655 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001656
1657 if (checkForErrors(TU) != 0)
1658 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001659
1660 for (Loc = 0; Loc < NumLocations; ++Loc) {
1661 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1662 if (!file)
1663 continue;
1664
1665 Cursor = clang_getCursor(TU,
1666 clang_getLocation(TU, file, Locations[Loc].line,
1667 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001668
1669 if (checkForErrors(TU) != 0)
1670 return -1;
1671
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001672 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001673 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001674 PrintCursor(Cursor);
1675 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001676 clang_findReferencesInFile(Cursor, file, visitor);
1677 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001678
1679 if (checkForErrors(TU) != 0)
1680 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001681 }
1682 }
1683 }
1684
1685 PrintDiagnostics(TU);
1686 clang_disposeTranslationUnit(TU);
1687 clang_disposeIndex(CIdx);
1688 free(Locations);
1689 free_remapped_files(unsaved_files, num_unsaved_files);
1690 return 0;
1691}
1692
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001693typedef struct {
1694 const char *check_prefix;
1695 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001696 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001697 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001698 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001699} IndexData;
1700
1701static void printCheck(IndexData *data) {
1702 if (data->check_prefix) {
1703 if (data->first_check_printed) {
1704 printf("// %s-NEXT: ", data->check_prefix);
1705 } else {
1706 printf("// %s : ", data->check_prefix);
1707 data->first_check_printed = 1;
1708 }
1709 }
1710}
1711
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001712static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001713 CXString filename = clang_getFileName((CXFile)file);
1714 printf("%s", clang_getCString(filename));
1715 clang_disposeString(filename);
1716}
1717
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001718static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1719 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001720 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001721 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001722 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001723 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001724 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001725
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001726 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001727 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1728 if (line == 0) {
1729 printf("<null loc>");
1730 return;
1731 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001732 if (!file) {
1733 printf("<no idxfile>");
1734 return;
1735 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001736 filename = clang_getFileName((CXFile)file);
1737 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001738 if (strcmp(cname, index_data->main_filename) == 0)
1739 isMainFile = 1;
1740 else
1741 isMainFile = 0;
1742 clang_disposeString(filename);
1743
1744 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001745 printCXIndexFile(file);
1746 printf(":");
1747 }
1748 printf("%d:%d", line, column);
1749}
1750
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001751static unsigned digitCount(unsigned val) {
1752 unsigned c = 1;
1753 while (1) {
1754 if (val < 10)
1755 return c;
1756 ++c;
1757 val /= 10;
1758 }
1759}
1760
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001761static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1762 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001763 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001764 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001765 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001766 unsigned line, column;
1767
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001768 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001769 if (!name)
1770 name = "<anon-tag>";
1771
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001772 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001773 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001774 newStr = (char *)malloc(strlen(name) +
1775 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001776 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001777 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001778}
1779
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001780static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1781 CXIdxClientContainer container;
1782 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001783 if (!container)
1784 printf("[<<NULL>>]");
1785 else
1786 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001787}
1788
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001789static const char *getEntityKindString(CXIdxEntityKind kind) {
1790 switch (kind) {
1791 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1792 case CXIdxEntity_Typedef: return "typedef";
1793 case CXIdxEntity_Function: return "function";
1794 case CXIdxEntity_Variable: return "variable";
1795 case CXIdxEntity_Field: return "field";
1796 case CXIdxEntity_EnumConstant: return "enumerator";
1797 case CXIdxEntity_ObjCClass: return "objc-class";
1798 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1799 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001800 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1801 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001802 case CXIdxEntity_ObjCProperty: return "objc-property";
1803 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1804 case CXIdxEntity_Enum: return "enum";
1805 case CXIdxEntity_Struct: return "struct";
1806 case CXIdxEntity_Union: return "union";
1807 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001808 case CXIdxEntity_CXXNamespace: return "namespace";
1809 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1810 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1811 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1812 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1813 case CXIdxEntity_CXXConstructor: return "constructor";
1814 case CXIdxEntity_CXXDestructor: return "destructor";
1815 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1816 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1817 }
1818 assert(0 && "Garbage entity kind");
1819 return 0;
1820}
1821
1822static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1823 switch (kind) {
1824 case CXIdxEntity_NonTemplate: return "";
1825 case CXIdxEntity_Template: return "-template";
1826 case CXIdxEntity_TemplatePartialSpecialization:
1827 return "-template-partial-spec";
1828 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001829 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001830 assert(0 && "Garbage entity kind");
1831 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001832}
1833
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001834static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1835 switch (kind) {
1836 case CXIdxEntityLang_None: return "<none>";
1837 case CXIdxEntityLang_C: return "C";
1838 case CXIdxEntityLang_ObjC: return "ObjC";
1839 case CXIdxEntityLang_CXX: return "C++";
1840 }
1841 assert(0 && "Garbage language kind");
1842 return 0;
1843}
1844
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001845static void printEntityInfo(const char *cb,
1846 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001847 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001848 const char *name;
1849 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001850 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001851 index_data = (IndexData *)client_data;
1852 printCheck(index_data);
1853
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001854 if (!info) {
1855 printf("%s: <<NULL>>", cb);
1856 return;
1857 }
1858
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001859 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001860 if (!name)
1861 name = "<anon-tag>";
1862
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001863 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1864 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001865 printf(" | name: %s", name);
1866 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001867 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001868
1869 for (i = 0; i != info->numAttributes; ++i) {
1870 const CXIdxAttrInfo *Attr = info->attributes[i];
1871 printf(" <attribute>: ");
1872 PrintCursor(Attr->cursor);
1873 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001874}
1875
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001876static void printBaseClassInfo(CXClientData client_data,
1877 const CXIdxBaseClassInfo *info) {
1878 printEntityInfo(" <base>", client_data, info->base);
1879 printf(" | cursor: ");
1880 PrintCursor(info->cursor);
1881 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001882 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001883}
1884
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001885static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1886 CXClientData client_data) {
1887 unsigned i;
1888 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1889 printEntityInfo(" <protocol>", client_data,
1890 ProtoInfo->protocols[i]->protocol);
1891 printf(" | cursor: ");
1892 PrintCursor(ProtoInfo->protocols[i]->cursor);
1893 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001894 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001895 printf("\n");
1896 }
1897}
1898
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001899static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001900 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001901 CXString str;
1902 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001903 unsigned numDiags, i;
1904 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001905 IndexData *index_data;
1906 index_data = (IndexData *)client_data;
1907 printCheck(index_data);
1908
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001909 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1910 for (i = 0; i != numDiags; ++i) {
1911 diag = clang_getDiagnosticInSet(diagSet, i);
1912 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1913 cstr = clang_getCString(str);
1914 printf("[diagnostic]: %s\n", cstr);
1915 clang_disposeString(str);
1916
1917 if (getenv("CINDEXTEST_FAILONERROR") &&
1918 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1919 index_data->fail_for_error = 1;
1920 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001921 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001922}
1923
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001924static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1925 CXFile file, void *reserved) {
1926 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001927 CXString filename;
1928
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001929 index_data = (IndexData *)client_data;
1930 printCheck(index_data);
1931
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001932 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001933 index_data->main_filename = clang_getCString(filename);
1934 clang_disposeString(filename);
1935
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001936 printf("[enteredMainFile]: ");
1937 printCXIndexFile((CXIdxClientFile)file);
1938 printf("\n");
1939
1940 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001941}
1942
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001943static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001944 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001945 IndexData *index_data;
1946 index_data = (IndexData *)client_data;
1947 printCheck(index_data);
1948
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001949 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001950 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001951 printf(" | name: \"%s\"", info->filename);
1952 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001953 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001954 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001955
1956 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001957}
1958
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001959static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001960 void *reserved) {
1961 IndexData *index_data;
1962 index_data = (IndexData *)client_data;
1963 printCheck(index_data);
1964
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001965 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001966 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001967}
1968
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001969static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001970 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001971 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001972 const CXIdxObjCCategoryDeclInfo *CatInfo;
1973 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001974 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001975 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001976 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001977 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001978 index_data = (IndexData *)client_data;
1979
1980 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1981 printf(" | cursor: ");
1982 PrintCursor(info->cursor);
1983 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001984 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001985 printf(" | semantic-container: ");
1986 printCXIndexContainer(info->semanticContainer);
1987 printf(" | lexical-container: ");
1988 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001989 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001990 printf(" | isDef: %d", info->isDefinition);
1991 printf(" | isContainer: %d", info->isContainer);
1992 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001993
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001994 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001995 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001996 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001997 PrintCursor(Attr->cursor);
1998 printf("\n");
1999 }
2000
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002001 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2002 const char *kindName = 0;
2003 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2004 switch (K) {
2005 case CXIdxObjCContainer_ForwardRef:
2006 kindName = "forward-ref"; break;
2007 case CXIdxObjCContainer_Interface:
2008 kindName = "interface"; break;
2009 case CXIdxObjCContainer_Implementation:
2010 kindName = "implementation"; break;
2011 }
2012 printCheck(index_data);
2013 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2014 }
2015
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002016 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002017 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2018 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002019 printf(" | cursor: ");
2020 PrintCursor(CatInfo->classCursor);
2021 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002022 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002023 printf("\n");
2024 }
2025
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002026 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2027 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002028 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002029 printf("\n");
2030 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002031 }
2032
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002033 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2034 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002035 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002036
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002037 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2038 if (PropInfo->getter) {
2039 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2040 printf("\n");
2041 }
2042 if (PropInfo->setter) {
2043 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2044 printf("\n");
2045 }
2046 }
2047
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002048 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2049 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2050 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2051 printf("\n");
2052 }
2053 }
2054
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002055 if (info->declAsContainer)
2056 clang_index_setClientContainer(info->declAsContainer,
2057 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002058}
2059
2060static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002061 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002062 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002063 printf(" | cursor: ");
2064 PrintCursor(info->cursor);
2065 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002066 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002067 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002068 printf(" | container: ");
2069 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002070 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002071 switch (info->kind) {
2072 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002073 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002074 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002075 printf("\n");
2076}
2077
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002078static int index_abortQuery(CXClientData client_data, void *reserved) {
2079 IndexData *index_data;
2080 index_data = (IndexData *)client_data;
2081 return index_data->abort;
2082}
2083
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002084static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002085 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002086 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002087 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002088 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002089 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002090 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002091 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002092 index_indexEntityReference
2093};
2094
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002095static unsigned getIndexOptions(void) {
2096 unsigned index_opts;
2097 index_opts = 0;
2098 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2099 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2100 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2101 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2102
2103 return index_opts;
2104}
2105
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002106static int index_file(int argc, const char **argv) {
2107 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002108 CXIndex Idx;
2109 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002110 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002111 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002112 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002113
2114 check_prefix = 0;
2115 if (argc > 0) {
2116 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2117 check_prefix = argv[0] + strlen("-check-prefix=");
2118 ++argv;
2119 --argc;
2120 }
2121 }
2122
2123 if (argc == 0) {
2124 fprintf(stderr, "no compiler arguments\n");
2125 return -1;
2126 }
2127
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002128 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2129 /* displayDiagnosics=*/1))) {
2130 fprintf(stderr, "Could not create Index\n");
2131 return 1;
2132 }
2133 idxAction = 0;
2134 result = 1;
2135
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002136 index_data.check_prefix = check_prefix;
2137 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002138 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002139 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002140
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002141 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002142 idxAction = clang_IndexAction_create(Idx);
2143 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002144 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002145 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002146 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002147 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002148
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002149 clang_IndexAction_dispose(idxAction);
2150 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002151 return result;
2152}
2153
2154static int index_tu(int argc, const char **argv) {
2155 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002156 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002157 CXTranslationUnit TU;
2158 const char *check_prefix;
2159 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002160 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002161 int result;
2162
2163 check_prefix = 0;
2164 if (argc > 0) {
2165 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2166 check_prefix = argv[0] + strlen("-check-prefix=");
2167 ++argv;
2168 --argc;
2169 }
2170 }
2171
2172 if (argc == 0) {
2173 fprintf(stderr, "no ast file\n");
2174 return -1;
2175 }
2176
2177 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2178 /* displayDiagnosics=*/1))) {
2179 fprintf(stderr, "Could not create Index\n");
2180 return 1;
2181 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002182 idxAction = 0;
2183 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002184
2185 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002186 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002187
2188 index_data.check_prefix = check_prefix;
2189 index_data.first_check_printed = 0;
2190 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002191 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002192
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002193 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002194 idxAction = clang_IndexAction_create(Idx);
2195 result = clang_indexTranslationUnit(idxAction, &index_data,
2196 &IndexCB,sizeof(IndexCB),
2197 index_opts, TU);
2198 if (index_data.fail_for_error)
2199 goto finished;
2200
2201 finished:
2202 clang_IndexAction_dispose(idxAction);
2203 clang_disposeIndex(Idx);
2204
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002205 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002206}
2207
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002208int perform_token_annotation(int argc, const char **argv) {
2209 const char *input = argv[1];
2210 char *filename = 0;
2211 unsigned line, second_line;
2212 unsigned column, second_column;
2213 CXIndex CIdx;
2214 CXTranslationUnit TU = 0;
2215 int errorCode;
2216 struct CXUnsavedFile *unsaved_files = 0;
2217 int num_unsaved_files = 0;
2218 CXToken *tokens;
2219 unsigned num_tokens;
2220 CXSourceRange range;
2221 CXSourceLocation startLoc, endLoc;
2222 CXFile file = 0;
2223 CXCursor *cursors = 0;
2224 unsigned i;
2225
2226 input += strlen("-test-annotate-tokens=");
2227 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2228 &second_line, &second_column)))
2229 return errorCode;
2230
2231 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2232 return -1;
2233
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002234 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002235 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2236 argv + num_unsaved_files + 2,
2237 argc - num_unsaved_files - 3,
2238 unsaved_files,
2239 num_unsaved_files,
2240 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002241 if (!TU) {
2242 fprintf(stderr, "unable to parse input\n");
2243 clang_disposeIndex(CIdx);
2244 free(filename);
2245 free_remapped_files(unsaved_files, num_unsaved_files);
2246 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002247 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002248 errorCode = 0;
2249
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002250 if (checkForErrors(TU) != 0)
2251 return -1;
2252
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002253 if (getenv("CINDEXTEST_EDITING")) {
2254 for (i = 0; i < 5; ++i) {
2255 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2256 clang_defaultReparseOptions(TU))) {
2257 fprintf(stderr, "Unable to reparse translation unit!\n");
2258 errorCode = -1;
2259 goto teardown;
2260 }
2261 }
2262 }
2263
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002264 if (checkForErrors(TU) != 0) {
2265 errorCode = -1;
2266 goto teardown;
2267 }
2268
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002269 file = clang_getFile(TU, filename);
2270 if (!file) {
2271 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2272 errorCode = -1;
2273 goto teardown;
2274 }
2275
2276 startLoc = clang_getLocation(TU, file, line, column);
2277 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002278 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002279 column);
2280 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002281 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002282 }
2283
2284 endLoc = clang_getLocation(TU, file, second_line, second_column);
2285 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002286 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002287 second_line, second_column);
2288 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002289 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002290 }
2291
2292 range = clang_getRange(startLoc, endLoc);
2293 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002294
2295 if (checkForErrors(TU) != 0) {
2296 errorCode = -1;
2297 goto teardown;
2298 }
2299
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002300 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2301 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002302
2303 if (checkForErrors(TU) != 0) {
2304 errorCode = -1;
2305 goto teardown;
2306 }
2307
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002308 for (i = 0; i != num_tokens; ++i) {
2309 const char *kind = "<unknown>";
2310 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2311 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2312 unsigned start_line, start_column, end_line, end_column;
2313
2314 switch (clang_getTokenKind(tokens[i])) {
2315 case CXToken_Punctuation: kind = "Punctuation"; break;
2316 case CXToken_Keyword: kind = "Keyword"; break;
2317 case CXToken_Identifier: kind = "Identifier"; break;
2318 case CXToken_Literal: kind = "Literal"; break;
2319 case CXToken_Comment: kind = "Comment"; break;
2320 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002321 clang_getSpellingLocation(clang_getRangeStart(extent),
2322 0, &start_line, &start_column, 0);
2323 clang_getSpellingLocation(clang_getRangeEnd(extent),
2324 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002325 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002326 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002327 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002328 if (!clang_isInvalid(cursors[i].kind)) {
2329 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002330 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002331 }
2332 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002333 }
2334 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002335 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002336
2337 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002338 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002339 clang_disposeTranslationUnit(TU);
2340 clang_disposeIndex(CIdx);
2341 free(filename);
2342 free_remapped_files(unsaved_files, num_unsaved_files);
2343 return errorCode;
2344}
2345
Ted Kremenek0d435192009-11-17 18:13:31 +00002346/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002347/* USR printing. */
2348/******************************************************************************/
2349
2350static int insufficient_usr(const char *kind, const char *usage) {
2351 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2352 return 1;
2353}
2354
2355static unsigned isUSR(const char *s) {
2356 return s[0] == 'c' && s[1] == ':';
2357}
2358
2359static int not_usr(const char *s, const char *arg) {
2360 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2361 return 1;
2362}
2363
2364static void print_usr(CXString usr) {
2365 const char *s = clang_getCString(usr);
2366 printf("%s\n", s);
2367 clang_disposeString(usr);
2368}
2369
2370static void display_usrs() {
2371 fprintf(stderr, "-print-usrs options:\n"
2372 " ObjCCategory <class name> <category name>\n"
2373 " ObjCClass <class name>\n"
2374 " ObjCIvar <ivar name> <class USR>\n"
2375 " ObjCMethod <selector> [0=class method|1=instance method] "
2376 "<class USR>\n"
2377 " ObjCProperty <property name> <class USR>\n"
2378 " ObjCProtocol <protocol name>\n");
2379}
2380
2381int print_usrs(const char **I, const char **E) {
2382 while (I != E) {
2383 const char *kind = *I;
2384 unsigned len = strlen(kind);
2385 switch (len) {
2386 case 8:
2387 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2388 if (I + 2 >= E)
2389 return insufficient_usr(kind, "<ivar name> <class USR>");
2390 if (!isUSR(I[2]))
2391 return not_usr("<class USR>", I[2]);
2392 else {
2393 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002394 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002395 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002396 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2397 }
2398
2399 I += 3;
2400 continue;
2401 }
2402 break;
2403 case 9:
2404 if (memcmp(kind, "ObjCClass", 9) == 0) {
2405 if (I + 1 >= E)
2406 return insufficient_usr(kind, "<class name>");
2407 print_usr(clang_constructUSR_ObjCClass(I[1]));
2408 I += 2;
2409 continue;
2410 }
2411 break;
2412 case 10:
2413 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2414 if (I + 3 >= E)
2415 return insufficient_usr(kind, "<method selector> "
2416 "[0=class method|1=instance method] <class USR>");
2417 if (!isUSR(I[3]))
2418 return not_usr("<class USR>", I[3]);
2419 else {
2420 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002421 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002422 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002423 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2424 }
2425 I += 4;
2426 continue;
2427 }
2428 break;
2429 case 12:
2430 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2431 if (I + 2 >= E)
2432 return insufficient_usr(kind, "<class name> <category name>");
2433 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2434 I += 3;
2435 continue;
2436 }
2437 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2438 if (I + 1 >= E)
2439 return insufficient_usr(kind, "<protocol name>");
2440 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2441 I += 2;
2442 continue;
2443 }
2444 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2445 if (I + 2 >= E)
2446 return insufficient_usr(kind, "<property name> <class USR>");
2447 if (!isUSR(I[2]))
2448 return not_usr("<class USR>", I[2]);
2449 else {
2450 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002451 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002452 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002453 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2454 }
2455 I += 3;
2456 continue;
2457 }
2458 break;
2459 default:
2460 break;
2461 }
2462 break;
2463 }
2464
2465 if (I != E) {
2466 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2467 display_usrs();
2468 return 1;
2469 }
2470 return 0;
2471}
2472
2473int print_usrs_file(const char *file_name) {
2474 char line[2048];
2475 const char *args[128];
2476 unsigned numChars = 0;
2477
2478 FILE *fp = fopen(file_name, "r");
2479 if (!fp) {
2480 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2481 return 1;
2482 }
2483
2484 /* This code is not really all that safe, but it works fine for testing. */
2485 while (!feof(fp)) {
2486 char c = fgetc(fp);
2487 if (c == '\n') {
2488 unsigned i = 0;
2489 const char *s = 0;
2490
2491 if (numChars == 0)
2492 continue;
2493
2494 line[numChars] = '\0';
2495 numChars = 0;
2496
2497 if (line[0] == '/' && line[1] == '/')
2498 continue;
2499
2500 s = strtok(line, " ");
2501 while (s) {
2502 args[i] = s;
2503 ++i;
2504 s = strtok(0, " ");
2505 }
2506 if (print_usrs(&args[0], &args[i]))
2507 return 1;
2508 }
2509 else
2510 line[numChars++] = c;
2511 }
2512
2513 fclose(fp);
2514 return 0;
2515}
2516
2517/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002518/* Command line processing. */
2519/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002520int write_pch_file(const char *filename, int argc, const char *argv[]) {
2521 CXIndex Idx;
2522 CXTranslationUnit TU;
2523 struct CXUnsavedFile *unsaved_files = 0;
2524 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002525 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002526
2527 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2528
2529 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2530 clang_disposeIndex(Idx);
2531 return -1;
2532 }
2533
2534 TU = clang_parseTranslationUnit(Idx, 0,
2535 argv + num_unsaved_files,
2536 argc - num_unsaved_files,
2537 unsaved_files,
2538 num_unsaved_files,
2539 CXTranslationUnit_Incomplete);
2540 if (!TU) {
2541 fprintf(stderr, "Unable to load translation unit!\n");
2542 free_remapped_files(unsaved_files, num_unsaved_files);
2543 clang_disposeIndex(Idx);
2544 return 1;
2545 }
2546
Douglas Gregor39c411f2011-07-06 16:43:36 +00002547 switch (clang_saveTranslationUnit(TU, filename,
2548 clang_defaultSaveOptions(TU))) {
2549 case CXSaveError_None:
2550 break;
2551
2552 case CXSaveError_TranslationErrors:
2553 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2554 filename);
2555 result = 2;
2556 break;
2557
2558 case CXSaveError_InvalidTU:
2559 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2560 filename);
2561 result = 3;
2562 break;
2563
2564 case CXSaveError_Unknown:
2565 default:
2566 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2567 result = 1;
2568 break;
2569 }
2570
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002571 clang_disposeTranslationUnit(TU);
2572 free_remapped_files(unsaved_files, num_unsaved_files);
2573 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002574 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002575}
2576
2577/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002578/* Serialized diagnostics. */
2579/******************************************************************************/
2580
2581static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2582 switch (error) {
2583 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2584 case CXLoadDiag_None: break;
2585 case CXLoadDiag_Unknown: return "Unknown";
2586 case CXLoadDiag_InvalidFile: return "Invalid File";
2587 }
2588 return "None";
2589}
2590
2591static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2592 switch (severity) {
2593 case CXDiagnostic_Note: return "note";
2594 case CXDiagnostic_Error: return "error";
2595 case CXDiagnostic_Fatal: return "fatal";
2596 case CXDiagnostic_Ignored: return "ignored";
2597 case CXDiagnostic_Warning: return "warning";
2598 }
2599 return "unknown";
2600}
2601
2602static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002603 if (indent == 0)
2604 return;
2605 fprintf(stderr, "+");
2606 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002607 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002608 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002609 --indent;
2610 }
2611}
2612
2613static void printLocation(CXSourceLocation L) {
2614 CXFile File;
2615 CXString FileName;
2616 unsigned line, column, offset;
2617
2618 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2619 FileName = clang_getFileName(File);
2620
2621 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2622 clang_disposeString(FileName);
2623}
2624
2625static void printRanges(CXDiagnostic D, unsigned indent) {
2626 unsigned i, n = clang_getDiagnosticNumRanges(D);
2627
2628 for (i = 0; i < n; ++i) {
2629 CXSourceLocation Start, End;
2630 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2631 Start = clang_getRangeStart(SR);
2632 End = clang_getRangeEnd(SR);
2633
2634 printIndent(indent);
2635 fprintf(stderr, "Range: ");
2636 printLocation(Start);
2637 fprintf(stderr, " ");
2638 printLocation(End);
2639 fprintf(stderr, "\n");
2640 }
2641}
2642
2643static void printFixIts(CXDiagnostic D, unsigned indent) {
2644 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002645 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002646 for (i = 0 ; i < n; ++i) {
2647 CXSourceRange ReplacementRange;
2648 CXString text;
2649 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2650
2651 printIndent(indent);
2652 fprintf(stderr, "FIXIT: (");
2653 printLocation(clang_getRangeStart(ReplacementRange));
2654 fprintf(stderr, " - ");
2655 printLocation(clang_getRangeEnd(ReplacementRange));
2656 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2657 clang_disposeString(text);
2658 }
2659}
2660
2661static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002662 unsigned i, n;
2663
Ted Kremenek15322172011-11-10 08:43:12 +00002664 if (!Diags)
2665 return;
2666
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002667 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002668 for (i = 0; i < n; ++i) {
2669 CXSourceLocation DiagLoc;
2670 CXDiagnostic D;
2671 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002672 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002673 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002674 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002675
2676 D = clang_getDiagnosticInSet(Diags, i);
2677 DiagLoc = clang_getDiagnosticLocation(D);
2678 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2679 FileName = clang_getFileName(File);
2680 DiagSpelling = clang_getDiagnosticSpelling(D);
2681
2682 printIndent(indent);
2683
2684 fprintf(stderr, "%s:%d:%d: %s: %s",
2685 clang_getCString(FileName),
2686 line,
2687 column,
2688 getSeverityString(clang_getDiagnosticSeverity(D)),
2689 clang_getCString(DiagSpelling));
2690
2691 DiagOption = clang_getDiagnosticOption(D, 0);
2692 DiagOptionStr = clang_getCString(DiagOption);
2693 if (DiagOptionStr) {
2694 fprintf(stderr, " [%s]", DiagOptionStr);
2695 }
2696
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002697 DiagCat = clang_getDiagnosticCategoryText(D);
2698 DiagCatStr = clang_getCString(DiagCat);
2699 if (DiagCatStr) {
2700 fprintf(stderr, " [%s]", DiagCatStr);
2701 }
2702
Ted Kremenek15322172011-11-10 08:43:12 +00002703 fprintf(stderr, "\n");
2704
2705 printRanges(D, indent);
2706 printFixIts(D, indent);
2707
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002708 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002709 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2710
2711 clang_disposeString(FileName);
2712 clang_disposeString(DiagSpelling);
2713 clang_disposeString(DiagOption);
2714 }
2715}
2716
2717static int read_diagnostics(const char *filename) {
2718 enum CXLoadDiag_Error error;
2719 CXString errorString;
2720 CXDiagnosticSet Diags = 0;
2721
2722 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2723 if (!Diags) {
2724 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2725 getDiagnosticCodeStr(error),
2726 clang_getCString(errorString));
2727 clang_disposeString(errorString);
2728 return 1;
2729 }
2730
2731 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002732 fprintf(stderr, "Number of diagnostics: %d\n",
2733 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002734 clang_disposeDiagnosticSet(Diags);
2735 return 0;
2736}
2737
2738/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002739/* Command line processing. */
2740/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002741
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002742static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002743 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002744 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002745 if (strcmp(s, "-usrs") == 0)
2746 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002747 if (strncmp(s, "-memory-usage", 13) == 0)
2748 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002749 return NULL;
2750}
2751
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002752static void print_usage(void) {
2753 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002754 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002755 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002756 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002757 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002758 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002759 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002760 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002761 "[FileCheck prefix]\n");
2762 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002763 " c-index-test -test-load-tu <AST file> <symbol filter> "
2764 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002765 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2766 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002767 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002768 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002769 " c-index-test -test-load-source-memory-usage "
2770 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002771 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2772 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002773 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002774 " c-index-test -test-load-source-usrs-memory-usage "
2775 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002776 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2777 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002778 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002779 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002780 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002781 " c-index-test -test-print-typekind {<args>}*\n"
2782 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002783 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002784 " c-index-test -write-pch <file> <compiler arguments>\n");
2785 fprintf(stderr,
2786 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002787 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002788 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002789 " all - load all symbols, including those from PCH\n"
2790 " local - load all symbols except those in PCH\n"
2791 " category - only load ObjC categories (non-PCH)\n"
2792 " interface - only load ObjC interfaces (non-PCH)\n"
2793 " protocol - only load ObjC protocols (non-PCH)\n"
2794 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002795 " typedef - only load typdefs (non-PCH)\n"
2796 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002797}
2798
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002799/***/
2800
2801int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002802 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002803 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2804 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002805 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002806 return perform_code_completion(argc, argv, 0);
2807 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2808 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002809 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2810 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002811 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2812 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002813 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2814 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002815 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2816 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002817 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002818 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002819 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002820 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2821 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002822 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002823 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2824 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2825 if (I) {
2826 int trials = atoi(argv[2]);
2827 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2828 NULL);
2829 }
2830 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002831 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002832 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002833
2834 PostVisitTU postVisit = 0;
2835 if (strstr(argv[1], "-memory-usage"))
2836 postVisit = PrintMemoryUsage;
2837
Ted Kremenek7d405622010-01-12 23:34:26 +00002838 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002839 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2840 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002841 }
2842 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002843 return perform_file_scan(argv[2], argv[3],
2844 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002845 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2846 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002847 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2848 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2849 PrintInclusionStack);
2850 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2851 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2852 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002853 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2854 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2855 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002856 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2857 return perform_test_load_source(argc - 2, argv + 2, "all",
2858 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002859 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2860 if (argc > 2)
2861 return print_usrs(argv + 2, argv + argc);
2862 else {
2863 display_usrs();
2864 return 1;
2865 }
2866 }
2867 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2868 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002869 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2870 return write_pch_file(argv[2], argc - 3, argv + 3);
2871
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002872 print_usage();
2873 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002874}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002875
2876/***/
2877
2878/* We intentionally run in a separate thread to ensure we at least minimal
2879 * testing of a multithreaded environment (for example, having a reduced stack
2880 * size). */
2881
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002882typedef struct thread_info {
2883 int argc;
2884 const char **argv;
2885 int result;
2886} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002887void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002888 thread_info *client_data = client_data_v;
2889 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00002890#ifdef __CYGWIN__
2891 fflush(stdout); /* stdout is not flushed on Cygwin. */
2892#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002893}
2894
2895int main(int argc, const char **argv) {
2896 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002897
Douglas Gregor61605982010-10-27 16:00:01 +00002898 if (getenv("CINDEXTEST_NOTHREADS"))
2899 return cindextest_main(argc, argv);
2900
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002901 client_data.argc = argc;
2902 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002903 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002904 return client_data.result;
2905}